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