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.
17 * @file language_subtag_rst_tree.cpp
18 * @author Lukasz Wrzosek (l.wrzosek@samsung.com)
21 #include <language_subtag_rst_tree.h>
22 #include <dpl/log/log.h>
23 #include <dpl/db/orm.h>
24 #include <dpl/string.h>
25 #include <dpl/scope_guard.h>
26 #include <wrt-commons/i18n-dao-ro/i18n_dao_read_only.h>
27 #include <wrt-commons/i18n-dao-ro/i18n_database.h>
31 #include <dpl/singleton_impl.h>
32 IMPLEMENT_SINGLETON(LanguageSubtagRstTree)
34 namespace I18nDAOReadOnly = I18n::DB::I18nDAOReadOnly;
36 bool LanguageSubtagRstTree::ValidateLanguageTag(const std::string &tag_input)
38 std::string tag = tag_input;
39 std::transform(tag.begin(), tag.end(), tag.begin(), &tolower);
41 std::vector<DPL::String> parts;
42 DPL::Tokenize(DPL::FromUTF8String(tag),
44 std::back_inserter(parts),
46 std::vector<DPL::String>::iterator token = parts.begin();
47 if (token == parts.end())
52 I18n::DB::Interface::attachDatabaseRO();
55 I18n::DB::Interface::detachDatabase();
58 if (I18nDAOReadOnly::IsValidSubTag(*token, RECORD_TYPE_LANGUAGE))
67 if (token == parts.end())
72 if (I18nDAOReadOnly::IsValidSubTag(*token, RECORD_TYPE_EXTLANG))
77 if (token == parts.end())
82 if (I18nDAOReadOnly::IsValidSubTag(*token, RECORD_TYPE_SCRIPT))
87 if (token == parts.end())
92 if (I18nDAOReadOnly::IsValidSubTag(*token, RECORD_TYPE_REGION))
97 if (token == parts.end())
102 while (token != parts.end())
104 if (I18nDAOReadOnly::IsValidSubTag(*token, RECORD_TYPE_VARIANT))
114 //'u' - unicode extension - only one BCP47 extension is registered.
115 //TODO: unicode extension should be also validated (l.wrzosek)
116 if (token == parts.end())
124 bool one_or_more = false;
125 while (token != parts.end() &&
139 if (token == parts.end())
147 bool one_or_more = false;
148 while (token != parts.end() &&
161 if (token == parts.end())
166 //Try private use now:
167 token = parts.begin();
171 bool one_or_more = false;
172 while (token != parts.end() &&
182 //grandfathered is always rejected
186 #define TEST_LANG(str, cond) \
187 if (LanguageSubtagRstTreeSingleton::Instance(). \
188 ValidateLanguageTag(str) == cond) { \
189 LogDebug("Good validate status for lang: " << str); \
191 LogError("Wrong validate status for lang: " << str \
192 << ", should be " << cond); \
195 void LanguageSubtagRstTree::Initialize()
197 /* Temporarily added unit test. Commented out due to performance drop.
198 * TEST_LANG("zh", true);
199 * TEST_LANG("esx-al", true);
200 * TEST_LANG("zh-Hant", true);
201 * TEST_LANG("zh-Hant-CN", true);
202 * TEST_LANG("zh-Hant-CN-x-private1-private2", true);
203 * TEST_LANG("plxxx", false);
204 * TEST_LANG("pl-x-private111", false);
205 * TEST_LANG("x-private1", false); //do not support pure private ones
206 * TEST_LANG("x-private22", false);
207 * TEST_LANG("i-private22", false); //do not support i-*