Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <comphelper/numbers.hxx>
21 : #include <osl/diagnose.h>
22 : #include <com/sun/star/util/NumberFormat.hpp>
23 : #include <com/sun/star/util/XNumberFormatTypes.hpp>
24 : #include <com/sun/star/beans/XPropertySet.hpp>
25 : #include <com/sun/star/lang/Locale.hpp>
26 :
27 :
28 : namespace comphelper
29 : {
30 :
31 182 : sal_Int16 getNumberFormatType(const css::uno::Reference<css::util::XNumberFormats>& xFormats, sal_Int32 nKey)
32 : {
33 182 : sal_Int16 nReturn(css::util::NumberFormat::UNDEFINED);
34 182 : if (xFormats.is())
35 : {
36 : try
37 : {
38 182 : css::uno::Reference<css::beans::XPropertySet> xFormat(xFormats->getByKey(nKey));
39 182 : if (xFormat.is())
40 182 : xFormat->getPropertyValue("Type") >>= nReturn;
41 : }
42 0 : catch(...)
43 : {
44 : OSL_TRACE("getNumberFormatType : invalid key! (maybe created with another formatter ?)");
45 : }
46 : }
47 182 : return nReturn;
48 : }
49 :
50 :
51 0 : sal_Int16 getNumberFormatType(const css::uno::Reference<css::util::XNumberFormatter>& xFormatter, sal_Int32 nKey)
52 : {
53 : OSL_ENSURE(xFormatter.is(), "getNumberFormatType : the formatter isn't valid !");
54 0 : css::uno::Reference<css::util::XNumberFormatsSupplier> xSupplier( xFormatter->getNumberFormatsSupplier());
55 : OSL_ENSURE(xSupplier.is(), "getNumberFormatType : the formatter doesn't implement a supplier !");
56 0 : css::uno::Reference<css::util::XNumberFormats> xFormats( xSupplier->getNumberFormats());
57 0 : return getNumberFormatType(xFormats, nKey);
58 : }
59 :
60 :
61 0 : css::uno::Any getNumberFormatDecimals(const css::uno::Reference<css::util::XNumberFormats>& xFormats, sal_Int32 nKey)
62 : {
63 0 : if (xFormats.is())
64 : {
65 : try
66 : {
67 0 : css::uno::Reference<css::beans::XPropertySet> xFormat( xFormats->getByKey(nKey));
68 0 : if (xFormat.is())
69 : {
70 0 : static OUString PROPERTY_DECIMALS( "Decimals" );
71 0 : return xFormat->getPropertyValue(PROPERTY_DECIMALS);
72 0 : }
73 : }
74 0 : catch(...)
75 : {
76 : OSL_TRACE("getNumberFormatDecimals : invalid key! (may be created with another formatter ?)");
77 : }
78 : }
79 0 : return css::uno::makeAny((sal_Int16)0);
80 : }
81 :
82 :
83 :
84 0 : sal_Int32 getStandardFormat(
85 : const css::uno::Reference<css::util::XNumberFormatter>& xFormatter,
86 : sal_Int16 nType,
87 : const css::lang::Locale& _rLocale)
88 : {
89 0 : css::uno::Reference<css::util::XNumberFormatsSupplier> xSupplier( xFormatter.is() ? xFormatter->getNumberFormatsSupplier() : css::uno::Reference<css::util::XNumberFormatsSupplier>(NULL));
90 0 : css::uno::Reference<css::util::XNumberFormats> xFormats( xSupplier.is() ? xSupplier->getNumberFormats() : css::uno::Reference<css::util::XNumberFormats>(NULL));
91 0 : css::uno::Reference<css::util::XNumberFormatTypes> xTypes(xFormats, css::uno::UNO_QUERY);
92 : OSL_ENSURE(xTypes.is(), "getStandardFormat : no format types !");
93 :
94 0 : return xTypes.is() ? xTypes->getStandardFormat(nType, _rLocale) : 0;
95 : }
96 :
97 :
98 : using namespace ::com::sun::star::uno;
99 : using namespace ::com::sun::star::util;
100 : using namespace ::com::sun::star::beans;
101 :
102 :
103 0 : Any getNumberFormatProperty( const Reference< XNumberFormatter >& _rxFormatter, sal_Int32 _nKey, const OUString& _rPropertyName )
104 : {
105 0 : Any aReturn;
106 :
107 : OSL_ENSURE( _rxFormatter.is() && !_rPropertyName.isEmpty(), "getNumberFormatProperty: invalid arguments!" );
108 : try
109 : {
110 0 : Reference< XNumberFormatsSupplier > xSupplier;
111 0 : Reference< XNumberFormats > xFormats;
112 0 : Reference< XPropertySet > xFormatProperties;
113 :
114 0 : if ( _rxFormatter.is() )
115 0 : xSupplier = _rxFormatter->getNumberFormatsSupplier();
116 0 : if ( xSupplier.is() )
117 0 : xFormats = xSupplier->getNumberFormats();
118 0 : if ( xFormats.is() )
119 0 : xFormatProperties = xFormats->getByKey( _nKey );
120 :
121 0 : if ( xFormatProperties.is() )
122 0 : aReturn = xFormatProperties->getPropertyValue( _rPropertyName );
123 : }
124 0 : catch( const Exception& )
125 : {
126 : OSL_FAIL( "::getNumberFormatProperty: caught an exception (did you create the key with another formatter?)!" );
127 : }
128 :
129 0 : return aReturn;
130 : }
131 :
132 :
133 : } // namespace comphelper
134 :
135 :
136 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|