1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "oox/helper/attributelist.hxx"
25 
26 #include <osl/diagnose.h>
27 #include <rtl/ustrbuf.hxx>
28 #include "oox/token/tokenmap.hxx"
29 
30 namespace oox {
31 
32 // ============================================================================
33 
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::util;
36 using namespace ::com::sun::star::xml::sax;
37 
38 using ::rtl::OUString;
39 using ::rtl::OUStringBuffer;
40 
41 // ============================================================================
42 
43 namespace {
44 
45 const sal_Int32 XSTRING_ENCCHAR_LEN     = 7;
46 
lclAddHexDigit(sal_Unicode & orcChar,sal_Unicode cDigit,int nBitShift)47 bool lclAddHexDigit( sal_Unicode& orcChar, sal_Unicode cDigit, int nBitShift )
48 {
49     if( ('0' <= cDigit) && (cDigit <= '9') ) { orcChar |= ((cDigit - '0') << nBitShift); return true; }
50     if( ('a' <= cDigit) && (cDigit <= 'f') ) { orcChar |= ((cDigit - 'a' + 10) << nBitShift); return true; }
51     if( ('A' <= cDigit) && (cDigit <= 'F') ) { orcChar |= ((cDigit - 'A' + 10) << nBitShift); return true; }
52     return false;
53 }
54 
lclGetXChar(const sal_Unicode * & rpcStr,const sal_Unicode * pcEnd)55 sal_Unicode lclGetXChar( const sal_Unicode*& rpcStr, const sal_Unicode* pcEnd )
56 {
57     sal_Unicode cChar = 0;
58     if( (pcEnd - rpcStr >= XSTRING_ENCCHAR_LEN) &&
59         (rpcStr[ 0 ] == '_') &&
60         (rpcStr[ 1 ] == 'x') &&
61         (rpcStr[ 6 ] == '_') &&
62         lclAddHexDigit( cChar, rpcStr[ 2 ], 12 ) &&
63         lclAddHexDigit( cChar, rpcStr[ 3 ], 8 ) &&
64         lclAddHexDigit( cChar, rpcStr[ 4 ], 4 ) &&
65         lclAddHexDigit( cChar, rpcStr[ 5 ], 0 ) )
66     {
67         rpcStr += XSTRING_ENCCHAR_LEN;
68         return cChar;
69     }
70     return *rpcStr++;
71 }
72 
73 } // namespace
74 
75 // ----------------------------------------------------------------------------
76 
decodeToken(const OUString & rValue)77 sal_Int32 AttributeConversion::decodeToken( const OUString& rValue )
78 {
79     return StaticTokenMap::get().getTokenFromUnicode( rValue );
80 }
81 
decodeXString(const OUString & rValue)82 OUString AttributeConversion::decodeXString( const OUString& rValue )
83 {
84     // string shorter than one encoded character - no need to decode
85     if( rValue.getLength() < XSTRING_ENCCHAR_LEN )
86         return rValue;
87     OUStringBuffer aBuffer;
88     const sal_Unicode* pcStr = rValue.getStr();
89     const sal_Unicode* pcEnd = pcStr + rValue.getLength();
90     while( pcStr < pcEnd )
91         aBuffer.append( lclGetXChar( pcStr, pcEnd ) );
92     return aBuffer.makeStringAndClear();
93 }
94 
decodeDouble(const OUString & rValue)95 double AttributeConversion::decodeDouble( const OUString& rValue )
96 {
97     return rValue.toDouble();
98 }
99 
decodeInteger(const OUString & rValue)100 sal_Int32 AttributeConversion::decodeInteger( const OUString& rValue )
101 {
102     return rValue.toInt32();
103 }
104 
decodeUnsigned(const OUString & rValue)105 sal_uInt32 AttributeConversion::decodeUnsigned( const OUString& rValue )
106 {
107     return getLimitedValue< sal_uInt32, sal_Int64 >( rValue.toInt64(), 0, SAL_MAX_UINT32 );
108 }
109 
decodeHyper(const OUString & rValue)110 sal_Int64 AttributeConversion::decodeHyper( const OUString& rValue )
111 {
112     return rValue.toInt64();
113 }
114 
decodeIntegerHex(const OUString & rValue)115 sal_Int32 AttributeConversion::decodeIntegerHex( const OUString& rValue )
116 {
117     return rValue.toInt32( 16 );
118 }
119 
decodeUnsignedHex(const OUString & rValue)120 sal_uInt32 AttributeConversion::decodeUnsignedHex( const OUString& rValue )
121 {
122     return getLimitedValue< sal_uInt32, sal_Int64 >( rValue.toInt64( 16 ), 0, SAL_MAX_UINT32 );
123 }
124 
decodeHyperHex(const OUString & rValue)125 sal_Int64 AttributeConversion::decodeHyperHex( const OUString& rValue )
126 {
127     return rValue.toInt64( 16 );
128 }
129 
130 // ============================================================================
131 
AttributeList(const Reference<XFastAttributeList> & rxAttribs)132 AttributeList::AttributeList( const Reference< XFastAttributeList >& rxAttribs ) :
133     mxAttribs( rxAttribs )
134 {
135     OSL_ENSURE( mxAttribs.is(), "AttributeList::AttributeList - missing attribute list interface" );
136 }
137 
hasAttribute(sal_Int32 nAttrToken) const138 bool AttributeList::hasAttribute( sal_Int32 nAttrToken ) const
139 {
140     return mxAttribs->hasAttribute( nAttrToken );
141 }
142 
143 // optional return values -----------------------------------------------------
144 
getToken(sal_Int32 nAttrToken) const145 OptValue< sal_Int32 > AttributeList::getToken( sal_Int32 nAttrToken ) const
146 {
147     sal_Int32 nToken = mxAttribs->getOptionalValueToken( nAttrToken, XML_TOKEN_INVALID );
148     return OptValue< sal_Int32 >( nToken != XML_TOKEN_INVALID, nToken );
149 }
150 
getString(sal_Int32 nAttrToken) const151 OptValue< OUString > AttributeList::getString( sal_Int32 nAttrToken ) const
152 {
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 >();
157 }
158 
getXString(sal_Int32 nAttrToken) const159 OptValue< OUString > AttributeList::getXString( sal_Int32 nAttrToken ) const
160 {
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 >();
165 }
166 
getDouble(sal_Int32 nAttrToken) const167 OptValue< double > AttributeList::getDouble( sal_Int32 nAttrToken ) const
168 {
169     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
170     bool bValid = aValue.getLength() > 0;
171     return OptValue< double >( bValid, bValid ? AttributeConversion::decodeDouble( aValue ) : 0.0 );
172 }
173 
getInteger(sal_Int32 nAttrToken) const174 OptValue< sal_Int32 > AttributeList::getInteger( sal_Int32 nAttrToken ) const
175 {
176     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
177     bool bValid = aValue.getLength() > 0;
178     return OptValue< sal_Int32 >( bValid, bValid ? AttributeConversion::decodeInteger( aValue ) : 0 );
179 }
180 
getUnsigned(sal_Int32 nAttrToken) const181 OptValue< sal_uInt32 > AttributeList::getUnsigned( sal_Int32 nAttrToken ) const
182 {
183     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
184     bool bValid = aValue.getLength() > 0;
185     return OptValue< sal_uInt32 >( bValid, AttributeConversion::decodeUnsigned( aValue ) );
186 }
187 
getHyper(sal_Int32 nAttrToken) const188 OptValue< sal_Int64 > AttributeList::getHyper( sal_Int32 nAttrToken ) const
189 {
190     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
191     bool bValid = aValue.getLength() > 0;
192     return OptValue< sal_Int64 >( bValid, bValid ? AttributeConversion::decodeHyper( aValue ) : 0 );
193 }
194 
getIntegerHex(sal_Int32 nAttrToken) const195 OptValue< sal_Int32 > AttributeList::getIntegerHex( sal_Int32 nAttrToken ) const
196 {
197     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
198     bool bValid = aValue.getLength() > 0;
199     return OptValue< sal_Int32 >( bValid, bValid ? AttributeConversion::decodeIntegerHex( aValue ) : 0 );
200 }
201 
getUnsignedHex(sal_Int32 nAttrToken) const202 OptValue< sal_uInt32 > AttributeList::getUnsignedHex( sal_Int32 nAttrToken ) const
203 {
204     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
205     bool bValid = aValue.getLength() > 0;
206     return OptValue< sal_uInt32 >( bValid, bValid ? AttributeConversion::decodeUnsignedHex( aValue ) : 0 );
207 }
208 
getHyperHex(sal_Int32 nAttrToken) const209 OptValue< sal_Int64 > AttributeList::getHyperHex( sal_Int32 nAttrToken ) const
210 {
211     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
212     bool bValid = aValue.getLength() > 0;
213     return OptValue< sal_Int64 >( bValid, bValid ? AttributeConversion::decodeHyperHex( aValue ) : 0 );
214 }
215 
getBool(sal_Int32 nAttrToken) const216 OptValue< bool > AttributeList::getBool( sal_Int32 nAttrToken ) const
217 {
218     // boolean attributes may be "t", "f", "true", "false", "on", "off", "1", or "0"
219     switch( getToken( nAttrToken, XML_TOKEN_INVALID ) )
220     {
221         case XML_t:     return OptValue< bool >( true );  // used in VML
222         case XML_true:  return OptValue< bool >( true );
223         case XML_on:    return OptValue< bool >( true );
224         case XML_f:     return OptValue< bool >( false ); // used in VML
225         case XML_false: return OptValue< bool >( false );
226         case XML_off:   return OptValue< bool >( false );
227     }
228     OptValue< sal_Int32 > onValue = getInteger( nAttrToken );
229     return OptValue< bool >( onValue.has(), onValue.get() != 0 );
230 }
231 
getDateTime(sal_Int32 nAttrToken) const232 OptValue< DateTime > AttributeList::getDateTime( sal_Int32 nAttrToken ) const
233 {
234     OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
235     DateTime aDateTime;
236     bool bValid = (aValue.getLength() == 19) && (aValue[ 4 ] == '-') && (aValue[ 7 ] == '-') &&
237         (aValue[ 10 ] == 'T') && (aValue[ 13 ] == ':') && (aValue[ 16 ] == ':');
238     if( bValid )
239     {
240         aDateTime.Year    = static_cast< sal_uInt16 >( aValue.copy( 0, 4 ).toInt32() );
241         aDateTime.Month   = static_cast< sal_uInt16 >( aValue.copy( 5, 2 ).toInt32() );
242         aDateTime.Day     = static_cast< sal_uInt16 >( aValue.copy( 8, 2 ).toInt32() );
243         aDateTime.Hours   = static_cast< sal_uInt16 >( aValue.copy( 11, 2 ).toInt32() );
244         aDateTime.Minutes = static_cast< sal_uInt16 >( aValue.copy( 14, 2 ).toInt32() );
245         aDateTime.Seconds = static_cast< sal_uInt16 >( aValue.copy( 17, 2 ).toInt32() );
246     }
247     return OptValue< DateTime >( bValid, aDateTime );
248 }
249 
250 // defaulted return values ----------------------------------------------------
251 
getToken(sal_Int32 nAttrToken,sal_Int32 nDefault) const252 sal_Int32 AttributeList::getToken( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
253 {
254     return mxAttribs->getOptionalValueToken( nAttrToken, nDefault );
255 }
256 
getString(sal_Int32 nAttrToken,const OUString & rDefault) const257 OUString AttributeList::getString( sal_Int32 nAttrToken, const OUString& rDefault ) const
258 {
259     try
260     {
261         return mxAttribs->getValue( nAttrToken );
262     }
263     catch( Exception& )
264     {
265     }
266     return rDefault;
267 }
268 
getXString(sal_Int32 nAttrToken,const OUString & rDefault) const269 OUString AttributeList::getXString( sal_Int32 nAttrToken, const OUString& rDefault ) const
270 {
271     return getXString( nAttrToken ).get( rDefault );
272 }
273 
getDouble(sal_Int32 nAttrToken,double fDefault) const274 double AttributeList::getDouble( sal_Int32 nAttrToken, double fDefault ) const
275 {
276     return getDouble( nAttrToken ).get( fDefault );
277 }
278 
getInteger(sal_Int32 nAttrToken,sal_Int32 nDefault) const279 sal_Int32 AttributeList::getInteger( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
280 {
281     return getInteger( nAttrToken ).get( nDefault );
282 }
283 
getUnsigned(sal_Int32 nAttrToken,sal_uInt32 nDefault) const284 sal_uInt32 AttributeList::getUnsigned( sal_Int32 nAttrToken, sal_uInt32 nDefault ) const
285 {
286     return getUnsigned( nAttrToken ).get( nDefault );
287 }
288 
getHyper(sal_Int32 nAttrToken,sal_Int64 nDefault) const289 sal_Int64 AttributeList::getHyper( sal_Int32 nAttrToken, sal_Int64 nDefault ) const
290 {
291     return getHyper( nAttrToken ).get( nDefault );
292 }
293 
getIntegerHex(sal_Int32 nAttrToken,sal_Int32 nDefault) const294 sal_Int32 AttributeList::getIntegerHex( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
295 {
296     return getIntegerHex( nAttrToken ).get( nDefault );
297 }
298 
getUnsignedHex(sal_Int32 nAttrToken,sal_uInt32 nDefault) const299 sal_uInt32 AttributeList::getUnsignedHex( sal_Int32 nAttrToken, sal_uInt32 nDefault ) const
300 {
301     return getUnsignedHex( nAttrToken ).get( nDefault );
302 }
303 
getHyperHex(sal_Int32 nAttrToken,sal_Int64 nDefault) const304 sal_Int64 AttributeList::getHyperHex( sal_Int32 nAttrToken, sal_Int64 nDefault ) const
305 {
306     return getHyperHex( nAttrToken ).get( nDefault );
307 }
308 
getBool(sal_Int32 nAttrToken,bool bDefault) const309 bool AttributeList::getBool( sal_Int32 nAttrToken, bool bDefault ) const
310 {
311     return getBool( nAttrToken ).get( bDefault );
312 }
313 
getDateTime(sal_Int32 nAttrToken,const DateTime & rDefault) const314 DateTime AttributeList::getDateTime( sal_Int32 nAttrToken, const DateTime& rDefault ) const
315 {
316     return getDateTime( nAttrToken ).get( rDefault );
317 }
318 
319 // ============================================================================
320 
321 } // namespace oox
322