merge the formfield patch from ooo-build
[ooovba.git] / comphelper / source / compare / AnyCompareFactory.cxx
bloba3c9362aab4bb48d5363e1ee7e4c32dae1751194
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: AnyCompareFactory.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
34 #include "comphelper_module.hxx"
36 #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
37 #include <com/sun/star/i18n/XCollator.hpp>
38 #include <com/sun/star/lang/Locale.hpp>
39 #include <com/sun/star/uno/Sequence.h>
40 #include <com/sun/star/beans/PropertyValue.hpp>
41 #include <cppuhelper/implbase3.hxx>
42 #include <cppuhelper/implbase1.hxx>
43 #include <com/sun/star/lang/XServiceInfo.hpp>
44 #include <com/sun/star/lang/XInitialization.hpp>
45 #include <com/sun/star/lang/IllegalArgumentException.hpp>
46 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
47 #include <comphelper/stl_types.hxx>
48 #include <map>
51 using namespace com::sun::star::uno;
52 using namespace com::sun::star::ucb;
53 using namespace com::sun::star::lang;
54 using namespace com::sun::star::i18n;
55 using namespace rtl;
57 //=============================================================================
59 class AnyCompare : public ::cppu::WeakImplHelper1< XAnyCompare >
61 Reference< XCollator > m_rCollator;
63 public:
64 AnyCompare( Reference< XComponentContext > xContext, const Locale& rLocale ) throw()
66 Reference< XMultiComponentFactory > xFactory = xContext->getServiceManager();
67 if ( xFactory.is() )
69 m_rCollator = Reference< XCollator >(
70 xFactory->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.i18n.Collator" ), xContext ),
71 UNO_QUERY );
72 m_rCollator->loadDefaultCollator( rLocale,
73 0 ); //???
78 virtual sal_Int16 SAL_CALL compare( const Any& any1, const Any& any2 ) throw(RuntimeException);
81 //=============================================================================
83 class AnyCompareFactory : public cppu::WeakImplHelper3< XAnyCompareFactory, XInitialization, XServiceInfo >
85 Reference< XAnyCompare > m_rAnyCompare;
86 Reference< XComponentContext > m_rContext;
87 Locale m_Locale;
89 public:
90 AnyCompareFactory( Reference< XComponentContext > xContext ) : m_rContext( xContext )
93 // XAnyCompareFactory
94 virtual Reference< XAnyCompare > SAL_CALL createAnyCompareByName ( const OUString& aPropertyName ) throw(::com::sun::star::uno::RuntimeException);
96 // XInitialization
97 virtual void SAL_CALL initialize( const Sequence< Any >& aArguments )
98 throw ( Exception, RuntimeException );
100 // XServiceInfo
101 virtual OUString SAL_CALL getImplementationName( ) throw(RuntimeException);
102 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(RuntimeException);
103 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames( ) throw(RuntimeException);
105 // XServiceInfo - static versions (used for component registration)
106 static ::rtl::OUString SAL_CALL getImplementationName_static();
107 static Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
108 static Reference< XInterface > SAL_CALL Create( const Reference< XComponentContext >& );
111 //===========================================================================================
113 sal_Int16 SAL_CALL AnyCompare::compare( const Any& any1, const Any& any2 ) throw(::com::sun::star::uno::RuntimeException)
115 sal_Int16 aResult = 0;
117 if( m_rCollator.is() )
119 OUString aStr1;
120 OUString aStr2;
122 any1 >>= aStr1;
123 any2 >>= aStr2;
125 aResult = ( sal_Int16 )m_rCollator->compareString( aStr1, aStr2 );
128 return aResult;
131 //===========================================================================================
133 Reference< XAnyCompare > SAL_CALL AnyCompareFactory::createAnyCompareByName( const OUString& aPropertyName ) throw(::com::sun::star::uno::RuntimeException)
135 // for now only OUString properties compare is implemented
136 // so no check for the property name is done
138 if( aPropertyName.equals( OUString::createFromAscii( "Title" ) ) )
139 return m_rAnyCompare;
141 return Reference< XAnyCompare >();
144 void SAL_CALL AnyCompareFactory::initialize( const Sequence< Any >& aArguments ) throw ( Exception, RuntimeException )
146 if( aArguments.getLength() )
148 if( aArguments[0] >>= m_Locale )
150 m_rAnyCompare = new AnyCompare( m_rContext, m_Locale );
151 return;
157 OUString SAL_CALL AnyCompareFactory::getImplementationName( ) throw( RuntimeException )
159 return getImplementationName_static();
162 OUString SAL_CALL AnyCompareFactory::getImplementationName_static( )
164 return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "AnyCompareFactory" ) );
167 sal_Bool SAL_CALL AnyCompareFactory::supportsService( const OUString& ServiceName ) throw(RuntimeException)
169 rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ucb.AnyCompareFactory" ) );
170 return aServiceName == ServiceName;
173 Sequence< OUString > SAL_CALL AnyCompareFactory::getSupportedServiceNames( ) throw(RuntimeException)
175 return getSupportedServiceNames_static();
178 Sequence< OUString > SAL_CALL AnyCompareFactory::getSupportedServiceNames_static( )
180 const rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ucb.AnyCompareFactory" ) );
181 const Sequence< rtl::OUString > aSeq( &aServiceName, 1 );
182 return aSeq;
185 Reference< XInterface > SAL_CALL AnyCompareFactory::Create(
186 const Reference< XComponentContext >& rxContext )
188 return (cppu::OWeakObject*)new AnyCompareFactory( rxContext );
191 void createRegistryInfo_AnyCompareFactory()
193 static ::comphelper::module::OAutoRegistration< AnyCompareFactory > aAutoRegistration;