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 
27 #include <indexentrysupplier_common.hxx>
28 #include <com/sun/star/i18n/CollatorOptions.hpp>
29 #include <localedata.hxx>
30 
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star;
33 using namespace ::rtl;
34 
35 namespace com { namespace sun { namespace star { namespace i18n {
36 
IndexEntrySupplier_Common(const Reference<lang::XMultiServiceFactory> & rxMSF)37 IndexEntrySupplier_Common::IndexEntrySupplier_Common(const Reference < lang::XMultiServiceFactory >& rxMSF)
38 {
39 	implementationName = "com.sun.star.i18n.IndexEntrySupplier_Common";
40 	collator = new CollatorImpl(rxMSF);
41 	usePhonetic = sal_False;
42 }
43 
~IndexEntrySupplier_Common()44 IndexEntrySupplier_Common::~IndexEntrySupplier_Common()
45 {
46 	delete collator;
47 }
48 
getLocaleList()49 Sequence < lang::Locale > SAL_CALL IndexEntrySupplier_Common::getLocaleList() throw (RuntimeException)
50 {
51     throw RuntimeException();
52 }
53 
getAlgorithmList(const lang::Locale &)54 Sequence < OUString > SAL_CALL IndexEntrySupplier_Common::getAlgorithmList( const lang::Locale& ) throw (RuntimeException)
55 {
56     throw RuntimeException();
57 }
58 
getPhoneticCandidate(const OUString &,const lang::Locale &)59 OUString SAL_CALL IndexEntrySupplier_Common::getPhoneticCandidate( const OUString&,
60 	const lang::Locale& ) throw (RuntimeException)
61 {
62     return OUString();
63 }
64 
usePhoneticEntry(const lang::Locale &)65 sal_Bool SAL_CALL IndexEntrySupplier_Common::usePhoneticEntry( const lang::Locale& ) throw (RuntimeException)
66 {
67     throw RuntimeException();
68 }
69 
loadAlgorithm(const lang::Locale & rLocale,const OUString & rAlgorithm,sal_Int32 collatorOptions)70 sal_Bool SAL_CALL IndexEntrySupplier_Common::loadAlgorithm( const lang::Locale& rLocale,
71 	const OUString& rAlgorithm, sal_Int32 collatorOptions ) throw (RuntimeException)
72 {
73     usePhonetic = LocaleData().isPhonetic(rLocale, rAlgorithm);
74     collator->loadCollatorAlgorithm(rAlgorithm, rLocale, collatorOptions);
75     aLocale = rLocale;
76     aAlgorithm = rAlgorithm;
77     return sal_True;
78 }
79 
getIndexKey(const OUString & rIndexEntry,const OUString &,const lang::Locale &)80 OUString SAL_CALL IndexEntrySupplier_Common::getIndexKey( const OUString& rIndexEntry,
81 	const OUString&, const lang::Locale& ) throw (RuntimeException)
82 {
83     sal_Int32 nPos=0;
84     sal_uInt32 indexChar=rIndexEntry.iterateCodePoints(&nPos, 0);
85     return OUString(&indexChar, 1);
86 }
87 
compareIndexEntry(const OUString & rIndexEntry1,const OUString &,const lang::Locale &,const OUString & rIndexEntry2,const OUString &,const lang::Locale &)88 sal_Int16 SAL_CALL IndexEntrySupplier_Common::compareIndexEntry(
89 	const OUString& rIndexEntry1, const OUString&, const lang::Locale&,
90 	const OUString& rIndexEntry2, const OUString&, const lang::Locale& )
91 	throw (RuntimeException)
92 {
93     return sal::static_int_cast< sal_Int16 >(
94         collator->compareString(rIndexEntry1, rIndexEntry2));
95         // return value of compareString in { -1, 0, 1 }
96 }
97 
getIndexCharacter(const OUString & rIndexEntry,const lang::Locale & rLocale,const OUString &)98 OUString SAL_CALL IndexEntrySupplier_Common::getIndexCharacter( const OUString& rIndexEntry,
99 	const lang::Locale& rLocale, const OUString& ) throw (RuntimeException)
100 {
101     return getIndexKey(rIndexEntry, rIndexEntry, rLocale);
102 }
103 
getIndexFollowPageWord(sal_Bool,const lang::Locale &)104 OUString SAL_CALL IndexEntrySupplier_Common::getIndexFollowPageWord( sal_Bool,
105 	const lang::Locale& ) throw (RuntimeException)
106 {
107     throw RuntimeException();
108 }
109 
110 const OUString& SAL_CALL
getEntry(const OUString & IndexEntry,const OUString & PhoneticEntry,const lang::Locale & rLocale)111 IndexEntrySupplier_Common::getEntry( const OUString& IndexEntry,
112 	const OUString& PhoneticEntry, const lang::Locale& rLocale ) throw (RuntimeException)
113 {
114 	// The condition for using phonetic entry is:
115 	// usePhonetic is set for the algorithm;
116 	// rLocale for phonetic entry is same as aLocale for algorithm,
117 	// which means Chinese phonetic will not be used for Japanese algorithm;
118 	// phonetic entry is not blank.
119 	if (usePhonetic && PhoneticEntry.getLength() > 0 && rLocale.Language == aLocale.Language &&
120             rLocale.Country == aLocale.Country && rLocale.Variant == aLocale.Variant)
121 	    return PhoneticEntry;
122 	else
123 	    return IndexEntry;
124 }
125 
126 OUString SAL_CALL
getImplementationName()127 IndexEntrySupplier_Common::getImplementationName() throw( RuntimeException )
128 {
129     return OUString::createFromAscii( implementationName );
130 }
131 
132 sal_Bool SAL_CALL
supportsService(const OUString & rServiceName)133 IndexEntrySupplier_Common::supportsService(const OUString& rServiceName) throw( RuntimeException )
134 {
135     return rServiceName.compareToAscii(implementationName) == 0;
136 }
137 
138 Sequence< OUString > SAL_CALL
getSupportedServiceNames()139 IndexEntrySupplier_Common::getSupportedServiceNames() throw( RuntimeException )
140 {
141     Sequence< OUString > aRet(1);
142     aRet[0] = OUString::createFromAscii( implementationName );
143     return aRet;
144 }
145 
146 } } } }
147