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 <osl/diagnose.h>
23#include <rtl/ustrbuf.hxx>
24#include <sax/fastattribs.hxx>
26#include <o3tl/string_view.hxx>
27
28namespace oox {
29
30using namespace ::com::sun::star;
31using namespace ::com::sun::star::uno;
32using namespace ::com::sun::star::xml::sax;
33
34namespace {
35
36const sal_Int32 XSTRING_ENCCHAR_LEN = 7;
37
38bool lclAddHexDigit( sal_Unicode& orcChar, sal_Unicode cDigit, int nBitShift )
39{
40 if( ('0' <= cDigit) && (cDigit <= '9') ) { orcChar |= ((cDigit - '0') << nBitShift); return true; }
41 if( ('a' <= cDigit) && (cDigit <= 'f') ) { orcChar |= ((cDigit - 'a' + 10) << nBitShift); return true; }
42 if( ('A' <= cDigit) && (cDigit <= 'F') ) { orcChar |= ((cDigit - 'A' + 10) << nBitShift); return true; }
43 return false;
44}
45
46sal_Unicode lclGetXChar( const sal_Unicode*& rpcStr, const sal_Unicode* pcEnd )
47{
48 sal_Unicode cChar = 0;
49 if( (pcEnd - rpcStr >= XSTRING_ENCCHAR_LEN) &&
50 (rpcStr[ 0 ] == '_') &&
51 (rpcStr[ 1 ] == 'x') &&
52 (rpcStr[ 6 ] == '_') &&
53 lclAddHexDigit( cChar, rpcStr[ 2 ], 12 ) &&
54 lclAddHexDigit( cChar, rpcStr[ 3 ], 8 ) &&
55 lclAddHexDigit( cChar, rpcStr[ 4 ], 4 ) &&
56 lclAddHexDigit( cChar, rpcStr[ 5 ], 0 ) )
57 {
58 rpcStr += XSTRING_ENCCHAR_LEN;
59 return cChar;
60 }
61 return *rpcStr++;
62}
63
64} // namespace
65
66#define STRING_TO_TOKEN(color) if (sColorName == u"" #color) return XML_##color
67sal_Int32 getHighlightColorTokenFromString(std::u16string_view sColorName)
68{
69 STRING_TO_TOKEN(black);
70 STRING_TO_TOKEN(blue);
71 STRING_TO_TOKEN(cyan);
72 STRING_TO_TOKEN(darkBlue);
73 STRING_TO_TOKEN(darkCyan);
74 STRING_TO_TOKEN(darkGreen);
75 STRING_TO_TOKEN(darkMagenta);
76 STRING_TO_TOKEN(darkRed);
77 STRING_TO_TOKEN(darkYellow);
78 STRING_TO_TOKEN(darkGray);
79 STRING_TO_TOKEN(green);
80 STRING_TO_TOKEN(lightGray);
81 STRING_TO_TOKEN(magenta);
82 STRING_TO_TOKEN(red);
83 STRING_TO_TOKEN(white);
84 STRING_TO_TOKEN(yellow);
86
87 return XML_TOKEN_INVALID;
88}
89
90sal_Int32 AttributeConversion::decodeToken( std::u16string_view rValue )
91{
92 return TokenMap::getTokenFromUnicode( rValue );
93}
94
95OUString AttributeConversion::decodeXString( const OUString& rValue )
96{
97 // string shorter than one encoded character - no need to decode
98 if( rValue.getLength() < XSTRING_ENCCHAR_LEN )
99 return rValue;
100 OUStringBuffer aBuffer;
101 const sal_Unicode* pcStr = rValue.getStr();
102 const sal_Unicode* pcEnd = pcStr + rValue.getLength();
103 while( pcStr < pcEnd )
104 aBuffer.append( lclGetXChar( pcStr, pcEnd ) );
105 return aBuffer.makeStringAndClear();
106}
107
108sal_Int32 AttributeConversion::decodeInteger( std::u16string_view rValue )
109{
110 return o3tl::toInt32(rValue);
111}
112
113sal_uInt32 AttributeConversion::decodeUnsigned( std::u16string_view rValue )
114{
115 return getLimitedValue< sal_uInt32, sal_Int64 >( o3tl::toInt64(rValue), 0, SAL_MAX_UINT32 );
116}
117
118sal_Int64 AttributeConversion::decodeHyper( std::u16string_view rValue )
119{
120 return o3tl::toInt64(rValue);
121}
122
123sal_Int32 AttributeConversion::decodeIntegerHex( std::u16string_view rValue )
124{
125 // It looks like all Office Open XML attributes containing hexadecimal
126 // values are based on xsd:hexBinary and so use an unsigned representation:
127 return static_cast< sal_Int32 >(o3tl::toUInt32(rValue, 16));
128 //TODO: Change this function to return sal_uInt32 and get rid of the
129 // cast, but that will have a ripple effect
130}
131
132AttributeList::AttributeList( const Reference< XFastAttributeList >& rxAttribs ) :
133 mxAttribs( rxAttribs ),
134 mpAttribList( nullptr )
135{
136 OSL_ENSURE( mxAttribs.is(), "AttributeList::AttributeList - missing attribute list interface" );
137}
138
140{
141 if( mpAttribList == nullptr )
142 {
144 }
145 return mpAttribList;
146}
147
148bool AttributeList::hasAttribute( sal_Int32 nAttrToken ) const
149{
150 return mxAttribs->hasAttribute( nAttrToken );
151}
152
154{
155 OUString sColorVal = mxAttribs->getValue(nAttrToken);
158 return aColor;
159}
160
161// optional return values -----------------------------------------------------
162
163std::optional< sal_Int32 > AttributeList::getToken( sal_Int32 nAttrToken ) const
164{
165 sal_Int32 nToken = mxAttribs->getOptionalValueToken( nAttrToken, XML_TOKEN_INVALID );
166 return nToken == XML_TOKEN_INVALID ? std::optional< sal_Int32 >() : std::optional< sal_Int32 >( nToken );
167}
168
169std::optional< OUString > AttributeList::getString( sal_Int32 nAttrToken ) const
170{
171 // check if the attribute exists (empty string may be different to missing attribute)
172 if( mxAttribs->hasAttribute( nAttrToken ) )
173 return std::optional< OUString >( mxAttribs->getOptionalValue( nAttrToken ) );
174 return std::optional< OUString >();
175}
176
177OUString AttributeList::getStringDefaulted( sal_Int32 nAttrToken ) const
178{
179 // check if the attribute exists (empty string may be different to missing attribute)
180 if( mxAttribs->hasAttribute( nAttrToken ) )
181 return mxAttribs->getOptionalValue( nAttrToken );
182 return OUString();
183}
184
185std::optional< OUString > AttributeList::getXString( sal_Int32 nAttrToken ) const
186{
187 // check if the attribute exists (empty string may be different to missing attribute)
188 if( mxAttribs->hasAttribute( nAttrToken ) )
189 return std::optional< OUString >( AttributeConversion::decodeXString( mxAttribs->getOptionalValue( nAttrToken ) ) );
190 return std::optional< OUString >();
191}
192
193std::optional< double > AttributeList::getDouble( sal_Int32 nAttrToken ) const
194{
195 double nValue;
196 bool bValid = getAttribList()->getAsDouble( nAttrToken, nValue );
197 return bValid ? std::optional< double >( nValue ) : std::optional< double >();
198}
199
200std::optional< sal_Int32 > AttributeList::getInteger( sal_Int32 nAttrToken ) const
201{
202 sal_Int32 nValue;
203 bool bValid = getAttribList()->getAsInteger( nAttrToken, nValue );
204 return bValid ? std::optional< sal_Int32 >( nValue ) : std::optional< sal_Int32 >();
205}
206
207std::optional< sal_uInt32 > AttributeList::getUnsigned( sal_Int32 nAttrToken ) const
208{
209 OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
210 bool bValid = !aValue.isEmpty();
211 return bValid ? std::optional< sal_uInt32 >( AttributeConversion::decodeUnsigned( aValue ) ) : std::optional< sal_uInt32 >();
212}
213
214std::optional< sal_Int64 > AttributeList::getHyper( sal_Int32 nAttrToken ) const
215{
216 OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
217 bool bValid = !aValue.isEmpty();
218 return bValid ? std::optional< sal_Int64 >( AttributeConversion::decodeHyper( aValue ) ) : std::optional< sal_Int64 >();
219}
220
221std::optional< sal_Int32 > AttributeList::getIntegerHex( sal_Int32 nAttrToken ) const
222{
223 OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
224 bool bValid = !aValue.isEmpty();
225 return bValid ? std::optional< sal_Int32 >( AttributeConversion::decodeIntegerHex( aValue ) ) : std::optional< sal_Int32 >();
226}
227
228std::optional< bool > AttributeList::getBool( sal_Int32 nAttrToken ) const
229{
230 const char *pAttr;
231
232 // catch the common cases as quickly as possible first
233 bool bHasAttr = getAttribList()->getAsChar( nAttrToken, pAttr );
234 if( !bHasAttr )
235 return std::optional< bool >();
236 if( !strcmp( pAttr, "false" ) )
237 return std::optional< bool >( false );
238 if( !strcmp( pAttr, "true" ) )
239 return std::optional< bool >( true );
240
241 // now for all the crazy stuff
242
243 // boolean attributes may be "t", "f", "true", "false", "on", "off", "1", or "0"
244 switch( getToken( nAttrToken, XML_TOKEN_INVALID ) )
245 {
246 case XML_t: return std::optional< bool >( true ); // used in VML
247 case XML_true: return std::optional< bool >( true );
248 case XML_on: return std::optional< bool >( true );
249 case XML_f: return std::optional< bool >( false ); // used in VML
250 case XML_false: return std::optional< bool >( false );
251 case XML_off: return std::optional< bool >( false );
252 }
253 std::optional< sal_Int32 > onValue = getInteger( nAttrToken );
254 return onValue.has_value() ? std::optional< bool >( onValue.value() != 0 ) : std::optional< bool >();
255}
256
257std::optional< util::DateTime > AttributeList::getDateTime( sal_Int32 nAttrToken ) const
258{
259 OUString aValue = mxAttribs->getOptionalValue( nAttrToken );
260 util::DateTime aDateTime;
261 bool bValid = (aValue.getLength() == 19) && (aValue[ 4 ] == '-') && (aValue[ 7 ] == '-') &&
262 (aValue[ 10 ] == 'T') && (aValue[ 13 ] == ':') && (aValue[ 16 ] == ':');
263 if (!bValid)
264 return std::optional< util::DateTime >();
265 aDateTime.Year = static_cast< sal_uInt16 >( o3tl::toInt32(aValue.subView( 0, 4 )) );
266 aDateTime.Month = static_cast< sal_uInt16 >( o3tl::toInt32(aValue.subView( 5, 2 )) );
267 aDateTime.Day = static_cast< sal_uInt16 >( o3tl::toInt32(aValue.subView( 8, 2 )) );
268 aDateTime.Hours = static_cast< sal_uInt16 >( o3tl::toInt32(aValue.subView( 11, 2 )) );
269 aDateTime.Minutes = static_cast< sal_uInt16 >( o3tl::toInt32(aValue.subView( 14, 2 )) );
270 aDateTime.Seconds = static_cast< sal_uInt16 >( o3tl::toInt32(aValue.subView( 17, 2 )) );
271 return std::optional< util::DateTime >( aDateTime );
272}
273
274// defaulted return values ----------------------------------------------------
275
276sal_Int32 AttributeList::getToken( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
277{
278 return mxAttribs->getOptionalValueToken( nAttrToken, nDefault );
279}
280
281OUString AttributeList::getString( sal_Int32 nAttrToken, const OUString& rDefault ) const
282{
283 // try to avoid slow exception throw/catch if we can
284 if (rDefault.isEmpty())
285 return mxAttribs->getOptionalValue( nAttrToken );
286
287 try
288 {
289 return mxAttribs->getValue( nAttrToken );
290 }
291 catch( Exception& )
292 {
293 }
294 return rDefault;
295}
296
297OUString AttributeList::getXString( sal_Int32 nAttrToken, const OUString& rDefault ) const
298{
299 return getXString( nAttrToken ).value_or( rDefault );
300}
301
302const char* AttributeList::getChar( sal_Int32 nAttrToken ) const
303{
304 const char* p = nullptr;
305 bool bValid = getAttribList()->getAsChar(nAttrToken, p);
306 if (!bValid)
307 p = nullptr;
308
309 return p;
310}
311
312double AttributeList::getDouble( sal_Int32 nAttrToken, double fDefault ) const
313{
314 return getDouble( nAttrToken ).value_or( fDefault );
315}
316
317sal_Int32 AttributeList::getInteger( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
318{
319 return getInteger( nAttrToken ).value_or( nDefault );
320}
321
322sal_uInt32 AttributeList::getUnsigned( sal_Int32 nAttrToken, sal_uInt32 nDefault ) const
323{
324 return getUnsigned( nAttrToken ).value_or( nDefault );
325}
326
327sal_Int64 AttributeList::getHyper( sal_Int32 nAttrToken, sal_Int64 nDefault ) const
328{
329 return getHyper( nAttrToken ).value_or( nDefault );
330}
331
332sal_Int32 AttributeList::getIntegerHex( sal_Int32 nAttrToken, sal_Int32 nDefault ) const
333{
334 return getIntegerHex( nAttrToken ).value_or( nDefault );
335}
336
337sal_uInt32 AttributeList::getUnsignedHex( sal_Int32 nAttrToken, sal_uInt32 nDefault ) const
338{
339 return getIntegerHex( nAttrToken ).value_or( nDefault );
340}
341
342bool AttributeList::getBool( sal_Int32 nAttrToken, bool bDefault ) const
343{
344 return getBool( nAttrToken ).value_or( bDefault );
345}
346
347util::DateTime AttributeList::getDateTime( sal_Int32 nAttrToken, const util::DateTime& rDefault ) const
348{
349 return getDateTime( nAttrToken ).value_or( rDefault );
350}
351
352std::vector<sal_Int32> AttributeList::getTokenList(sal_Int32 nAttrToken) const
353{
354 std::vector<sal_Int32> aValues;
355 OUString sValue = getString(nAttrToken, "");
356 sal_Int32 nIndex = 0;
357 do
358 {
359 aValues.push_back(AttributeConversion::decodeToken(o3tl::getToken(sValue, 0, ' ', nIndex)));
360 } while (nIndex >= 0);
361
362 return aValues;
363}
364
365} // namespace oox
366
367/* 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
const char * getChar(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::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 getAsChar(sal_Int32 nToken, const char *&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
@ Exception
none
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