1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: attributelist.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "oox/helper/attributelist.hxx"
32 #include <osl/diagnose.h>
33 #include <rtl/ustrbuf.hxx>
35 using ::rtl::OUString
;
36 using ::rtl::OUStringBuffer
;
37 using ::com::sun::star::uno::Reference
;
38 using ::com::sun::star::uno::Exception
;
39 using ::com::sun::star::util::DateTime
;
40 using ::com::sun::star::xml::sax::XFastAttributeList
;
44 // ============================================================================
48 const sal_Int32 XSTRING_ENCCHAR_LEN
= 7;
50 bool lclAddHexDigit( sal_Unicode
& orcChar
, sal_Unicode cDigit
, int nBitShift
)
52 if( ('0' <= cDigit
) && (cDigit
<= '9') ) { orcChar
|= ((cDigit
- '0') << nBitShift
); return true; }
53 if( ('a' <= cDigit
) && (cDigit
<= 'f') ) { orcChar
|= ((cDigit
- 'a' + 10) << nBitShift
); return true; }
54 if( ('A' <= cDigit
) && (cDigit
<= 'F') ) { orcChar
|= ((cDigit
- 'A' + 10) << nBitShift
); return true; }
58 sal_Unicode
lclGetXChar( const sal_Unicode
*& rpcStr
, const sal_Unicode
* pcEnd
)
60 sal_Unicode cChar
= 0;
61 if( (pcEnd
- rpcStr
>= XSTRING_ENCCHAR_LEN
) &&
62 (rpcStr
[ 0 ] == '_') &&
63 (rpcStr
[ 1 ] == 'x') &&
64 (rpcStr
[ 6 ] == '_') &&
65 lclAddHexDigit( cChar
, rpcStr
[ 2 ], 12 ) &&
66 lclAddHexDigit( cChar
, rpcStr
[ 3 ], 8 ) &&
67 lclAddHexDigit( cChar
, rpcStr
[ 4 ], 4 ) &&
68 lclAddHexDigit( cChar
, rpcStr
[ 5 ], 0 ) )
70 rpcStr
+= XSTRING_ENCCHAR_LEN
;
78 // ============================================================================
80 AttributeList::AttributeList( const Reference
< XFastAttributeList
>& rxAttribs
) :
81 mxAttribs( rxAttribs
)
83 OSL_ENSURE( mxAttribs
.is(), "AttributeList::AttributeList - missing attribute list interface" );
86 bool AttributeList::hasAttribute( sal_Int32 nElement
) const
88 return mxAttribs
->hasAttribute( nElement
);
91 // static string conversion -----------------------------------------------
93 OUString
AttributeList::decodeXString( const OUString
& rValue
)
95 // string shorter than one encoded character - no need to decode
96 if( rValue
.getLength() < XSTRING_ENCCHAR_LEN
)
98 OUStringBuffer aBuffer
;
99 const sal_Unicode
* pcStr
= rValue
.getStr();
100 const sal_Unicode
* pcEnd
= pcStr
+ rValue
.getLength();
101 while( pcStr
< pcEnd
)
102 aBuffer
.append( lclGetXChar( pcStr
, pcEnd
) );
103 return aBuffer
.makeStringAndClear();
106 double AttributeList::decodeDouble( const OUString
& rValue
)
108 return rValue
.toDouble();
111 sal_Int32
AttributeList::decodeInteger( const OUString
& rValue
)
113 return rValue
.toInt32();
116 sal_uInt32
AttributeList::decodeUnsigned( const OUString
& rValue
)
118 return getLimitedValue
< sal_uInt32
, sal_Int64
>( rValue
.toInt64(), 0, SAL_MAX_UINT32
);
121 sal_Int64
AttributeList::decodeHyper( const OUString
& rValue
)
123 return rValue
.toInt64();
126 sal_Int32
AttributeList::decodeIntegerHex( const OUString
& rValue
)
128 return rValue
.toInt32( 16 );
131 sal_uInt32
AttributeList::decodeUnsignedHex( const OUString
& rValue
)
133 return getLimitedValue
< sal_uInt32
, sal_Int64
>( rValue
.toInt64( 16 ), 0, SAL_MAX_UINT32
);
136 sal_Int64
AttributeList::decodeHyperHex( const OUString
& rValue
)
138 return rValue
.toInt64( 16 );
141 // optional return values -----------------------------------------------------
143 OptValue
< sal_Int32
> AttributeList::getToken( sal_Int32 nElement
) const
145 sal_Int32 nToken
= mxAttribs
->getOptionalValueToken( nElement
, XML_TOKEN_INVALID
);
146 return OptValue
< sal_Int32
>( nToken
!= XML_TOKEN_INVALID
, nToken
);
149 OptValue
< OUString
> AttributeList::getString( sal_Int32 nElement
) const
151 return OptValue
< OUString
>( mxAttribs
->hasAttribute( nElement
), mxAttribs
->getOptionalValue( nElement
) );
154 OptValue
< OUString
> AttributeList::getXString( sal_Int32 nElement
) const
156 return OptValue
< OUString
>( mxAttribs
->hasAttribute( nElement
), decodeXString( mxAttribs
->getOptionalValue( nElement
) ) );
159 OptValue
< double > AttributeList::getDouble( sal_Int32 nElement
) const
161 OUString aValue
= mxAttribs
->getOptionalValue( nElement
);
162 bool bValid
= aValue
.getLength() > 0;
163 return OptValue
< double >( bValid
, bValid
? decodeDouble( aValue
) : 0.0 );
166 OptValue
< sal_Int32
> AttributeList::getInteger( sal_Int32 nElement
) const
168 OUString aValue
= mxAttribs
->getOptionalValue( nElement
);
169 bool bValid
= aValue
.getLength() > 0;
170 return OptValue
< sal_Int32
>( bValid
, bValid
? decodeInteger( aValue
) : 0 );
173 OptValue
< sal_uInt32
> AttributeList::getUnsigned( sal_Int32 nElement
) const
175 OUString aValue
= mxAttribs
->getOptionalValue( nElement
);
176 bool bValid
= aValue
.getLength() > 0;
177 return OptValue
< sal_uInt32
>( bValid
, decodeUnsigned( aValue
) );
180 OptValue
< sal_Int64
> AttributeList::getHyper( sal_Int32 nElement
) const
182 OUString aValue
= mxAttribs
->getOptionalValue( nElement
);
183 bool bValid
= aValue
.getLength() > 0;
184 return OptValue
< sal_Int64
>( bValid
, bValid
? decodeHyper( aValue
) : 0 );
187 OptValue
< sal_Int32
> AttributeList::getIntegerHex( sal_Int32 nElement
) const
189 OUString aValue
= mxAttribs
->getOptionalValue( nElement
);
190 bool bValid
= aValue
.getLength() > 0;
191 return OptValue
< sal_Int32
>( bValid
, bValid
? decodeIntegerHex( aValue
) : 0 );
194 OptValue
< sal_uInt32
> AttributeList::getUnsignedHex( sal_Int32 nElement
) const
196 OUString aValue
= mxAttribs
->getOptionalValue( nElement
);
197 bool bValid
= aValue
.getLength() > 0;
198 return OptValue
< sal_uInt32
>( bValid
, bValid
? decodeUnsignedHex( aValue
) : 0 );
201 OptValue
< sal_Int64
> AttributeList::getHyperHex( sal_Int32 nElement
) const
203 OUString aValue
= mxAttribs
->getOptionalValue( nElement
);
204 bool bValid
= aValue
.getLength() > 0;
205 return OptValue
< sal_Int64
>( bValid
, bValid
? decodeHyperHex( aValue
) : 0 );
208 OptValue
< bool > AttributeList::getBool( sal_Int32 nElement
) const
210 // boolean attributes may be "t", "f", "true", "false", "on", "off", "1", or "0"
211 switch( getToken( nElement
, -1 ) )
213 case XML_t
: return OptValue
< bool >( true, true ); // used in VML
214 case XML_true
: return OptValue
< bool >( true, true );
215 case XML_on
: return OptValue
< bool >( true, true );
216 case XML_f
: return OptValue
< bool >( true, false ); // used in VML
217 case XML_false
: return OptValue
< bool >( true, false );
218 case XML_off
: return OptValue
< bool >( true, false );
220 OptValue
< sal_Int32
> onValue
= getInteger( nElement
);
221 return OptValue
< bool >( onValue
.has(), onValue
.get() != 0 );
224 OptValue
< DateTime
> AttributeList::getDateTime( sal_Int32 nElement
) const
226 OUString aValue
= mxAttribs
->getOptionalValue( nElement
);
228 bool bValid
= (aValue
.getLength() == 19) && (aValue
[ 4 ] == '-') && (aValue
[ 7 ] == '-') &&
229 (aValue
[ 10 ] == 'T') && (aValue
[ 13 ] == ':') && (aValue
[ 16 ] == ':');
232 aDateTime
.Year
= static_cast< sal_uInt16
>( aValue
.copy( 0, 4 ).toInt32() );
233 aDateTime
.Month
= static_cast< sal_uInt16
>( aValue
.copy( 5, 2 ).toInt32() );
234 aDateTime
.Day
= static_cast< sal_uInt16
>( aValue
.copy( 8, 2 ).toInt32() );
235 aDateTime
.Hours
= static_cast< sal_uInt16
>( aValue
.copy( 11, 2 ).toInt32() );
236 aDateTime
.Minutes
= static_cast< sal_uInt16
>( aValue
.copy( 14, 2 ).toInt32() );
237 aDateTime
.Seconds
= static_cast< sal_uInt16
>( aValue
.copy( 17, 2 ).toInt32() );
239 return OptValue
< DateTime
>( bValid
, aDateTime
);
242 // defaulted return values ----------------------------------------------------
244 sal_Int32
AttributeList::getToken( sal_Int32 nElement
, sal_Int32 nDefault
) const
246 return mxAttribs
->getOptionalValueToken( nElement
, nDefault
);
249 OUString
AttributeList::getString( sal_Int32 nElement
, const OUString
& rDefault
) const
253 return mxAttribs
->getValue( nElement
);
261 OUString
AttributeList::getXString( sal_Int32 nElement
, const OUString
& rDefault
) const
263 return getXString( nElement
).get( rDefault
);
266 double AttributeList::getDouble( sal_Int32 nElement
, double fDefault
) const
268 return getDouble( nElement
).get( fDefault
);
271 sal_Int32
AttributeList::getInteger( sal_Int32 nElement
, sal_Int32 nDefault
) const
273 return getInteger( nElement
).get( nDefault
);
276 sal_uInt32
AttributeList::getUnsigned( sal_Int32 nElement
, sal_uInt32 nDefault
) const
278 return getUnsigned( nElement
).get( nDefault
);
281 sal_Int64
AttributeList::getHyper( sal_Int32 nElement
, sal_Int64 nDefault
) const
283 return getHyper( nElement
).get( nDefault
);
286 sal_Int32
AttributeList::getIntegerHex( sal_Int32 nElement
, sal_Int32 nDefault
) const
288 return getIntegerHex( nElement
).get( nDefault
);
291 sal_uInt32
AttributeList::getUnsignedHex( sal_Int32 nElement
, sal_uInt32 nDefault
) const
293 return getUnsignedHex( nElement
).get( nDefault
);
296 sal_Int64
AttributeList::getHyperHex( sal_Int32 nElement
, sal_Int64 nDefault
) const
298 return getHyperHex( nElement
).get( nDefault
);
301 bool AttributeList::getBool( sal_Int32 nElement
, bool bDefault
) const
303 return getBool( nElement
).get( bDefault
);
306 DateTime
AttributeList::getDateTime( sal_Int32 nElement
, const DateTime
& rDefault
) const
308 return getDateTime( nElement
).get( rDefault
);
311 // ============================================================================