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 
31 #include <unotools/nativenumberwrapper.hxx>
32 #include <tools/debug.hxx>
33 
34 #ifndef _COMPHELPER_COMPONENTFACTORY_HXX_
35 #include <comphelper/componentfactory.hxx>
36 #endif
37 #include <com/sun/star/uno/XInterface.hpp>
38 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 
40 #define LOCALEDATA_LIBRARYNAME "i18npool"
41 #define LOCALEDATA_SERVICENAME "com.sun.star.i18n.NativeNumberSupplier"
42 
43 using namespace ::com::sun::star;
44 
45 
46 NativeNumberWrapper::NativeNumberWrapper(
47             const uno::Reference< lang::XMultiServiceFactory > & xSF
48             )
49         :
50         xSMgr( xSF )
51 {
52     if ( xSMgr.is() )
53     {
54         try
55         {
56             xNNS = uno::Reference< i18n::XNativeNumberSupplier > ( xSMgr->createInstance(
57                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( LOCALEDATA_SERVICENAME ) ) ),
58                 uno::UNO_QUERY );
59         }
60         catch ( uno::Exception& e )
61         {
62             (void)e;
63             DBG_ERRORFILE( "NativeNumberWrapper ctor: Exception caught!" );
64         }
65     }
66     else
67     {   // try to get an instance somehow
68         DBG_ERRORFILE( "NativeNumberWrapper: no service manager, trying own" );
69         try
70         {
71             uno::Reference< uno::XInterface > xI = ::comphelper::getComponentInstance(
72                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( LLCF_LIBNAME( LOCALEDATA_LIBRARYNAME ) ) ),
73                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( LOCALEDATA_SERVICENAME ) ) );
74             if ( xI.is() )
75             {
76                 uno::Any x = xI->queryInterface( ::getCppuType((const uno::Reference< i18n::XNativeNumberSupplier >*)0) );
77                 x >>= xNNS;
78             }
79         }
80         catch ( uno::Exception& e )
81         {
82             (void)e;
83             DBG_ERRORFILE( "getComponentInstance: Exception caught!" );
84         }
85     }
86     DBG_ASSERT( xNNS.is(), "NativeNumberWrapper: no NativeNumberSupplier" );
87 }
88 
89 
90 NativeNumberWrapper::~NativeNumberWrapper()
91 {
92 }
93 
94 
95 ::rtl::OUString
96 NativeNumberWrapper::getNativeNumberString(
97                     const ::rtl::OUString& rNumberString,
98                     const ::com::sun::star::lang::Locale& rLocale,
99                     sal_Int16 nNativeNumberMode ) const
100 {
101     try
102     {
103         if ( xNNS.is() )
104             return xNNS->getNativeNumberString( rNumberString, rLocale, nNativeNumberMode );
105     }
106     catch ( uno::Exception& e )
107     {
108         (void)e;
109         DBG_ERRORFILE( "getNativeNumberString: Exception caught!" );
110     }
111     return ::rtl::OUString();
112 }
113 
114 
115 sal_Bool
116 NativeNumberWrapper::isValidNatNum(
117                     const ::com::sun::star::lang::Locale& rLocale,
118                     sal_Int16 nNativeNumberMode ) const
119 {
120     try
121     {
122         if ( xNNS.is() )
123             return xNNS->isValidNatNum( rLocale, nNativeNumberMode );
124     }
125     catch ( uno::Exception& e )
126     {
127         (void)e;
128         DBG_ERRORFILE( "isValidNatNum: Exception caught!" );
129     }
130     return sal_False;
131 }
132 
133 
134 i18n::NativeNumberXmlAttributes
135 NativeNumberWrapper::convertToXmlAttributes(
136                     const ::com::sun::star::lang::Locale& rLocale,
137                     sal_Int16 nNativeNumberMode ) const
138 {
139     try
140     {
141         if ( xNNS.is() )
142             return xNNS->convertToXmlAttributes( rLocale, nNativeNumberMode );
143     }
144     catch ( uno::Exception& e )
145     {
146         (void)e;
147         DBG_ERRORFILE( "convertToXmlAttributes: Exception caught!" );
148     }
149     return i18n::NativeNumberXmlAttributes();
150 }
151 
152 
153 sal_Int16
154 NativeNumberWrapper::convertFromXmlAttributes(
155                     const i18n::NativeNumberXmlAttributes& rAttr ) const
156 {
157     try
158     {
159         if ( xNNS.is() )
160             return xNNS->convertFromXmlAttributes( rAttr );
161     }
162     catch ( uno::Exception& e )
163     {
164         (void)e;
165         DBG_ERRORFILE( "convertFromXmlAttributes: Exception caught!" );
166     }
167     return 0;
168 }
169 
170 
171