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.
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Declaration of the JSFilter class
25 #include "AnyFactory.h"
28 #include <Commons/Exception.h>
29 #include <PlatformException.h>
37 using namespace DeviceAPI::Common;
39 class PrivateAny : public Any
43 PrivateAny(JSContextRef context, const JSValueRef value, PrimitiveType type);
44 virtual ~PrivateAny();
47 bool isDate(JSContextRef jsContext, JSValueRef jsValue) const;
48 std::string toJSON(JSContextRef jsContext, JSValueRef jsValue) const;
49 std::string toString(JSContextRef jsContext, JSValueRef jsValue) const;
52 PrivateAny::PrivateAny() : Any()
54 m_isNullOrUndefined = true;
55 m_type = PrimitiveType_Null;
60 PrivateAny::PrivateAny(JSContextRef context, const JSValueRef value, PrimitiveType type) : Any()
64 if(type == PrimitiveType_NoType)
65 throw UnknownException("Cannot create any with NoType");
67 if(JSValueIsUndefined(context, value) || JSValueIsNull(context, value))
68 m_isNullOrUndefined = true;
72 if(type == PrimitiveType_Boolean)
73 m_value.b = JSUtil::JSValueToBoolean(context, value);
74 else if(type == PrimitiveType_Long)
75 m_value.l = JSUtil::JSValueToLong(context, value);
76 else if(type == PrimitiveType_ULong)
77 m_value.ul = JSUtil::JSValueToULong(context, value);
78 else if(type == PrimitiveType_LongLong)
79 m_value.ll = JSUtil::JSValueToLongLong(context, value);
80 else if(type == PrimitiveType_ULongLong)
81 m_value.ull = JSUtil::JSValueToULongLong(context, value);
82 else if(type == PrimitiveType_Double)
83 m_value.d = JSUtil::JSValueToDouble(context, value);
84 else if(type == PrimitiveType_String)
86 m_value.str = new std::string();
87 *m_value.str = JSUtil::JSValueToString(context, value);
89 else if(type == PrimitiveType_Time)
91 m_value.t = new std::tm;
92 *m_value.t = JSUtil::JSValueToDateTm(context, value);
95 catch(BasePlatformException &e)
97 if(!m_isNullOrUndefined)
102 m_type = PrimitiveType_Null;
107 m_json = toJSON(context, value);
109 m_str = toString(context, value);
112 PrivateAny::~PrivateAny()
114 if(m_type == PrimitiveType_String)
116 if(m_value.str != NULL)
119 else if(m_type == PrimitiveType_Time)
121 if(m_value.t != NULL)
126 bool PrivateAny::isDate(JSContextRef jsContext, JSValueRef jsValue) const
128 JSObjectRef jsObjectGlobal = JSContextGetGlobalObject(jsContext);
129 JSStringRef jsStringDate = JSStringCreateWithUTF8CString("Date");
130 JSValueRef jsValueDate = JSObjectGetProperty(jsContext, jsObjectGlobal, jsStringDate, NULL);
131 JSStringRelease(jsStringDate);
133 JSObjectRef jsObjectDate = JSValueToObject(jsContext, jsValueDate, NULL);
135 return JSValueIsInstanceOfConstructor(jsContext, jsValue, jsObjectDate, NULL);
138 std::string PrivateAny::toJSON(JSContextRef context, JSValueRef value) const
140 JSValueRef jsError = NULL;
141 JSStringRef jsStrJson = JSValueCreateJSONString(context, value, 0, &jsError);
142 if(jsError != NULL || jsStrJson == NULL)
144 LoggerE("Fail to create JSON string");
145 return std::string("");
148 std::string result = JSUtil::JSStringToString(context, jsStrJson);
149 JSStringRelease(jsStrJson);
154 std::string PrivateAny::toString(JSContextRef context, JSValueRef value) const
158 if(m_type == PrimitiveType_String)
160 result = *m_value.str;
162 else if(m_type == PrimitiveType_Long ||
163 m_type == PrimitiveType_ULong ||
164 m_type == PrimitiveType_LongLong ||
165 m_type == PrimitiveType_ULongLong)
167 std::stringstream ss;
169 if(m_type == PrimitiveType_Long)
171 else if(m_type == PrimitiveType_ULong)
173 else if(m_type == PrimitiveType_LongLong)
175 else if(m_type == PrimitiveType_ULongLong)
182 JSValueRef jsError = NULL;
183 JSStringRef jsStrJson = JSValueToStringCopy(context, value, &jsError);
184 if(jsError == NULL && jsStrJson != NULL)
186 result = JSUtil::JSStringToString(context, jsStrJson);
187 JSStringRelease(jsStrJson);
194 AnyPtr AnyFactory::createAny(JSContextRef context, JSValueRef value, PrimitiveType type)
200 any = AnyPtr(new PrivateAny(context, value, type));
202 catch(TypeMismatchException &e)
204 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Type error occured while converting JSValueRef");
206 catch(BasePlatformException &e)
208 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Unknown error occured while converting JSValueRef");
214 AnyPtr AnyFactory::createAnyNoException(JSContextRef context, JSValueRef value, PrimitiveType type)
218 return AnyPtr(new PrivateAny(context, value, type));
220 catch(BasePlatformException &e)
222 LoggerE("Error while converting (" << e.getName() << ") : " << e.getMessage());
225 return AnyPtr(new PrivateAny());
228 AnyPtr AnyFactory::createAnyEmpty(JSContextRef context)
230 return AnyPtr(new PrivateAny());