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