Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "cellkeytranslator.hxx"
21 : #include <comphelper/processfactory.hxx>
22 : #include <i18nlangtag/mslangid.hxx>
23 : #include <i18nlangtag/lang.h>
24 : #include <rtl/ustring.hxx>
25 :
26 : #include <com/sun/star/i18n/TransliterationModules.hpp>
27 :
28 : using ::com::sun::star::uno::Sequence;
29 : using ::std::list;
30 : using ::boost::unordered_map;
31 :
32 : using namespace ::com::sun::star;
33 :
34 : enum LocaleMatch
35 : {
36 : LOCALE_MATCH_NONE = 0,
37 : LOCALE_MATCH_LANG,
38 : LOCALE_MATCH_LANG_SCRIPT,
39 : LOCALE_MATCH_LANG_SCRIPT_COUNTRY,
40 : LOCALE_MATCH_ALL
41 : };
42 :
43 0 : static LocaleMatch lclLocaleCompare(const lang::Locale& rLocale1, const LanguageTag& rLanguageTag2)
44 : {
45 0 : LocaleMatch eMatchLevel = LOCALE_MATCH_NONE;
46 0 : LanguageTag aLanguageTag1( rLocale1);
47 :
48 0 : if ( aLanguageTag1.getLanguage() == rLanguageTag2.getLanguage() )
49 0 : eMatchLevel = LOCALE_MATCH_LANG;
50 : else
51 0 : return eMatchLevel;
52 :
53 0 : if ( aLanguageTag1.getScript() == rLanguageTag2.getScript() )
54 0 : eMatchLevel = LOCALE_MATCH_LANG_SCRIPT;
55 : else
56 0 : return eMatchLevel;
57 :
58 0 : if ( aLanguageTag1.getCountry() == rLanguageTag2.getCountry() )
59 0 : eMatchLevel = LOCALE_MATCH_LANG_SCRIPT_COUNTRY;
60 : else
61 0 : return eMatchLevel;
62 :
63 0 : if (aLanguageTag1 == rLanguageTag2)
64 0 : return LOCALE_MATCH_ALL;
65 :
66 0 : return eMatchLevel;
67 : }
68 :
69 188 : ScCellKeyword::ScCellKeyword(const sal_Char* pName, OpCode eOpCode, const lang::Locale& rLocale) :
70 : mpName(pName),
71 : meOpCode(eOpCode),
72 188 : mrLocale(rLocale)
73 : {
74 188 : }
75 :
76 76 : ::std::unique_ptr<ScCellKeywordTranslator> ScCellKeywordTranslator::spInstance;
77 :
78 56 : static void lclMatchKeyword(OUString& rName, const ScCellKeywordHashMap& aMap,
79 : OpCode eOpCode = ocNone, const lang::Locale* pLocale = NULL)
80 : {
81 56 : ScCellKeywordHashMap::const_iterator itrEnd = aMap.end();
82 56 : ScCellKeywordHashMap::const_iterator itr = aMap.find(rName);
83 :
84 56 : if ( itr == itrEnd || itr->second.empty() )
85 : // No candidate strings exist. Bail out.
86 112 : return;
87 :
88 0 : if ( eOpCode == ocNone && !pLocale )
89 : {
90 : // Since no locale nor opcode matching is needed, simply return
91 : // the first item on the list.
92 0 : rName = OUString::createFromAscii( itr->second.front().mpName );
93 0 : return;
94 : }
95 :
96 0 : LanguageTag aLanguageTag( pLocale ? *pLocale : lang::Locale("","",""));
97 0 : const sal_Char* aBestMatchName = itr->second.front().mpName;
98 0 : LocaleMatch eLocaleMatchLevel = LOCALE_MATCH_NONE;
99 0 : bool bOpCodeMatched = false;
100 :
101 0 : list<ScCellKeyword>::const_iterator itrListEnd = itr->second.end();
102 0 : list<ScCellKeyword>::const_iterator itrList = itr->second.begin();
103 0 : for ( ; itrList != itrListEnd; ++itrList )
104 : {
105 0 : if ( eOpCode != ocNone && pLocale )
106 : {
107 0 : if ( itrList->meOpCode == eOpCode )
108 : {
109 0 : LocaleMatch eLevel = lclLocaleCompare(itrList->mrLocale, aLanguageTag);
110 0 : if ( eLevel == LOCALE_MATCH_ALL )
111 : {
112 : // Name with matching opcode and locale found.
113 0 : rName = OUString::createFromAscii( itrList->mpName );
114 0 : return;
115 : }
116 0 : else if ( eLevel > eLocaleMatchLevel )
117 : {
118 : // Name with a better matching locale.
119 0 : eLocaleMatchLevel = eLevel;
120 0 : aBestMatchName = itrList->mpName;
121 : }
122 0 : else if ( !bOpCodeMatched )
123 : // At least the opcode matches.
124 0 : aBestMatchName = itrList->mpName;
125 :
126 0 : bOpCodeMatched = true;
127 0 : }
128 : }
129 0 : else if ( eOpCode != ocNone && !pLocale )
130 : {
131 0 : if ( itrList->meOpCode == eOpCode )
132 : {
133 : // Name with a matching opcode preferred.
134 0 : rName = OUString::createFromAscii( itrList->mpName );
135 0 : return;
136 : }
137 : }
138 0 : else if ( pLocale )
139 : {
140 0 : LocaleMatch eLevel = lclLocaleCompare(itrList->mrLocale, aLanguageTag);
141 0 : if ( eLevel == LOCALE_MATCH_ALL )
142 : {
143 : // Name with matching locale preferred.
144 0 : rName = OUString::createFromAscii( itrList->mpName );
145 0 : return;
146 : }
147 0 : else if ( eLevel > eLocaleMatchLevel )
148 : {
149 : // Name with a better matching locale.
150 0 : eLocaleMatchLevel = eLevel;
151 0 : aBestMatchName = itrList->mpName;
152 : }
153 : }
154 : }
155 :
156 : // No preferred strings found. Return the best matching name.
157 0 : rName = OUString::createFromAscii(aBestMatchName);
158 : }
159 :
160 56 : void ScCellKeywordTranslator::transKeyword(OUString& rName, const lang::Locale* pLocale, OpCode eOpCode)
161 : {
162 56 : if ( !spInstance.get() )
163 4 : spInstance.reset( new ScCellKeywordTranslator );
164 :
165 56 : LanguageType eLang = pLocale ? LanguageTag(*pLocale).makeFallback().getLanguageType() : LANGUAGE_SYSTEM;
166 56 : Sequence<sal_Int32> aOffsets;
167 56 : rName = spInstance->maTransWrapper.transliterate(rName, eLang, 0, rName.getLength(), &aOffsets);
168 56 : lclMatchKeyword(rName, spInstance->maStringNameMap, eOpCode, pLocale);
169 56 : }
170 :
171 4 : ScCellKeywordTranslator::ScCellKeywordTranslator() :
172 : maTransWrapper( ::comphelper::getProcessComponentContext(),
173 4 : i18n::TransliterationModules_LOWERCASE_UPPERCASE )
174 : {
175 4 : init();
176 4 : }
177 :
178 4 : ScCellKeywordTranslator::~ScCellKeywordTranslator()
179 : {
180 4 : }
181 :
182 : struct TransItem
183 : {
184 : const sal_Unicode* from;
185 : const sal_Char* to;
186 : OpCode func;
187 : };
188 :
189 4 : void ScCellKeywordTranslator::init()
190 : {
191 4 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
192 :
193 : // The file below has been autogenerated by sc/workben/celltrans/parse.py.
194 : // To add new locale keywords, edit sc/workben/celltrans/keywords_utf16.txt
195 : // and re-run the parse.py script.
196 : //
197 : // All keywords must be uppercase, and the mapping must be from the
198 : // localized keyword to the English keyword.
199 : //
200 : // Make sure that the original keyword file (keywords_utf16.txt) is
201 : // encoded in UCS-2/UTF-16!
202 :
203 : #include "cellkeywords.inl"
204 4 : }
205 :
206 188 : void ScCellKeywordTranslator::addToMap(const OUString& rKey, const sal_Char* pName, const lang::Locale& rLocale, OpCode eOpCode)
207 : {
208 188 : ScCellKeyword aKeyItem( pName, eOpCode, rLocale );
209 :
210 188 : ScCellKeywordHashMap::iterator itrEnd = maStringNameMap.end();
211 188 : ScCellKeywordHashMap::iterator itr = maStringNameMap.find(rKey);
212 :
213 188 : if ( itr == itrEnd )
214 : {
215 : // New keyword.
216 180 : list<ScCellKeyword> aList;
217 180 : aList.push_back(aKeyItem);
218 180 : maStringNameMap.insert( ScCellKeywordHashMap::value_type(rKey, aList) );
219 : }
220 : else
221 8 : itr->second.push_back(aKeyItem);
222 188 : }
223 :
224 12 : void ScCellKeywordTranslator::addToMap(const TransItem* pItems, const lang::Locale& rLocale)
225 : {
226 200 : for (sal_uInt16 i = 0; pItems[i].from != NULL; ++i)
227 188 : addToMap(OUString(pItems[i].from), pItems[i].to, rLocale, pItems[i].func);
228 240 : }
229 :
230 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|