1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
23 #include <osl/diagnose.h>
24 #include <rtl/ustrbuf.hxx>
25 #include <sax/fastattribs.hxx>
26 #include <oox/token/tokenmap.hxx>
30 using namespace ::com::sun::star
;
31 using namespace ::com::sun::star::uno
;
32 using namespace ::com::sun::star::xml::sax
;
36 const sal_Int32 XSTRING_ENCCHAR_LEN
= 7;
38 bool lclAddHexDigit( sal_Unicode
& orcChar
, sal_Unicode cDigit
, int nBitShift
)
40 if( ('0' <= cDigit
) && (cDigit
<= '9') ) { orcChar
|= ((cDigit
- '0') << nBitShift
); return true; }
41 if( ('a' <= cDigit
) && (cDigit
<= 'f') ) { orcChar
|= ((cDigit
- 'a' + 10) << nBitShift
); return true; }
42 if( ('A' <= cDigit
) && (cDigit
<= 'F') ) { orcChar
|= ((cDigit
- 'A' + 10) << nBitShift
); return true; }
46 sal_Unicode
lclGetXChar( const sal_Unicode
*& rpcStr
, const sal_Unicode
* pcEnd
)
48 sal_Unicode cChar
= 0;
49 if( (pcEnd
- rpcStr
>= XSTRING_ENCCHAR_LEN
) &&
50 (rpcStr
[ 0 ] == '_') &&
51 (rpcStr
[ 1 ] == 'x') &&
52 (rpcStr
[ 6 ] == '_') &&
53 lclAddHexDigit( cChar
, rpcStr
[ 2 ], 12 ) &&
54 lclAddHexDigit( cChar
, rpcStr
[ 3 ], 8 ) &&
55 lclAddHexDigit( cChar
, rpcStr
[ 4 ], 4 ) &&
56 lclAddHexDigit( cChar
, rpcStr
[ 5 ], 0 ) )
58 rpcStr
+= XSTRING_ENCCHAR_LEN
;
66 sal_Int32
AttributeConversion::decodeToken( const OUString
& rValue
)
68 return TokenMap::getTokenFromUnicode( rValue
);
71 OUString
AttributeConversion::decodeXString( const OUString
& rValue
)
73 // string shorter than one encoded character - no need to decode
74 if( rValue
.getLength() < XSTRING_ENCCHAR_LEN
)
76 OUStringBuffer aBuffer
;
77 const sal_Unicode
* pcStr
= rValue
.getStr();
78 const sal_Unicode
* pcEnd
= pcStr
+ rValue
.getLength();
79 while( pcStr
< pcEnd
)
80 aBuffer
.append( lclGetXChar( pcStr
, pcEnd
) );
81 return aBuffer
.makeStringAndClear();
84 sal_Int32
AttributeConversion::decodeInteger( const OUString
& rValue
)
86 return rValue
.toInt32();
89 sal_uInt32
AttributeConversion::decodeUnsigned( const OUString
& rValue
)
91 return getLimitedValue
< sal_uInt32
, sal_Int64
>( rValue
.toInt64(), 0, SAL_MAX_UINT32
);
94 sal_Int64
AttributeConversion::decodeHyper( const OUString
& rValue
)
96 return rValue
.toInt64();
99 sal_Int32
AttributeConversion::decodeIntegerHex( const OUString
& rValue
)
101 // It looks like all Office Open XML attributes containing hexadecimal
102 // values are based on xsd:hexBinary and so use an unsigned representation:
103 return static_cast< sal_Int32
>(rValue
.toUInt32( 16 ));
104 //TODO: Change this function to return sal_uInt32 and get rid of the
105 // cast, but that will have a ripple effect
108 AttributeList::AttributeList( const Reference
< XFastAttributeList
>& rxAttribs
) :
109 mxAttribs( rxAttribs
),
110 mpAttribList( nullptr )
112 OSL_ENSURE( mxAttribs
.is(), "AttributeList::AttributeList - missing attribute list interface" );
115 sax_fastparser::FastAttributeList
*AttributeList::getAttribList() const
117 if( mpAttribList
== nullptr )
119 mpAttribList
= sax_fastparser::FastAttributeList::castToFastAttributeList( mxAttribs
);
124 bool AttributeList::hasAttribute( sal_Int32 nAttrToken
) const
126 return mxAttribs
->hasAttribute( nAttrToken
);
129 // optional return values -----------------------------------------------------
131 OptValue
< sal_Int32
> AttributeList::getToken( sal_Int32 nAttrToken
) const
133 sal_Int32 nToken
= mxAttribs
->getOptionalValueToken( nAttrToken
, XML_TOKEN_INVALID
);
134 return OptValue
< sal_Int32
>( nToken
!= XML_TOKEN_INVALID
, nToken
);
137 OptValue
< OUString
> AttributeList::getString( sal_Int32 nAttrToken
) const
139 // check if the attribute exists (empty string may be different to missing attribute)
140 if( mxAttribs
->hasAttribute( nAttrToken
) )
141 return OptValue
< OUString
>( mxAttribs
->getOptionalValue( nAttrToken
) );
142 return OptValue
< OUString
>();
145 OptValue
< OUString
> AttributeList::getXString( sal_Int32 nAttrToken
) const
147 // check if the attribute exists (empty string may be different to missing attribute)
148 if( mxAttribs
->hasAttribute( nAttrToken
) )
149 return OptValue
< OUString
>( AttributeConversion::decodeXString( mxAttribs
->getOptionalValue( nAttrToken
) ) );
150 return OptValue
< OUString
>();
153 OptValue
< double > AttributeList::getDouble( sal_Int32 nAttrToken
) const
156 bool bValid
= getAttribList()->getAsDouble( nAttrToken
, nValue
);
157 return OptValue
< double >( bValid
, nValue
);
160 OptValue
< sal_Int32
> AttributeList::getInteger( sal_Int32 nAttrToken
) const
163 bool bValid
= getAttribList()->getAsInteger( nAttrToken
, nValue
);
164 return OptValue
< sal_Int32
>( bValid
, nValue
);
167 OptValue
< sal_uInt32
> AttributeList::getUnsigned( sal_Int32 nAttrToken
) const
169 OUString aValue
= mxAttribs
->getOptionalValue( nAttrToken
);
170 bool bValid
= !aValue
.isEmpty();
171 return OptValue
< sal_uInt32
>( bValid
, AttributeConversion::decodeUnsigned( aValue
) );
174 OptValue
< sal_Int64
> AttributeList::getHyper( sal_Int32 nAttrToken
) const
176 OUString aValue
= mxAttribs
->getOptionalValue( nAttrToken
);
177 bool bValid
= !aValue
.isEmpty();
178 return OptValue
< sal_Int64
>( bValid
, bValid
? AttributeConversion::decodeHyper( aValue
) : 0 );
181 OptValue
< sal_Int32
> AttributeList::getIntegerHex( sal_Int32 nAttrToken
) const
183 OUString aValue
= mxAttribs
->getOptionalValue( nAttrToken
);
184 bool bValid
= !aValue
.isEmpty();
185 return OptValue
< sal_Int32
>( bValid
, bValid
? AttributeConversion::decodeIntegerHex( aValue
) : 0 );
188 OptValue
< bool > AttributeList::getBool( sal_Int32 nAttrToken
) const
192 // catch the common cases as quickly as possible first
193 bool bHasAttr
= getAttribList()->getAsChar( nAttrToken
, pAttr
);
195 return OptValue
< bool >();
196 if( !strcmp( pAttr
, "false" ) )
197 return OptValue
< bool >( false );
198 if( !strcmp( pAttr
, "true" ) )
199 return OptValue
< bool >( true );
201 // now for all the crazy stuff
203 // boolean attributes may be "t", "f", "true", "false", "on", "off", "1", or "0"
204 switch( getToken( nAttrToken
, XML_TOKEN_INVALID
) )
206 case XML_t
: return OptValue
< bool >( true ); // used in VML
207 case XML_true
: return OptValue
< bool >( true );
208 case XML_on
: return OptValue
< bool >( true );
209 case XML_f
: return OptValue
< bool >( false ); // used in VML
210 case XML_false
: return OptValue
< bool >( false );
211 case XML_off
: return OptValue
< bool >( false );
213 OptValue
< sal_Int32
> onValue
= getInteger( nAttrToken
);
214 return OptValue
< bool >( onValue
.has(), onValue
.get() != 0 );
217 OptValue
< util::DateTime
> AttributeList::getDateTime( sal_Int32 nAttrToken
) const
219 OUString aValue
= mxAttribs
->getOptionalValue( nAttrToken
);
220 util::DateTime aDateTime
;
221 bool bValid
= (aValue
.getLength() == 19) && (aValue
[ 4 ] == '-') && (aValue
[ 7 ] == '-') &&
222 (aValue
[ 10 ] == 'T') && (aValue
[ 13 ] == ':') && (aValue
[ 16 ] == ':');
225 aDateTime
.Year
= static_cast< sal_uInt16
>( aValue
.copy( 0, 4 ).toInt32() );
226 aDateTime
.Month
= static_cast< sal_uInt16
>( aValue
.copy( 5, 2 ).toInt32() );
227 aDateTime
.Day
= static_cast< sal_uInt16
>( aValue
.copy( 8, 2 ).toInt32() );
228 aDateTime
.Hours
= static_cast< sal_uInt16
>( aValue
.copy( 11, 2 ).toInt32() );
229 aDateTime
.Minutes
= static_cast< sal_uInt16
>( aValue
.copy( 14, 2 ).toInt32() );
230 aDateTime
.Seconds
= static_cast< sal_uInt16
>( aValue
.copy( 17, 2 ).toInt32() );
232 return OptValue
< util::DateTime
>( bValid
, aDateTime
);
235 // defaulted return values ----------------------------------------------------
237 sal_Int32
AttributeList::getToken( sal_Int32 nAttrToken
, sal_Int32 nDefault
) const
239 return mxAttribs
->getOptionalValueToken( nAttrToken
, nDefault
);
242 OUString
AttributeList::getString( sal_Int32 nAttrToken
, const OUString
& rDefault
) const
244 // try to avoid slow exception throw/catch if we can
245 if (rDefault
.isEmpty())
246 return mxAttribs
->getOptionalValue( nAttrToken
);
250 return mxAttribs
->getValue( nAttrToken
);
258 OUString
AttributeList::getXString( sal_Int32 nAttrToken
, const OUString
& rDefault
) const
260 return getXString( nAttrToken
).get( rDefault
);
263 const char* AttributeList::getChar( sal_Int32 nAttrToken
) const
265 const char* p
= nullptr;
266 bool bValid
= getAttribList()->getAsChar(nAttrToken
, p
);
273 double AttributeList::getDouble( sal_Int32 nAttrToken
, double fDefault
) const
275 return getDouble( nAttrToken
).get( fDefault
);
278 sal_Int32
AttributeList::getInteger( sal_Int32 nAttrToken
, sal_Int32 nDefault
) const
280 return getInteger( nAttrToken
).get( nDefault
);
283 sal_uInt32
AttributeList::getUnsigned( sal_Int32 nAttrToken
, sal_uInt32 nDefault
) const
285 return getUnsigned( nAttrToken
).get( nDefault
);
288 sal_Int64
AttributeList::getHyper( sal_Int32 nAttrToken
, sal_Int64 nDefault
) const
290 return getHyper( nAttrToken
).get( nDefault
);
293 sal_Int32
AttributeList::getIntegerHex( sal_Int32 nAttrToken
, sal_Int32 nDefault
) const
295 return getIntegerHex( nAttrToken
).get( nDefault
);
298 sal_uInt32
AttributeList::getUnsignedHex( sal_Int32 nAttrToken
, sal_uInt32 nDefault
) const
300 return getIntegerHex( nAttrToken
).get( nDefault
);
303 bool AttributeList::getBool( sal_Int32 nAttrToken
, bool bDefault
) const
305 return getBool( nAttrToken
).get( bDefault
);
308 util::DateTime
AttributeList::getDateTime( sal_Int32 nAttrToken
, const util::DateTime
& rDefault
) const
310 return getDateTime( nAttrToken
).get( rDefault
);
313 std::vector
<sal_Int32
> AttributeList::getTokenList(sal_Int32 nAttrToken
) const
315 std::vector
<sal_Int32
> aValues
;
316 OUString sValue
= getString(nAttrToken
, "");
317 sal_Int32 nIndex
= 0;
320 aValues
.push_back(AttributeConversion::decodeToken(sValue
.getToken(0, ' ', nIndex
)));
321 } while (nIndex
>= 0);
328 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */