2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #include "Validator.h"
18 #include <dpl/assert.h>
19 #include "ScopedJSStringRef.h"
20 #include "Converter.h"
22 namespace WrtDeviceApis {
23 namespace CommonsJavaScript {
24 Validator::Validator(JSContextRef context,
25 JSValueRef* exception) :
27 m_exception(exception)
29 AssertMsg(NULL != m_context, "Context cannot be NULL.");
32 Validator::~Validator()
35 bool Validator::isDate(const JSValueRef& arg)
37 if (JSValueIsNull(m_context,
39 JSValueIsUndefined(m_context,
40 arg) || !JSValueIsObject(m_context, arg))
44 Converter converter(m_context);
47 converter.toDateTm(arg);
49 Catch(Commons::ConversionException)
57 Validator::isCallback(const JSValueRef& arg)
59 Converter converter(m_context);
60 return !JSValueIsNull(m_context, arg) &&
61 !JSValueIsUndefined(m_context, arg) &&
62 JSObjectIsFunction(m_context, converter.toJSObjectRef(arg));
66 Validator::checkArrayKeys(const std::vector<std::string> &allowed,
69 if (argument == NULL) {
72 if (!JSValueIsObject(m_context, argument)) {
75 JSObjectRef jsOptions = JSValueToObject(m_context, argument, NULL);
76 if (jsOptions == NULL) {
79 JSPropertyNameArrayRef jsProps = JSObjectCopyPropertyNames(m_context,
81 if (jsProps == NULL) {
82 // No properties found; and empty array.
85 size_t nCount = JSPropertyNameArrayGetCount(jsProps);
88 std::list<JSStringRef> allowedJS;
89 for (size_t j = 0; j < allowed.size(); j++) {
90 allowedJS.push_back(JSStringCreateWithUTF8CString(allowed[j].c_str()));
93 for (size_t i = 0; i < nCount; i++) {
95 for (std::list<JSStringRef>::const_iterator it = allowedJS.begin();
96 it != allowedJS.end();
99 if (JSStringIsEqual(*it,
100 JSPropertyNameArrayGetNameAtIndex(jsProps,
108 for (size_t j = 0; j < allowed.size(); j++) {
109 JSStringRelease(allowedJS.front());
110 allowedJS.pop_front();
112 JSPropertyNameArrayRelease(jsProps);
117 for (size_t j = 0; j < allowed.size(); j++) {
118 JSStringRelease(allowedJS.front());
119 allowedJS.pop_front();
121 JSPropertyNameArrayRelease(jsProps);
125 bool Validator::isNullOrUndefined(const JSValueRef& arg)
127 return (JSValueIsNull(m_context, arg) ||
128 JSValueIsUndefined(m_context, arg));
130 } // CommonsJavaScript