xref: /aoo42x/main/comphelper/source/misc/numbers.cxx (revision 49b34792)
1dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3dde7d3faSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4dde7d3faSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5dde7d3faSAndrew Rist  * distributed with this work for additional information
6dde7d3faSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7dde7d3faSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8dde7d3faSAndrew Rist  * "License"); you may not use this file except in compliance
9dde7d3faSAndrew Rist  * with the License.  You may obtain a copy of the License at
10dde7d3faSAndrew Rist  *
11dde7d3faSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12dde7d3faSAndrew Rist  *
13dde7d3faSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14dde7d3faSAndrew Rist  * software distributed under the License is distributed on an
15dde7d3faSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16dde7d3faSAndrew Rist  * KIND, either express or implied.  See the License for the
17dde7d3faSAndrew Rist  * specific language governing permissions and limitations
18dde7d3faSAndrew Rist  * under the License.
19dde7d3faSAndrew Rist  *
20dde7d3faSAndrew Rist  *************************************************************/
21dde7d3faSAndrew Rist 
22dde7d3faSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
26cdf0e10cSrcweir #include <comphelper/numbers.hxx>
27cdf0e10cSrcweir #include <osl/diagnose.h>
28cdf0e10cSrcweir #include <com/sun/star/util/NumberFormat.hpp>
29cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatTypes.hpp>
30cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
31cdf0e10cSrcweir #include <com/sun/star/lang/Locale.hpp>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir //.........................................................................
34cdf0e10cSrcweir namespace comphelper
35cdf0e10cSrcweir {
36cdf0e10cSrcweir //.........................................................................
37cdf0e10cSrcweir 
38cdf0e10cSrcweir namespace starbeans	= ::com::sun::star::beans;
39cdf0e10cSrcweir namespace starlang	= ::com::sun::star::lang;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir //------------------------------------------------------------------------------
getNumberFormatType(const staruno::Reference<starutil::XNumberFormats> & xFormats,sal_Int32 nKey)42cdf0e10cSrcweir sal_Int16 getNumberFormatType(const staruno::Reference<starutil::XNumberFormats>& xFormats, sal_Int32 nKey)
43cdf0e10cSrcweir {
44cdf0e10cSrcweir 	sal_Int16 nReturn(starutil::NumberFormat::UNDEFINED);
45cdf0e10cSrcweir 	if (xFormats.is())
46cdf0e10cSrcweir 	{
47cdf0e10cSrcweir 		try
48cdf0e10cSrcweir 		{
49cdf0e10cSrcweir 			staruno::Reference<starbeans::XPropertySet> xFormat(xFormats->getByKey(nKey));
50cdf0e10cSrcweir 			if (xFormat.is())
51cdf0e10cSrcweir 				xFormat->getPropertyValue(rtl::OUString::createFromAscii("Type")) >>= nReturn;
52cdf0e10cSrcweir 		}
53cdf0e10cSrcweir 		catch(...)
54cdf0e10cSrcweir 		{
55cdf0e10cSrcweir 			OSL_TRACE("getNumberFormatType : invalid key! (maybe created with another formatter ?)");
56cdf0e10cSrcweir 		}
57cdf0e10cSrcweir 	}
58cdf0e10cSrcweir 	return nReturn;
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
61cdf0e10cSrcweir //------------------------------------------------------------------------------
getNumberFormatType(const staruno::Reference<starutil::XNumberFormatter> & xFormatter,sal_Int32 nKey)62cdf0e10cSrcweir sal_Int16 getNumberFormatType(const staruno::Reference<starutil::XNumberFormatter>& xFormatter, sal_Int32 nKey)
63cdf0e10cSrcweir {
64cdf0e10cSrcweir 	OSL_ENSURE(xFormatter.is(), "getNumberFormatType : the formatter isn't valid !");
65cdf0e10cSrcweir 	staruno::Reference<starutil::XNumberFormatsSupplier> xSupplier( xFormatter->getNumberFormatsSupplier());
66cdf0e10cSrcweir 	OSL_ENSURE(xSupplier.is(), "getNumberFormatType : the formatter doesn't implement a supplier !");
67cdf0e10cSrcweir 	staruno::Reference<starutil::XNumberFormats> xFormats( xSupplier->getNumberFormats());
68cdf0e10cSrcweir 	return getNumberFormatType(xFormats, nKey);
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir //------------------------------------------------------------------------------
getNumberFormatDecimals(const staruno::Reference<starutil::XNumberFormats> & xFormats,sal_Int32 nKey)72cdf0e10cSrcweir staruno::Any getNumberFormatDecimals(const staruno::Reference<starutil::XNumberFormats>& xFormats, sal_Int32 nKey)
73cdf0e10cSrcweir {
74cdf0e10cSrcweir 	if (xFormats.is())
75cdf0e10cSrcweir 	{
76cdf0e10cSrcweir 		try
77cdf0e10cSrcweir 		{
78cdf0e10cSrcweir 			staruno::Reference<starbeans::XPropertySet> xFormat( xFormats->getByKey(nKey));
79cdf0e10cSrcweir 			if (xFormat.is())
80cdf0e10cSrcweir 			{
81cdf0e10cSrcweir 				static ::rtl::OUString PROPERTY_DECIMALS = ::rtl::OUString::createFromAscii("Decimals");
82cdf0e10cSrcweir 				return xFormat->getPropertyValue(PROPERTY_DECIMALS);
83cdf0e10cSrcweir 			}
84cdf0e10cSrcweir 		}
85cdf0e10cSrcweir 		catch(...)
86cdf0e10cSrcweir 		{
87cdf0e10cSrcweir 			OSL_TRACE("getNumberFormatDecimals : invalid key! (may be created with another formatter ?)");
88cdf0e10cSrcweir 		}
89cdf0e10cSrcweir 	}
90cdf0e10cSrcweir 	return staruno::makeAny((sal_Int16)0);
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 
94cdf0e10cSrcweir //------------------------------------------------------------------------------
getStandardFormat(const staruno::Reference<starutil::XNumberFormatter> & xFormatter,sal_Int16 nType,const starlang::Locale & _rLocale)95cdf0e10cSrcweir sal_Int32 getStandardFormat(
96cdf0e10cSrcweir 		const staruno::Reference<starutil::XNumberFormatter>& xFormatter,
97cdf0e10cSrcweir 		sal_Int16 nType,
98cdf0e10cSrcweir 		const starlang::Locale& _rLocale)
99cdf0e10cSrcweir {
100cdf0e10cSrcweir 	staruno::Reference<starutil::XNumberFormatsSupplier> xSupplier( xFormatter.is() ? xFormatter->getNumberFormatsSupplier() : staruno::Reference<starutil::XNumberFormatsSupplier>(NULL));
101cdf0e10cSrcweir 	staruno::Reference<starutil::XNumberFormats> xFormats( xSupplier.is() ? xSupplier->getNumberFormats() : staruno::Reference<starutil::XNumberFormats>(NULL));
102cdf0e10cSrcweir 	staruno::Reference<starutil::XNumberFormatTypes> xTypes(xFormats, staruno::UNO_QUERY);
103cdf0e10cSrcweir 	OSL_ENSURE(xTypes.is(), "getStandardFormat : no format types !");
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 	return xTypes.is() ? xTypes->getStandardFormat(nType, _rLocale) : 0;
106cdf0e10cSrcweir }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir //------------------------------------------------------------------------------
109cdf0e10cSrcweir using namespace ::com::sun::star::uno;
110cdf0e10cSrcweir using namespace ::com::sun::star::util;
111cdf0e10cSrcweir using namespace ::com::sun::star::beans;
112cdf0e10cSrcweir 
113cdf0e10cSrcweir //------------------------------------------------------------------------------
getNumberFormatProperty(const Reference<XNumberFormatter> & _rxFormatter,sal_Int32 _nKey,const rtl::OUString & _rPropertyName)114cdf0e10cSrcweir Any getNumberFormatProperty( const Reference< XNumberFormatter >& _rxFormatter, sal_Int32 _nKey, const rtl::OUString& _rPropertyName )
115cdf0e10cSrcweir {
116cdf0e10cSrcweir 	Any aReturn;
117cdf0e10cSrcweir 
118*49b34792SHerbert Dürr 	OSL_ENSURE( _rxFormatter.is() && !_rPropertyName.isEmpty(), "getNumberFormatProperty: invalid arguments!" );
119cdf0e10cSrcweir 	try
120cdf0e10cSrcweir 	{
121cdf0e10cSrcweir 		Reference< XNumberFormatsSupplier > xSupplier;
122cdf0e10cSrcweir 		Reference< XNumberFormats > xFormats;
123cdf0e10cSrcweir 		Reference< XPropertySet > xFormatProperties;
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 		if ( _rxFormatter.is() )
126cdf0e10cSrcweir 			xSupplier = _rxFormatter->getNumberFormatsSupplier();
127cdf0e10cSrcweir 		if ( xSupplier.is() )
128cdf0e10cSrcweir 			xFormats = xSupplier->getNumberFormats();
129cdf0e10cSrcweir 		if ( xFormats.is() )
130cdf0e10cSrcweir 			xFormatProperties = xFormats->getByKey( _nKey );
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 		if ( xFormatProperties.is() )
133cdf0e10cSrcweir 			aReturn = xFormatProperties->getPropertyValue( _rPropertyName );
134cdf0e10cSrcweir 	}
135cdf0e10cSrcweir 	catch( const Exception& )
136cdf0e10cSrcweir 	{
137cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "::getNumberFormatProperty: caught an exception (did you create the key with another formatter?)!" );
138cdf0e10cSrcweir 	}
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 	return aReturn;
141cdf0e10cSrcweir }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir //.........................................................................
144cdf0e10cSrcweir }	// namespace comphelper
145cdf0e10cSrcweir //.........................................................................
146cdf0e10cSrcweir 
147