bump product version to 4.1.6.2
[LibreOffice.git] / oox / source / helper / attributelist.cxx
blobdebd1f8a98c90a89bd3f8daa0307d428b7d46b23
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "oox/helper/attributelist.hxx"
22 #include <osl/diagnose.h>
23 #include <rtl/ustrbuf.hxx>
24 #include "oox/token/tokenmap.hxx"
26 namespace oox {
28 // ============================================================================
30 using namespace ::com::sun::star;
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::xml::sax;
34 // ============================================================================
36 namespace {
38 const sal_Int32 XSTRING_ENCCHAR_LEN = 7;
40 bool lclAddHexDigit( sal_Unicode& orcChar, sal_Unicode cDigit, int nBitShift )
42 if( ('0' <= cDigit) && (cDigit <= '9') ) { orcChar |= ((cDigit - '0') << nBitShift); return true; }
43 if( ('a' <= cDigit) && (cDigit <= 'f') ) { orcChar |= ((cDigit - 'a' + 10) << nBitShift); return true; }
44 if( ('A' <= cDigit) && (cDigit <= 'F') ) { orcChar |= ((cDigit - 'A' + 10) << nBitShift); return true; }
45 return false;
48 sal_Unicode lclGetXChar( const sal_Unicode*& rpcStr, const sal_Unicode* pcEnd )
50 sal_Unicode cChar = 0;
51 if( (pcEnd - rpcStr >= XSTRING_ENCCHAR_LEN) &&
52 (rpcStr[ 0 ] == '_') &&
53 (rpcStr[ 1 ] == 'x') &&
54 (rpcStr[ 6 ] == '_') &&
55 lclAddHexDigit( cChar, rpcStr[ 2 ], 12 ) &&
56 lclAddHexDigit( cChar, rpcStr[ 3 ], 8 ) &&
57 lclAddHexDigit( cChar, rpcStr[ 4 ], 4 ) &&
58 lclAddHexDigit( cChar, rpcStr[ 5 ], 0 ) )
60 rpcStr += XSTRING_ENCCHAR_LEN;
61 return cChar;
63 return *rpcStr++;
66 } // namespace
68 // ----------------------------------------------------------------------------
70 sal_Int32 AttributeConversion::decodeToken( const OUString& rValue )
72 return StaticTokenMap::get().getTokenFromUnicode( rValue );
75 OUString AttributeConversion::decodeXString( const OUString& rValue )
77 // string shorter than one encoded character - no need to decode
78 if( rValue.getLength() < XSTRING_ENCCHAR_LEN )
79 return rValue;
80 OUStringBuffer aBuffer;
81 const sal_Unicode* pcStr = rValue.getStr();
82 const sal_Unicode* pcEnd = pcStr + rValue.getLength();
83 while( pcStr < pcEnd )
84 aBuffer.append( lclGetXChar( pcStr, pcEnd ) );
85 return aBuffer.makeStringAndClear();
88 double AttributeConversion::decodeDouble( const OUString& rValue )
90 return rValue.toDouble();
93 sal_Int32 AttributeConversion::decodeInteger( const OUString& rValue )
95 return rValue.toInt32();
98 sal_uInt32 AttributeConversion::decodeUnsigned( const OUString& rValue )
100 return getLimitedValue< sal_uInt32, sal_Int64 >( rValue.toInt64(), 0, SAL_MAX_UINT32 );
103 sal_Int64 AttributeConversion::decodeHyper( const OUString& rValue )
105 return rValue.toInt64();
108 sal_Int32 AttributeConversion::decodeIntegerHex( const OUString& rValue )
110 return rValue.toInt32( 16 );
113 // ============================================================================
115 AttributeList::AttributeList( const Reference< XFastAttributeList >& rxAttribs ) :
116 mxAttribs( rxAttribs )
118 OSL_ENSURE( mxAttribs.is(), "AttributeList::AttributeList - missing attribute list interface" );
121 bool AttributeList::hasAttribute( sal_Int32 nAttrToken ) const
123 return mxAttribs->hasAttribute( nAttrToken );
126 // optional return values -----------------------------------------------------
128 OptValue< sal_Int32 > AttributeList::getToken( sal_Int32 nAttrToken ) const
130 sal_Int32 nToken = mxAttribs->getOptionalValueToken( nAttrToken, XML_TOKEN_INVALID );
131 return OptValue< sal_Int32 >( nToken != XML_TOKEN_INVALID, nToken );
134 OptValue< OUString > AttributeList::getString( sal_Int32 nAttrToken ) const
136 // check if the attribute exists (empty string may be different to missing attribute)
137 if( mxAttribs->hasAttribute( nAttrToken ) )
138 return OptValue< OUString >( mxAttribs->getOptionalValue( nAttrToken ) );
139 return OptValue< OUString >();
142 OptValue< OUString > AttributeList::getXString( sal_Int32 nAttrToken ) const
144 // check if the attribute exists (empty string may be different to missing attribute)
145 if( mxAttribs->hasAttribute( nAttrToken ) )
146 return OptValue< OUString >( AttributeConversion::decodeXString( mxAttribs->getOptionalValue( nAttrToken ) ) );
147 return OptValue< OUString >();
150 OptValue< double > AttributeList::getDouble( sal_Int32 nAttrToken ) const
152 OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
153 bool bValid = !aValue.isEmpty();
154 return OptValue< double >( bValid, bValid ? AttributeConversion::decodeDouble( aValue ) : 0.0 );
157 OptValue< sal_Int32 > AttributeList::getInteger( sal_Int32 nAttrToken ) const
159 OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
160 bool bValid = !aValue.isEmpty();
161 return OptValue< sal_Int32 >( bValid, bValid ? AttributeConversion::decodeInteger( aValue ) : 0 );
164 OptValue< sal_uInt32 > AttributeList::getUnsigned( sal_Int32 nAttrToken ) const
166 OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
167 bool bValid = !aValue.isEmpty();
168 return OptValue< sal_uInt32 >( bValid, AttributeConversion::decodeUnsigned( aValue ) );
171 OptValue< sal_Int64 > AttributeList::getHyper( sal_Int32 nAttrToken ) const
173 OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
174 bool bValid = !aValue.isEmpty();
175 return OptValue< sal_Int64 >( bValid, bValid ? AttributeConversion::decodeHyper( aValue ) : 0 );
178 OptValue< sal_Int32 > AttributeList::getIntegerHex( sal_Int32 nAttrToken ) const
180 OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
181 bool bValid = !aValue.isEmpty();
182 return OptValue< sal_Int32 >( bValid, bValid ? AttributeConversion::decodeIntegerHex( aValue ) : 0 );
185 OptValue< bool > AttributeList::getBool( sal_Int32 nAttrToken ) const
187 // boolean attributes may be "t", "f", "true", "false", "on", "off", "1", or "0"
188 switch( getToken( nAttrToken, XML_TOKEN_INVALID ) )
190 case XML_t: return OptValue< bool >( true ); // used in VML
191 case XML_true: return OptValue< bool >( true );
192 case XML_on: return OptValue< bool >( true );
193 case XML_f: return OptValue< bool >( false ); // used in VML
194 case XML_false: return OptValue< bool >( false );
195 case XML_off: return OptValue< bool >( false );
197 OptValue< sal_Int32 > onValue = getInteger( nAttrToken );
198 return OptValue< bool >( onValue.has(), onValue.get() != 0 );
201 OptValue< util::DateTime > AttributeList::getDateTime( sal_Int32 nAttrToken ) const
203 OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
204 util::DateTime aDateTime;
205 bool bValid = (aValue.getLength() == 19) && (aValue[ 4 ] == '-') && (aValue[ 7 ] == '-') &&
206 (aValue[ 10 ] == 'T') && (aValue[ 13 ] == ':') && (aValue[ 16 ] == ':');
207 if( bValid )
209 aDateTime.Year = static_cast< sal_uInt16 >( aValue.copy( 0, 4 ).toInt32() );
210 aDateTime.Month = static_cast< sal_uInt16 >( aValue.copy( 5, 2 ).toInt32() );
211 aDateTime.Day = static_cast< sal_uInt16 >( aValue.copy( 8, 2 ).toInt32() );
212 aDateTime.Hours = static_cast< sal_uInt16 >( aValue.copy( 11, 2 ).toInt32() );
213 aDateTime.Minutes = static_cast< sal_uInt16 >( aValue.copy( 14, 2 ).toInt32() );
214 aDateTime.Seconds = static_cast< sal_uInt16 >( aValue.copy( 17, 2 ).toInt32() );
216 return OptValue< util::DateTime >( bValid, aDateTime );
219 // defaulted return values ----------------------------------------------------
221 sal_Int32 AttributeList::getToken( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
223 return mxAttribs->getOptionalValueToken( nAttrToken, nDefault );
226 OUString AttributeList::getString( sal_Int32 nAttrToken, const OUString& rDefault ) const
228 // try to avoid slow exception throw/catch if we can
229 if (rDefault.isEmpty())
230 return mxAttribs->getOptionalValue( nAttrToken );
234 return mxAttribs->getValue( nAttrToken );
236 catch( Exception& )
239 return rDefault;
242 OUString AttributeList::getXString( sal_Int32 nAttrToken, const OUString& rDefault ) const
244 return getXString( nAttrToken ).get( rDefault );
247 double AttributeList::getDouble( sal_Int32 nAttrToken, double fDefault ) const
249 return getDouble( nAttrToken ).get( fDefault );
252 sal_Int32 AttributeList::getInteger( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
254 return getInteger( nAttrToken ).get( nDefault );
257 sal_uInt32 AttributeList::getUnsigned( sal_Int32 nAttrToken, sal_uInt32 nDefault ) const
259 return getUnsigned( nAttrToken ).get( nDefault );
262 sal_Int64 AttributeList::getHyper( sal_Int32 nAttrToken, sal_Int64 nDefault ) const
264 return getHyper( nAttrToken ).get( nDefault );
267 sal_Int32 AttributeList::getIntegerHex( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
269 return getIntegerHex( nAttrToken ).get( nDefault );
272 bool AttributeList::getBool( sal_Int32 nAttrToken, bool bDefault ) const
274 return getBool( nAttrToken ).get( bDefault );
277 util::DateTime AttributeList::getDateTime( sal_Int32 nAttrToken, const util::DateTime& rDefault ) const
279 return getDateTime( nAttrToken ).get( rDefault );
282 // ============================================================================
284 } // namespace oox
286 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */