1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "oox/helper/attributelist.hxx"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <osl/diagnose.h>
31*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
32*cdf0e10cSrcweir #include "oox/token/tokenmap.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir namespace oox {
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir // ============================================================================
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
39*cdf0e10cSrcweir using namespace ::com::sun::star::util;
40*cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir using ::rtl::OUString;
43*cdf0e10cSrcweir using ::rtl::OUStringBuffer;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir // ============================================================================
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir namespace {
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir const sal_Int32 XSTRING_ENCCHAR_LEN     = 7;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir bool lclAddHexDigit( sal_Unicode& orcChar, sal_Unicode cDigit, int nBitShift )
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir     if( ('0' <= cDigit) && (cDigit <= '9') ) { orcChar |= ((cDigit - '0') << nBitShift); return true; }
54*cdf0e10cSrcweir     if( ('a' <= cDigit) && (cDigit <= 'f') ) { orcChar |= ((cDigit - 'a' + 10) << nBitShift); return true; }
55*cdf0e10cSrcweir     if( ('A' <= cDigit) && (cDigit <= 'F') ) { orcChar |= ((cDigit - 'A' + 10) << nBitShift); return true; }
56*cdf0e10cSrcweir     return false;
57*cdf0e10cSrcweir }
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir sal_Unicode lclGetXChar( const sal_Unicode*& rpcStr, const sal_Unicode* pcEnd )
60*cdf0e10cSrcweir {
61*cdf0e10cSrcweir     sal_Unicode cChar = 0;
62*cdf0e10cSrcweir     if( (pcEnd - rpcStr >= XSTRING_ENCCHAR_LEN) &&
63*cdf0e10cSrcweir         (rpcStr[ 0 ] == '_') &&
64*cdf0e10cSrcweir         (rpcStr[ 1 ] == 'x') &&
65*cdf0e10cSrcweir         (rpcStr[ 6 ] == '_') &&
66*cdf0e10cSrcweir         lclAddHexDigit( cChar, rpcStr[ 2 ], 12 ) &&
67*cdf0e10cSrcweir         lclAddHexDigit( cChar, rpcStr[ 3 ], 8 ) &&
68*cdf0e10cSrcweir         lclAddHexDigit( cChar, rpcStr[ 4 ], 4 ) &&
69*cdf0e10cSrcweir         lclAddHexDigit( cChar, rpcStr[ 5 ], 0 ) )
70*cdf0e10cSrcweir     {
71*cdf0e10cSrcweir         rpcStr += XSTRING_ENCCHAR_LEN;
72*cdf0e10cSrcweir         return cChar;
73*cdf0e10cSrcweir     }
74*cdf0e10cSrcweir     return *rpcStr++;
75*cdf0e10cSrcweir }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir } // namespace
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir // ----------------------------------------------------------------------------
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir sal_Int32 AttributeConversion::decodeToken( const OUString& rValue )
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir     return StaticTokenMap::get().getTokenFromUnicode( rValue );
84*cdf0e10cSrcweir }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir OUString AttributeConversion::decodeXString( const OUString& rValue )
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir     // string shorter than one encoded character - no need to decode
89*cdf0e10cSrcweir     if( rValue.getLength() < XSTRING_ENCCHAR_LEN )
90*cdf0e10cSrcweir         return rValue;
91*cdf0e10cSrcweir     OUStringBuffer aBuffer;
92*cdf0e10cSrcweir     const sal_Unicode* pcStr = rValue.getStr();
93*cdf0e10cSrcweir     const sal_Unicode* pcEnd = pcStr + rValue.getLength();
94*cdf0e10cSrcweir     while( pcStr < pcEnd )
95*cdf0e10cSrcweir         aBuffer.append( lclGetXChar( pcStr, pcEnd ) );
96*cdf0e10cSrcweir     return aBuffer.makeStringAndClear();
97*cdf0e10cSrcweir }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir double AttributeConversion::decodeDouble( const OUString& rValue )
100*cdf0e10cSrcweir {
101*cdf0e10cSrcweir     return rValue.toDouble();
102*cdf0e10cSrcweir }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir sal_Int32 AttributeConversion::decodeInteger( const OUString& rValue )
105*cdf0e10cSrcweir {
106*cdf0e10cSrcweir     return rValue.toInt32();
107*cdf0e10cSrcweir }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir sal_uInt32 AttributeConversion::decodeUnsigned( const OUString& rValue )
110*cdf0e10cSrcweir {
111*cdf0e10cSrcweir     return getLimitedValue< sal_uInt32, sal_Int64 >( rValue.toInt64(), 0, SAL_MAX_UINT32 );
112*cdf0e10cSrcweir }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir sal_Int64 AttributeConversion::decodeHyper( const OUString& rValue )
115*cdf0e10cSrcweir {
116*cdf0e10cSrcweir     return rValue.toInt64();
117*cdf0e10cSrcweir }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir sal_Int32 AttributeConversion::decodeIntegerHex( const OUString& rValue )
120*cdf0e10cSrcweir {
121*cdf0e10cSrcweir     return rValue.toInt32( 16 );
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir sal_uInt32 AttributeConversion::decodeUnsignedHex( const OUString& rValue )
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir     return getLimitedValue< sal_uInt32, sal_Int64 >( rValue.toInt64( 16 ), 0, SAL_MAX_UINT32 );
127*cdf0e10cSrcweir }
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir sal_Int64 AttributeConversion::decodeHyperHex( const OUString& rValue )
130*cdf0e10cSrcweir {
131*cdf0e10cSrcweir     return rValue.toInt64( 16 );
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir // ============================================================================
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir AttributeList::AttributeList( const Reference< XFastAttributeList >& rxAttribs ) :
137*cdf0e10cSrcweir     mxAttribs( rxAttribs )
138*cdf0e10cSrcweir {
139*cdf0e10cSrcweir     OSL_ENSURE( mxAttribs.is(), "AttributeList::AttributeList - missing attribute list interface" );
140*cdf0e10cSrcweir }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir bool AttributeList::hasAttribute( sal_Int32 nAttrToken ) const
143*cdf0e10cSrcweir {
144*cdf0e10cSrcweir     return mxAttribs->hasAttribute( nAttrToken );
145*cdf0e10cSrcweir }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir // optional return values -----------------------------------------------------
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir OptValue< sal_Int32 > AttributeList::getToken( sal_Int32 nAttrToken ) const
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir     sal_Int32 nToken = mxAttribs->getOptionalValueToken( nAttrToken, XML_TOKEN_INVALID );
152*cdf0e10cSrcweir     return OptValue< sal_Int32 >( nToken != XML_TOKEN_INVALID, nToken );
153*cdf0e10cSrcweir }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir OptValue< OUString > AttributeList::getString( sal_Int32 nAttrToken ) const
156*cdf0e10cSrcweir {
157*cdf0e10cSrcweir     // check if the attribute exists (empty string may be different to missing attribute)
158*cdf0e10cSrcweir     if( mxAttribs->hasAttribute( nAttrToken ) )
159*cdf0e10cSrcweir         return OptValue< OUString >( mxAttribs->getOptionalValue( nAttrToken ) );
160*cdf0e10cSrcweir     return OptValue< OUString >();
161*cdf0e10cSrcweir }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir OptValue< OUString > AttributeList::getXString( sal_Int32 nAttrToken ) const
164*cdf0e10cSrcweir {
165*cdf0e10cSrcweir     // check if the attribute exists (empty string may be different to missing attribute)
166*cdf0e10cSrcweir     if( mxAttribs->hasAttribute( nAttrToken ) )
167*cdf0e10cSrcweir         return OptValue< OUString >( AttributeConversion::decodeXString( mxAttribs->getOptionalValue( nAttrToken ) ) );
168*cdf0e10cSrcweir     return OptValue< OUString >();
169*cdf0e10cSrcweir }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir OptValue< double > AttributeList::getDouble( sal_Int32 nAttrToken ) const
172*cdf0e10cSrcweir {
173*cdf0e10cSrcweir     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
174*cdf0e10cSrcweir     bool bValid = aValue.getLength() > 0;
175*cdf0e10cSrcweir     return OptValue< double >( bValid, bValid ? AttributeConversion::decodeDouble( aValue ) : 0.0 );
176*cdf0e10cSrcweir }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir OptValue< sal_Int32 > AttributeList::getInteger( sal_Int32 nAttrToken ) const
179*cdf0e10cSrcweir {
180*cdf0e10cSrcweir     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
181*cdf0e10cSrcweir     bool bValid = aValue.getLength() > 0;
182*cdf0e10cSrcweir     return OptValue< sal_Int32 >( bValid, bValid ? AttributeConversion::decodeInteger( aValue ) : 0 );
183*cdf0e10cSrcweir }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir OptValue< sal_uInt32 > AttributeList::getUnsigned( sal_Int32 nAttrToken ) const
186*cdf0e10cSrcweir {
187*cdf0e10cSrcweir     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
188*cdf0e10cSrcweir     bool bValid = aValue.getLength() > 0;
189*cdf0e10cSrcweir     return OptValue< sal_uInt32 >( bValid, AttributeConversion::decodeUnsigned( aValue ) );
190*cdf0e10cSrcweir }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir OptValue< sal_Int64 > AttributeList::getHyper( sal_Int32 nAttrToken ) const
193*cdf0e10cSrcweir {
194*cdf0e10cSrcweir     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
195*cdf0e10cSrcweir     bool bValid = aValue.getLength() > 0;
196*cdf0e10cSrcweir     return OptValue< sal_Int64 >( bValid, bValid ? AttributeConversion::decodeHyper( aValue ) : 0 );
197*cdf0e10cSrcweir }
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir OptValue< sal_Int32 > AttributeList::getIntegerHex( sal_Int32 nAttrToken ) const
200*cdf0e10cSrcweir {
201*cdf0e10cSrcweir     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
202*cdf0e10cSrcweir     bool bValid = aValue.getLength() > 0;
203*cdf0e10cSrcweir     return OptValue< sal_Int32 >( bValid, bValid ? AttributeConversion::decodeIntegerHex( aValue ) : 0 );
204*cdf0e10cSrcweir }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir OptValue< sal_uInt32 > AttributeList::getUnsignedHex( sal_Int32 nAttrToken ) const
207*cdf0e10cSrcweir {
208*cdf0e10cSrcweir     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
209*cdf0e10cSrcweir     bool bValid = aValue.getLength() > 0;
210*cdf0e10cSrcweir     return OptValue< sal_uInt32 >( bValid, bValid ? AttributeConversion::decodeUnsignedHex( aValue ) : 0 );
211*cdf0e10cSrcweir }
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir OptValue< sal_Int64 > AttributeList::getHyperHex( sal_Int32 nAttrToken ) const
214*cdf0e10cSrcweir {
215*cdf0e10cSrcweir     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
216*cdf0e10cSrcweir     bool bValid = aValue.getLength() > 0;
217*cdf0e10cSrcweir     return OptValue< sal_Int64 >( bValid, bValid ? AttributeConversion::decodeHyperHex( aValue ) : 0 );
218*cdf0e10cSrcweir }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir OptValue< bool > AttributeList::getBool( sal_Int32 nAttrToken ) const
221*cdf0e10cSrcweir {
222*cdf0e10cSrcweir     // boolean attributes may be "t", "f", "true", "false", "on", "off", "1", or "0"
223*cdf0e10cSrcweir     switch( getToken( nAttrToken, XML_TOKEN_INVALID ) )
224*cdf0e10cSrcweir     {
225*cdf0e10cSrcweir         case XML_t:     return OptValue< bool >( true );  // used in VML
226*cdf0e10cSrcweir         case XML_true:  return OptValue< bool >( true );
227*cdf0e10cSrcweir         case XML_on:    return OptValue< bool >( true );
228*cdf0e10cSrcweir         case XML_f:     return OptValue< bool >( false ); // used in VML
229*cdf0e10cSrcweir         case XML_false: return OptValue< bool >( false );
230*cdf0e10cSrcweir         case XML_off:   return OptValue< bool >( false );
231*cdf0e10cSrcweir     }
232*cdf0e10cSrcweir     OptValue< sal_Int32 > onValue = getInteger( nAttrToken );
233*cdf0e10cSrcweir     return OptValue< bool >( onValue.has(), onValue.get() != 0 );
234*cdf0e10cSrcweir }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir OptValue< DateTime > AttributeList::getDateTime( sal_Int32 nAttrToken ) const
237*cdf0e10cSrcweir {
238*cdf0e10cSrcweir     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
239*cdf0e10cSrcweir     DateTime aDateTime;
240*cdf0e10cSrcweir     bool bValid = (aValue.getLength() == 19) && (aValue[ 4 ] == '-') && (aValue[ 7 ] == '-') &&
241*cdf0e10cSrcweir         (aValue[ 10 ] == 'T') && (aValue[ 13 ] == ':') && (aValue[ 16 ] == ':');
242*cdf0e10cSrcweir     if( bValid )
243*cdf0e10cSrcweir     {
244*cdf0e10cSrcweir         aDateTime.Year    = static_cast< sal_uInt16 >( aValue.copy( 0, 4 ).toInt32() );
245*cdf0e10cSrcweir         aDateTime.Month   = static_cast< sal_uInt16 >( aValue.copy( 5, 2 ).toInt32() );
246*cdf0e10cSrcweir         aDateTime.Day     = static_cast< sal_uInt16 >( aValue.copy( 8, 2 ).toInt32() );
247*cdf0e10cSrcweir         aDateTime.Hours   = static_cast< sal_uInt16 >( aValue.copy( 11, 2 ).toInt32() );
248*cdf0e10cSrcweir         aDateTime.Minutes = static_cast< sal_uInt16 >( aValue.copy( 14, 2 ).toInt32() );
249*cdf0e10cSrcweir         aDateTime.Seconds = static_cast< sal_uInt16 >( aValue.copy( 17, 2 ).toInt32() );
250*cdf0e10cSrcweir     }
251*cdf0e10cSrcweir     return OptValue< DateTime >( bValid, aDateTime );
252*cdf0e10cSrcweir }
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir // defaulted return values ----------------------------------------------------
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir sal_Int32 AttributeList::getToken( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
257*cdf0e10cSrcweir {
258*cdf0e10cSrcweir     return mxAttribs->getOptionalValueToken( nAttrToken, nDefault );
259*cdf0e10cSrcweir }
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir OUString AttributeList::getString( sal_Int32 nAttrToken, const OUString& rDefault ) const
262*cdf0e10cSrcweir {
263*cdf0e10cSrcweir     try
264*cdf0e10cSrcweir     {
265*cdf0e10cSrcweir         return mxAttribs->getValue( nAttrToken );
266*cdf0e10cSrcweir     }
267*cdf0e10cSrcweir     catch( Exception& )
268*cdf0e10cSrcweir     {
269*cdf0e10cSrcweir     }
270*cdf0e10cSrcweir     return rDefault;
271*cdf0e10cSrcweir }
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir OUString AttributeList::getXString( sal_Int32 nAttrToken, const OUString& rDefault ) const
274*cdf0e10cSrcweir {
275*cdf0e10cSrcweir     return getXString( nAttrToken ).get( rDefault );
276*cdf0e10cSrcweir }
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir double AttributeList::getDouble( sal_Int32 nAttrToken, double fDefault ) const
279*cdf0e10cSrcweir {
280*cdf0e10cSrcweir     return getDouble( nAttrToken ).get( fDefault );
281*cdf0e10cSrcweir }
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir sal_Int32 AttributeList::getInteger( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
284*cdf0e10cSrcweir {
285*cdf0e10cSrcweir     return getInteger( nAttrToken ).get( nDefault );
286*cdf0e10cSrcweir }
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir sal_uInt32 AttributeList::getUnsigned( sal_Int32 nAttrToken, sal_uInt32 nDefault ) const
289*cdf0e10cSrcweir {
290*cdf0e10cSrcweir     return getUnsigned( nAttrToken ).get( nDefault );
291*cdf0e10cSrcweir }
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir sal_Int64 AttributeList::getHyper( sal_Int32 nAttrToken, sal_Int64 nDefault ) const
294*cdf0e10cSrcweir {
295*cdf0e10cSrcweir     return getHyper( nAttrToken ).get( nDefault );
296*cdf0e10cSrcweir }
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir sal_Int32 AttributeList::getIntegerHex( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
299*cdf0e10cSrcweir {
300*cdf0e10cSrcweir     return getIntegerHex( nAttrToken ).get( nDefault );
301*cdf0e10cSrcweir }
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir sal_uInt32 AttributeList::getUnsignedHex( sal_Int32 nAttrToken, sal_uInt32 nDefault ) const
304*cdf0e10cSrcweir {
305*cdf0e10cSrcweir     return getUnsignedHex( nAttrToken ).get( nDefault );
306*cdf0e10cSrcweir }
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir sal_Int64 AttributeList::getHyperHex( sal_Int32 nAttrToken, sal_Int64 nDefault ) const
309*cdf0e10cSrcweir {
310*cdf0e10cSrcweir     return getHyperHex( nAttrToken ).get( nDefault );
311*cdf0e10cSrcweir }
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir bool AttributeList::getBool( sal_Int32 nAttrToken, bool bDefault ) const
314*cdf0e10cSrcweir {
315*cdf0e10cSrcweir     return getBool( nAttrToken ).get( bDefault );
316*cdf0e10cSrcweir }
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir DateTime AttributeList::getDateTime( sal_Int32 nAttrToken, const DateTime& rDefault ) const
319*cdf0e10cSrcweir {
320*cdf0e10cSrcweir     return getDateTime( nAttrToken ).get( rDefault );
321*cdf0e10cSrcweir }
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir // ============================================================================
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir } // namespace oox
326