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, RECORD_TYPE_LANGUAGE)) {
52 if (token == parts.end()) {
55 if (WrtDB::GlobalDAOReadOnly::IsValidSubTag(*token, RECORD_TYPE_EXTLANG)) {
59 if (token == parts.end()) {
62 if (WrtDB::GlobalDAOReadOnly::IsValidSubTag(*token, RECORD_TYPE_SCRIPT)) {
66 if (token == parts.end()) {
69 if (WrtDB::GlobalDAOReadOnly::IsValidSubTag(*token, RECORD_TYPE_REGION)) {
73 if (token == parts.end()) {
76 while (token != parts.end()) {
77 if (WrtDB::GlobalDAOReadOnly::IsValidSubTag(
78 *token, RECORD_TYPE_VARIANT))
86 //'u' - unicode extension - only one BCP47 extension is registered.
87 //TODO: unicode extension should be also validated (l.wrzosek)
88 if (token == parts.end()) {
93 bool one_or_more = false;
94 while (token != parts.end() &&
106 if (token == parts.end()) {
109 if (*token == L"x") {
111 bool one_or_more = false;
112 while (token != parts.end() &&
114 token->size() <= 8) {
123 if (token == parts.end()) {
127 //Try private use now:
128 token = parts.begin();
129 if (*token == L"x") {
131 bool one_or_more = false;
132 while (token != parts.end() &&
134 token->size() <= 8) {
141 //grandfathered is always rejected
145 #define TEST_LANG(str, cond) \
146 if (LanguageSubtagRstTreeSingleton::Instance().\
147 ValidateLanguageTag(str) == cond) {\
148 LogDebug("Good validate status for lang: " << str);\
150 LogError("Wrong validate status for lang: " << str\
151 << ", should be " << cond);\
154 void LanguageSubtagRstTree::Initialize()
156 /* Temporarily added unit test. Commented out due to performance drop.
157 TEST_LANG("zh", true);
158 TEST_LANG("esx-al", true);
159 TEST_LANG("zh-Hant", true);
160 TEST_LANG("zh-Hant-CN", true);
161 TEST_LANG("zh-Hant-CN-x-private1-private2", true);
162 TEST_LANG("plxxx", false);
163 TEST_LANG("pl-x-private111", false);
164 TEST_LANG("x-private1", false); //do not support pure private ones
165 TEST_LANG("x-private22", false);
166 TEST_LANG("i-private22", false); //do not support i-*