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 OUStringBuffer aBuffer;
102 const sal_Unicode* pcStr = rValue.getStr();
103 const sal_Unicode* pcEnd = pcStr + rValue.getLength();
104 while( pcStr < pcEnd )
105 aBuffer.append( lclGetXChar( pcStr, pcEnd ) );
106 return comphelper::string::sanitizeStringSurrogates(aBuffer.makeStringAndClear());
107}
108
109sal_Int32 AttributeConversion::decodeInteger( std::u16string_view rValue )
110{
111 return o3tl::toInt32(rValue);
112}
113
114sal_uInt32 AttributeConversion::decodeUnsigned( std::u16string_view rValue )
115{
116 return getLimitedValue< sal_uInt32, sal_Int64 >( o3tl::toInt64(rValue), 0, SAL_MAX_UINT32 );
117}
118
119sal_Int64 AttributeConversion::decodeHyper( std::u16string_view rValue )
120{
121 return o3tl::toInt64(rValue);
122}
123
124sal_Int32 AttributeConversion::decodeIntegerHex( std::u16string_view rValue )
125{
126 // It looks like all Office Open XML attributes containing hexadecimal
127 // values are based on xsd:hexBinary and so use an unsigned representation:
128 return static_cast< sal_Int32 >(o3tl::toUInt32(rValue, 16));
129 //TODO: Change this function to return sal_uInt32 and get rid of the
130 // cast, but that will have a ripple effect
131}
132
133AttributeList::AttributeList( const Reference< XFastAttributeList >& rxAttribs ) :
134 mxAttribs( rxAttribs ),
135 mpAttribList( nullptr )
136{
137 OSL_ENSURE( mxAttribs.is(), "AttributeList::AttributeList - missing attribute list interface" );
138}
139
141{
142 if( mpAttribList == nullptr )
143 {
145 }
146 return mpAttribList;
147}
148
149bool AttributeList::hasAttribute( sal_Int32 nAttrToken ) const
150{
151 return mxAttribs->hasAttribute( nAttrToken );
152}
153
155{
156 OUString sColorVal = mxAttribs->getValue(nAttrToken);
159 return aColor;
160}
161
162// optional return values -----------------------------------------------------
163
164std::optional< sal_Int32 > AttributeList::getToken( sal_Int32 nAttrToken ) const
165{
166 sal_Int32 nToken = mxAttribs->getOptionalValueToken( nAttrToken, XML_TOKEN_INVALID );
167 return nToken == XML_TOKEN_INVALID ? std::optional< sal_Int32 >() : std::optional< sal_Int32 >( nToken );
168}
169
170std::optional< OUString > AttributeList::getString( sal_Int32 nAttrToken ) const
171{
172 // check if the attribute exists (empty string may be different to missing attribute)
173 if( mxAttribs->hasAttribute( nAttrToken ) )
174 return std::optional< OUString >( mxAttribs->getOptionalValue( nAttrToken ) );
175 return std::optional< OUString >();
176}
177
178OUString AttributeList::getStringDefaulted( sal_Int32 nAttrToken ) const
179{
180 // check if the attribute exists (empty string may be different to missing attribute)
181 if( mxAttribs->hasAttribute( nAttrToken ) )
182 return mxAttribs->getOptionalValue( nAttrToken );
183 return OUString();
184}
185
186std::optional< OUString > AttributeList::getXString( sal_Int32 nAttrToken ) const
187{
188 // check if the attribute exists (empty string may be different to missing attribute)
189 if( mxAttribs->hasAttribute( nAttrToken ) )
190 return std::optional< OUString >( AttributeConversion::decodeXString( mxAttribs->getOptionalValue( nAttrToken ) ) );
191 return std::optional< OUString >();
192}
193
194std::optional< double > AttributeList::getDouble( sal_Int32 nAttrToken ) const
195{
196 double nValue;
197 bool bValid = getAttribList()->getAsDouble( nAttrToken, nValue );
198 return bValid ? std::optional< double >( nValue ) : std::optional< double >();
199}
200
201std::optional< sal_Int32 > AttributeList::getInteger( sal_Int32 nAttrToken ) const
202{
203 sal_Int32 nValue;
204 bool bValid = getAttribList()->getAsInteger( nAttrToken, nValue );
205 return bValid ? std::optional< sal_Int32 >( nValue ) : std::optional< sal_Int32 >();
206}
207
208std::optional< sal_uInt32 > AttributeList::getUnsigned( sal_Int32 nAttrToken ) const
209{
210 OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
211 bool bValid = !aValue.isEmpty();
212 return bValid ? std::optional< sal_uInt32 >( AttributeConversion::decodeUnsigned( aValue ) ) : std::optional< sal_uInt32 >();
213}
214
215std::optional< sal_Int64 > AttributeList::getHyper( sal_Int32 nAttrToken ) const
216{
217 std::string_view aValue = getView( nAttrToken );
218 bool bValid = !aValue.empty();
219 return bValid ? std::optional< sal_Int64 >( o3tl::toInt64( aValue ) ) : std::optional< sal_Int64 >();
220}
221
222std::optional< sal_Int32 > AttributeList::getIntegerHex( sal_Int32 nAttrToken ) const
223{
224 OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
225 bool bValid = !aValue.isEmpty();
226 return bValid ? std::optional< sal_Int32 >( AttributeConversion::decodeIntegerHex( aValue ) ) : std::optional< sal_Int32 >();
227}
228
229std::optional< bool > AttributeList::getBool( sal_Int32 nAttrToken ) const
230{
231 std::string_view pAttr;
232
233 // catch the common cases as quickly as possible first
234 bool bHasAttr = getAttribList()->getAsView( nAttrToken, pAttr );
235 if( !bHasAttr )
236 return std::optional< bool >();
237 if( pAttr == "false" )
238 return std::optional< bool >( false );
239 if( pAttr == "true" )
240 return std::optional< bool >( true );
241
242 // now for all the crazy stuff
243
244 // boolean attributes may be "t", "f", "true", "false", "on", "off", "1", or "0"
245 switch( getToken( nAttrToken, XML_TOKEN_INVALID ) )
246 {
247 case XML_t: return std::optional< bool >( true ); // used in VML
248 case XML_true: return std::optional< bool >( true );
249 case XML_on: return std::optional< bool >( true );
250 case XML_f: return std::optional< bool >( false ); // used in VML
251 case XML_false: return std::optional< bool >( false );
252 case XML_off: return std::optional< bool >( false );
253 }
254 std::optional< sal_Int32 > onValue = getInteger( nAttrToken );
255 return onValue.has_value() ? std::optional< bool >( onValue.value() != 0 ) : std::optional< bool >();
256}
257
258std::optional< util::DateTime > AttributeList::getDateTime( sal_Int32 nAttrToken ) const
259{
260 std::string_view aValue = getView( nAttrToken );
261 util::DateTime aDateTime;
262 bool bValid = (aValue.size() == 19 || (aValue.size() == 20 && aValue[19] == 'Z')) &&
263 (aValue[ 4 ] == '-') && (aValue[ 7 ] == '-') && (aValue[ 10 ] == 'T') &&
264 (aValue[ 13 ] == ':') && (aValue[ 16 ] == ':');
265 if (!bValid)
266 {
267 SAL_WARN("oox", "bad date string: " << aValue);
268 return std::optional< util::DateTime >();
269 }
270 aDateTime.Year = static_cast< sal_uInt16 >( o3tl::toInt32(aValue.substr( 0, 4 )) );
271 aDateTime.Month = static_cast< sal_uInt16 >( o3tl::toInt32(aValue.substr( 5, 2 )) );
272 aDateTime.Day = static_cast< sal_uInt16 >( o3tl::toInt32(aValue.substr( 8, 2 )) );
273 aDateTime.Hours = static_cast< sal_uInt16 >( o3tl::toInt32(aValue.substr( 11, 2 )) );
274 aDateTime.Minutes = static_cast< sal_uInt16 >( o3tl::toInt32(aValue.substr( 14, 2 )) );
275 aDateTime.Seconds = static_cast< sal_uInt16 >( o3tl::toInt32(aValue.substr( 17, 2 )) );
276 return std::optional< util::DateTime >( aDateTime );
277}
278
279// defaulted return values ----------------------------------------------------
280
281sal_Int32 AttributeList::getToken( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
282{
283 return mxAttribs->getOptionalValueToken( nAttrToken, nDefault );
284}
285
286OUString AttributeList::getString( sal_Int32 nAttrToken, const OUString& rDefault ) const
287{
288 // try to avoid slow exception throw/catch if we can
289 if (rDefault.isEmpty())
290 return mxAttribs->getOptionalValue( nAttrToken );
291
292 try
293 {
294 return mxAttribs->getValue( nAttrToken );
295 }
296 catch( Exception& )
297 {
298 }
299 return rDefault;
300}
301
302OUString AttributeList::getXString( sal_Int32 nAttrToken, const OUString& rDefault ) const
303{
304 return getXString( nAttrToken ).value_or( rDefault );
305}
306
307std::string_view AttributeList::getView( sal_Int32 nAttrToken ) const
308{
309 std::string_view p;
310 getAttribList()->getAsView(nAttrToken, p);
311 return p;
312}
313
314double AttributeList::getDouble( sal_Int32 nAttrToken, double fDefault ) const
315{
316 return getDouble( nAttrToken ).value_or( fDefault );
317}
318
319sal_Int32 AttributeList::getInteger( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
320{
321 return getInteger( nAttrToken ).value_or( nDefault );
322}
323
324sal_uInt32 AttributeList::getUnsigned( sal_Int32 nAttrToken, sal_uInt32 nDefault ) const
325{
326 return getUnsigned( nAttrToken ).value_or( nDefault );
327}
328
329sal_Int64 AttributeList::getHyper( sal_Int32 nAttrToken, sal_Int64 nDefault ) const
330{
331 return getHyper( nAttrToken ).value_or( nDefault );
332}
333
334sal_Int32 AttributeList::getIntegerHex( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
335{
336 return getIntegerHex( nAttrToken ).value_or( nDefault );
337}
338
339sal_uInt32 AttributeList::getUnsignedHex( sal_Int32 nAttrToken, sal_uInt32 nDefault ) const
340{
341 return getIntegerHex( nAttrToken ).value_or( nDefault );
342}
343
344bool AttributeList::getBool( sal_Int32 nAttrToken, bool bDefault ) const
345{
346 return getBool( nAttrToken ).value_or( bDefault );
347}
348
349util::DateTime AttributeList::getDateTime( sal_Int32 nAttrToken, const util::DateTime& rDefault ) const
350{
351 return getDateTime( nAttrToken ).value_or( rDefault );
352}
353
354std::vector<sal_Int32> AttributeList::getTokenList(sal_Int32 nAttrToken) const
355{
356 std::vector<sal_Int32> aValues;
357 OUString sValue = getString(nAttrToken, "");
358 sal_Int32 nIndex = 0;
359 do
360 {
361 aValues.push_back(AttributeConversion::decodeToken(o3tl::getToken(sValue, 0, ' ', nIndex)));
362 } while (nIndex >= 0);
363
364 return aValues;
365}
366
367} // namespace oox
368
369/* 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:297
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
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