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/db/orm.h>
23 #include <dpl/string.h>
24 #include <dpl/scope_guard.h>
25 #include <wrt-commons/i18n-dao-ro/i18n_dao_read_only.h>
26 #include <wrt-commons/i18n-dao-ro/i18n_database.h>
30 #include <dpl/singleton_impl.h>
31 #include <installer_log.h>
33 IMPLEMENT_SINGLETON(LanguageSubtagRstTree)
35 namespace I18nDAOReadOnly = I18n::DB::I18nDAOReadOnly;
37 bool LanguageSubtagRstTree::ValidateLanguageTag(const std::string &tag_input)
39 std::string tag = tag_input;
40 std::transform(tag.begin(), tag.end(), tag.begin(), &tolower);
42 std::vector<DPL::String> parts;
43 DPL::Tokenize(DPL::FromUTF8String(tag),
45 std::back_inserter(parts),
47 std::vector<DPL::String>::iterator token = parts.begin();
48 if (token == parts.end())
53 I18n::DB::Interface::attachDatabaseRO();
56 I18n::DB::Interface::detachDatabase();
59 if (I18nDAOReadOnly::IsValidSubTag(*token, RECORD_TYPE_LANGUAGE))
68 if (token == parts.end())
73 if (I18nDAOReadOnly::IsValidSubTag(*token, RECORD_TYPE_EXTLANG))
78 if (token == parts.end())
83 if (I18nDAOReadOnly::IsValidSubTag(*token, RECORD_TYPE_SCRIPT))
88 if (token == parts.end())
93 if (I18nDAOReadOnly::IsValidSubTag(*token, RECORD_TYPE_REGION))
98 if (token == parts.end())
103 while (token != parts.end())
105 if (I18nDAOReadOnly::IsValidSubTag(*token, RECORD_TYPE_VARIANT))
115 //'u' - unicode extension - only one BCP47 extension is registered.
116 //TODO: unicode extension should be also validated (l.wrzosek)
117 if (token == parts.end())
125 bool one_or_more = false;
126 while (token != parts.end() &&
140 if (token == parts.end())
148 bool one_or_more = false;
149 while (token != parts.end() &&
162 if (token == parts.end())
167 //Try private use now:
168 token = parts.begin();
172 bool one_or_more = false;
173 while (token != parts.end() &&
183 //grandfathered is always rejected
187 #define TEST_LANG(str, cond) \
188 if (LanguageSubtagRstTreeSingleton::Instance(). \
189 ValidateLanguageTag(str) == cond) { \
190 _D("Good validate status for lang: %s", str); \
192 _E("Wrong validate status for lang: %s, should be %d", str, 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-*