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/wrt-dao-ro/global_dao_read_only.h>
29 #include <dpl/singleton_impl.h>
30 IMPLEMENT_SINGLETON(LanguageSubtagRstTree)
32 bool LanguageSubtagRstTree::ValidateLanguageTag(const std::string &tag_input)
34 std::string tag = tag_input;
35 std::transform(tag.begin(), tag.end(), tag.begin(), &tolower);
37 std::vector<DPL::String> parts;
38 DPL::Tokenize(DPL::FromUTF8String(tag),
40 std::back_inserter(parts),
42 std::vector<DPL::String>::iterator token = parts.begin();
43 if (token == parts.end()) {
46 if (WrtDB::GlobalDAOReadOnly::IsValidSubTag(*token,
47 RECORD_TYPE_LANGUAGE))
54 if (token == parts.end()) {
57 if (WrtDB::GlobalDAOReadOnly::IsValidSubTag(*token, RECORD_TYPE_EXTLANG)) {
61 if (token == parts.end()) {
64 if (WrtDB::GlobalDAOReadOnly::IsValidSubTag(*token, RECORD_TYPE_SCRIPT)) {
68 if (token == parts.end()) {
71 if (WrtDB::GlobalDAOReadOnly::IsValidSubTag(*token, RECORD_TYPE_REGION)) {
75 if (token == parts.end()) {
78 while (token != parts.end()) {
79 if (WrtDB::GlobalDAOReadOnly::IsValidSubTag(
80 *token, RECORD_TYPE_VARIANT))
88 //'u' - unicode extension - only one BCP47 extension is registered.
89 //TODO: unicode extension should be also validated (l.wrzosek)
90 if (token == parts.end()) {
95 bool one_or_more = false;
96 while (token != parts.end() &&
109 if (token == parts.end()) {
112 if (*token == L"x") {
114 bool one_or_more = false;
115 while (token != parts.end() &&
127 if (token == parts.end()) {
131 //Try private use now:
132 token = parts.begin();
133 if (*token == L"x") {
135 bool one_or_more = false;
136 while (token != parts.end() &&
146 //grandfathered is always rejected
150 #define TEST_LANG(str, cond) \
151 if (LanguageSubtagRstTreeSingleton::Instance(). \
152 ValidateLanguageTag(str) == cond) { \
153 LogDebug("Good validate status for lang: " << str); \
155 LogError("Wrong validate status for lang: " << str \
156 << ", should be " << cond); \
159 void LanguageSubtagRstTree::Initialize()
161 /* Temporarily added unit test. Commented out due to performance drop.
162 * TEST_LANG("zh", true);
163 * TEST_LANG("esx-al", true);
164 * TEST_LANG("zh-Hant", true);
165 * TEST_LANG("zh-Hant-CN", true);
166 * TEST_LANG("zh-Hant-CN-x-private1-private2", true);
167 * TEST_LANG("plxxx", false);
168 * TEST_LANG("pl-x-private111", false);
169 * TEST_LANG("x-private1", false); //do not support pure private ones
170 * TEST_LANG("x-private22", false);
171 * TEST_LANG("i-private22", false); //do not support i-*