1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sal/config.h>
24 #include <com/sun/star/uno/Any.hxx>
25 #include <com/sun/star/uno/RuntimeException.hpp>
26 #include <com/sun/star/uno/Sequence.hxx>
27 #include <comphelper/sequence.hxx>
28 #include <o3tl/string_view.hxx>
30 #include <rtl/string.h>
31 #include <rtl/string.hxx>
32 #include <rtl/ustring.hxx>
33 #include <sal/types.h>
34 #include <xmlreader/span.hxx>
35 #include <xmlreader/xmlreader.hxx>
38 #include "localizedvaluenode.hxx"
40 #include "nodemap.hxx"
41 #include "parsemanager.hxx"
42 #include "propertynode.hxx"
44 #include "valueparser.hxx"
50 bool parseHexDigit(char c
, int * value
) {
51 assert(value
!= nullptr);
52 if (c
>= '0' && c
<= '9') {
56 if (c
>= 'A' && c
<= 'F') {
57 *value
= c
- 'A' + 10;
60 if (c
>= 'a' && c
<= 'f') {
61 *value
= c
- 'a' + 10;
67 bool parseValue(xmlreader::Span
const & text
, sal_Bool
* value
) {
68 assert(text
.is() && value
!= nullptr);
69 if (text
== "true" || text
== "1") {
73 if (text
== "false" || text
== "0") {
80 bool parseValue(xmlreader::Span
const & text
, sal_Int16
* value
) {
81 assert(text
.is() && value
!= nullptr);
82 // For backwards compatibility, support hexadecimal values:
84 rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
85 text
.begin
, text
.length
, RTL_CONSTASCII_STRINGPARAM("0X"),
86 RTL_CONSTASCII_LENGTH("0X")) == 0 ?
87 static_cast< sal_Int32
>(
89 text
.begin
+ RTL_CONSTASCII_LENGTH("0X"),
90 text
.length
- RTL_CONSTASCII_LENGTH("0X")).toUInt32(16)) :
91 OString(text
.begin
, text
.length
).toInt32();
92 //TODO: check valid lexical representation
93 if (n
>= SAL_MIN_INT16
&& n
<= SAL_MAX_INT16
) {
94 *value
= static_cast< sal_Int16
>(n
);
100 bool parseValue(xmlreader::Span
const & text
, sal_Int32
* value
) {
101 assert(text
.is() && value
!= nullptr);
102 // For backwards compatibility, support hexadecimal values:
103 bool bStartWithHexPrefix
= rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
104 text
.begin
, text
.length
, RTL_CONSTASCII_STRINGPARAM("0X"),
105 RTL_CONSTASCII_LENGTH("0X")) == 0;
107 if (bStartWithHexPrefix
)
109 std::string_view
sView(text
.begin
+ RTL_CONSTASCII_LENGTH("0X"),
110 text
.length
- RTL_CONSTASCII_LENGTH("0X"));
111 *value
= static_cast< sal_Int32
>(o3tl::toUInt32(sView
, 16));
115 std::string_view
sView(text
.begin
, text
.length
);
116 *value
= o3tl::toInt32(sView
);
118 //TODO: check valid lexical representation
122 bool parseValue(xmlreader::Span
const & text
, sal_Int64
* value
) {
123 assert(text
.is() && value
!= nullptr);
124 // For backwards compatibility, support hexadecimal values:
126 rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
127 text
.begin
, text
.length
, RTL_CONSTASCII_STRINGPARAM("0X"),
128 RTL_CONSTASCII_LENGTH("0X")) == 0 ?
129 static_cast< sal_Int64
>(
131 text
.begin
+ RTL_CONSTASCII_LENGTH("0X"),
132 text
.length
- RTL_CONSTASCII_LENGTH("0X")).toUInt64(16)) :
133 OString(text
.begin
, text
.length
).toInt64();
134 //TODO: check valid lexical representation
138 bool parseValue(xmlreader::Span
const & text
, double * value
) {
139 assert(text
.is() && value
!= nullptr);
140 *value
= rtl_math_stringToDouble(
141 text
.begin
, text
.begin
+ text
.length
, '.', 0, nullptr, nullptr);
142 //TODO: check valid lexical representation
146 bool parseValue(xmlreader::Span
const & text
, OUString
* value
) {
147 assert(text
.is() && value
!= nullptr);
148 *value
= text
.convertFromUtf8();
153 xmlreader::Span
const & text
, css::uno::Sequence
< sal_Int8
> * value
)
155 assert(text
.is() && value
!= nullptr);
156 if ((text
.length
& 1) != 0) {
159 std::vector
< sal_Int8
> seq
;
160 for (sal_Int32 i
= 0; i
!= text
.length
;) {
163 if (!parseHexDigit(text
.begin
[i
++], &n1
) ||
164 !parseHexDigit(text
.begin
[i
++], &n2
))
168 seq
.push_back(static_cast< sal_Int8
>((n1
<< 4) | n2
));
170 *value
= comphelper::containerToSequence(seq
);
174 template< typename T
> css::uno::Any
parseSingleValue(
175 xmlreader::Span
const & text
)
178 if (!parseValue(text
, &val
)) {
179 throw css::uno::RuntimeException("invalid value");
181 return css::uno::Any(val
);
184 template< typename T
> css::uno::Any
parseListValue(
185 OString
const & separator
, xmlreader::Span
const & text
)
187 std::vector
< T
> seq
;
189 if (separator
.isEmpty()) {
190 sep
= xmlreader::Span(RTL_CONSTASCII_STRINGPARAM(" "));
192 sep
= xmlreader::Span(separator
.getStr(), separator
.getLength());
194 if (text
.length
!= 0) {
195 for (xmlreader::Span
t(text
);;) {
196 sal_Int32 i
= rtl_str_indexOfStr_WithLength(
197 t
.begin
, t
.length
, sep
.begin
, sep
.length
);
200 xmlreader::Span(t
.begin
, i
== -1 ? t
.length
: i
), &val
))
202 throw css::uno::RuntimeException("invalid value");
208 t
.begin
+= i
+ sep
.length
;
209 t
.length
-= i
+ sep
.length
;
212 return css::uno::Any(comphelper::containerToSequence(seq
));
215 css::uno::Any
parseValue(
216 OString
const & separator
, xmlreader::Span
const & text
, Type type
)
220 throw css::uno::RuntimeException("invalid value of type any");
222 return parseSingleValue
< sal_Bool
>(text
);
224 return parseSingleValue
< sal_Int16
>(text
);
226 return parseSingleValue
< sal_Int32
>(text
);
228 return parseSingleValue
< sal_Int64
>(text
);
230 return parseSingleValue
< double >(text
);
232 return parseSingleValue
< OUString
>(text
);
234 return parseSingleValue
< css::uno::Sequence
< sal_Int8
> >(text
);
235 case TYPE_BOOLEAN_LIST
:
236 return parseListValue
< sal_Bool
>(separator
, text
);
237 case TYPE_SHORT_LIST
:
238 return parseListValue
< sal_Int16
>(separator
, text
);
240 return parseListValue
< sal_Int32
>(separator
, text
);
242 return parseListValue
< sal_Int64
>(separator
, text
);
243 case TYPE_DOUBLE_LIST
:
244 return parseListValue
< double >(separator
, text
);
245 case TYPE_STRING_LIST
:
246 return parseListValue
< OUString
>(separator
, text
);
247 case TYPE_HEXBINARY_LIST
:
248 return parseListValue
< css::uno::Sequence
< sal_Int8
> >(
252 throw css::uno::RuntimeException("this cannot happen");
258 ValueParser::ValueParser(int layer
): type_(TYPE_ERROR
), layer_(layer
), state_() {}
260 ValueParser::~ValueParser() {}
262 xmlreader::XmlReader::Text
ValueParser::getTextMode() const {
264 return xmlreader::XmlReader::Text::NONE
;
268 if (!items_
.empty()) {
274 (type_
== TYPE_STRING
|| type_
== TYPE_STRING_LIST
||
275 !separator_
.isEmpty())
276 ? xmlreader::XmlReader::Text::Raw
277 : xmlreader::XmlReader::Text::Normalized
;
281 return xmlreader::XmlReader::Text::NONE
;
284 bool ValueParser::startElement(
285 xmlreader::XmlReader
& reader
, int nsId
, xmlreader::Span
const & name
)
292 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&& name
== "it" &&
293 isListType(type_
) && separator_
.isEmpty())
296 // before first <it>, characters are not ignored; assume they
297 // are only whitespace
303 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
305 (type_
== TYPE_STRING
|| type_
== TYPE_STRING_LIST
))
307 sal_Int32 scalar
= -1;
310 xmlreader::Span attrLn
;
311 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
314 if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
317 if (!parseValue(reader
.getAttributeValue(true), &scalar
)) {
323 if (scalar
>= 0 && scalar
< 0x20 && scalar
!= 0x09 &&
324 scalar
!= 0x0A && scalar
!= 0x0D)
326 char c
= static_cast< char >(scalar
);
328 } else if (scalar
== 0xFFFE) {
329 pad_
.add(RTL_CONSTASCII_STRINGPARAM("\xEF\xBF\xBE"));
330 } else if (scalar
== 0xFFFF) {
331 pad_
.add(RTL_CONSTASCII_STRINGPARAM("\xEF\xBF\xBF"));
333 throw css::uno::RuntimeException(
334 "bad unicode scalar attribute in " + reader
.getUrl());
336 state_
= state_
== State::Text
? State::TextUnicode
: State::ITUnicode
;
343 throw css::uno::RuntimeException(
344 "bad member <" + name
.convertFromUtf8() + "> in " + reader
.getUrl());
347 bool ValueParser::endElement() {
354 css::uno::Any
*pValue
= nullptr;
356 switch (node_
->kind()) {
357 case Node::KIND_PROPERTY
:
358 pValue
= static_cast< PropertyNode
* >(node_
.get())->getValuePtr(layer_
, layer_
== Data::NO_LAYER
);
360 case Node::KIND_LOCALIZED_PROPERTY
:
362 NodeMap
& members
= node_
->getMembers();
363 auto [i
, bInserted
] = members
.insert(NodeMap::value_type(localizedName_
, nullptr));
364 LocalizedValueNode
*pLVNode
;
366 pLVNode
= new LocalizedValueNode(layer_
);
369 pLVNode
= static_cast< LocalizedValueNode
* >(i
->second
.get());
371 pValue
= pLVNode
->getValuePtr(layer_
, layer_
== Data::NO_LAYER
);
375 assert(false); // this cannot happen
379 if (items_
.empty()) {
380 *pValue
= parseValue(separator_
, pad_
.get(), type_
);
384 case TYPE_BOOLEAN_LIST
:
385 *pValue
= convertItems
< sal_Bool
>();
387 case TYPE_SHORT_LIST
:
388 *pValue
= convertItems
< sal_Int16
>();
391 *pValue
= convertItems
< sal_Int32
>();
394 *pValue
= convertItems
< sal_Int64
>();
396 case TYPE_DOUBLE_LIST
:
397 *pValue
= convertItems
< double >();
399 case TYPE_STRING_LIST
:
400 *pValue
= convertItems
< OUString
>();
402 case TYPE_HEXBINARY_LIST
:
403 *pValue
= convertItems
< css::uno::Sequence
< sal_Int8
> >();
406 assert(false); // this cannot happen
415 case State::TextUnicode
:
416 state_
= State::Text
;
418 case State::ITUnicode
:
423 parseValue(OString(), pad_
.get(), elementType(type_
)));
425 state_
= State::Text
;
431 void ValueParser::characters(xmlreader::Span
const & text
) {
433 assert(state_
== State::Text
|| state_
== State::IT
);
434 pad_
.add(text
.begin
, text
.length
);
438 void ValueParser::start(
439 rtl::Reference
< Node
> const & node
, OUString
const & localizedName
)
441 assert(node
.is() && !node_
.is());
443 localizedName_
= localizedName
;
444 state_
= State::Text
;
448 template< typename T
> css::uno::Any
ValueParser::convertItems() {
449 css::uno::Sequence
< T
> seq(items_
.size());
450 auto seqRange
= asNonConstRange(seq
);
451 for (sal_Int32 i
= 0; i
< seq
.getLength(); ++i
) {
452 bool ok
= (items_
[i
] >>= seqRange
[i
]);
454 (void) ok
; // avoid warnings
456 return css::uno::Any(seq
);
461 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */