GPU-Calc: remove Alloc_Host_Ptr for clmem of NAN vector
[LibreOffice.git] / oox / source / helper / attributelist.cxx
blob8f0707a80703a6b94043a1766f50350c03187f1f
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 <cassert>
23 #include <osl/diagnose.h>
24 #include <rtl/ustrbuf.hxx>
25 #include <sax/fastattribs.hxx>
26 #include "oox/token/tokenmap.hxx"
28 namespace oox {
30 // ============================================================================
32 using namespace ::com::sun::star;
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::xml::sax;
36 // ============================================================================
38 namespace {
40 const sal_Int32 XSTRING_ENCCHAR_LEN = 7;
42 bool lclAddHexDigit( sal_Unicode& orcChar, sal_Unicode cDigit, int nBitShift )
44 if( ('0' <= cDigit) && (cDigit <= '9') ) { orcChar |= ((cDigit - '0') << nBitShift); return true; }
45 if( ('a' <= cDigit) && (cDigit <= 'f') ) { orcChar |= ((cDigit - 'a' + 10) << nBitShift); return true; }
46 if( ('A' <= cDigit) && (cDigit <= 'F') ) { orcChar |= ((cDigit - 'A' + 10) << nBitShift); return true; }
47 return false;
50 sal_Unicode lclGetXChar( const sal_Unicode*& rpcStr, const sal_Unicode* pcEnd )
52 sal_Unicode cChar = 0;
53 if( (pcEnd - rpcStr >= XSTRING_ENCCHAR_LEN) &&
54 (rpcStr[ 0 ] == '_') &&
55 (rpcStr[ 1 ] == 'x') &&
56 (rpcStr[ 6 ] == '_') &&
57 lclAddHexDigit( cChar, rpcStr[ 2 ], 12 ) &&
58 lclAddHexDigit( cChar, rpcStr[ 3 ], 8 ) &&
59 lclAddHexDigit( cChar, rpcStr[ 4 ], 4 ) &&
60 lclAddHexDigit( cChar, rpcStr[ 5 ], 0 ) )
62 rpcStr += XSTRING_ENCCHAR_LEN;
63 return cChar;
65 return *rpcStr++;
68 } // namespace
70 // ----------------------------------------------------------------------------
72 sal_Int32 AttributeConversion::decodeToken( const OUString& rValue )
74 return StaticTokenMap::get().getTokenFromUnicode( rValue );
77 OUString AttributeConversion::decodeXString( const OUString& rValue )
79 // string shorter than one encoded character - no need to decode
80 if( rValue.getLength() < XSTRING_ENCCHAR_LEN )
81 return rValue;
82 OUStringBuffer aBuffer;
83 const sal_Unicode* pcStr = rValue.getStr();
84 const sal_Unicode* pcEnd = pcStr + rValue.getLength();
85 while( pcStr < pcEnd )
86 aBuffer.append( lclGetXChar( pcStr, pcEnd ) );
87 return aBuffer.makeStringAndClear();
90 double AttributeConversion::decodeDouble( const OUString& rValue )
92 return rValue.toDouble();
95 sal_Int32 AttributeConversion::decodeInteger( const OUString& rValue )
97 return rValue.toInt32();
100 sal_uInt32 AttributeConversion::decodeUnsigned( const OUString& rValue )
102 return getLimitedValue< sal_uInt32, sal_Int64 >( rValue.toInt64(), 0, SAL_MAX_UINT32 );
105 sal_Int64 AttributeConversion::decodeHyper( const OUString& rValue )
107 return rValue.toInt64();
110 sal_Int32 AttributeConversion::decodeIntegerHex( const OUString& rValue )
112 // It looks like all Office Open XML attributes containing hexadecimal
113 // values are based on xsd:hexBinary and so use an unsigned representation:
114 return static_cast< sal_Int32 >(rValue.toUInt32( 16 ));
115 //TODO: Change this function to return sal_uInt32 and get rid of the
116 // cast, but that will have a ripple effect
119 // ============================================================================
121 AttributeList::AttributeList( const Reference< XFastAttributeList >& rxAttribs ) :
122 mxAttribs( rxAttribs ),
123 mpAttribList( NULL )
125 OSL_ENSURE( mxAttribs.is(), "AttributeList::AttributeList - missing attribute list interface" );
128 sax_fastparser::FastAttributeList *AttributeList::getAttribList() const
130 if( mpAttribList == NULL )
132 assert( dynamic_cast< sax_fastparser::FastAttributeList *>( mxAttribs.get() ) != NULL );
133 mpAttribList = static_cast< sax_fastparser::FastAttributeList *>( mxAttribs.get() );
135 return mpAttribList;
138 bool AttributeList::hasAttribute( sal_Int32 nAttrToken ) const
140 return mxAttribs->hasAttribute( nAttrToken );
143 // optional return values -----------------------------------------------------
145 OptValue< sal_Int32 > AttributeList::getToken( sal_Int32 nAttrToken ) const
147 sal_Int32 nToken = mxAttribs->getOptionalValueToken( nAttrToken, XML_TOKEN_INVALID );
148 return OptValue< sal_Int32 >( nToken != XML_TOKEN_INVALID, nToken );
151 OptValue< OUString > AttributeList::getString( sal_Int32 nAttrToken ) const
153 // check if the attribute exists (empty string may be different to missing attribute)
154 if( mxAttribs->hasAttribute( nAttrToken ) )
155 return OptValue< OUString >( mxAttribs->getOptionalValue( nAttrToken ) );
156 return OptValue< OUString >();
159 OptValue< OUString > AttributeList::getXString( sal_Int32 nAttrToken ) const
161 // check if the attribute exists (empty string may be different to missing attribute)
162 if( mxAttribs->hasAttribute( nAttrToken ) )
163 return OptValue< OUString >( AttributeConversion::decodeXString( mxAttribs->getOptionalValue( nAttrToken ) ) );
164 return OptValue< OUString >();
167 OptValue< double > AttributeList::getDouble( sal_Int32 nAttrToken ) const
169 double nValue;
170 bool bValid = getAttribList()->getAsDouble( nAttrToken, nValue );
171 return OptValue< double >( bValid, nValue );
174 OptValue< sal_Int32 > AttributeList::getInteger( sal_Int32 nAttrToken ) const
176 sal_Int32 nValue;
177 bool bValid = getAttribList()->getAsInteger( nAttrToken, nValue );
178 return OptValue< sal_Int32 >( bValid, nValue );
181 OptValue< sal_uInt32 > AttributeList::getUnsigned( sal_Int32 nAttrToken ) const
183 OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
184 bool bValid = !aValue.isEmpty();
185 return OptValue< sal_uInt32 >( bValid, AttributeConversion::decodeUnsigned( aValue ) );
188 OptValue< sal_Int64 > AttributeList::getHyper( sal_Int32 nAttrToken ) const
190 OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
191 bool bValid = !aValue.isEmpty();
192 return OptValue< sal_Int64 >( bValid, bValid ? AttributeConversion::decodeHyper( aValue ) : 0 );
195 OptValue< sal_Int32 > AttributeList::getIntegerHex( sal_Int32 nAttrToken ) const
197 OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
198 bool bValid = !aValue.isEmpty();
199 return OptValue< sal_Int32 >( bValid, bValid ? AttributeConversion::decodeIntegerHex( aValue ) : 0 );
202 OptValue< bool > AttributeList::getBool( sal_Int32 nAttrToken ) const
204 const char *pAttr;
206 // catch the common cases as quickly as possible first
207 bool bHasAttr = getAttribList()->getAsChar( nAttrToken, pAttr );
208 if( !bHasAttr )
209 return OptValue< bool >();
210 if( !strcmp( pAttr, "false" ) )
211 return OptValue< bool >( false );
212 if( !strcmp( pAttr, "true" ) )
213 return OptValue< bool >( true );
215 // now for all the crazy stuff
217 // boolean attributes may be "t", "f", "true", "false", "on", "off", "1", or "0"
218 switch( getToken( nAttrToken, XML_TOKEN_INVALID ) )
220 case XML_t: return OptValue< bool >( true ); // used in VML
221 case XML_true: return OptValue< bool >( true );
222 case XML_on: return OptValue< bool >( true );
223 case XML_f: return OptValue< bool >( false ); // used in VML
224 case XML_false: return OptValue< bool >( false );
225 case XML_off: return OptValue< bool >( false );
227 OptValue< sal_Int32 > onValue = getInteger( nAttrToken );
228 return OptValue< bool >( onValue.has(), onValue.get() != 0 );
231 OptValue< util::DateTime > AttributeList::getDateTime( sal_Int32 nAttrToken ) const
233 OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
234 util::DateTime aDateTime;
235 bool bValid = (aValue.getLength() == 19) && (aValue[ 4 ] == '-') && (aValue[ 7 ] == '-') &&
236 (aValue[ 10 ] == 'T') && (aValue[ 13 ] == ':') && (aValue[ 16 ] == ':');
237 if( bValid )
239 aDateTime.Year = static_cast< sal_uInt16 >( aValue.copy( 0, 4 ).toInt32() );
240 aDateTime.Month = static_cast< sal_uInt16 >( aValue.copy( 5, 2 ).toInt32() );
241 aDateTime.Day = static_cast< sal_uInt16 >( aValue.copy( 8, 2 ).toInt32() );
242 aDateTime.Hours = static_cast< sal_uInt16 >( aValue.copy( 11, 2 ).toInt32() );
243 aDateTime.Minutes = static_cast< sal_uInt16 >( aValue.copy( 14, 2 ).toInt32() );
244 aDateTime.Seconds = static_cast< sal_uInt16 >( aValue.copy( 17, 2 ).toInt32() );
246 return OptValue< util::DateTime >( bValid, aDateTime );
249 // defaulted return values ----------------------------------------------------
251 sal_Int32 AttributeList::getToken( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
253 return mxAttribs->getOptionalValueToken( nAttrToken, nDefault );
256 OUString AttributeList::getString( sal_Int32 nAttrToken, const OUString& rDefault ) const
258 // try to avoid slow exception throw/catch if we can
259 if (rDefault.isEmpty())
260 return mxAttribs->getOptionalValue( nAttrToken );
264 return mxAttribs->getValue( nAttrToken );
266 catch( Exception& )
269 return rDefault;
272 OUString AttributeList::getXString( sal_Int32 nAttrToken, const OUString& rDefault ) const
274 return getXString( nAttrToken ).get( rDefault );
277 const char* AttributeList::getChar( sal_Int32 nAttrToken ) const
279 const char* p = NULL;
280 bool bValid = getAttribList()->getAsChar(nAttrToken, p);
281 if (!bValid)
282 p = NULL;
284 return p;
287 double AttributeList::getDouble( sal_Int32 nAttrToken, double fDefault ) const
289 return getDouble( nAttrToken ).get( fDefault );
292 sal_Int32 AttributeList::getInteger( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
294 return getInteger( nAttrToken ).get( nDefault );
297 sal_uInt32 AttributeList::getUnsigned( sal_Int32 nAttrToken, sal_uInt32 nDefault ) const
299 return getUnsigned( nAttrToken ).get( nDefault );
302 sal_Int64 AttributeList::getHyper( sal_Int32 nAttrToken, sal_Int64 nDefault ) const
304 return getHyper( nAttrToken ).get( nDefault );
307 sal_Int32 AttributeList::getIntegerHex( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
309 return getIntegerHex( nAttrToken ).get( nDefault );
312 bool AttributeList::getBool( sal_Int32 nAttrToken, bool bDefault ) const
314 return getBool( nAttrToken ).get( bDefault );
317 util::DateTime AttributeList::getDateTime( sal_Int32 nAttrToken, const util::DateTime& rDefault ) const
319 return getDateTime( nAttrToken ).get( rDefault );
322 // ============================================================================
324 } // namespace oox
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */