xref: /aoo41x/main/comphelper/source/misc/numbers.cxx (revision cdf0e10c)
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_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>
36 
37 //.........................................................................
38 namespace comphelper
39 {
40 //.........................................................................
41 
42 namespace starbeans	= ::com::sun::star::beans;
43 namespace starlang	= ::com::sun::star::lang;
44 
45 //------------------------------------------------------------------------------
46 sal_Int16 getNumberFormatType(const staruno::Reference<starutil::XNumberFormats>& xFormats, sal_Int32 nKey)
47 {
48 	sal_Int16 nReturn(starutil::NumberFormat::UNDEFINED);
49 	if (xFormats.is())
50 	{
51 		try
52 		{
53 			staruno::Reference<starbeans::XPropertySet> xFormat(xFormats->getByKey(nKey));
54 			if (xFormat.is())
55 				xFormat->getPropertyValue(rtl::OUString::createFromAscii("Type")) >>= nReturn;
56 		}
57 		catch(...)
58 		{
59 			OSL_TRACE("getNumberFormatType : invalid key! (maybe created with another formatter ?)");
60 		}
61 	}
62 	return nReturn;
63 }
64 
65 //------------------------------------------------------------------------------
66 sal_Int16 getNumberFormatType(const staruno::Reference<starutil::XNumberFormatter>& xFormatter, sal_Int32 nKey)
67 {
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);
73 }
74 
75 //------------------------------------------------------------------------------
76 staruno::Any getNumberFormatDecimals(const staruno::Reference<starutil::XNumberFormats>& xFormats, sal_Int32 nKey)
77 {
78 	if (xFormats.is())
79 	{
80 		try
81 		{
82 			staruno::Reference<starbeans::XPropertySet> xFormat( xFormats->getByKey(nKey));
83 			if (xFormat.is())
84 			{
85 				static ::rtl::OUString PROPERTY_DECIMALS = ::rtl::OUString::createFromAscii("Decimals");
86 				return xFormat->getPropertyValue(PROPERTY_DECIMALS);
87 			}
88 		}
89 		catch(...)
90 		{
91 			OSL_TRACE("getNumberFormatDecimals : invalid key! (may be created with another formatter ?)");
92 		}
93 	}
94 	return staruno::makeAny((sal_Int16)0);
95 }
96 
97 
98 //------------------------------------------------------------------------------
99 sal_Int32 getStandardFormat(
100 		const staruno::Reference<starutil::XNumberFormatter>& xFormatter,
101 		sal_Int16 nType,
102 		const starlang::Locale& _rLocale)
103 {
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 !");
108 
109 	return xTypes.is() ? xTypes->getStandardFormat(nType, _rLocale) : 0;
110 }
111 
112 //------------------------------------------------------------------------------
113 using namespace ::com::sun::star::uno;
114 using namespace ::com::sun::star::util;
115 using namespace ::com::sun::star::beans;
116 
117 //------------------------------------------------------------------------------
118 Any getNumberFormatProperty( const Reference< XNumberFormatter >& _rxFormatter, sal_Int32 _nKey, const rtl::OUString& _rPropertyName )
119 {
120 	Any aReturn;
121 
122 	OSL_ENSURE( _rxFormatter.is() && _rPropertyName.getLength(), "getNumberFormatProperty: invalid arguments!" );
123 	try
124 	{
125 		Reference< XNumberFormatsSupplier > xSupplier;
126 		Reference< XNumberFormats > xFormats;
127 		Reference< XPropertySet > xFormatProperties;
128 
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 );
135 
136 		if ( xFormatProperties.is() )
137 			aReturn = xFormatProperties->getPropertyValue( _rPropertyName );
138 	}
139 	catch( const Exception& )
140 	{
141 		OSL_ENSURE( sal_False, "::getNumberFormatProperty: caught an exception (did you create the key with another formatter?)!" );
142 	}
143 
144 	return aReturn;
145 }
146 
147 //.........................................................................
148 }	// namespace comphelper
149 //.........................................................................
150 
151