update dev300-m58
[ooovba.git] / comphelper / source / misc / numbers.cxx
blobc8fb5046ee67ba5c1f272def179e3f7f9b3e7165
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: numbers.cxx,v $
10 * $Revision: 1.6 $
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"
33 #include <comphelper/numbers.hxx>
34 #include <osl/diagnose.h>
35 #include <com/sun/star/util/NumberFormat.hpp>
36 #include <com/sun/star/util/XNumberFormatTypes.hpp>
37 #include <com/sun/star/beans/XPropertySet.hpp>
38 #include <com/sun/star/lang/Locale.hpp>
40 //.........................................................................
41 namespace comphelper
43 //.........................................................................
45 namespace starbeans = ::com::sun::star::beans;
46 namespace starlang = ::com::sun::star::lang;
48 //------------------------------------------------------------------------------
49 sal_Int16 getNumberFormatType(const staruno::Reference<starutil::XNumberFormats>& xFormats, sal_Int32 nKey)
51 sal_Int16 nReturn(starutil::NumberFormat::UNDEFINED);
52 if (xFormats.is())
54 try
56 staruno::Reference<starbeans::XPropertySet> xFormat(xFormats->getByKey(nKey));
57 if (xFormat.is())
58 xFormat->getPropertyValue(rtl::OUString::createFromAscii("Type")) >>= nReturn;
60 catch(...)
62 OSL_TRACE("getNumberFormatType : invalid key! (maybe created with another formatter ?)");
65 return nReturn;
68 //------------------------------------------------------------------------------
69 sal_Int16 getNumberFormatType(const staruno::Reference<starutil::XNumberFormatter>& xFormatter, sal_Int32 nKey)
71 OSL_ENSURE(xFormatter.is(), "getNumberFormatType : the formatter isn't valid !");
72 staruno::Reference<starutil::XNumberFormatsSupplier> xSupplier( xFormatter->getNumberFormatsSupplier());
73 OSL_ENSURE(xSupplier.is(), "getNumberFormatType : the formatter doesn't implement a supplier !");
74 staruno::Reference<starutil::XNumberFormats> xFormats( xSupplier->getNumberFormats());
75 return getNumberFormatType(xFormats, nKey);
78 //------------------------------------------------------------------------------
79 staruno::Any getNumberFormatDecimals(const staruno::Reference<starutil::XNumberFormats>& xFormats, sal_Int32 nKey)
81 if (xFormats.is())
83 try
85 staruno::Reference<starbeans::XPropertySet> xFormat( xFormats->getByKey(nKey));
86 if (xFormat.is())
88 static ::rtl::OUString PROPERTY_DECIMALS = ::rtl::OUString::createFromAscii("Decimals");
89 return xFormat->getPropertyValue(PROPERTY_DECIMALS);
92 catch(...)
94 OSL_TRACE("getNumberFormatDecimals : invalid key! (may be created with another formatter ?)");
97 return staruno::makeAny((sal_Int16)0);
101 //------------------------------------------------------------------------------
102 sal_Int32 getStandardFormat(
103 const staruno::Reference<starutil::XNumberFormatter>& xFormatter,
104 sal_Int16 nType,
105 const starlang::Locale& _rLocale)
107 staruno::Reference<starutil::XNumberFormatsSupplier> xSupplier( xFormatter.is() ? xFormatter->getNumberFormatsSupplier() : staruno::Reference<starutil::XNumberFormatsSupplier>(NULL));
108 staruno::Reference<starutil::XNumberFormats> xFormats( xSupplier.is() ? xSupplier->getNumberFormats() : staruno::Reference<starutil::XNumberFormats>(NULL));
109 staruno::Reference<starutil::XNumberFormatTypes> xTypes(xFormats, staruno::UNO_QUERY);
110 OSL_ENSURE(xTypes.is(), "getStandardFormat : no format types !");
112 return xTypes.is() ? xTypes->getStandardFormat(nType, _rLocale) : 0;
115 //------------------------------------------------------------------------------
116 using namespace ::com::sun::star::uno;
117 using namespace ::com::sun::star::util;
118 using namespace ::com::sun::star::beans;
120 //------------------------------------------------------------------------------
121 Any getNumberFormatProperty( const Reference< XNumberFormatter >& _rxFormatter, sal_Int32 _nKey, const rtl::OUString& _rPropertyName )
123 Any aReturn;
125 OSL_ENSURE( _rxFormatter.is() && _rPropertyName.getLength(), "getNumberFormatProperty: invalid arguments!" );
128 Reference< XNumberFormatsSupplier > xSupplier;
129 Reference< XNumberFormats > xFormats;
130 Reference< XPropertySet > xFormatProperties;
132 if ( _rxFormatter.is() )
133 xSupplier = _rxFormatter->getNumberFormatsSupplier();
134 if ( xSupplier.is() )
135 xFormats = xSupplier->getNumberFormats();
136 if ( xFormats.is() )
137 xFormatProperties = xFormats->getByKey( _nKey );
139 if ( xFormatProperties.is() )
140 aReturn = xFormatProperties->getPropertyValue( _rPropertyName );
142 catch( const Exception& )
144 OSL_ENSURE( sal_False, "::getNumberFormatProperty: caught an exception (did you create the key with another formatter?)!" );
147 return aReturn;
150 //.........................................................................
151 } // namespace comphelper
152 //.........................................................................