xref: /trunk/main/unotools/source/i18n/collatorwrapper.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*b5088357SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b5088357SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b5088357SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b5088357SAndrew Rist  * distributed with this work for additional information
6*b5088357SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b5088357SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b5088357SAndrew Rist  * "License"); you may not use this file except in compliance
9*b5088357SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*b5088357SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*b5088357SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b5088357SAndrew Rist  * software distributed under the License is distributed on an
15*b5088357SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b5088357SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b5088357SAndrew Rist  * specific language governing permissions and limitations
18*b5088357SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*b5088357SAndrew Rist  *************************************************************/
21*b5088357SAndrew Rist 
22*b5088357SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_unotools.hxx"
26cdf0e10cSrcweir #include <unotools/collatorwrapper.hxx>
27cdf0e10cSrcweir #include <tools/debug.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #ifndef _COMPHELPER_COMPONENTFACTORY_HXX_
30cdf0e10cSrcweir #include <comphelper/componentfactory.hxx>
31cdf0e10cSrcweir #endif
32cdf0e10cSrcweir #include <com/sun/star/uno/XInterface.hpp>
33cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using namespace ::com::sun::star;
36cdf0e10cSrcweir 
CollatorWrapper(const uno::Reference<lang::XMultiServiceFactory> & xServiceFactory)37cdf0e10cSrcweir CollatorWrapper::CollatorWrapper (
38cdf0e10cSrcweir         const uno::Reference< lang::XMultiServiceFactory > &xServiceFactory)
39cdf0e10cSrcweir     : mxServiceFactory (xServiceFactory)
40cdf0e10cSrcweir {
41cdf0e10cSrcweir     ::rtl::OUString aService (RTL_CONSTASCII_USTRINGPARAM("com.sun.star.i18n.Collator"));
42cdf0e10cSrcweir 
43cdf0e10cSrcweir     if (mxServiceFactory.is())
44cdf0e10cSrcweir     {
45cdf0e10cSrcweir         try
46cdf0e10cSrcweir         {
47cdf0e10cSrcweir             mxInternationalCollator = uno::Reference< i18n::XCollator > (
48cdf0e10cSrcweir                 mxServiceFactory->createInstance(aService), uno::UNO_QUERY);
49cdf0e10cSrcweir         }
50cdf0e10cSrcweir         catch (uno::Exception& rException)
51cdf0e10cSrcweir         {
52cdf0e10cSrcweir             (void)rException;
53cdf0e10cSrcweir             DBG_ERRORFILE ("CollatorWrapper: failed to create instance");
54cdf0e10cSrcweir         }
55cdf0e10cSrcweir     }
56cdf0e10cSrcweir     else
57cdf0e10cSrcweir     {
58cdf0e10cSrcweir         ::rtl::OUString aLibrary (RTL_CONSTASCII_USTRINGPARAM(LLCF_LIBNAME("i18n")));
59cdf0e10cSrcweir 
60cdf0e10cSrcweir         try
61cdf0e10cSrcweir         {
62cdf0e10cSrcweir             uno::Reference< uno::XInterface > xInstance =
63cdf0e10cSrcweir                 ::comphelper::getComponentInstance (aLibrary, aService);
64cdf0e10cSrcweir 
65cdf0e10cSrcweir             if (xInstance.is())
66cdf0e10cSrcweir             {
67cdf0e10cSrcweir                 uno::Any xInterface = xInstance->queryInterface (
68cdf0e10cSrcweir                     ::getCppuType((const uno::Reference< i18n::XCollator >*)0) );
69cdf0e10cSrcweir                 xInterface >>= mxInternationalCollator;
70cdf0e10cSrcweir             }
71cdf0e10cSrcweir         }
72cdf0e10cSrcweir         catch (uno::Exception& rException)
73cdf0e10cSrcweir         {
74cdf0e10cSrcweir             (void)rException;
75cdf0e10cSrcweir             DBG_ERRORFILE ("CollatorWrapper: failed to get component instance!");
76cdf0e10cSrcweir         }
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     DBG_ASSERT (mxInternationalCollator.is(), "CollatorWrapper: no i18n collator");
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
~CollatorWrapper()82cdf0e10cSrcweir CollatorWrapper::~CollatorWrapper()
83cdf0e10cSrcweir {
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir sal_Int32
compareSubstring(const::rtl::OUString & s1,sal_Int32 off1,sal_Int32 len1,const::rtl::OUString & s2,sal_Int32 off2,sal_Int32 len2) const87cdf0e10cSrcweir CollatorWrapper::compareSubstring (
88cdf0e10cSrcweir         const ::rtl::OUString& s1, sal_Int32 off1, sal_Int32 len1,
89cdf0e10cSrcweir         const ::rtl::OUString& s2, sal_Int32 off2, sal_Int32 len2) const
90cdf0e10cSrcweir {
91cdf0e10cSrcweir     try
92cdf0e10cSrcweir     {
93cdf0e10cSrcweir         if (mxInternationalCollator.is())
94cdf0e10cSrcweir             return mxInternationalCollator->compareSubstring (
95cdf0e10cSrcweir                                                         s1, off1, len1, s2, off2, len2);
96cdf0e10cSrcweir     }
97cdf0e10cSrcweir     catch (uno::RuntimeException& rRuntimeException)
98cdf0e10cSrcweir     {
99cdf0e10cSrcweir         (void)rRuntimeException;
100cdf0e10cSrcweir         DBG_ERRORFILE ("CollatorWrapper: compareSubstring failed");
101cdf0e10cSrcweir     }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir     return 0;
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir sal_Int32
compareString(const::rtl::OUString & s1,const::rtl::OUString & s2) const107cdf0e10cSrcweir CollatorWrapper::compareString (const ::rtl::OUString& s1, const ::rtl::OUString& s2) const
108cdf0e10cSrcweir {
109cdf0e10cSrcweir     try
110cdf0e10cSrcweir     {
111cdf0e10cSrcweir         if (mxInternationalCollator.is())
112cdf0e10cSrcweir             return mxInternationalCollator->compareString (s1, s2);
113cdf0e10cSrcweir     }
114cdf0e10cSrcweir     catch (uno::RuntimeException& rRuntimeException)
115cdf0e10cSrcweir     {
116cdf0e10cSrcweir         (void)rRuntimeException;
117cdf0e10cSrcweir         DBG_ERRORFILE ("CollatorWrapper: compareString failed");
118cdf0e10cSrcweir     }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     return 0;
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir uno::Sequence< ::rtl::OUString >
listCollatorAlgorithms(const lang::Locale & rLocale) const124cdf0e10cSrcweir CollatorWrapper::listCollatorAlgorithms (const lang::Locale& rLocale) const
125cdf0e10cSrcweir {
126cdf0e10cSrcweir     try
127cdf0e10cSrcweir     {
128cdf0e10cSrcweir         if (mxInternationalCollator.is())
129cdf0e10cSrcweir             return mxInternationalCollator->listCollatorAlgorithms (rLocale);
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir     catch (uno::RuntimeException& rRuntimeException)
132cdf0e10cSrcweir     {
133cdf0e10cSrcweir         (void)rRuntimeException;
134cdf0e10cSrcweir         DBG_ERRORFILE ("CollatorWrapper: listCollatorAlgorithms failed");
135cdf0e10cSrcweir     }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     return uno::Sequence< ::rtl::OUString > ();
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir uno::Sequence< sal_Int32 >
listCollatorOptions(const::rtl::OUString & rAlgorithm) const141cdf0e10cSrcweir CollatorWrapper::listCollatorOptions (const ::rtl::OUString& rAlgorithm) const
142cdf0e10cSrcweir {
143cdf0e10cSrcweir     try
144cdf0e10cSrcweir     {
145cdf0e10cSrcweir         if (mxInternationalCollator.is())
146cdf0e10cSrcweir             return mxInternationalCollator->listCollatorOptions (rAlgorithm);
147cdf0e10cSrcweir     }
148cdf0e10cSrcweir     catch (uno::RuntimeException& rRuntimeException)
149cdf0e10cSrcweir     {
150cdf0e10cSrcweir         (void)rRuntimeException;
151cdf0e10cSrcweir         DBG_ERRORFILE ("CollatorWrapper: listCollatorOptions failed");
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir     return uno::Sequence< sal_Int32 > ();
155cdf0e10cSrcweir }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir sal_Int32
loadDefaultCollator(const lang::Locale & rLocale,sal_Int32 nOptions)158cdf0e10cSrcweir CollatorWrapper::loadDefaultCollator (const lang::Locale& rLocale, sal_Int32 nOptions)
159cdf0e10cSrcweir {
160cdf0e10cSrcweir     try
161cdf0e10cSrcweir     {
162cdf0e10cSrcweir         if (mxInternationalCollator.is())
163cdf0e10cSrcweir             return mxInternationalCollator->loadDefaultCollator (rLocale, nOptions);
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir     catch (uno::RuntimeException& rRuntimeException)
166cdf0e10cSrcweir     {
167cdf0e10cSrcweir         (void)rRuntimeException;
168cdf0e10cSrcweir         DBG_ERRORFILE ("CollatorWrapper: loadDefaultCollator failed");
169cdf0e10cSrcweir     }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     return 0;
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir sal_Int32
loadCollatorAlgorithm(const::rtl::OUString & rAlgorithm,const lang::Locale & rLocale,sal_Int32 nOptions)175cdf0e10cSrcweir CollatorWrapper::loadCollatorAlgorithm (const ::rtl::OUString& rAlgorithm,
176cdf0e10cSrcweir         const lang::Locale& rLocale, sal_Int32 nOptions)
177cdf0e10cSrcweir {
178cdf0e10cSrcweir     try
179cdf0e10cSrcweir     {
180cdf0e10cSrcweir         if (mxInternationalCollator.is())
181cdf0e10cSrcweir             return mxInternationalCollator->loadCollatorAlgorithm (
182cdf0e10cSrcweir                                                         rAlgorithm, rLocale, nOptions);
183cdf0e10cSrcweir     }
184cdf0e10cSrcweir     catch (uno::RuntimeException& rRuntimeException)
185cdf0e10cSrcweir     {
186cdf0e10cSrcweir         (void)rRuntimeException;
187cdf0e10cSrcweir         DBG_ERRORFILE ("CollatorWrapper: loadCollatorAlgorithm failed");
188cdf0e10cSrcweir     }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir     return 0;
191cdf0e10cSrcweir 
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir void
loadCollatorAlgorithmWithEndUserOption(const::rtl::OUString & rAlgorithm,const lang::Locale & rLocale,const uno::Sequence<sal_Int32> & rOption)195cdf0e10cSrcweir CollatorWrapper::loadCollatorAlgorithmWithEndUserOption (
196cdf0e10cSrcweir         const ::rtl::OUString& rAlgorithm,
197cdf0e10cSrcweir         const lang::Locale& rLocale, const uno::Sequence< sal_Int32 >& rOption)
198cdf0e10cSrcweir {
199cdf0e10cSrcweir     try
200cdf0e10cSrcweir     {
201cdf0e10cSrcweir         if (mxInternationalCollator.is())
202cdf0e10cSrcweir             mxInternationalCollator->loadCollatorAlgorithmWithEndUserOption (
203cdf0e10cSrcweir                                                         rAlgorithm, rLocale, rOption);
204cdf0e10cSrcweir     }
205cdf0e10cSrcweir     catch (uno::RuntimeException& rRuntimeException)
206cdf0e10cSrcweir     {
207cdf0e10cSrcweir         (void)rRuntimeException;
208cdf0e10cSrcweir         DBG_ERRORFILE ("CollatorWrapper: loadCollatorAlgorithmWithEndUserOption failed");
209cdf0e10cSrcweir     }
210cdf0e10cSrcweir }
211