Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / oox / source / helper / attributelist.cxx
blob7a973975f3d29dfaee42c339840898409a145e53
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 <comphelper/string.hxx>
23 #include <osl/diagnose.h>
24 #include <rtl/ustrbuf.hxx>
25 #include <sax/fastattribs.hxx>
26 #include <oox/token/tokenmap.hxx>
27 #include <o3tl/string_view.hxx>
29 namespace oox {
31 using namespace ::com::sun::star;
32 using namespace ::com::sun::star::uno;
33 using namespace ::com::sun::star::xml::sax;
35 namespace {
37 const sal_Int32 XSTRING_ENCCHAR_LEN = 7;
39 bool lclAddHexDigit( sal_Unicode& orcChar, sal_Unicode cDigit, int nBitShift )
41 if( ('0' <= cDigit) && (cDigit <= '9') ) { orcChar |= ((cDigit - '0') << nBitShift); return true; }
42 if( ('a' <= cDigit) && (cDigit <= 'f') ) { orcChar |= ((cDigit - 'a' + 10) << nBitShift); return true; }
43 if( ('A' <= cDigit) && (cDigit <= 'F') ) { orcChar |= ((cDigit - 'A' + 10) << nBitShift); return true; }
44 return false;
47 sal_Unicode lclGetXChar( const sal_Unicode*& rpcStr, const sal_Unicode* pcEnd )
49 sal_Unicode cChar = 0;
50 if( (pcEnd - rpcStr >= XSTRING_ENCCHAR_LEN) &&
51 (rpcStr[ 0 ] == '_') &&
52 (rpcStr[ 1 ] == 'x') &&
53 (rpcStr[ 6 ] == '_') &&
54 lclAddHexDigit( cChar, rpcStr[ 2 ], 12 ) &&
55 lclAddHexDigit( cChar, rpcStr[ 3 ], 8 ) &&
56 lclAddHexDigit( cChar, rpcStr[ 4 ], 4 ) &&
57 lclAddHexDigit( cChar, rpcStr[ 5 ], 0 ) )
59 rpcStr += XSTRING_ENCCHAR_LEN;
60 return cChar;
62 return *rpcStr++;
65 } // namespace
67 #define STRING_TO_TOKEN(color) if (sColorName == u"" #color) return XML_##color
68 sal_Int32 getHighlightColorTokenFromString(std::u16string_view sColorName)
70 STRING_TO_TOKEN(black);
71 STRING_TO_TOKEN(blue);
72 STRING_TO_TOKEN(cyan);
73 STRING_TO_TOKEN(darkBlue);
74 STRING_TO_TOKEN(darkCyan);
75 STRING_TO_TOKEN(darkGreen);
76 STRING_TO_TOKEN(darkMagenta);
77 STRING_TO_TOKEN(darkRed);
78 STRING_TO_TOKEN(darkYellow);
79 STRING_TO_TOKEN(darkGray);
80 STRING_TO_TOKEN(green);
81 STRING_TO_TOKEN(lightGray);
82 STRING_TO_TOKEN(magenta);
83 STRING_TO_TOKEN(red);
84 STRING_TO_TOKEN(white);
85 STRING_TO_TOKEN(yellow);
86 STRING_TO_TOKEN(none);
88 return XML_TOKEN_INVALID;
91 sal_Int32 AttributeConversion::decodeToken( std::u16string_view rValue )
93 return TokenMap::getTokenFromUnicode( rValue );
96 OUString AttributeConversion::decodeXString( const OUString& rValue )
98 // string shorter than one encoded character - no need to decode
99 if( rValue.getLength() < XSTRING_ENCCHAR_LEN )
100 return rValue;
101 if (rValue.indexOf(u"_x") == -1)
102 return rValue;
104 OUStringBuffer aBuffer;
105 const sal_Unicode* pcStr = rValue.getStr();
106 const sal_Unicode* pcEnd = pcStr + rValue.getLength();
107 while( pcStr < pcEnd )
108 aBuffer.append( lclGetXChar( pcStr, pcEnd ) );
109 return comphelper::string::sanitizeStringSurrogates(aBuffer.makeStringAndClear());
112 sal_Int32 AttributeConversion::decodeInteger( std::u16string_view rValue )
114 return o3tl::toInt32(rValue);
117 sal_uInt32 AttributeConversion::decodeUnsigned( std::u16string_view rValue )
119 return getLimitedValue< sal_uInt32, sal_Int64 >( o3tl::toInt64(rValue), 0, SAL_MAX_UINT32 );
122 sal_Int64 AttributeConversion::decodeHyper( std::u16string_view rValue )
124 return o3tl::toInt64(rValue);
127 sal_Int32 AttributeConversion::decodeIntegerHex( std::u16string_view rValue )
129 // It looks like all Office Open XML attributes containing hexadecimal
130 // values are based on xsd:hexBinary and so use an unsigned representation:
131 return static_cast< sal_Int32 >(o3tl::toUInt32(rValue, 16));
132 //TODO: Change this function to return sal_uInt32 and get rid of the
133 // cast, but that will have a ripple effect
136 AttributeList::AttributeList( const Reference< XFastAttributeList >& rxAttribs ) :
137 mxAttribs( rxAttribs ),
138 mpAttribList( nullptr )
140 OSL_ENSURE( mxAttribs.is(), "AttributeList::AttributeList - missing attribute list interface" );
143 sax_fastparser::FastAttributeList *AttributeList::getAttribList() const
145 if( mpAttribList == nullptr )
147 mpAttribList = &sax_fastparser::castToFastAttributeList( mxAttribs );
149 return mpAttribList;
152 bool AttributeList::hasAttribute( sal_Int32 nAttrToken ) const
154 return mxAttribs->hasAttribute( nAttrToken );
157 oox::drawingml::Color AttributeList::getHighlightColor(sal_Int32 nAttrToken) const
159 OUString sColorVal = mxAttribs->getValue(nAttrToken);
160 oox::drawingml::Color aColor;
161 aColor.setHighlight(getHighlightColorTokenFromString(sColorVal));
162 return aColor;
165 // optional return values -----------------------------------------------------
167 std::optional< sal_Int32 > AttributeList::getToken( sal_Int32 nAttrToken ) const
169 sal_Int32 nToken = mxAttribs->getOptionalValueToken( nAttrToken, XML_TOKEN_INVALID );
170 return nToken == XML_TOKEN_INVALID ? std::optional< sal_Int32 >() : std::optional< sal_Int32 >( nToken );
173 std::optional< OUString > AttributeList::getString( sal_Int32 nAttrToken ) const
175 // check if the attribute exists (empty string may be different to missing attribute)
176 if( mxAttribs->hasAttribute( nAttrToken ) )
177 return std::optional< OUString >( mxAttribs->getOptionalValue( nAttrToken ) );
178 return std::optional< OUString >();
181 OUString AttributeList::getStringDefaulted( sal_Int32 nAttrToken ) const
183 // check if the attribute exists (empty string may be different to missing attribute)
184 if( mxAttribs->hasAttribute( nAttrToken ) )
185 return mxAttribs->getOptionalValue( nAttrToken );
186 return OUString();
189 std::optional< OUString > AttributeList::getXString( sal_Int32 nAttrToken ) const
191 // check if the attribute exists (empty string may be different to missing attribute)
192 if( mxAttribs->hasAttribute( nAttrToken ) )
193 return std::optional< OUString >( AttributeConversion::decodeXString( mxAttribs->getOptionalValue( nAttrToken ) ) );
194 return std::optional< OUString >();
197 std::optional< double > AttributeList::getDouble( sal_Int32 nAttrToken ) const
199 double nValue;
200 bool bValid = getAttribList()->getAsDouble( nAttrToken, nValue );
201 return bValid ? std::optional< double >( nValue ) : std::optional< double >();
204 std::optional< sal_Int32 > AttributeList::getInteger( sal_Int32 nAttrToken ) const
206 sal_Int32 nValue;
207 bool bValid = getAttribList()->getAsInteger( nAttrToken, nValue );
208 return bValid ? std::optional< sal_Int32 >( nValue ) : std::optional< sal_Int32 >();
211 std::optional< sal_uInt32 > AttributeList::getUnsigned( sal_Int32 nAttrToken ) const
213 OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
214 bool bValid = !aValue.isEmpty();
215 return bValid ? std::optional< sal_uInt32 >( AttributeConversion::decodeUnsigned( aValue ) ) : std::optional< sal_uInt32 >();
218 std::optional< sal_Int64 > AttributeList::getHyper( sal_Int32 nAttrToken ) const
220 std::string_view aValue = getView( nAttrToken );
221 bool bValid = !aValue.empty();
222 return bValid ? std::optional< sal_Int64 >( o3tl::toInt64( aValue ) ) : std::optional< sal_Int64 >();
225 std::optional< sal_Int32 > AttributeList::getIntegerHex( sal_Int32 nAttrToken ) const
227 OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
228 bool bValid = !aValue.isEmpty();
229 return bValid ? std::optional< sal_Int32 >( AttributeConversion::decodeIntegerHex( aValue ) ) : std::optional< sal_Int32 >();
232 std::optional< bool > AttributeList::getBool( sal_Int32 nAttrToken ) const
234 std::string_view pAttr;
236 // catch the common cases as quickly as possible first
237 bool bHasAttr = getAttribList()->getAsView( nAttrToken, pAttr );
238 if( !bHasAttr )
239 return std::optional< bool >();
240 if( pAttr == "false" )
241 return std::optional< bool >( false );
242 if( pAttr == "true" )
243 return std::optional< bool >( true );
245 // now for all the crazy stuff
247 // boolean attributes may be "t", "f", "true", "false", "on", "off", "1", or "0"
248 switch( getToken( nAttrToken, XML_TOKEN_INVALID ) )
250 case XML_t: return std::optional< bool >( true ); // used in VML
251 case XML_true: return std::optional< bool >( true );
252 case XML_on: return std::optional< bool >( true );
253 case XML_f: return std::optional< bool >( false ); // used in VML
254 case XML_false: return std::optional< bool >( false );
255 case XML_off: return std::optional< bool >( false );
257 std::optional< sal_Int32 > onValue = getInteger( nAttrToken );
258 return onValue.has_value() ? std::optional< bool >( onValue.value() != 0 ) : std::optional< bool >();
261 std::optional< util::DateTime > AttributeList::getDateTime( sal_Int32 nAttrToken ) const
263 std::string_view aValue = getView( nAttrToken );
264 util::DateTime aDateTime;
265 bool bValid = (aValue.size() == 19 || (aValue.size() == 20 && aValue[19] == 'Z')) &&
266 (aValue[ 4 ] == '-') && (aValue[ 7 ] == '-') && (aValue[ 10 ] == 'T') &&
267 (aValue[ 13 ] == ':') && (aValue[ 16 ] == ':');
268 if (!bValid)
270 SAL_WARN("oox", "bad date string: " << aValue);
271 return std::optional< util::DateTime >();
273 aDateTime.Year = static_cast< sal_uInt16 >( o3tl::toInt32(aValue.substr( 0, 4 )) );
274 aDateTime.Month = static_cast< sal_uInt16 >( o3tl::toInt32(aValue.substr( 5, 2 )) );
275 aDateTime.Day = static_cast< sal_uInt16 >( o3tl::toInt32(aValue.substr( 8, 2 )) );
276 aDateTime.Hours = static_cast< sal_uInt16 >( o3tl::toInt32(aValue.substr( 11, 2 )) );
277 aDateTime.Minutes = static_cast< sal_uInt16 >( o3tl::toInt32(aValue.substr( 14, 2 )) );
278 aDateTime.Seconds = static_cast< sal_uInt16 >( o3tl::toInt32(aValue.substr( 17, 2 )) );
279 return std::optional< util::DateTime >( aDateTime );
282 // defaulted return values ----------------------------------------------------
284 sal_Int32 AttributeList::getToken( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
286 return mxAttribs->getOptionalValueToken( nAttrToken, nDefault );
289 OUString AttributeList::getString( sal_Int32 nAttrToken, const OUString& rDefault ) const
291 // try to avoid slow exception throw/catch if we can
292 if (rDefault.isEmpty())
293 return mxAttribs->getOptionalValue( nAttrToken );
297 return mxAttribs->getValue( nAttrToken );
299 catch( Exception& )
302 return rDefault;
305 OUString AttributeList::getXString( sal_Int32 nAttrToken, const OUString& rDefault ) const
307 return getXString( nAttrToken ).value_or( rDefault );
310 std::string_view AttributeList::getView( sal_Int32 nAttrToken ) const
312 std::string_view p;
313 getAttribList()->getAsView(nAttrToken, p);
314 return p;
317 double AttributeList::getDouble( sal_Int32 nAttrToken, double fDefault ) const
319 return getDouble( nAttrToken ).value_or( fDefault );
322 sal_Int32 AttributeList::getInteger( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
324 return getInteger( nAttrToken ).value_or( nDefault );
327 sal_uInt32 AttributeList::getUnsigned( sal_Int32 nAttrToken, sal_uInt32 nDefault ) const
329 return getUnsigned( nAttrToken ).value_or( nDefault );
332 sal_Int64 AttributeList::getHyper( sal_Int32 nAttrToken, sal_Int64 nDefault ) const
334 return getHyper( nAttrToken ).value_or( nDefault );
337 sal_Int32 AttributeList::getIntegerHex( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
339 return getIntegerHex( nAttrToken ).value_or( nDefault );
342 sal_uInt32 AttributeList::getUnsignedHex( sal_Int32 nAttrToken, sal_uInt32 nDefault ) const
344 return getIntegerHex( nAttrToken ).value_or( nDefault );
347 bool AttributeList::getBool( sal_Int32 nAttrToken, bool bDefault ) const
349 return getBool( nAttrToken ).value_or( bDefault );
352 util::DateTime AttributeList::getDateTime( sal_Int32 nAttrToken, const util::DateTime& rDefault ) const
354 return getDateTime( nAttrToken ).value_or( rDefault );
357 std::vector<sal_Int32> AttributeList::getTokenList(sal_Int32 nAttrToken) const
359 std::vector<sal_Int32> aValues;
360 OUString sValue = getString(nAttrToken, "");
361 sal_Int32 nIndex = 0;
364 aValues.push_back(AttributeConversion::decodeToken(o3tl::getToken(sValue, 0, ' ', nIndex)));
365 } while (nIndex >= 0);
367 return aValues;
370 } // namespace oox
372 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */