2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file JSAttributeFilter.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSAttributeFilter class
26 #include <dpl/log/log.h>
27 #include <dpl/shared_ptr.h>
28 #include <CommonsJavaScript/Converter.h>
29 #include <CommonsJavaScript/Validator.h>
30 #include <JSTizenExceptionFactory.h>
31 #include <JSTizenException.h>
32 #include "AttributeFilter.h"
33 #include "FilterConverter.h"
34 #include "JSAttributeFilter.h"
36 #define ATTRIBUTE_FILTER_CLASS_NAME "AttributeFilter"
38 #define ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME "attributeName"
39 #define ATTRIBUTE_FILTER_ATTR_MATCH_FLAG "matchFlag"
40 #define ATTRIBUTE_FILTER_ATTR_MATCH_VALUE "matchValue"
42 namespace DeviceAPI {
\rnamespace Tizen {
44 using namespace DeviceAPI::Common;
45 using namespace DeviceAPI::Tizen;
46 using namespace WrtDeviceApis::Commons;
47 using namespace WrtDeviceApis::CommonsJavaScript;
49 JSClassRef JSAttributeFilter::m_classRef = NULL;
51 JSClassDefinition JSAttributeFilter::m_classInfo =
54 kJSClassAttributeNone,
55 ATTRIBUTE_FILTER_CLASS_NAME,
64 NULL, //DeleteProperty,
65 NULL, //getPropertyNames,
66 NULL, //CallAsFunction,
67 NULL, //CallAsConstructor,
69 NULL, //ConvertToType,
72 JSStaticValue JSAttributeFilter::m_property[] = {
73 { ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME, getAttributeName, setAttributeName, kJSPropertyAttributeNone },
74 { ATTRIBUTE_FILTER_ATTR_MATCH_FLAG, getMatchFlag, setMatchFlag, kJSPropertyAttributeNone },
75 { ATTRIBUTE_FILTER_ATTR_MATCH_VALUE, getMatchValue, setMatchValue, kJSPropertyAttributeNone },
79 JSStaticFunction JSAttributeFilter::m_functions[] =
84 JSClassRef JSAttributeFilter::getClassRef() {
86 m_classRef = JSClassCreate(&m_classInfo);
91 bool JSAttributeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
93 return JSValueIsObjectOfClass(context, value, getClassRef());
96 AttributeFilterPtr JSAttributeFilter::getAttributeFilter(JSContextRef context, JSValueRef value)
98 if (!isObjectOfClass(context, value)) {
99 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
101 JSObjectRef object = JSValueToObject(context, value, NULL);
103 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
105 JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
107 Throw(WrtDeviceApis::Commons::NullPointerException);
109 return priv->getObject();
112 JSObjectRef JSAttributeFilter::createJSObject(JSContextRef context, AttributeFilterPtr privateData)
114 JSAttributeFilterPriv *priv = new JSAttributeFilterPriv(context, privateData);
115 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
116 if (NULL == jsObjectRef) {
117 LogError("object creation error");
123 JSObjectRef JSAttributeFilter::constructor(JSContextRef context,
124 JSObjectRef constructor,
125 size_t argumentCount,
126 const JSValueRef arguments[],
127 JSValueRef* exception)
131 bool js2ndParamIsString = false;
132 bool js3rdParamIsValue = false;
134 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
136 if (argumentCount < 1)
137 ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
139 if (!JSValueIsString(context, arguments[0]))
140 ThrowMsg(InvalidArgumentException, "1st argument is not string.");
142 if (argumentCount >= 2)
144 if (JSValueIsString(context, arguments[1]))
145 js2ndParamIsString = true;
147 if (!js2ndParamIsString &&
148 !JSValueIsNull(context, arguments[1]) &&
149 !JSValueIsUndefined(context, arguments[1]))
150 ThrowMsg(InvalidArgumentException, "2nd argument is not array.");
153 if (argumentCount >= 3)
155 if (!JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2]))
156 js3rdParamIsValue = true;
160 } Catch(Exception ) {
161 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
162 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
166 FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(context);
168 std::string attributeName;
170 AnyPtr matchValue(NULL);
173 attributeName = converter->toString(arguments[0]);
175 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
176 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
181 if(js2ndParamIsString)
182 matchFlag = converter->toMatchFlag(arguments[1]);
184 matchFlag = MATCH_EXACTLY;
187 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
188 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
193 if(js3rdParamIsValue)
194 matchValue = converter->toAny(arguments[2]);
196 matchValue = AnyPtr(new Any());
199 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
200 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
204 AttributeFilterPtr attributeFilter(new AttributeFilter(attributeName, matchFlag, matchValue));
206 JSObjectRef jsobject;
209 jsobject = createJSObject(context, attributeFilter);
211 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
212 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
219 void JSAttributeFilter::Initialize(JSContextRef context, JSObjectRef object)
221 if (!JSObjectGetPrivate(object))
223 AttributeFilterPtr filter(new AttributeFilter("", MATCH_NONE, AnyPtr(NULL)));
224 JSAttributeFilterPriv *priv = new JSAttributeFilterPriv(context, AttributeFilterPtr(filter));
225 if (!JSObjectSetPrivate(object, priv)) {
231 void JSAttributeFilter::Finalize(JSObjectRef object)
233 JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
243 AttributeFilterPtr JSAttributeFilter::getPrivData(JSObjectRef object)
246 JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
248 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
250 AttributeFilterPtr result = priv->getObject();
252 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
257 JSValueRef JSAttributeFilter::getAttributeName(JSContextRef context,
259 JSStringRef propertyName,
260 JSValueRef* exception)
265 FilterConverterFactory::ConverterType converter =
266 FilterConverterFactory::getConverter(context);
267 AttributeFilterPtr attributeFilter = getPrivData(object);
268 return converter->toJSValueRef(attributeFilter->getAttributeName());
270 Catch(WrtDeviceApis::Commons::Exception)
272 LogWarning("trying to get incorrect value");
274 return JSValueMakeUndefined(context);
277 bool JSAttributeFilter::setAttributeName(JSContextRef context,
279 JSStringRef propertyName,
281 JSValueRef* exception)
285 AttributeFilterPtr attributeFilter = getPrivData(object);
286 FilterConverterFactory::ConverterType converter =
287 FilterConverterFactory::getConverter(context);
288 attributeFilter->setAttributeName(converter->toString(value));
291 Catch(WrtDeviceApis::Commons::Exception)
293 LogWarning("trying to set incorrect value");
296 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
300 JSValueRef JSAttributeFilter::getMatchFlag(JSContextRef context,
302 JSStringRef propertyName,
303 JSValueRef* exception)
308 FilterConverterFactory::ConverterType converter =
309 FilterConverterFactory::getConverter(context);
310 AttributeFilterPtr attributeFilter = getPrivData(object);
311 return converter->toJSValueRef(attributeFilter->getMatchFlag());
313 Catch(WrtDeviceApis::Commons::Exception)
315 LogWarning("trying to get incorrect value");
317 return JSValueMakeUndefined(context);
320 bool JSAttributeFilter::setMatchFlag(JSContextRef context,
322 JSStringRef propertyName,
324 JSValueRef* exception)
328 AttributeFilterPtr attributeFilter = getPrivData(object);
329 FilterConverterFactory::ConverterType converter =
330 FilterConverterFactory::getConverter(context);
331 attributeFilter->setMatchFlag(converter->toMatchFlag(value));
334 Catch(WrtDeviceApis::Commons::Exception)
336 LogWarning("trying to set incorrect value");
339 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
343 JSValueRef JSAttributeFilter::getMatchValue(JSContextRef context,
345 JSStringRef propertyName,
346 JSValueRef* exception)
351 FilterConverterFactory::ConverterType converter =
352 FilterConverterFactory::getConverter(context);
353 AttributeFilterPtr attributeFilter = getPrivData(object);
354 return converter->toJSValueRef(attributeFilter->getMatchValue());
356 Catch(WrtDeviceApis::Commons::Exception)
358 LogWarning("trying to get incorrect value");
361 return JSValueMakeUndefined(context);
364 bool JSAttributeFilter::setMatchValue(JSContextRef context,
366 JSStringRef propertyName,
368 JSValueRef* exception)
372 AttributeFilterPtr attributeFilter = getPrivData(object);
373 FilterConverterFactory::ConverterType converter =
374 FilterConverterFactory::getConverter(context);
375 attributeFilter->setMatchValue(converter->toAny(value));
378 Catch(WrtDeviceApis::Commons::Exception)
380 LogWarning("trying to set incorrect value");
383 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");