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