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>
29 #include <rtl/string.h>
30 #include <rtl/string.hxx>
31 #include <rtl/ustring.hxx>
32 #include <sal/types.h>
33 #include <xmlreader/span.hxx>
34 #include <xmlreader/xmlreader.hxx>
36 #include "localizedvaluenode.hxx"
38 #include "nodemap.hxx"
39 #include "parsemanager.hxx"
40 #include "propertynode.hxx"
42 #include "valueparser.hxx"
48 bool parseHexDigit(char c
, int * value
) {
49 assert(value
!= nullptr);
50 if (c
>= '0' && c
<= '9') {
54 if (c
>= 'A' && c
<= 'F') {
55 *value
= c
- 'A' + 10;
58 if (c
>= 'a' && c
<= 'f') {
59 *value
= c
- 'a' + 10;
65 bool parseValue(xmlreader::Span
const & text
, sal_Bool
* value
) {
66 assert(text
.is() && value
!= nullptr);
67 if (text
== "true" || text
== "1") {
71 if (text
== "false" || text
== "0") {
78 bool parseValue(xmlreader::Span
const & text
, sal_Int16
* value
) {
79 assert(text
.is() && value
!= nullptr);
80 // For backwards compatibility, support hexadecimal values:
82 rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
83 text
.begin
, text
.length
, RTL_CONSTASCII_STRINGPARAM("0X"),
84 RTL_CONSTASCII_LENGTH("0X")) == 0 ?
85 static_cast< sal_Int32
>(
87 text
.begin
+ RTL_CONSTASCII_LENGTH("0X"),
88 text
.length
- RTL_CONSTASCII_LENGTH("0X")).toUInt32(16)) :
89 OString(text
.begin
, text
.length
).toInt32();
90 //TODO: check valid lexical representation
91 if (n
>= SAL_MIN_INT16
&& n
<= SAL_MAX_INT16
) {
92 *value
= static_cast< sal_Int16
>(n
);
98 bool parseValue(xmlreader::Span
const & text
, sal_Int32
* value
) {
99 assert(text
.is() && value
!= nullptr);
100 // For backwards compatibility, support hexadecimal values:
102 rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
103 text
.begin
, text
.length
, RTL_CONSTASCII_STRINGPARAM("0X"),
104 RTL_CONSTASCII_LENGTH("0X")) == 0 ?
105 static_cast< sal_Int32
>(
107 text
.begin
+ RTL_CONSTASCII_LENGTH("0X"),
108 text
.length
- RTL_CONSTASCII_LENGTH("0X")).toUInt32(16)) :
109 OString(text
.begin
, text
.length
).toInt32();
110 //TODO: check valid lexical representation
114 bool parseValue(xmlreader::Span
const & text
, sal_Int64
* value
) {
115 assert(text
.is() && value
!= nullptr);
116 // For backwards compatibility, support hexadecimal values:
118 rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
119 text
.begin
, text
.length
, RTL_CONSTASCII_STRINGPARAM("0X"),
120 RTL_CONSTASCII_LENGTH("0X")) == 0 ?
121 static_cast< sal_Int64
>(
123 text
.begin
+ RTL_CONSTASCII_LENGTH("0X"),
124 text
.length
- RTL_CONSTASCII_LENGTH("0X")).toUInt64(16)) :
125 OString(text
.begin
, text
.length
).toInt64();
126 //TODO: check valid lexical representation
130 bool parseValue(xmlreader::Span
const & text
, double * value
) {
131 assert(text
.is() && value
!= nullptr);
132 *value
= rtl_math_stringToDouble(
133 text
.begin
, text
.begin
+ text
.length
, '.', 0, nullptr, nullptr);
134 //TODO: check valid lexical representation
138 bool parseValue(xmlreader::Span
const & text
, OUString
* value
) {
139 assert(text
.is() && value
!= nullptr);
140 *value
= text
.convertFromUtf8();
145 xmlreader::Span
const & text
, css::uno::Sequence
< sal_Int8
> * value
)
147 assert(text
.is() && value
!= nullptr);
148 if ((text
.length
& 1) != 0) {
151 std::vector
< sal_Int8
> seq
;
152 for (sal_Int32 i
= 0; i
!= text
.length
;) {
155 if (!parseHexDigit(text
.begin
[i
++], &n1
) ||
156 !parseHexDigit(text
.begin
[i
++], &n2
))
160 seq
.push_back(static_cast< sal_Int8
>((n1
<< 4) | n2
));
162 *value
= comphelper::containerToSequence(seq
);
166 template< typename T
> css::uno::Any
parseSingleValue(
167 xmlreader::Span
const & text
)
170 if (!parseValue(text
, &val
)) {
171 throw css::uno::RuntimeException("invalid value");
173 return css::uno::Any(val
);
176 template< typename T
> css::uno::Any
parseListValue(
177 OString
const & separator
, xmlreader::Span
const & text
)
179 std::vector
< T
> seq
;
181 if (separator
.isEmpty()) {
182 sep
= xmlreader::Span(RTL_CONSTASCII_STRINGPARAM(" "));
184 sep
= xmlreader::Span(separator
.getStr(), separator
.getLength());
186 if (text
.length
!= 0) {
187 for (xmlreader::Span
t(text
);;) {
188 sal_Int32 i
= rtl_str_indexOfStr_WithLength(
189 t
.begin
, t
.length
, sep
.begin
, sep
.length
);
192 xmlreader::Span(t
.begin
, i
== -1 ? t
.length
: i
), &val
))
194 throw css::uno::RuntimeException("invalid value");
200 t
.begin
+= i
+ sep
.length
;
201 t
.length
-= i
+ sep
.length
;
204 return css::uno::Any(comphelper::containerToSequence(seq
));
207 css::uno::Any
parseValue(
208 OString
const & separator
, xmlreader::Span
const & text
, Type type
)
212 throw css::uno::RuntimeException("invalid value of type any");
214 return parseSingleValue
< sal_Bool
>(text
);
216 return parseSingleValue
< sal_Int16
>(text
);
218 return parseSingleValue
< sal_Int32
>(text
);
220 return parseSingleValue
< sal_Int64
>(text
);
222 return parseSingleValue
< double >(text
);
224 return parseSingleValue
< OUString
>(text
);
226 return parseSingleValue
< css::uno::Sequence
< sal_Int8
> >(text
);
227 case TYPE_BOOLEAN_LIST
:
228 return parseListValue
< sal_Bool
>(separator
, text
);
229 case TYPE_SHORT_LIST
:
230 return parseListValue
< sal_Int16
>(separator
, text
);
232 return parseListValue
< sal_Int32
>(separator
, text
);
234 return parseListValue
< sal_Int64
>(separator
, text
);
235 case TYPE_DOUBLE_LIST
:
236 return parseListValue
< double >(separator
, text
);
237 case TYPE_STRING_LIST
:
238 return parseListValue
< OUString
>(separator
, text
);
239 case TYPE_HEXBINARY_LIST
:
240 return parseListValue
< css::uno::Sequence
< sal_Int8
> >(
244 throw css::uno::RuntimeException("this cannot happen");
250 ValueParser::ValueParser(int layer
): type_(TYPE_ERROR
), layer_(layer
), state_() {}
252 ValueParser::~ValueParser() {}
254 xmlreader::XmlReader::Text
ValueParser::getTextMode() const {
256 return xmlreader::XmlReader::Text::NONE
;
260 if (!items_
.empty()) {
266 (type_
== TYPE_STRING
|| type_
== TYPE_STRING_LIST
||
267 !separator_
.isEmpty())
268 ? xmlreader::XmlReader::Text::Raw
269 : xmlreader::XmlReader::Text::Normalized
;
273 return xmlreader::XmlReader::Text::NONE
;
276 bool ValueParser::startElement(
277 xmlreader::XmlReader
& reader
, int nsId
, xmlreader::Span
const & name
)
284 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&& name
== "it" &&
285 isListType(type_
) && separator_
.isEmpty())
288 // before first <it>, characters are not ignored; assume they
289 // are only whitespace
295 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
297 (type_
== TYPE_STRING
|| type_
== TYPE_STRING_LIST
))
299 sal_Int32 scalar
= -1;
302 xmlreader::Span attrLn
;
303 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
306 if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
309 if (!parseValue(reader
.getAttributeValue(true), &scalar
)) {
315 if (scalar
>= 0 && scalar
< 0x20 && scalar
!= 0x09 &&
316 scalar
!= 0x0A && scalar
!= 0x0D)
318 char c
= static_cast< char >(scalar
);
320 } else if (scalar
== 0xFFFE) {
321 pad_
.add(RTL_CONSTASCII_STRINGPARAM("\xEF\xBF\xBE"));
322 } else if (scalar
== 0xFFFF) {
323 pad_
.add(RTL_CONSTASCII_STRINGPARAM("\xEF\xBF\xBF"));
325 throw css::uno::RuntimeException(
326 "bad unicode scalar attribute in " + reader
.getUrl());
328 state_
= state_
== State::Text
? State::TextUnicode
: State::ITUnicode
;
335 throw css::uno::RuntimeException(
336 "bad member <" + name
.convertFromUtf8() + "> in " + reader
.getUrl());
339 bool ValueParser::endElement() {
346 css::uno::Any
*pValue
= nullptr;
348 switch (node_
->kind()) {
349 case Node::KIND_PROPERTY
:
350 pValue
= static_cast< PropertyNode
* >(node_
.get())->getValuePtr(layer_
);
352 case Node::KIND_LOCALIZED_PROPERTY
:
354 NodeMap
& members
= node_
->getMembers();
355 NodeMap::iterator
i(members
.find(localizedName_
));
356 LocalizedValueNode
*pLVNode
;
357 if (i
== members
.end()) {
358 pLVNode
= new LocalizedValueNode(layer_
);
360 NodeMap::value_type(localizedName_
, pLVNode
));
362 pLVNode
= static_cast< LocalizedValueNode
* >(i
->second
.get());
364 pValue
= pLVNode
->getValuePtr(layer_
);
368 assert(false); // this cannot happen
372 if (items_
.empty()) {
373 *pValue
= parseValue(separator_
, pad_
.get(), type_
);
377 case TYPE_BOOLEAN_LIST
:
378 *pValue
= convertItems
< sal_Bool
>();
380 case TYPE_SHORT_LIST
:
381 *pValue
= convertItems
< sal_Int16
>();
384 *pValue
= convertItems
< sal_Int32
>();
387 *pValue
= convertItems
< sal_Int64
>();
389 case TYPE_DOUBLE_LIST
:
390 *pValue
= convertItems
< double >();
392 case TYPE_STRING_LIST
:
393 *pValue
= convertItems
< OUString
>();
395 case TYPE_HEXBINARY_LIST
:
396 *pValue
= convertItems
< css::uno::Sequence
< sal_Int8
> >();
399 assert(false); // this cannot happen
408 case State::TextUnicode
:
409 state_
= State::Text
;
411 case State::ITUnicode
:
416 parseValue(OString(), pad_
.get(), elementType(type_
)));
418 state_
= State::Text
;
424 void ValueParser::characters(xmlreader::Span
const & text
) {
426 assert(state_
== State::Text
|| state_
== State::IT
);
427 pad_
.add(text
.begin
, text
.length
);
431 void ValueParser::start(
432 rtl::Reference
< Node
> const & node
, OUString
const & localizedName
)
434 assert(node
.is() && !node_
.is());
436 localizedName_
= localizedName
;
437 state_
= State::Text
;
441 template< typename T
> css::uno::Any
ValueParser::convertItems() {
442 css::uno::Sequence
< T
> seq(items_
.size());
443 auto seqRange
= asNonConstRange(seq
);
444 for (sal_Int32 i
= 0; i
< seq
.getLength(); ++i
) {
445 bool ok
= (items_
[i
] >>= seqRange
[i
]);
447 (void) ok
; // avoid warnings
449 return css::uno::Any(seq
);
454 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */