CWS-TOOLING: integrate CWS os146
[LibreOffice.git] / comphelper / source / misc / numbers.cxx
blob06f9f519c31858c2ecf2892179e24736bcc8452e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 #include <comphelper/numbers.hxx>
31 #include <osl/diagnose.h>
32 #include <com/sun/star/util/NumberFormat.hpp>
33 #include <com/sun/star/util/XNumberFormatTypes.hpp>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/lang/Locale.hpp>
37 //.........................................................................
38 namespace comphelper
40 //.........................................................................
42 namespace starbeans = ::com::sun::star::beans;
43 namespace starlang = ::com::sun::star::lang;
45 //------------------------------------------------------------------------------
46 sal_Int16 getNumberFormatType(const staruno::Reference<starutil::XNumberFormats>& xFormats, sal_Int32 nKey)
48 sal_Int16 nReturn(starutil::NumberFormat::UNDEFINED);
49 if (xFormats.is())
51 try
53 staruno::Reference<starbeans::XPropertySet> xFormat(xFormats->getByKey(nKey));
54 if (xFormat.is())
55 xFormat->getPropertyValue(rtl::OUString::createFromAscii("Type")) >>= nReturn;
57 catch(...)
59 OSL_TRACE("getNumberFormatType : invalid key! (maybe created with another formatter ?)");
62 return nReturn;
65 //------------------------------------------------------------------------------
66 sal_Int16 getNumberFormatType(const staruno::Reference<starutil::XNumberFormatter>& xFormatter, sal_Int32 nKey)
68 OSL_ENSURE(xFormatter.is(), "getNumberFormatType : the formatter isn't valid !");
69 staruno::Reference<starutil::XNumberFormatsSupplier> xSupplier( xFormatter->getNumberFormatsSupplier());
70 OSL_ENSURE(xSupplier.is(), "getNumberFormatType : the formatter doesn't implement a supplier !");
71 staruno::Reference<starutil::XNumberFormats> xFormats( xSupplier->getNumberFormats());
72 return getNumberFormatType(xFormats, nKey);
75 //------------------------------------------------------------------------------
76 staruno::Any getNumberFormatDecimals(const staruno::Reference<starutil::XNumberFormats>& xFormats, sal_Int32 nKey)
78 if (xFormats.is())
80 try
82 staruno::Reference<starbeans::XPropertySet> xFormat( xFormats->getByKey(nKey));
83 if (xFormat.is())
85 static ::rtl::OUString PROPERTY_DECIMALS = ::rtl::OUString::createFromAscii("Decimals");
86 return xFormat->getPropertyValue(PROPERTY_DECIMALS);
89 catch(...)
91 OSL_TRACE("getNumberFormatDecimals : invalid key! (may be created with another formatter ?)");
94 return staruno::makeAny((sal_Int16)0);
98 //------------------------------------------------------------------------------
99 sal_Int32 getStandardFormat(
100 const staruno::Reference<starutil::XNumberFormatter>& xFormatter,
101 sal_Int16 nType,
102 const starlang::Locale& _rLocale)
104 staruno::Reference<starutil::XNumberFormatsSupplier> xSupplier( xFormatter.is() ? xFormatter->getNumberFormatsSupplier() : staruno::Reference<starutil::XNumberFormatsSupplier>(NULL));
105 staruno::Reference<starutil::XNumberFormats> xFormats( xSupplier.is() ? xSupplier->getNumberFormats() : staruno::Reference<starutil::XNumberFormats>(NULL));
106 staruno::Reference<starutil::XNumberFormatTypes> xTypes(xFormats, staruno::UNO_QUERY);
107 OSL_ENSURE(xTypes.is(), "getStandardFormat : no format types !");
109 return xTypes.is() ? xTypes->getStandardFormat(nType, _rLocale) : 0;
112 //------------------------------------------------------------------------------
113 using namespace ::com::sun::star::uno;
114 using namespace ::com::sun::star::util;
115 using namespace ::com::sun::star::beans;
117 //------------------------------------------------------------------------------
118 Any getNumberFormatProperty( const Reference< XNumberFormatter >& _rxFormatter, sal_Int32 _nKey, const rtl::OUString& _rPropertyName )
120 Any aReturn;
122 OSL_ENSURE( _rxFormatter.is() && _rPropertyName.getLength(), "getNumberFormatProperty: invalid arguments!" );
125 Reference< XNumberFormatsSupplier > xSupplier;
126 Reference< XNumberFormats > xFormats;
127 Reference< XPropertySet > xFormatProperties;
129 if ( _rxFormatter.is() )
130 xSupplier = _rxFormatter->getNumberFormatsSupplier();
131 if ( xSupplier.is() )
132 xFormats = xSupplier->getNumberFormats();
133 if ( xFormats.is() )
134 xFormatProperties = xFormats->getByKey( _nKey );
136 if ( xFormatProperties.is() )
137 aReturn = xFormatProperties->getPropertyValue( _rPropertyName );
139 catch( const Exception& )
141 OSL_ENSURE( sal_False, "::getNumberFormatProperty: caught an exception (did you create the key with another formatter?)!" );
144 return aReturn;
147 //.........................................................................
148 } // namespace comphelper
149 //.........................................................................