xref: /trunk/main/unotools/source/i18n/collatorwrapper.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_unotools.hxx"
30 #include <unotools/collatorwrapper.hxx>
31 #include <tools/debug.hxx>
32 
33 #ifndef _COMPHELPER_COMPONENTFACTORY_HXX_
34 #include <comphelper/componentfactory.hxx>
35 #endif
36 #include <com/sun/star/uno/XInterface.hpp>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 
39 using namespace ::com::sun::star;
40 
41 CollatorWrapper::CollatorWrapper (
42         const uno::Reference< lang::XMultiServiceFactory > &xServiceFactory)
43     : mxServiceFactory (xServiceFactory)
44 {
45     ::rtl::OUString aService (RTL_CONSTASCII_USTRINGPARAM("com.sun.star.i18n.Collator"));
46 
47     if (mxServiceFactory.is())
48     {
49         try
50         {
51             mxInternationalCollator = uno::Reference< i18n::XCollator > (
52                 mxServiceFactory->createInstance(aService), uno::UNO_QUERY);
53         }
54         catch (uno::Exception& rException)
55         {
56             (void)rException;
57             DBG_ERRORFILE ("CollatorWrapper: failed to create instance");
58         }
59     }
60     else
61     {
62         ::rtl::OUString aLibrary (RTL_CONSTASCII_USTRINGPARAM(LLCF_LIBNAME("i18n")));
63 
64         try
65         {
66             uno::Reference< uno::XInterface > xInstance =
67                 ::comphelper::getComponentInstance (aLibrary, aService);
68 
69             if (xInstance.is())
70             {
71                 uno::Any xInterface = xInstance->queryInterface (
72                     ::getCppuType((const uno::Reference< i18n::XCollator >*)0) );
73                 xInterface >>= mxInternationalCollator;
74             }
75         }
76         catch (uno::Exception& rException)
77         {
78             (void)rException;
79             DBG_ERRORFILE ("CollatorWrapper: failed to get component instance!");
80         }
81     }
82 
83     DBG_ASSERT (mxInternationalCollator.is(), "CollatorWrapper: no i18n collator");
84 }
85 
86 CollatorWrapper::~CollatorWrapper()
87 {
88 }
89 
90 sal_Int32
91 CollatorWrapper::compareSubstring (
92         const ::rtl::OUString& s1, sal_Int32 off1, sal_Int32 len1,
93         const ::rtl::OUString& s2, sal_Int32 off2, sal_Int32 len2) const
94 {
95     try
96     {
97         if (mxInternationalCollator.is())
98             return mxInternationalCollator->compareSubstring (
99                                                         s1, off1, len1, s2, off2, len2);
100     }
101     catch (uno::RuntimeException& rRuntimeException)
102     {
103         (void)rRuntimeException;
104         DBG_ERRORFILE ("CollatorWrapper: compareSubstring failed");
105     }
106 
107     return 0;
108 }
109 
110 sal_Int32
111 CollatorWrapper::compareString (const ::rtl::OUString& s1, const ::rtl::OUString& s2) const
112 {
113     try
114     {
115         if (mxInternationalCollator.is())
116             return mxInternationalCollator->compareString (s1, s2);
117     }
118     catch (uno::RuntimeException& rRuntimeException)
119     {
120         (void)rRuntimeException;
121         DBG_ERRORFILE ("CollatorWrapper: compareString failed");
122     }
123 
124     return 0;
125 }
126 
127 uno::Sequence< ::rtl::OUString >
128 CollatorWrapper::listCollatorAlgorithms (const lang::Locale& rLocale) const
129 {
130     try
131     {
132         if (mxInternationalCollator.is())
133             return mxInternationalCollator->listCollatorAlgorithms (rLocale);
134     }
135     catch (uno::RuntimeException& rRuntimeException)
136     {
137         (void)rRuntimeException;
138         DBG_ERRORFILE ("CollatorWrapper: listCollatorAlgorithms failed");
139     }
140 
141     return uno::Sequence< ::rtl::OUString > ();
142 }
143 
144 uno::Sequence< sal_Int32 >
145 CollatorWrapper::listCollatorOptions (const ::rtl::OUString& rAlgorithm) const
146 {
147     try
148     {
149         if (mxInternationalCollator.is())
150             return mxInternationalCollator->listCollatorOptions (rAlgorithm);
151     }
152     catch (uno::RuntimeException& rRuntimeException)
153     {
154         (void)rRuntimeException;
155         DBG_ERRORFILE ("CollatorWrapper: listCollatorOptions failed");
156     }
157 
158     return uno::Sequence< sal_Int32 > ();
159 }
160 
161 sal_Int32
162 CollatorWrapper::loadDefaultCollator (const lang::Locale& rLocale, sal_Int32 nOptions)
163 {
164     try
165     {
166         if (mxInternationalCollator.is())
167             return mxInternationalCollator->loadDefaultCollator (rLocale, nOptions);
168     }
169     catch (uno::RuntimeException& rRuntimeException)
170     {
171         (void)rRuntimeException;
172         DBG_ERRORFILE ("CollatorWrapper: loadDefaultCollator failed");
173     }
174 
175     return 0;
176 }
177 
178 sal_Int32
179 CollatorWrapper::loadCollatorAlgorithm (const ::rtl::OUString& rAlgorithm,
180         const lang::Locale& rLocale, sal_Int32 nOptions)
181 {
182     try
183     {
184         if (mxInternationalCollator.is())
185             return mxInternationalCollator->loadCollatorAlgorithm (
186                                                         rAlgorithm, rLocale, nOptions);
187     }
188     catch (uno::RuntimeException& rRuntimeException)
189     {
190         (void)rRuntimeException;
191         DBG_ERRORFILE ("CollatorWrapper: loadCollatorAlgorithm failed");
192     }
193 
194     return 0;
195 
196 }
197 
198 void
199 CollatorWrapper::loadCollatorAlgorithmWithEndUserOption (
200         const ::rtl::OUString& rAlgorithm,
201         const lang::Locale& rLocale, const uno::Sequence< sal_Int32 >& rOption)
202 {
203     try
204     {
205         if (mxInternationalCollator.is())
206             mxInternationalCollator->loadCollatorAlgorithmWithEndUserOption (
207                                                         rAlgorithm, rLocale, rOption);
208     }
209     catch (uno::RuntimeException& rRuntimeException)
210     {
211         (void)rRuntimeException;
212         DBG_ERRORFILE ("CollatorWrapper: loadCollatorAlgorithmWithEndUserOption failed");
213     }
214 }
215