xref: /trunk/main/i18npool/source/indexentry/indexentrysupplier_default.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_i18npool.hxx"
26 #include <indexentrysupplier_default.hxx>
27 #include <localedata.hxx>
28 #include <i18nutil/unicode.hxx>
29 #include <com/sun/star/i18n/CollatorOptions.hpp>
30 
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::lang;
33 using namespace ::rtl;
34 
35 namespace com { namespace sun { namespace star { namespace i18n {
36 
IndexEntrySupplier_Unicode(const com::sun::star::uno::Reference<com::sun::star::lang::XMultiServiceFactory> & rxMSF)37 IndexEntrySupplier_Unicode::IndexEntrySupplier_Unicode(
38     const com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory >& rxMSF ) :
39     IndexEntrySupplier_Common(rxMSF)
40 {
41     implementationName = "com.sun.star.i18n.IndexEntrySupplier_Unicode";
42     index = new Index(rxMSF);
43 }
44 
~IndexEntrySupplier_Unicode()45 IndexEntrySupplier_Unicode::~IndexEntrySupplier_Unicode()
46 {
47     delete index;
48 }
49 
loadAlgorithm(const lang::Locale & rLocale,const OUString & rAlgorithm,sal_Int32 collatorOptions)50 sal_Bool SAL_CALL IndexEntrySupplier_Unicode::loadAlgorithm( const lang::Locale& rLocale,
51     const OUString& rAlgorithm, sal_Int32 collatorOptions )
52 {
53     index->init(rLocale, rAlgorithm);
54     return IndexEntrySupplier_Common::loadAlgorithm(rLocale, rAlgorithm, collatorOptions);
55 }
56 
getIndexKey(const OUString & rIndexEntry,const OUString & rPhoneticEntry,const lang::Locale & rLocale)57 OUString SAL_CALL IndexEntrySupplier_Unicode::getIndexKey( const OUString& rIndexEntry,
58     const OUString& rPhoneticEntry, const lang::Locale& rLocale )
59 {
60     return index->getIndexDescription(getEntry(rIndexEntry, rPhoneticEntry, rLocale));
61 }
62 
compareIndexEntry(const OUString & rIndexEntry1,const OUString & rPhoneticEntry1,const lang::Locale & rLocale1,const OUString & rIndexEntry2,const OUString & rPhoneticEntry2,const lang::Locale & rLocale2)63 sal_Int16 SAL_CALL IndexEntrySupplier_Unicode::compareIndexEntry(
64     const OUString& rIndexEntry1, const OUString& rPhoneticEntry1, const lang::Locale& rLocale1,
65     const OUString& rIndexEntry2, const OUString& rPhoneticEntry2, const lang::Locale& rLocale2 )
66 {
67     sal_Int16 result =
68             index->getIndexWeight(getEntry(rIndexEntry1, rPhoneticEntry1, rLocale1)) -
69             index->getIndexWeight(getEntry(rIndexEntry2, rPhoneticEntry2, rLocale2));
70     if (result == 0)
71         return IndexEntrySupplier_Common::compareIndexEntry(
72                     rIndexEntry1, rPhoneticEntry1, rLocale1,
73                     rIndexEntry2, rPhoneticEntry2, rLocale2);
74     return result > 0 ? 1 : -1;
75 }
76 
getIndexCharacter(const OUString & rIndexEntry,const lang::Locale & rLocale,const OUString & rAlgorithm)77 OUString SAL_CALL IndexEntrySupplier_Unicode::getIndexCharacter( const OUString& rIndexEntry,
78     const lang::Locale& rLocale, const OUString& rAlgorithm ) {
79 
80     if (loadAlgorithm( rLocale, rAlgorithm, CollatorOptions::CollatorOptions_IGNORE_CASE_ACCENT))
81         return index->getIndexDescription(rIndexEntry);
82     else
83         return IndexEntrySupplier_Common::getIndexCharacter(rIndexEntry, rLocale, rAlgorithm);
84 }
85 
IndexTable()86 IndexTable::IndexTable()
87 {
88     table = NULL;
89 }
90 
~IndexTable()91 IndexTable::~IndexTable()
92 {
93     if (table) free(table);
94 }
95 
init(sal_Unicode start_,sal_Unicode end_,IndexKey * keys,sal_Int16 key_count,Index * index)96 void IndexTable::init(sal_Unicode start_, sal_Unicode end_, IndexKey *keys, sal_Int16 key_count, Index *index)
97 {
98     start=start_;
99     end=end_;
100     table = (sal_uInt8*) malloc((end-start+1)*sizeof(sal_uInt8));
101     for (sal_Unicode i = start; i <= end; i++) {
102         sal_Int16 j;
103         for (j = 0; j < key_count; j++) {
104             if (keys[j].key > 0 && (i == keys[j].key || index->compare(i, keys[j].key) == 0)) {
105                 table[i-start] = sal::static_int_cast<sal_uInt8>(j);
106                 break;
107             }
108         }
109         if (j == key_count)
110             table[i-start] = 0xFF;
111     }
112 }
113 
Index(const com::sun::star::uno::Reference<com::sun::star::lang::XMultiServiceFactory> & rxMSF)114 Index::Index(const com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory >& rxMSF)
115 {
116     collator = new CollatorImpl(rxMSF);
117 }
118 
~Index()119 Index::~Index()
120 {
121     delete collator;
122 }
123 
compare(sal_Unicode c1,sal_Unicode c2)124 sal_Int16 Index::compare(sal_Unicode c1, sal_Unicode c2)
125 {
126     return sal::static_int_cast<sal_Int16>( collator->compareString(OUString(&c1, 1), OUString(&c2, 1)) );
127 }
128 
getIndexWeight(const OUString & rIndexEntry)129 sal_Int16 Index::getIndexWeight(const OUString& rIndexEntry)
130 {
131     sal_Int32 startPos=0;
132     if (skipping_chars.getLength() > 0)
133         while (skipping_chars.indexOf(rIndexEntry[startPos]) >= 0)
134             startPos++;
135     if (mkey_count > 0) {
136         for (sal_Int16 i = 0; i < mkey_count; i++) {
137             sal_Int32 len = keys[mkeys[i]].mkey.getLength();
138             if (collator->compareSubstring(rIndexEntry, startPos, len,
139                                     keys[mkeys[i]].mkey, 0, len) == 0)
140                 return mkeys[i];
141         }
142     }
143     sal_Unicode code = rIndexEntry[startPos];
144     for (sal_Int16 i = 0; i < table_count; i++) {
145         if (tables[i].start <= code && code <= tables[i].end)
146             return tables[i].table[code-tables[i].start];
147     }
148     return 0xFF;
149 }
150 
getIndexDescription(const OUString & rIndexEntry)151 OUString Index::getIndexDescription(const OUString& rIndexEntry)
152 {
153     sal_Int16 wgt = getIndexWeight(rIndexEntry);
154     if (wgt < MAX_KEYS) {
155         if (keys[wgt].desc.getLength())
156             return keys[wgt].desc;
157         else if (keys[wgt].key > 0)
158             return OUString(&keys[wgt].key, 1);
159         else
160             return keys[wgt].mkey;
161     }
162     sal_Int32 nPos=0;
163     sal_uInt32 indexChar=rIndexEntry.iterateCodePoints(&nPos, 0);
164     return OUString(&indexChar, 1);
165 }
166 
167 #define LOCALE_EN lang::Locale(OUString::createFromAscii("en"), OUString(), OUString())
168 
makeIndexKeys(const lang::Locale & rLocale,const OUString & algorithm)169 void Index::makeIndexKeys(const lang::Locale &rLocale, const OUString &algorithm)
170 {
171     OUString keyStr = LocaleData().getIndexKeysByAlgorithm(rLocale, algorithm);
172 
173     if (!keyStr.getLength()) {
174         keyStr = LocaleData().getIndexKeysByAlgorithm(LOCALE_EN,
175                     LocaleData().getDefaultIndexAlgorithm(LOCALE_EN));
176         if( keyStr.isEmpty() )
177             throw RuntimeException();
178     }
179 
180     sal_Int16 len = sal::static_int_cast<sal_Int16>( keyStr.getLength() );
181     mkey_count=key_count=0;
182     skipping_chars=OUString();
183     sal_Int16 i, j;
184 
185     for (i = 0; i < len && key_count < MAX_KEYS; i++)
186     {
187         sal_Unicode curr = keyStr[i];
188         sal_Unicode close = sal_Unicode(')');
189 
190         if (unicode::isWhiteSpace(curr))
191             continue;
192 
193         switch(curr) {
194             case sal_Unicode('-'):
195                 if (key_count > 0 && i + 1 < len ) {
196                     for (curr = keyStr[++i]; key_count < MAX_KEYS && keys[key_count-1].key < curr; key_count++) {
197                         keys[key_count].key = keys[key_count-1].key+1;
198                         keys[key_count].desc = OUString();
199                     }
200                 } else
201                     throw RuntimeException();
202                 break;
203             case sal_Unicode('['):
204                 for (i++; i < len && keyStr[i] != sal_Unicode(']'); i++) {
205                     if (unicode::isWhiteSpace(keyStr[i])) {
206                         continue;
207                     } else if (keyStr[i] == sal_Unicode('_')) {
208                         for (curr=keyStr[i-1]+1;  curr <= keyStr[i+1]; curr++)
209                             skipping_chars+=OUString(curr);
210                         i+=2;
211                     } else {
212                         skipping_chars+=OUString(keyStr[i]);
213                     }
214                 }
215                 break;
216             case sal_Unicode('{'):
217                 close = sal_Unicode('}');
218             case sal_Unicode('('):
219                 if (key_count > 0) {
220                     sal_Int16 end = i+1;
221                     for (end=i+1; end < len && keyStr[end] != close; end++) ;
222 
223                     if (end >= len) // no found
224                         throw RuntimeException();
225                     if (close == sal_Unicode(')'))
226                         keys[key_count-1].desc = keyStr.copy(i+1, end-i-1);
227                     else {
228                         mkeys[mkey_count++]=key_count;
229                         keys[key_count].key = 0;
230                         keys[key_count].mkey = keyStr.copy(i+1, end-i-1);
231                         keys[key_count++].desc=OUString();
232                     }
233                     i=end+1;
234                 } else
235                     throw RuntimeException();
236                 break;
237             default:
238                 keys[key_count].key = curr;
239                 keys[key_count++].desc = OUString();
240                 break;
241         }
242     }
243     for (i = 0; i < mkey_count; i++) {
244         for (j=i+1; j < mkey_count; j++) {
245             if (keys[mkeys[i]].mkey.getLength() < keys[mkeys[j]].mkey.getLength()) {
246                 sal_Int16 k = mkeys[i];
247                 mkeys[i] = mkeys[j];
248                 mkeys[j] = k;
249             }
250         }
251     }
252 }
253 
init(const lang::Locale & rLocale,const OUString & algorithm)254 void Index::init(const lang::Locale &rLocale, const OUString& algorithm)
255 {
256     makeIndexKeys(rLocale, algorithm);
257 
258     Sequence< UnicodeScript > scriptList = LocaleData().getUnicodeScripts( rLocale );
259 
260     if (scriptList.getLength() == 0) {
261         scriptList = LocaleData().getUnicodeScripts(LOCALE_EN);
262         if (scriptList.getLength() == 0)
263             throw RuntimeException();
264     }
265 
266     table_count = sal::static_int_cast<sal_Int16>( scriptList.getLength() );
267     if (table_count > MAX_TABLES)
268         throw RuntimeException();
269 
270     collator->loadCollatorAlgorithm(algorithm, rLocale, CollatorOptions::CollatorOptions_IGNORE_CASE_ACCENT);
271     sal_Int16 j=0;
272     sal_Unicode start = unicode::getUnicodeScriptStart((UnicodeScript)0);
273     sal_Unicode end = unicode::getUnicodeScriptEnd((UnicodeScript)0);
274     for (sal_Int16 i= (scriptList[0] == (UnicodeScript)0) ? 1 : 0; i< scriptList.getLength(); i++) {
275         if (unicode::getUnicodeScriptStart(scriptList[i]) != end+1) {
276             tables[j++].init(start, end, keys, key_count, this);
277             start = unicode::getUnicodeScriptStart(scriptList[i]);
278         }
279         end = unicode::getUnicodeScriptEnd(scriptList[i]);
280     }
281     tables[j++].init(start, end, keys, key_count, this);
282     table_count = j;
283 }
284 
285 } } } }
286