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 {
25 Validator::Validator(JSContextRef context,
26 JSValueRef* exception) :
28 m_exception(exception)
30 Assert(NULL != m_context && "Context cannot be NULL.");
33 Validator::~Validator()
37 bool Validator::isDate(const JSValueRef& arg)
39 if (JSValueIsNull(m_context,
41 JSValueIsUndefined(m_context,
42 arg) || !JSValueIsObject(m_context, arg)) {
45 Converter converter(m_context);
48 converter.toDateTm(arg);
50 Catch(Commons::ConversionException)
58 Validator::isCallback(const JSValueRef& arg)
60 Converter converter(m_context);
61 return !JSValueIsNull(m_context, arg) &&
62 !JSValueIsUndefined(m_context, arg) &&
63 JSObjectIsFunction(m_context, converter.toJSObjectRef(arg));
67 Validator::checkArrayKeys(const std::vector<std::string> &allowed,
70 if (argument == NULL) {
73 if (!JSValueIsObject(m_context, argument)) {
76 JSObjectRef jsOptions = JSValueToObject(m_context, argument, NULL);
77 if (jsOptions == NULL) {
80 JSPropertyNameArrayRef jsProps = JSObjectCopyPropertyNames(m_context,
82 if (jsProps == NULL) {
83 // No properties found; and empty array.
86 size_t nCount = JSPropertyNameArrayGetCount(jsProps);
89 std::list<JSStringRef> allowedJS;
90 for (size_t j = 0; j < allowed.size(); j++) {
91 allowedJS.push_back(JSStringCreateWithUTF8CString(allowed[j].c_str()));
94 for (size_t i = 0; i < nCount; i++) {
96 for (std::list<JSStringRef>::const_iterator it = allowedJS.begin();
97 it != allowedJS.end();
99 if (JSStringIsEqual(*it,
100 JSPropertyNameArrayGetNameAtIndex(jsProps,
107 for (size_t j = 0; j < allowed.size(); j++) {
108 JSStringRelease(allowedJS.front());
109 allowedJS.pop_front();
111 JSPropertyNameArrayRelease(jsProps);
116 for (size_t j = 0; j < allowed.size(); j++) {
117 JSStringRelease(allowedJS.front());
118 allowedJS.pop_front();
120 JSPropertyNameArrayRelease(jsProps);
124 bool Validator::isNullOrUndefined(const JSValueRef& arg)
126 return (JSValueIsNull(m_context, arg) ||
127 JSValueIsUndefined(m_context, arg));
130 } // CommonsJavaScript