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 <indexentrysupplier_default.hxx>
21 : #include <localedata.hxx>
22 : #include <i18nutil/unicode.hxx>
23 : #include <com/sun/star/i18n/CollatorOptions.hpp>
24 :
25 : using namespace ::com::sun::star::uno;
26 : using namespace ::com::sun::star::lang;
27 : using namespace ::rtl;
28 :
29 : namespace com { namespace sun { namespace star { namespace i18n {
30 :
31 0 : IndexEntrySupplier_Unicode::IndexEntrySupplier_Unicode(
32 : const com::sun::star::uno::Reference < com::sun::star::uno::XComponentContext >& rxContext ) :
33 0 : IndexEntrySupplier_Common(rxContext)
34 : {
35 0 : implementationName = "com.sun.star.i18n.IndexEntrySupplier_Unicode";
36 0 : index = new Index(rxContext);
37 0 : }
38 :
39 0 : IndexEntrySupplier_Unicode::~IndexEntrySupplier_Unicode()
40 : {
41 0 : delete index;
42 0 : }
43 :
44 0 : sal_Bool SAL_CALL IndexEntrySupplier_Unicode::loadAlgorithm( const lang::Locale& rLocale,
45 : const OUString& rAlgorithm, sal_Int32 collatorOptions ) throw (RuntimeException)
46 : {
47 0 : index->init(rLocale, rAlgorithm);
48 0 : return IndexEntrySupplier_Common::loadAlgorithm(rLocale, rAlgorithm, collatorOptions);
49 : }
50 :
51 0 : OUString SAL_CALL IndexEntrySupplier_Unicode::getIndexKey( const OUString& rIndexEntry,
52 : const OUString& rPhoneticEntry, const lang::Locale& rLocale ) throw (RuntimeException)
53 : {
54 0 : return index->getIndexDescription(getEntry(rIndexEntry, rPhoneticEntry, rLocale));
55 : }
56 :
57 0 : sal_Int16 SAL_CALL IndexEntrySupplier_Unicode::compareIndexEntry(
58 : const OUString& rIndexEntry1, const OUString& rPhoneticEntry1, const lang::Locale& rLocale1,
59 : const OUString& rIndexEntry2, const OUString& rPhoneticEntry2, const lang::Locale& rLocale2 )
60 : throw (RuntimeException)
61 : {
62 : sal_Int16 result =
63 0 : index->getIndexWeight(getEntry(rIndexEntry1, rPhoneticEntry1, rLocale1)) -
64 0 : index->getIndexWeight(getEntry(rIndexEntry2, rPhoneticEntry2, rLocale2));
65 0 : if (result == 0)
66 : return IndexEntrySupplier_Common::compareIndexEntry(
67 : rIndexEntry1, rPhoneticEntry1, rLocale1,
68 0 : rIndexEntry2, rPhoneticEntry2, rLocale2);
69 0 : return result > 0 ? 1 : -1;
70 : }
71 :
72 0 : OUString SAL_CALL IndexEntrySupplier_Unicode::getIndexCharacter( const OUString& rIndexEntry,
73 : const lang::Locale& rLocale, const OUString& rAlgorithm ) throw (RuntimeException) {
74 :
75 0 : if (loadAlgorithm( rLocale, rAlgorithm, CollatorOptions::CollatorOptions_IGNORE_CASE_ACCENT))
76 0 : return index->getIndexDescription(rIndexEntry);
77 : else
78 0 : return IndexEntrySupplier_Common::getIndexCharacter(rIndexEntry, rLocale, rAlgorithm);
79 : }
80 :
81 0 : IndexTable::IndexTable()
82 : {
83 0 : table = NULL;
84 0 : }
85 :
86 0 : IndexTable::~IndexTable()
87 : {
88 0 : if (table) free(table);
89 0 : }
90 :
91 0 : void IndexTable::init(sal_Unicode start_, sal_Unicode end_, IndexKey *keys, sal_Int16 key_count, Index *index)
92 : {
93 0 : start=start_;
94 0 : end=end_;
95 0 : table = (sal_uInt8*) malloc((end-start+1)*sizeof(sal_uInt8));
96 0 : for (sal_Unicode i = start; i <= end; i++) {
97 : sal_Int16 j;
98 0 : for (j = 0; j < key_count; j++) {
99 0 : if (keys[j].key > 0 && (i == keys[j].key || index->compare(i, keys[j].key) == 0)) {
100 0 : table[i-start] = sal::static_int_cast<sal_uInt8>(j);
101 0 : break;
102 : }
103 : }
104 0 : if (j == key_count)
105 0 : table[i-start] = 0xFF;
106 : }
107 0 : }
108 :
109 0 : Index::Index(const com::sun::star::uno::Reference < com::sun::star::uno::XComponentContext >& rxContext)
110 : {
111 0 : collator = new CollatorImpl(rxContext);
112 0 : }
113 :
114 0 : Index::~Index()
115 : {
116 0 : delete collator;
117 0 : }
118 :
119 0 : sal_Int16 Index::compare(sal_Unicode c1, sal_Unicode c2)
120 : {
121 0 : return sal::static_int_cast<sal_Int16>( collator->compareString(OUString(&c1, 1), OUString(&c2, 1)) );
122 : }
123 :
124 0 : sal_Int16 Index::getIndexWeight(const OUString& rIndexEntry)
125 : {
126 0 : sal_Int32 startPos=0;
127 0 : if (!skipping_chars.isEmpty())
128 0 : while (skipping_chars.indexOf(rIndexEntry[startPos]) >= 0)
129 0 : startPos++;
130 0 : if (mkey_count > 0) {
131 0 : for (sal_Int16 i = 0; i < mkey_count; i++) {
132 0 : sal_Int32 len = keys[mkeys[i]].mkey.getLength();
133 0 : if (collator->compareSubstring(rIndexEntry, startPos, len,
134 0 : keys[mkeys[i]].mkey, 0, len) == 0)
135 0 : return mkeys[i];
136 : }
137 : }
138 0 : sal_Unicode code = rIndexEntry[startPos];
139 0 : for (sal_Int16 i = 0; i < table_count; i++) {
140 0 : if (tables[i].start <= code && code <= tables[i].end)
141 0 : return tables[i].table[code-tables[i].start];
142 : }
143 0 : return 0xFF;
144 : }
145 :
146 0 : OUString Index::getIndexDescription(const OUString& rIndexEntry)
147 : {
148 0 : sal_Int16 wgt = getIndexWeight(rIndexEntry);
149 0 : if (wgt < MAX_KEYS) {
150 0 : if (!keys[wgt].desc.isEmpty())
151 0 : return keys[wgt].desc;
152 0 : else if (keys[wgt].key > 0)
153 0 : return OUString(&keys[wgt].key, 1);
154 : else
155 0 : return keys[wgt].mkey;
156 : }
157 0 : sal_Int32 nPos=0;
158 0 : sal_uInt32 indexChar=rIndexEntry.iterateCodePoints(&nPos, 0);
159 0 : return OUString(&indexChar, 1);
160 : }
161 :
162 : #define LOCALE_EN lang::Locale(OUString("en"), OUString(), OUString())
163 :
164 0 : void Index::makeIndexKeys(const lang::Locale &rLocale, const OUString &algorithm) throw (RuntimeException)
165 : {
166 0 : OUString keyStr = LocaleData().getIndexKeysByAlgorithm(rLocale, algorithm);
167 :
168 0 : if (keyStr.isEmpty()) {
169 : keyStr = LocaleData().getIndexKeysByAlgorithm(LOCALE_EN,
170 0 : LocaleData().getDefaultIndexAlgorithm(LOCALE_EN));
171 0 : if (keyStr.isEmpty())
172 0 : throw RuntimeException();
173 : }
174 :
175 0 : sal_Int16 len = sal::static_int_cast<sal_Int16>( keyStr.getLength() );
176 0 : mkey_count=key_count=0;
177 0 : skipping_chars=OUString();
178 : sal_Int16 i, j;
179 :
180 0 : for (i = 0; i < len && key_count < MAX_KEYS; i++)
181 : {
182 0 : sal_Unicode curr = keyStr[i];
183 0 : sal_Unicode close = sal_Unicode(')');
184 :
185 0 : if (unicode::isWhiteSpace(curr))
186 0 : continue;
187 :
188 0 : switch(curr) {
189 : case sal_Unicode('-'):
190 0 : if (key_count > 0 && i + 1 < len ) {
191 0 : for (curr = keyStr[++i]; key_count < MAX_KEYS && keys[key_count-1].key < curr; key_count++) {
192 0 : keys[key_count].key = keys[key_count-1].key+1;
193 0 : keys[key_count].desc = OUString();
194 : }
195 : } else
196 0 : throw RuntimeException();
197 0 : break;
198 : case sal_Unicode('['):
199 0 : for (i++; i < len && keyStr[i] != sal_Unicode(']'); i++) {
200 0 : if (unicode::isWhiteSpace(keyStr[i])) {
201 0 : continue;
202 0 : } else if (keyStr[i] == sal_Unicode('_')) {
203 0 : for (curr=keyStr[i-1]+1; curr <= keyStr[i+1]; curr++)
204 0 : skipping_chars+=OUString(curr);
205 0 : i+=2;
206 : } else {
207 0 : skipping_chars+=OUString(keyStr[i]);
208 : }
209 : }
210 0 : break;
211 : case sal_Unicode('{'):
212 0 : close = sal_Unicode('}');
213 : case sal_Unicode('('):
214 0 : if (key_count > 0) {
215 0 : sal_Int16 end = i+1;
216 0 : for (end=i+1; end < len && keyStr[end] != close; end++) ;
217 :
218 0 : if (end >= len) // no found
219 0 : throw RuntimeException();
220 0 : if (close == sal_Unicode(')'))
221 0 : keys[key_count-1].desc = keyStr.copy(i+1, end-i-1);
222 : else {
223 0 : mkeys[mkey_count++]=key_count;
224 0 : keys[key_count].key = 0;
225 0 : keys[key_count].mkey = keyStr.copy(i+1, end-i-1);
226 0 : keys[key_count++].desc=OUString();
227 : }
228 0 : i=end+1;
229 : } else
230 0 : throw RuntimeException();
231 0 : break;
232 : default:
233 0 : keys[key_count].key = curr;
234 0 : keys[key_count++].desc = OUString();
235 0 : break;
236 : }
237 : }
238 0 : for (i = 0; i < mkey_count; i++) {
239 0 : for (j=i+1; j < mkey_count; j++) {
240 0 : if (keys[mkeys[i]].mkey.getLength() < keys[mkeys[j]].mkey.getLength()) {
241 0 : sal_Int16 k = mkeys[i];
242 0 : mkeys[i] = mkeys[j];
243 0 : mkeys[j] = k;
244 : }
245 : }
246 0 : }
247 0 : }
248 :
249 0 : void Index::init(const lang::Locale &rLocale, const OUString& algorithm) throw (RuntimeException)
250 : {
251 0 : makeIndexKeys(rLocale, algorithm);
252 :
253 0 : Sequence< UnicodeScript > scriptList = LocaleData().getUnicodeScripts( rLocale );
254 :
255 0 : if (scriptList.getLength() == 0) {
256 0 : scriptList = LocaleData().getUnicodeScripts(LOCALE_EN);
257 0 : if (scriptList.getLength() == 0)
258 0 : throw RuntimeException();
259 : }
260 :
261 0 : table_count = sal::static_int_cast<sal_Int16>( scriptList.getLength() );
262 0 : if (table_count > MAX_TABLES)
263 0 : throw RuntimeException();
264 :
265 0 : collator->loadCollatorAlgorithm(algorithm, rLocale, CollatorOptions::CollatorOptions_IGNORE_CASE_ACCENT);
266 0 : sal_Int16 j=0;
267 0 : sal_Unicode start = unicode::getUnicodeScriptStart((UnicodeScript)0);
268 0 : sal_Unicode end = unicode::getUnicodeScriptEnd((UnicodeScript)0);
269 0 : for (sal_Int16 i= (scriptList[0] == (UnicodeScript)0) ? 1 : 0; i< scriptList.getLength(); i++) {
270 0 : if (unicode::getUnicodeScriptStart(scriptList[i]) != end+1) {
271 0 : tables[j++].init(start, end, keys, key_count, this);
272 0 : start = unicode::getUnicodeScriptStart(scriptList[i]);
273 : }
274 0 : end = unicode::getUnicodeScriptEnd(scriptList[i]);
275 : }
276 0 : tables[j++].init(start, end, keys, key_count, this);
277 0 : table_count = j;
278 0 : }
279 :
280 : } } } }
281 :
282 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|