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_unotools.hxx"
26 #include <unotools/collatorwrapper.hxx>
27 #include <tools/debug.hxx>
28 
29 #ifndef _COMPHELPER_COMPONENTFACTORY_HXX_
30 #include <comphelper/componentfactory.hxx>
31 #endif
32 #include <com/sun/star/uno/XInterface.hpp>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 
35 using namespace ::com::sun::star;
36 
CollatorWrapper(const uno::Reference<lang::XMultiServiceFactory> & xServiceFactory)37 CollatorWrapper::CollatorWrapper (
38 		const uno::Reference< lang::XMultiServiceFactory > &xServiceFactory)
39 	: mxServiceFactory (xServiceFactory)
40 {
41 	::rtl::OUString aService (RTL_CONSTASCII_USTRINGPARAM("com.sun.star.i18n.Collator"));
42 
43 	if (mxServiceFactory.is())
44 	{
45 		try
46 		{
47 			mxInternationalCollator = uno::Reference< i18n::XCollator > (
48 				mxServiceFactory->createInstance(aService), uno::UNO_QUERY);
49 		}
50 		catch (uno::Exception& rException)
51 		{
52             (void)rException;
53 			DBG_ERRORFILE ("CollatorWrapper: failed to create instance");
54 		}
55 	}
56 	else
57 	{
58 		::rtl::OUString aLibrary (RTL_CONSTASCII_USTRINGPARAM(LLCF_LIBNAME("i18n")));
59 
60 		try
61 		{
62 			uno::Reference< uno::XInterface > xInstance =
63 				::comphelper::getComponentInstance (aLibrary, aService);
64 
65 			if (xInstance.is())
66 			{
67 				uno::Any xInterface = xInstance->queryInterface (
68 					::getCppuType((const uno::Reference< i18n::XCollator >*)0) );
69 				xInterface >>= mxInternationalCollator;
70 			}
71 		}
72 		catch (uno::Exception& rException)
73 		{
74             (void)rException;
75 			DBG_ERRORFILE ("CollatorWrapper: failed to get component instance!");
76 		}
77 	}
78 
79 	DBG_ASSERT (mxInternationalCollator.is(), "CollatorWrapper: no i18n collator");
80 }
81 
~CollatorWrapper()82 CollatorWrapper::~CollatorWrapper()
83 {
84 }
85 
86 sal_Int32
compareSubstring(const::rtl::OUString & s1,sal_Int32 off1,sal_Int32 len1,const::rtl::OUString & s2,sal_Int32 off2,sal_Int32 len2) const87 CollatorWrapper::compareSubstring (
88 		const ::rtl::OUString& s1, sal_Int32 off1, sal_Int32 len1,
89         const ::rtl::OUString& s2, sal_Int32 off2, sal_Int32 len2) const
90 {
91 	try
92 	{
93 		if (mxInternationalCollator.is())
94 			return mxInternationalCollator->compareSubstring (
95 														s1, off1, len1, s2, off2, len2);
96 	}
97 	catch (uno::RuntimeException& rRuntimeException)
98 	{
99         (void)rRuntimeException;
100 		DBG_ERRORFILE ("CollatorWrapper: compareSubstring failed");
101 	}
102 
103 	return 0;
104 }
105 
106 sal_Int32
compareString(const::rtl::OUString & s1,const::rtl::OUString & s2) const107 CollatorWrapper::compareString (const ::rtl::OUString& s1, const ::rtl::OUString& s2) const
108 {
109 	try
110 	{
111 		if (mxInternationalCollator.is())
112 			return mxInternationalCollator->compareString (s1, s2);
113 	}
114 	catch (uno::RuntimeException& rRuntimeException)
115 	{
116         (void)rRuntimeException;
117 		DBG_ERRORFILE ("CollatorWrapper: compareString failed");
118 	}
119 
120 	return 0;
121 }
122 
123 uno::Sequence< ::rtl::OUString >
listCollatorAlgorithms(const lang::Locale & rLocale) const124 CollatorWrapper::listCollatorAlgorithms (const lang::Locale& rLocale) const
125 {
126 	try
127 	{
128 		if (mxInternationalCollator.is())
129 			return mxInternationalCollator->listCollatorAlgorithms (rLocale);
130 	}
131 	catch (uno::RuntimeException& rRuntimeException)
132 	{
133         (void)rRuntimeException;
134 		DBG_ERRORFILE ("CollatorWrapper: listCollatorAlgorithms failed");
135 	}
136 
137 	return uno::Sequence< ::rtl::OUString > ();
138 }
139 
140 uno::Sequence< sal_Int32 >
listCollatorOptions(const::rtl::OUString & rAlgorithm) const141 CollatorWrapper::listCollatorOptions (const ::rtl::OUString& rAlgorithm) const
142 {
143 	try
144 	{
145 		if (mxInternationalCollator.is())
146 			return mxInternationalCollator->listCollatorOptions (rAlgorithm);
147 	}
148 	catch (uno::RuntimeException& rRuntimeException)
149 	{
150         (void)rRuntimeException;
151 		DBG_ERRORFILE ("CollatorWrapper: listCollatorOptions failed");
152 	}
153 
154 	return uno::Sequence< sal_Int32 > ();
155 }
156 
157 sal_Int32
loadDefaultCollator(const lang::Locale & rLocale,sal_Int32 nOptions)158 CollatorWrapper::loadDefaultCollator (const lang::Locale& rLocale, sal_Int32 nOptions)
159 {
160 	try
161 	{
162 		if (mxInternationalCollator.is())
163 			return mxInternationalCollator->loadDefaultCollator (rLocale, nOptions);
164 	}
165 	catch (uno::RuntimeException& rRuntimeException)
166 	{
167         (void)rRuntimeException;
168 		DBG_ERRORFILE ("CollatorWrapper: loadDefaultCollator failed");
169 	}
170 
171 	return 0;
172 }
173 
174 sal_Int32
loadCollatorAlgorithm(const::rtl::OUString & rAlgorithm,const lang::Locale & rLocale,sal_Int32 nOptions)175 CollatorWrapper::loadCollatorAlgorithm (const ::rtl::OUString& rAlgorithm,
176 		const lang::Locale& rLocale, sal_Int32 nOptions)
177 {
178 	try
179 	{
180 		if (mxInternationalCollator.is())
181 			return mxInternationalCollator->loadCollatorAlgorithm (
182 														rAlgorithm, rLocale, nOptions);
183 	}
184 	catch (uno::RuntimeException& rRuntimeException)
185 	{
186         (void)rRuntimeException;
187 		DBG_ERRORFILE ("CollatorWrapper: loadCollatorAlgorithm failed");
188 	}
189 
190 	return 0;
191 
192 }
193 
194 void
loadCollatorAlgorithmWithEndUserOption(const::rtl::OUString & rAlgorithm,const lang::Locale & rLocale,const uno::Sequence<sal_Int32> & rOption)195 CollatorWrapper::loadCollatorAlgorithmWithEndUserOption (
196 		const ::rtl::OUString& rAlgorithm,
197 		const lang::Locale& rLocale, const uno::Sequence< sal_Int32 >& rOption)
198 {
199 	try
200 	{
201 		if (mxInternationalCollator.is())
202 			mxInternationalCollator->loadCollatorAlgorithmWithEndUserOption (
203 														rAlgorithm, rLocale, rOption);
204 	}
205 	catch (uno::RuntimeException& rRuntimeException)
206 	{
207         (void)rRuntimeException;
208 		DBG_ERRORFILE ("CollatorWrapper: loadCollatorAlgorithmWithEndUserOption failed");
209 	}
210 }
211