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.
19 * @author Kisub Song (kisubs.song@samsung.com)
21 * @brief Declaration of the JSFilter class
26 #include <Commons/Exception.h>
27 #include <CommonsJavaScript/ScopedJSStringRef.h>
34 using namespace WrtDeviceApis::Commons;
35 using namespace WrtDeviceApis::CommonsJavaScript;
40 m_type = PrimitiveType_Null;
48 m_type = PrimitiveType_Boolean;
50 m_json = value?"true":"false";
54 Any::Any(double value, std::string json)
56 m_type = PrimitiveType_Double;
61 if(value == (double)((char)value))
62 m_type |= PrimitiveType_Char;
63 if(value == (double)((unsigned char)value))
64 m_type |= PrimitiveType_UChar;
65 if(value == (double)((int)value))
66 m_type |= PrimitiveType_Int;
67 if(value == (double)((unsigned int)value))
68 m_type |= PrimitiveType_UInt;
69 if(value == (double)((long)value))
70 m_type |= PrimitiveType_Long;
71 if(value == (double)((unsigned long)value))
72 m_type |= PrimitiveType_ULong;
75 Any::Any(std::string value)
77 m_type = PrimitiveType_String;
78 m_value.s = new std::string(value);
79 m_json = "\"" + value + "\"";
83 Any::Any(std::tm value, std::string json)
85 m_type = PrimitiveType_Time;
91 Any::Any(void * priv, std::string json)
93 m_type = PrimitiveType_Object;
101 if(m_type == PrimitiveType_String)
105 std::string Any::toString() const
107 std::stringstream oss;
109 if(m_type == PrimitiveType_Undefined || m_type == PrimitiveType_Null)
111 else if(m_type == PrimitiveType_Boolean)
113 else if(m_type == PrimitiveType_String)
115 else if(m_type == PrimitiveType_Object)
117 else if(m_type | PrimitiveType_Number)
119 if(m_type | PrimitiveType_ULong)
120 oss << (unsigned long)m_value.d;
121 else if(m_type | PrimitiveType_Long)
122 oss << (long)m_value.d;
125 oss << std::setprecision(std::numeric_limits<double>::digits10);
126 oss << std::setiosflags(std::ios::fixed);
132 else if(m_type == PrimitiveType_Time)
134 oss << std::setfill('0') << std::setiosflags(std::ios::right) << std::setw(4) << (m_value.t.tm_year + 1900);
135 oss << std::setfill('0') << std::setiosflags(std::ios::right) << std::setw(2) << (m_value.t.tm_mon + 1);
136 oss << std::setfill('0') << std::setiosflags(std::ios::right) << std::setw(2) << m_value.t.tm_mday;
137 oss << std::setfill('0') << std::setiosflags(std::ios::right) << std::setw(2) << m_value.t.tm_hour;
138 oss << std::setfill('0') << std::setiosflags(std::ios::right) << std::setw(2) << m_value.t.tm_min;
139 oss << std::setfill('0') << std::setiosflags(std::ios::right) << std::setw(2) << m_value.t.tm_sec;
147 std::string Any::toJSON() const
152 unsigned int Any::getType() const
157 bool Any::isType(PrimitiveType type) const
159 return (bool)(m_type & type);
162 bool Any::getBool() const
164 if(m_type != PrimitiveType_Boolean)
165 ThrowMsg(InvalidArgumentException, "Type mismatch.");
170 char Any::getChar() const
172 if(!(m_type & PrimitiveType_Char))
173 ThrowMsg(InvalidArgumentException, "Type mismatch.");
175 return (char)m_value.d;
178 unsigned char Any::getUChar() const
180 if(!(m_type & PrimitiveType_UChar))
181 ThrowMsg(InvalidArgumentException, "Type mismatch.");
183 return (unsigned char)m_value.d;
186 int Any::getInt() const
188 if(!(m_type & PrimitiveType_Int))
189 ThrowMsg(InvalidArgumentException, "Type mismatch.");
191 return (int)m_value.d;
194 unsigned int Any::getUInt() const
196 if(!(m_type & PrimitiveType_UInt))
197 ThrowMsg(InvalidArgumentException, "Type mismatch.");
199 return (unsigned int)m_value.d;
202 long Any::getLong() const
204 if(!(m_type & PrimitiveType_Long))
205 ThrowMsg(InvalidArgumentException, "Type mismatch.");
207 return (long)m_value.d;
210 unsigned long Any::getULong() const
212 if(!(m_type & PrimitiveType_ULong))
213 ThrowMsg(InvalidArgumentException, "Type mismatch.");
215 return (unsigned long)m_value.d;
218 double Any::getDouble() const
220 if(!(m_type & PrimitiveType_Double))
221 ThrowMsg(InvalidArgumentException, "Type mismatch.");
226 std::string Any::getString() const
228 if(m_type != PrimitiveType_String)
229 ThrowMsg(InvalidArgumentException, "Type mismatch.");
234 std::tm Any::getDateTm() const
236 if(m_type != PrimitiveType_Time)
237 ThrowMsg(InvalidArgumentException, "Type mismatch.");
242 std::time_t Any::getTimeT() const
244 if(m_type != PrimitiveType_Time)
245 ThrowMsg(InvalidArgumentException, "Type mismatch.");
247 std::tm *time = const_cast<std::tm*>(&(m_value.t));
248 return std::mktime(time);
251 AnyTypeConverter::AnyTypeConverter(JSContextRef context) :
256 AnyTypeConverter::~AnyTypeConverter()
260 JSValueRef AnyTypeConverter::toJSValueRef(const AnyPtr& arg)
262 if(arg->isType(PrimitiveType_Undefined))
263 return JSValueMakeUndefined(m_context);
264 else if(arg->isType(PrimitiveType_Null))
265 return JSValueMakeNull(m_context);
266 else if(arg->isType(PrimitiveType_Boolean))
267 return toJSValueRef(arg->getBool());
268 else if(arg->isType(PrimitiveType_Number))
269 return toJSValueRef(arg->getDouble());
270 else if(arg->isType(PrimitiveType_String))
271 return toJSValueRef(arg->getString());
272 else if(arg->isType(PrimitiveType_Time))
273 return toJSValueRef(arg->getDateTm());
274 else if(arg->isType(PrimitiveType_Object))
276 ScopedJSStringRef json(JSStringCreateWithUTF8CString(arg->toJSON().c_str()));
277 return JSValueMakeFromJSONString(m_context, json.get());
280 return JSValueMakeUndefined(m_context);
283 AnyPtr AnyTypeConverter::toAny(const JSValueRef &value)
285 JSType jstype = JSValueGetType(m_context, value);
286 ScopedJSStringRef json(JSValueCreateJSONString(m_context, value, 0, NULL));
288 if(jstype == kJSTypeBoolean)
289 return AnyPtr(new Any(Converter::toBool(value)));
290 else if(jstype == kJSTypeNumber)
291 return AnyPtr(new Any(Converter::toDouble(value), toString(json.get())));
292 else if(jstype == kJSTypeString)
293 return AnyPtr(new Any(Converter::toString(value)));
294 else if(jstype == kJSTypeObject)
297 return AnyPtr(new Any(Converter::toDateTm(value), toString(json.get())));
301 JSObjectRef object = JSValueToObject(m_context, value, NULL);
303 priv = JSObjectGetPrivate(object);
305 return AnyPtr(new Any(priv, toString(json.get())));
309 return AnyPtr(new Any());
312 bool AnyTypeConverter::toBool(const AnyPtr& arg)
314 return arg->getBool();
317 char AnyTypeConverter::toChar(const AnyPtr& arg)
319 return arg->getChar();
322 unsigned char AnyTypeConverter::toUChar(const AnyPtr& arg)
324 return arg->getUChar();
327 int AnyTypeConverter::toInt(const AnyPtr& arg)
329 return arg->getInt();
332 unsigned int AnyTypeConverter::toUInt(const AnyPtr& arg)
334 return arg->getUInt();
337 long AnyTypeConverter::toLong(const AnyPtr& arg)
339 return arg->getLong();
342 unsigned long AnyTypeConverter::toULong(const AnyPtr& arg)
344 return arg->getULong();
347 double AnyTypeConverter::toDouble(const AnyPtr& arg)
349 return arg->getDouble();
352 std::string AnyTypeConverter::toString(const AnyPtr& arg)
354 return arg->getString();
357 tm AnyTypeConverter::toDateTm(const AnyPtr& arg)
359 return arg->getDateTm();
362 time_t AnyTypeConverter::toTimeT(const AnyPtr& arg)
364 return arg->getTimeT();
367 bool AnyTypeConverter::isDate(const JSValueRef& arg)
369 // this method originated from TizenApis::Commons::Validator::isDate()
370 if ( JSValueIsNull( m_context, arg ) || JSValueIsUndefined( m_context, arg ) || !JSValueIsObject( m_context, arg ) )