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
25 #include "JSAttributeFilter.h"
28 #include <dpl/shared_ptr.h>
29 #include <CommonsJavaScript/ScopedJSStringRef.h>
31 #include <ArgumentValidator.h>
32 #include <JSWebAPIErrorFactory.h>
33 #include "AnyFactory.h"
36 #define ATTRIBUTE_FILTER_CLASS_NAME "AttributeFilter"
37 #define ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME "attributeName"
38 #define ATTRIBUTE_FILTER_ATTR_MATCH_FLAG "matchFlag"
39 #define ATTRIBUTE_FILTER_ATTR_MATCH_VALUE "matchValue"
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 },
78 JSStaticFunction JSAttributeFilter::m_functions[] =
83 JSClassRef JSAttributeFilter::getClassRef() {
85 m_classRef = JSClassCreate(&m_classInfo);
90 bool JSAttributeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
92 return JSValueIsObjectOfClass(context, value, getClassRef());
95 AttributeFilterPtr JSAttributeFilter::getAttributeFilter(JSContextRef context, JSValueRef value)
97 if (!isObjectOfClass(context, value))
98 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
100 JSObjectRef object = JSValueToObject(context, value, NULL);
102 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
104 JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
106 Throw(WrtDeviceApis::Commons::NullPointerException);
108 return priv->getObject();
111 JSObjectRef JSAttributeFilter::createJSObject(JSContextRef context,
112 AttributeFilterPtr privateData,
113 JSValueRef jsValueMatchValue)
115 JSAttributeFilterPriv *priv = new JSAttributeFilterPriv(context, privateData);
116 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
117 if (NULL == jsObjectRef)
119 LoggerE("object creation error");
123 const ScopedJSStringRef jsStrMatchValue(JSStringCreateWithUTF8CString(ATTRIBUTE_FILTER_ATTR_MATCH_VALUE));
125 JSObjectSetProperty(context, jsObjectRef, jsStrMatchValue.get(),
126 jsValueMatchValue, kJSPropertyAttributeNone, NULL);
131 JSObjectRef JSAttributeFilter::constructor(JSContextRef context,
132 JSObjectRef constructor,
133 size_t argumentCount,
134 const JSValueRef arguments[],
135 JSValueRef* exception)
139 std::string attributeName;
141 AnyPtr matchValue(NULL);
142 JSValueRef jsValueMatchValue = NULL;
144 ArgumentValidator validator(context, argumentCount, arguments);
148 attributeName = validator.toString(0, false);
150 catch(BasePlatformException &e)
152 return JSWebAPIErrorFactory::postException(context, exception, e);
157 std::string matchFlagStr;
158 if(argumentCount < 2 ||
159 JSValueIsUndefined(context, arguments[1]) ||
160 JSValueIsNull(context, arguments[1]))
161 matchFlagStr = "EXACTLY";
163 matchFlagStr = validator.toString(1, true);
165 if(matchFlagStr == "EXACTLY")
166 matchFlag = MATCH_EXACTLY;
167 else if(matchFlagStr == "FULLSTRING")
168 matchFlag = MATCH_FULLSTRING;
169 else if(matchFlagStr == "CONTAINS")
170 matchFlag = MATCH_CONTAINS;
171 else if(matchFlagStr == "STARTSWITH")
172 matchFlag = MATCH_STARTSWITH;
173 else if(matchFlagStr == "ENDSWITH")
174 matchFlag = MATCH_ENDSWITH;
175 else if(matchFlagStr == "EXISTS")
176 matchFlag = MATCH_EXISTS;
178 throw TypeMismatchException("Property is not valid FilterMatchFlag value");
180 catch(BasePlatformException &e)
182 return JSWebAPIErrorFactory::postException(context, exception, e);
185 matchValue = AnyFactory::createAnyEmpty(context);
187 if(argumentCount >= 3)
188 jsValueMatchValue = arguments[2];
190 jsValueMatchValue = JSValueMakeUndefined(context);
192 AttributeFilterPtr attributeFilter(new AttributeFilter(attributeName, matchFlag, matchValue));
194 JSObjectRef jsobject;
198 jsobject = createJSObject(context, attributeFilter, jsValueMatchValue);
202 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
203 return JSWebAPIErrorFactory::postException(context, exception,
204 TypeMismatchException("Error occurred while creating object"));
210 void JSAttributeFilter::Initialize(JSContextRef context, JSObjectRef object)
212 if (!JSObjectGetPrivate(object))
214 AttributeFilterPtr filter(new AttributeFilter("", MATCH_NONE, AnyPtr(NULL)));
215 JSAttributeFilterPriv *priv = new JSAttributeFilterPriv(context, AttributeFilterPtr(filter));
216 if (!JSObjectSetPrivate(object, priv))
223 void JSAttributeFilter::Finalize(JSObjectRef object)
225 JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
235 AttributeFilterPtr JSAttributeFilter::getPrivData(JSObjectRef object)
238 JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
240 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
242 AttributeFilterPtr result = priv->getObject();
244 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
249 JSValueRef JSAttributeFilter::getAttributeName(JSContextRef context,
251 JSStringRef propertyName,
252 JSValueRef* exception)
258 AttributeFilterPtr attributeFilter = getPrivData(object);
259 return JSUtil::toJSValueRef(context, attributeFilter->getAttributeName());
261 catch(BasePlatformException &e)
263 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
266 return JSValueMakeUndefined(context);
269 bool JSAttributeFilter::setAttributeName(JSContextRef context,
271 JSStringRef propertyName,
273 JSValueRef* exception)
279 AttributeFilterPtr attributeFilter = getPrivData(object);
280 attributeFilter->setAttributeName(JSUtil::JSValueToString(context, value));
282 catch(BasePlatformException &e)
284 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
290 JSValueRef JSAttributeFilter::getMatchFlag(JSContextRef context,
292 JSStringRef propertyName,
293 JSValueRef* exception)
299 AttributeFilterPtr attributeFilter = getPrivData(object);
300 MatchFlag matchFlag = attributeFilter->getMatchFlag();
301 std::string matchFlagStr;
302 if(matchFlag == MATCH_EXACTLY)
303 matchFlagStr = "EXACTLY";
304 else if(matchFlag == MATCH_FULLSTRING)
305 matchFlagStr = "FULLSTRING";
306 else if(matchFlag == MATCH_CONTAINS)
307 matchFlagStr = "CONTAINS";
308 else if(matchFlag == MATCH_STARTSWITH)
309 matchFlagStr = "STARTSWITH";
310 else if(matchFlag == MATCH_ENDSWITH)
311 matchFlagStr = "ENDSWITH";
312 else if(matchFlag == MATCH_EXISTS)
313 matchFlagStr = "EXISTS";
315 matchFlagStr = "EXACTLY";
317 return JSUtil::toJSValueRef(context, matchFlagStr);
319 catch(BasePlatformException &e)
321 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
324 return JSValueMakeUndefined(context);
327 bool JSAttributeFilter::setMatchFlag(JSContextRef context,
329 JSStringRef propertyName,
331 JSValueRef* exception)
337 AttributeFilterPtr attributeFilter = getPrivData(object);
340 std::string matchFlagStr = JSUtil::JSValueToString(context, value);
342 if(matchFlagStr == "EXACTLY")
343 matchFlag = MATCH_EXACTLY;
344 else if(matchFlagStr == "FULLSTRING")
345 matchFlag = MATCH_FULLSTRING;
346 else if(matchFlagStr == "CONTAINS")
347 matchFlag = MATCH_CONTAINS;
348 else if(matchFlagStr == "STARTSWITH")
349 matchFlag = MATCH_STARTSWITH;
350 else if(matchFlagStr == "ENDSWITH")
351 matchFlag = MATCH_ENDSWITH;
352 else if(matchFlagStr == "EXISTS")
353 matchFlag = MATCH_EXISTS;
355 throw TypeMismatchException("Property is not valid FilterMatchFlag value");
357 attributeFilter->setMatchFlag(matchFlag);
359 catch(BasePlatformException &e)
361 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());