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::tm *toDateTm(time_t time) const;
49 std::string toJSON(JSContextRef jsContext, JSValueRef jsValue) const;
50 std::string toString(JSContextRef jsContext, JSValueRef jsValue) const;
53 PrivateAny::PrivateAny() : Any()
55 m_isNullOrUndefined = true;
56 m_type = PrimitiveType_Null;
61 PrivateAny::PrivateAny(JSContextRef context, const JSValueRef value, PrimitiveType type) : Any()
65 if(type == PrimitiveType_NoType)
66 throw UnknownException("Cannot create any with NoType");
68 if(JSValueIsUndefined(context, value) || JSValueIsNull(context, value))
69 m_isNullOrUndefined = true;
73 if(type == PrimitiveType_Boolean)
74 m_value.b = JSUtil::JSValueToBoolean(context, value);
75 else if(type == PrimitiveType_Long)
76 m_value.l = JSUtil::JSValueToLong(context, value);
77 else if(type == PrimitiveType_ULong)
78 m_value.ul = JSUtil::JSValueToULong(context, value);
79 else if(type == PrimitiveType_LongLong)
80 m_value.ll = JSUtil::JSValueToLongLong(context, value);
81 else if(type == PrimitiveType_ULongLong)
82 m_value.ull = JSUtil::JSValueToULongLong(context, value);
83 else if(type == PrimitiveType_Double)
84 m_value.d = JSUtil::JSValueToDouble(context, value);
85 else if(type == PrimitiveType_String)
87 m_value.str = new std::string();
88 *m_value.str = JSUtil::JSValueToString(context, value);
90 else if(type == PrimitiveType_Time)
91 m_value.t = toDateTm(JSUtil::JSValueToTimeT(context, value));
93 catch(BasePlatformException &e)
95 if(!m_isNullOrUndefined)
100 m_type = PrimitiveType_Null;
105 m_json = toJSON(context, value);
107 m_str = toString(context, value);
110 PrivateAny::~PrivateAny()
112 if(m_type == PrimitiveType_String)
114 if(m_value.str != NULL)
117 else if(m_type == PrimitiveType_Time)
119 if(m_value.t != NULL)
124 bool PrivateAny::isDate(JSContextRef jsContext, JSValueRef jsValue) const
126 JSObjectRef jsObjectGlobal = JSContextGetGlobalObject(jsContext);
127 JSStringRef jsStringDate = JSStringCreateWithUTF8CString("Date");
128 JSValueRef jsValueDate = JSObjectGetProperty(jsContext, jsObjectGlobal, jsStringDate, NULL);
129 JSStringRelease(jsStringDate);
131 JSObjectRef jsObjectDate = JSValueToObject(jsContext, jsValueDate, NULL);
133 return JSValueIsInstanceOfConstructor(jsContext, jsValue, jsObjectDate, NULL);
136 std::tm * PrivateAny::toDateTm(time_t time) const
138 char* currentLocale = setlocale(LC_TIME, NULL);
139 if (currentLocale == NULL)
140 throw UnknownException("Fail to get current locale");
141 char* currentLocaleDup = strdup(currentLocale);
142 if (currentLocaleDup == NULL)
143 throw UnknownException("Fail to dup current locale");
144 if (setlocale(LC_TIME, "C") == NULL)
145 throw UnknownException("Fail to set locale");
147 std::tm *timeTm = new std::tm;
148 memcpy(timeTm, localtime(&time), sizeof(std::tm));
150 if (setlocale(LC_TIME, currentLocaleDup) == NULL)
151 throw UnknownException("Fail to set locale");
153 free(currentLocaleDup);
158 std::string PrivateAny::toJSON(JSContextRef context, JSValueRef value) const
160 JSValueRef jsError = NULL;
161 JSStringRef jsStrJson = JSValueCreateJSONString(context, value, 0, &jsError);
162 if(jsError != NULL || jsStrJson == NULL)
164 LoggerE("Fail to create JSON string");
165 return std::string("");
168 std::string result = JSUtil::JSStringToString(context, jsStrJson);
169 JSStringRelease(jsStrJson);
174 std::string PrivateAny::toString(JSContextRef context, JSValueRef value) const
178 if(m_type == PrimitiveType_String)
180 result = *m_value.str;
182 else if(m_type == PrimitiveType_Long ||
183 m_type == PrimitiveType_ULong ||
184 m_type == PrimitiveType_LongLong ||
185 m_type == PrimitiveType_ULongLong)
187 std::stringstream ss;
189 if(m_type == PrimitiveType_Long)
191 else if(m_type == PrimitiveType_ULong)
193 else if(m_type == PrimitiveType_LongLong)
195 else if(m_type == PrimitiveType_ULongLong)
202 JSValueRef jsError = NULL;
203 JSStringRef jsStrJson = JSValueToStringCopy(context, value, &jsError);
204 if(jsError == NULL && jsStrJson != NULL)
206 result = JSUtil::JSStringToString(context, jsStrJson);
207 JSStringRelease(jsStrJson);
214 AnyPtr AnyFactory::createAny(JSContextRef context, JSValueRef value, PrimitiveType type)
220 any = AnyPtr(new PrivateAny(context, value, type));
222 catch(TypeMismatchException &e)
224 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Type error occured while converting JSValueRef");
226 catch(BasePlatformException &e)
228 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Unknown error occured while converting JSValueRef");
234 AnyPtr AnyFactory::createAnyNoException(JSContextRef context, JSValueRef value, PrimitiveType type)
238 return AnyPtr(new PrivateAny(context, value, type));
240 catch(BasePlatformException &e)
242 LoggerE("Error while converting (" << e.getName() << ") : " << e.getMessage());
245 return AnyPtr(new PrivateAny());
248 AnyPtr AnyFactory::createAnyEmpty(JSContextRef context)
250 return AnyPtr(new PrivateAny());