LibreOffice Module oox (master) 1
attributelist.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21
22#include <comphelper/string.hxx>
23#include <osl/diagnose.h>
24#include <rtl/ustrbuf.hxx>
25#include <sax/fastattribs.hxx>
27#include <o3tl/string_view.hxx>
28
29namespace oox {
30
31using namespace ::com::sun::star;
32using namespace ::com::sun::star::uno;
33using namespace ::com::sun::star::xml::sax;
34
35namespace {
36
37const sal_Int32 XSTRING_ENCCHAR_LEN = 7;
38
39bool lclAddHexDigit( sal_Unicode& orcChar, sal_Unicode cDigit, int nBitShift )
40{
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;
45}
46
47sal_Unicode lclGetXChar( const sal_Unicode*& rpcStr, const sal_Unicode* pcEnd )
48{
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 ) )
58 {
59 rpcStr += XSTRING_ENCCHAR_LEN;
60 return cChar;
61 }
62 return *rpcStr++;
63}
64
65} // namespace
66
67#define STRING_TO_TOKEN(color) if (sColorName == u"" #color) return XML_##color
68sal_Int32 getHighlightColorTokenFromString(std::u16string_view sColorName)
69{
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);
87
88 return XML_TOKEN_INVALID;
89}
90
91sal_Int32 AttributeConversion::decodeToken( std::u16string_view rValue )
92{
93 return TokenMap::getTokenFromUnicode( rValue );
94}
95
96OUString AttributeConversion::decodeXString( const OUString& rValue )
97{
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;
103
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());
110}
111
112sal_Int32 AttributeConversion::decodeInteger( std::u16string_view rValue )
113{
114 return o3tl::toInt32(rValue);
115}
116
117sal_uInt32 AttributeConversion::decodeUnsigned( std::u16string_view rValue )
118{
119 return getLimitedValue< sal_uInt32, sal_Int64 >( o3tl::toInt64(rValue), 0, SAL_MAX_UINT32 );
120}
121
122sal_Int64 AttributeConversion::decodeHyper( std::u16string_view rValue )
123{
124 return o3tl::toInt64(rValue);
125}
126
127sal_Int32 AttributeConversion::decodeIntegerHex( std::u16string_view rValue )
128{
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
134}
135
136AttributeList::AttributeList( const Reference< XFastAttributeList >& rxAttribs ) :
137 mxAttribs( rxAttribs ),
138 mpAttribList( nullptr )
139{
140 OSL_ENSURE( mxAttribs.is(), "AttributeList::AttributeList - missing attribute list interface" );
141}
142
144{
145 if( mpAttribList == nullptr )
146 {
148 }
149 return mpAttribList;
150}
151
152bool AttributeList::hasAttribute( sal_Int32 nAttrToken ) const
153{
154 return mxAttribs->hasAttribute( nAttrToken );
155}
156
158{
159 OUString sColorVal = mxAttribs->getValue(nAttrToken);
162 return aColor;
163}
164
165// optional return values -----------------------------------------------------
166
167std::optional< sal_Int32 > AttributeList::getToken( sal_Int32 nAttrToken ) const
168{
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 );
171}
172
173std::optional< OUString > AttributeList::getString( sal_Int32 nAttrToken ) const
174{
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 >();
179}
180
181OUString AttributeList::getStringDefaulted( sal_Int32 nAttrToken ) const
182{
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();
187}
188
189std::optional< OUString > AttributeList::getXString( sal_Int32 nAttrToken ) const
190{
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 >();
195}
196
197std::optional< double > AttributeList::getDouble( sal_Int32 nAttrToken ) const
198{
199 double nValue;
200 bool bValid = getAttribList()->getAsDouble( nAttrToken, nValue );
201 return bValid ? std::optional< double >( nValue ) : std::optional< double >();
202}
203
204std::optional< sal_Int32 > AttributeList::getInteger( sal_Int32 nAttrToken ) const
205{
206 sal_Int32 nValue;
207 bool bValid = getAttribList()->getAsInteger( nAttrToken, nValue );
208 return bValid ? std::optional< sal_Int32 >( nValue ) : std::optional< sal_Int32 >();
209}
210
211std::optional< sal_uInt32 > AttributeList::getUnsigned( sal_Int32 nAttrToken ) const
212{
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 >();
216}
217
218std::optional< sal_Int64 > AttributeList::getHyper( sal_Int32 nAttrToken ) const
219{
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 >();
223}
224
225std::optional< sal_Int32 > AttributeList::getIntegerHex( sal_Int32 nAttrToken ) const
226{
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 >();
230}
231
232std::optional< bool > AttributeList::getBool( sal_Int32 nAttrToken ) const
233{
234 std::string_view pAttr;
235
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 );
244
245 // now for all the crazy stuff
246
247 // boolean attributes may be "t", "f", "true", "false", "on", "off", "1", or "0"
248 switch( getToken( nAttrToken, XML_TOKEN_INVALID ) )
249 {
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 );
256 }
257 std::optional< sal_Int32 > onValue = getInteger( nAttrToken );
258 return onValue.has_value() ? std::optional< bool >( onValue.value() != 0 ) : std::optional< bool >();
259}
260
261std::optional< util::DateTime > AttributeList::getDateTime( sal_Int32 nAttrToken ) const
262{
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)
269 {
270 SAL_WARN("oox", "bad date string: " << aValue);
271 return std::optional< util::DateTime >();
272 }
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 );
280}
281
282// defaulted return values ----------------------------------------------------
283
284sal_Int32 AttributeList::getToken( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
285{
286 return mxAttribs->getOptionalValueToken( nAttrToken, nDefault );
287}
288
289OUString AttributeList::getString( sal_Int32 nAttrToken, const OUString& rDefault ) const
290{
291 // try to avoid slow exception throw/catch if we can
292 if (rDefault.isEmpty())
293 return mxAttribs->getOptionalValue( nAttrToken );
294
295 try
296 {
297 return mxAttribs->getValue( nAttrToken );
298 }
299 catch( Exception& )
300 {
301 }
302 return rDefault;
303}
304
305OUString AttributeList::getXString( sal_Int32 nAttrToken, const OUString& rDefault ) const
306{
307 return getXString( nAttrToken ).value_or( rDefault );
308}
309
310std::string_view AttributeList::getView( sal_Int32 nAttrToken ) const
311{
312 std::string_view p;
313 getAttribList()->getAsView(nAttrToken, p);
314 return p;
315}
316
317double AttributeList::getDouble( sal_Int32 nAttrToken, double fDefault ) const
318{
319 return getDouble( nAttrToken ).value_or( fDefault );
320}
321
322sal_Int32 AttributeList::getInteger( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
323{
324 return getInteger( nAttrToken ).value_or( nDefault );
325}
326
327sal_uInt32 AttributeList::getUnsigned( sal_Int32 nAttrToken, sal_uInt32 nDefault ) const
328{
329 return getUnsigned( nAttrToken ).value_or( nDefault );
330}
331
332sal_Int64 AttributeList::getHyper( sal_Int32 nAttrToken, sal_Int64 nDefault ) const
333{
334 return getHyper( nAttrToken ).value_or( nDefault );
335}
336
337sal_Int32 AttributeList::getIntegerHex( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
338{
339 return getIntegerHex( nAttrToken ).value_or( nDefault );
340}
341
342sal_uInt32 AttributeList::getUnsignedHex( sal_Int32 nAttrToken, sal_uInt32 nDefault ) const
343{
344 return getIntegerHex( nAttrToken ).value_or( nDefault );
345}
346
347bool AttributeList::getBool( sal_Int32 nAttrToken, bool bDefault ) const
348{
349 return getBool( nAttrToken ).value_or( bDefault );
350}
351
352util::DateTime AttributeList::getDateTime( sal_Int32 nAttrToken, const util::DateTime& rDefault ) const
353{
354 return getDateTime( nAttrToken ).value_or( rDefault );
355}
356
357std::vector<sal_Int32> AttributeList::getTokenList(sal_Int32 nAttrToken) const
358{
359 std::vector<sal_Int32> aValues;
360 OUString sValue = getString(nAttrToken, "");
361 sal_Int32 nIndex = 0;
362 do
363 {
364 aValues.push_back(AttributeConversion::decodeToken(o3tl::getToken(sValue, 0, ' ', nIndex)));
365 } while (nIndex >= 0);
366
367 return aValues;
368}
369
370} // namespace oox
371
372/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define STRING_TO_TOKEN(color)
static sal_Int32 decodeIntegerHex(std::u16string_view rValue)
Returns the 32-bit signed integer value from the passed string (hexadecimal).
static sal_uInt32 decodeUnsigned(std::u16string_view rValue)
Returns the 32-bit unsigned integer value from the passed string (decimal).
static OUString decodeXString(const OUString &rValue)
Returns the decoded string value.
static sal_Int64 decodeHyper(std::u16string_view rValue)
Returns the 64-bit signed integer value from the passed string (decimal).
static sal_Int32 decodeInteger(std::u16string_view rValue)
Returns the 32-bit signed integer value from the passed string (decimal).
static sal_Int32 decodeToken(std::u16string_view rValue)
Returns the XML token identifier from the passed string.
std::optional< css::util::DateTime > getDateTime(sal_Int32 nAttrToken) const
Returns the date/time value of the specified attribute.
sax_fastparser::FastAttributeList * getAttribList() const
css::uno::Reference< css::xml::sax::XFastAttributeList > mxAttribs
sal_uInt32 getUnsignedHex(sal_Int32 nAttrToken, sal_uInt32 nDefault) const
oox::drawingml::Color getHighlightColor(sal_Int32 nAttrToken) const
Returns the Color object of highlight of the text.
std::vector< sal_Int32 > getTokenList(sal_Int32 nAttrToken) const
OUString getStringDefaulted(sal_Int32 nAttrToken) const
Returns the string value of the specified attribute, returns an empty string if attribute not present...
bool hasAttribute(sal_Int32 nAttrToken) const
Returns true, if the specified attribute is present.
sax_fastparser::FastAttributeList * mpAttribList
std::optional< OUString > getXString(sal_Int32 nAttrToken) const
Returns the string value of the specified attribute.
std::optional< sal_uInt32 > getUnsigned(sal_Int32 nAttrToken) const
Returns the 32-bit unsigned integer value of the specified attribute (decimal).
std::optional< sal_Int32 > getIntegerHex(sal_Int32 nAttrToken) const
Returns the 32-bit signed integer value of the specified attribute (hexadecimal).
AttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &rxAttribs)
std::optional< sal_Int32 > getInteger(sal_Int32 nAttrToken) const
Returns the 32-bit signed integer value of the specified attribute (decimal).
std::optional< sal_Int64 > getHyper(sal_Int32 nAttrToken) const
Returns the 64-bit signed integer value of the specified attribute (decimal).
std::string_view getView(sal_Int32 nAttrToken) const
std::optional< OUString > getString(sal_Int32 nAttrToken) const
Returns the string value of the specified attribute.
std::optional< bool > getBool(sal_Int32 nAttrToken) const
Returns the boolean value of the specified attribute.
std::optional< sal_Int32 > getToken(sal_Int32 nAttrToken) const
Returns the token identifier of the value of the specified attribute.
std::optional< double > getDouble(sal_Int32 nAttrToken) const
Returns the double value of the specified attribute.
static sal_Int32 getTokenFromUnicode(std::u16string_view rUnicodeName)
Returns the token identifier for the passed Unicode token name.
Definition: tokenmap.cxx:77
void setHighlight(sal_Int32 nToken)
Sets a predefined color from the w:highlight element.
Definition: color.cxx:376
bool getAsView(sal_Int32 nToken, std::string_view &rPos) const
bool getAsDouble(sal_Int32 nToken, double &rDouble) const
bool getAsInteger(sal_Int32 nToken, sal_Int32 &rInt) const
float u
sal_Int16 nValue
sal_Int32 nIndex
void * p
#define SAL_WARN(area, stream)
@ Exception
none
OUString sanitizeStringSurrogates(const OUString &rString)
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
sal_Int64 toInt64(std::u16string_view str, sal_Int16 radix=10)
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
sal_uInt32 toUInt32(std::u16string_view str, sal_Int16 radix=10)
sal_Int32 getHighlightColorTokenFromString(std::u16string_view sColorName)
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
XML_TOKEN_INVALID
DefTokenId nToken
sal_uInt16 sal_Unicode
#define SAL_MAX_UINT32
std::unique_ptr< char[]> aBuffer