2 // Tizen Web Device API
3 // Copyright (c) 2012-2013 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.
18 #include "AbstractFilter.h"
19 #include "JSAttributeFilter.h"
20 #include "JSAttributeRangeFilter.h"
21 #include "JSCompositeFilter.h"
22 #include <PlatformException.h>
30 using namespace Common;
32 AbstractFilter::AbstractFilter(FilterType filter_type) :
33 m_filter_type(filter_type)
37 AbstractFilter::~AbstractFilter()
41 FilterType AbstractFilter::getFilterType() const
46 JSValueRef AbstractFilter::makeJSValue(JSContextRef context,
47 AbstractFilterPtr native)
52 LOGE("Native is null");
53 throw UnknownException("Native is null");
56 switch (native->getFilterType()) {
57 case ATTRIBUTE_FILTER:
58 return JSAttributeFilter::makeJSObject(context, native);
59 case ATTRIBUTE_RANGE_FILTER:
60 return JSAttributeRangeFilter::makeJSObject(context, native);
61 case COMPOSITE_FILTER:
62 return JSCompositeFilter::makeJSObject(context, native);
64 LOGE("Unsupported filter type");
65 throw UnknownException("Unsupported filter type");
69 AbstractFilterPtr AbstractFilter::getPrivateObject(JSContextRef context,
72 JSObjectRef object = JSUtil::JSValueToObject(context, value);
74 if (!JSValueIsObjectOfClass(context, value, JSAttributeFilter::getClassRef()) &&
75 !JSValueIsObjectOfClass(context, value, JSAttributeRangeFilter::getClassRef()) &&
76 !JSValueIsObjectOfClass(context, value, JSCompositeFilter::getClassRef())) {
77 LOGE("JSObjectRef:%p - Object type is not JSAttributeFilter, "
78 "JSAttributeRangeFilter nor JSCompositeFilter", object);
79 throw TypeMismatchException("Object type is not valid filter");
82 AbstractFilterHolder* priv =
83 static_cast<AbstractFilterHolder*>(JSObjectGetPrivate(object));
85 LOGE("Holder is null");
86 throw UnknownException("Holder is null");
90 throw UnknownException("Priv is null");
96 bool AbstractFilter::isMatching(const FilterableObject* const tested_object) const
98 LOGE("Calling isMatching on AbstractFilter!");
99 throw UnknownException("Method not supported");
102 AttributeFilterPtr castToAttributeFilter(AbstractFilterPtr from)
104 if(ATTRIBUTE_FILTER != from->getFilterType()) {
105 LOGE("Trying to get AttributeFilterPtr but filter's type is: %d",
106 from->getFilterType());
107 return AttributeFilterPtr();
110 return std::dynamic_pointer_cast<AttributeFilter>(from);
113 AttributeRangeFilterPtr castToAttributeRangeFilter(AbstractFilterPtr from)
115 if(ATTRIBUTE_RANGE_FILTER != from->getFilterType()) {
116 LOGE("Trying to get AttributeRangeFilterPtr but filter's type is: %d",
117 from->getFilterType());
118 return AttributeRangeFilterPtr();
121 return std::dynamic_pointer_cast<AttributeRangeFilter>(from);
124 CompositeFilterPtr castToCompositeFilter(AbstractFilterPtr from)
126 if(COMPOSITE_FILTER != from->getFilterType()) {
127 LOGE("Trying to get CompositeFilterPtr but filter's type is: %d",
128 from->getFilterType());
129 return CompositeFilterPtr();
132 return std::dynamic_pointer_cast<CompositeFilter>(from);
137 inline std::string convertToLowerCase(const std::string& input_string)
139 std::string output_string = input_string;
140 std::transform(output_string.begin(), output_string.end(), output_string.begin(),
142 return output_string;
145 } // Anonymous namespace
147 bool FilterUtils::isStringMatching(const std::string& key,
148 const std::string& value,
149 Tizen::FilterMatchFlag flag)
153 case Tizen::ENDSWITH: {
157 if (key.size() > value.size()) {
160 std::string lvalue = convertToLowerCase(value);
161 std::string lkey = convertToLowerCase(key);
162 return lvalue.substr(lvalue.size() - lkey.size(), lkey.size()) == lkey;
165 case Tizen::EXACTLY: {
169 case Tizen::STARTSWITH: {
173 if (key.size() > value.size()) {
176 std::string lvalue = convertToLowerCase(value);
177 std::string lkey = convertToLowerCase(key);
178 return lvalue.substr(0, lkey.size()) == lkey;
181 case Tizen::CONTAINS: {
185 if (key.size() > value.size()) {
188 std::string lvalue = convertToLowerCase(value);
189 std::string lkey = convertToLowerCase(key);
190 return lvalue.find(lkey) != std::string::npos;
194 LOGE("Unknown match flag");
200 bool FilterUtils::isAnyStringMatching(const std::string& key,
201 const std::vector<std::string>& values,
202 Tizen::FilterMatchFlag flag)
204 for(auto it = values.begin(); it != values.end(); ++it) {
205 if(isStringMatching(key,*it,flag)) {
212 bool FilterUtils::isTimeStampInRange(const time_t& time_stamp,
213 Tizen::AnyPtr& initial_value,
214 Tizen::AnyPtr& end_value)
216 time_t from_time = 0;
219 bool initial_is_valid_time_value = false;
220 if (initial_value && !initial_value->isNullOrUndefined()) {
222 struct tm ftime = *initial_value->toDateTm();
223 from_time = mktime(&ftime);
224 initial_is_valid_time_value = true;
227 LOGE("Unknown exception occured during execution of Any::toDateTm()");
230 if(!initial_is_valid_time_value) {
231 LOGE("initialValue is not Time!");
232 throw InvalidValuesException("initialValue is not Time!");
235 bool end_is_valid_time_value = false;
236 if (end_value && !end_value->isNullOrUndefined()) {
238 struct tm ttime = *end_value->toDateTm();
239 to_time = mktime(&ttime);
240 end_is_valid_time_value = true;
243 LOGE("Unknown exception occured during execution of Any::toDateTm()");
246 if(end_is_valid_time_value) {
247 LOGE("endValue is not Time!");
248 throw InvalidValuesException("endValue is not Time!");
251 bool is_in_range = FilterUtils::isBetweenTimeRange(time_stamp, from_time, to_time);
253 LOGD("%d is%s in time range <%d, %d>", time_stamp, (is_in_range ? "" : " NOT"),