xref: /trunk/main/comphelper/source/compare/AnyCompareFactory.cxx (revision 9c15e1abd703a7953928d7a80655ae1a76a41f6d)
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_comphelper.hxx"
26 
27 #include "comphelper_module.hxx"
28 
29 #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
30 #include <com/sun/star/i18n/XCollator.hpp>
31 #include <com/sun/star/lang/Locale.hpp>
32 #include <com/sun/star/uno/Sequence.h>
33 #include <com/sun/star/beans/PropertyValue.hpp>
34 #include <cppuhelper/implbase3.hxx>
35 #include <cppuhelper/implbase1.hxx>
36 #include <com/sun/star/lang/XServiceInfo.hpp>
37 #include <com/sun/star/lang/XInitialization.hpp>
38 #include <com/sun/star/lang/IllegalArgumentException.hpp>
39 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
40 #include <comphelper/stl_types.hxx>
41 #include <map>
42 
43 
44 using namespace com::sun::star::uno;
45 using namespace com::sun::star::ucb;
46 using namespace com::sun::star::lang;
47 using namespace com::sun::star::i18n;
48 using namespace rtl;
49 
50 //=============================================================================
51 
52 class AnyCompare : public ::cppu::WeakImplHelper1< XAnyCompare >
53 {
54     Reference< XCollator > m_rCollator;
55 
56 public:
57     AnyCompare( Reference< XComponentContext > xContext, const Locale& rLocale ) throw()
58     {
59         Reference< XMultiComponentFactory > xFactory = xContext->getServiceManager();
60         if ( xFactory.is() )
61     {
62         m_rCollator = Reference< XCollator >(
63                 xFactory->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.i18n.Collator" ), xContext ),
64                         UNO_QUERY );
65         m_rCollator->loadDefaultCollator( rLocale,
66                                           0 ); //???
67         }
68 
69     }
70 
71     virtual sal_Int16 SAL_CALL compare( const Any& any1, const Any& any2 );
72 };
73 
74 //=============================================================================
75 
76 class AnyCompareFactory : public cppu::WeakImplHelper3< XAnyCompareFactory, XInitialization, XServiceInfo >
77 {
78     Reference< XAnyCompare >            m_rAnyCompare;
79     Reference< XComponentContext >      m_rContext;
80     Locale                              m_Locale;
81 
82 public:
83     AnyCompareFactory( Reference< XComponentContext > xContext ) : m_rContext( xContext )
84     {}
85 
86     // XAnyCompareFactory
87     virtual Reference< XAnyCompare > SAL_CALL createAnyCompareByName ( const OUString& aPropertyName );
88 
89     // XInitialization
90     virtual void SAL_CALL initialize( const Sequence< Any >& aArguments );
91 
92     // XServiceInfo
93     virtual OUString SAL_CALL getImplementationName(  );
94     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName );
95     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(  );
96 
97     // XServiceInfo - static versions (used for component registration)
98     static ::rtl::OUString SAL_CALL getImplementationName_static();
99     static Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
100     static Reference< XInterface > SAL_CALL Create( const Reference< XComponentContext >& );
101 };
102 
103 //===========================================================================================
104 
105 sal_Int16 SAL_CALL AnyCompare::compare( const Any& any1, const Any& any2 )
106 {
107     sal_Int16 aResult = 0;
108 
109     if( m_rCollator.is() )
110     {
111         OUString aStr1;
112         OUString aStr2;
113 
114         any1 >>= aStr1;
115         any2 >>= aStr2;
116 
117         aResult = ( sal_Int16 )m_rCollator->compareString( aStr1, aStr2 );
118     }
119 
120     return aResult;
121 }
122 
123 //===========================================================================================
124 
125 Reference< XAnyCompare > SAL_CALL AnyCompareFactory::createAnyCompareByName( const OUString& aPropertyName )
126 {
127     // for now only OUString properties compare is implemented
128     // so no check for the property name is done
129 
130     if( aPropertyName.equals( OUString::createFromAscii( "Title" ) ) )
131         return m_rAnyCompare;
132 
133     return Reference< XAnyCompare >();
134 }
135 
136 void SAL_CALL AnyCompareFactory::initialize( const Sequence< Any >& aArguments )
137 {
138     if( aArguments.getLength() )
139     {
140         if( aArguments[0] >>= m_Locale )
141         {
142             m_rAnyCompare = new AnyCompare( m_rContext, m_Locale );
143             return;
144         }
145     }
146 
147 }
148 
149 OUString SAL_CALL AnyCompareFactory::getImplementationName(  )
150 {
151     return getImplementationName_static();
152 }
153 
154 OUString SAL_CALL AnyCompareFactory::getImplementationName_static(  )
155 {
156     return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "AnyCompareFactory" ) );
157 }
158 
159 sal_Bool SAL_CALL AnyCompareFactory::supportsService( const OUString& ServiceName )
160 {
161     rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ucb.AnyCompareFactory" ) );
162     return aServiceName == ServiceName;
163 }
164 
165 Sequence< OUString > SAL_CALL AnyCompareFactory::getSupportedServiceNames(  )
166 {
167     return getSupportedServiceNames_static();
168 }
169 
170 Sequence< OUString > SAL_CALL AnyCompareFactory::getSupportedServiceNames_static(  )
171 {
172     const rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ucb.AnyCompareFactory" ) );
173     const Sequence< rtl::OUString > aSeq( &aServiceName, 1 );
174     return aSeq;
175 }
176 
177 Reference< XInterface > SAL_CALL AnyCompareFactory::Create(
178                 const Reference< XComponentContext >& rxContext )
179 {
180     return (cppu::OWeakObject*)new AnyCompareFactory( rxContext );
181 }
182 
183 void createRegistryInfo_AnyCompareFactory()
184 {
185     static ::comphelper::module::OAutoRegistration< AnyCompareFactory > aAutoRegistration;
186 }
187