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 <rtl/string.h>
29 #include <rtl/string.hxx>
30 #include <rtl/ustring.hxx>
31 #include <sal/types.h>
32 #include <xmlreader/span.hxx>
33 #include <xmlreader/xmlreader.hxx>
35 #include "localizedvaluenode.hxx"
37 #include "nodemap.hxx"
38 #include "parsemanager.hxx"
39 #include "propertynode.hxx"
41 #include "valueparser.hxx"
47 bool parseHexDigit(char c
, int * value
) {
48 assert(value
!= nullptr);
49 if (c
>= '0' && c
<= '9') {
53 if (c
>= 'A' && c
<= 'F') {
54 *value
= c
- 'A' + 10;
57 if (c
>= 'a' && c
<= 'f') {
58 *value
= c
- 'a' + 10;
64 bool parseValue(xmlreader::Span
const & text
, sal_Bool
* value
) {
65 assert(text
.is() && value
!= nullptr);
66 if (text
== "true" || text
== "1") {
70 if (text
== "false" || text
== "0") {
77 bool parseValue(xmlreader::Span
const & text
, sal_Int16
* value
) {
78 assert(text
.is() && value
!= nullptr);
79 // For backwards compatibility, support hexadecimal values:
81 rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
82 text
.begin
, text
.length
, RTL_CONSTASCII_STRINGPARAM("0X"),
83 RTL_CONSTASCII_LENGTH("0X")) == 0 ?
84 static_cast< sal_Int32
>(
86 text
.begin
+ RTL_CONSTASCII_LENGTH("0X"),
87 text
.length
- RTL_CONSTASCII_LENGTH("0X")).toUInt32(16)) :
88 OString(text
.begin
, text
.length
).toInt32();
89 //TODO: check valid lexical representation
90 if (n
>= SAL_MIN_INT16
&& n
<= SAL_MAX_INT16
) {
91 *value
= static_cast< sal_Int16
>(n
);
97 bool parseValue(xmlreader::Span
const & text
, sal_Int32
* value
) {
98 assert(text
.is() && value
!= nullptr);
99 // For backwards compatibility, support hexadecimal values:
101 rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
102 text
.begin
, text
.length
, RTL_CONSTASCII_STRINGPARAM("0X"),
103 RTL_CONSTASCII_LENGTH("0X")) == 0 ?
104 static_cast< sal_Int32
>(
106 text
.begin
+ RTL_CONSTASCII_LENGTH("0X"),
107 text
.length
- RTL_CONSTASCII_LENGTH("0X")).toUInt32(16)) :
108 OString(text
.begin
, text
.length
).toInt32();
109 //TODO: check valid lexical representation
113 bool parseValue(xmlreader::Span
const & text
, sal_Int64
* value
) {
114 assert(text
.is() && value
!= nullptr);
115 // For backwards compatibility, support hexadecimal values:
117 rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
118 text
.begin
, text
.length
, RTL_CONSTASCII_STRINGPARAM("0X"),
119 RTL_CONSTASCII_LENGTH("0X")) == 0 ?
120 static_cast< sal_Int64
>(
122 text
.begin
+ RTL_CONSTASCII_LENGTH("0X"),
123 text
.length
- RTL_CONSTASCII_LENGTH("0X")).toUInt64(16)) :
124 OString(text
.begin
, text
.length
).toInt64();
125 //TODO: check valid lexical representation
129 bool parseValue(xmlreader::Span
const & text
, double * value
) {
130 assert(text
.is() && value
!= nullptr);
131 *value
= OString(text
.begin
, text
.length
).toDouble();
132 //TODO: check valid lexical representation
136 bool parseValue(xmlreader::Span
const & text
, OUString
* value
) {
137 assert(text
.is() && value
!= nullptr);
138 *value
= text
.convertFromUtf8();
143 xmlreader::Span
const & text
, css::uno::Sequence
< sal_Int8
> * value
)
145 assert(text
.is() && value
!= nullptr);
146 if ((text
.length
& 1) != 0) {
149 std::vector
< sal_Int8
> seq
;
150 for (sal_Int32 i
= 0; i
!= text
.length
;) {
153 if (!parseHexDigit(text
.begin
[i
++], &n1
) ||
154 !parseHexDigit(text
.begin
[i
++], &n2
))
158 seq
.push_back(static_cast< sal_Int8
>((n1
<< 4) | n2
));
160 *value
= comphelper::containerToSequence(seq
);
164 template< typename T
> css::uno::Any
parseSingleValue(
165 xmlreader::Span
const & text
)
168 if (!parseValue(text
, &val
)) {
169 throw css::uno::RuntimeException("invalid value");
171 return css::uno::Any(val
);
174 template< typename T
> css::uno::Any
parseListValue(
175 OString
const & separator
, xmlreader::Span
const & text
)
177 std::vector
< T
> seq
;
179 if (separator
.isEmpty()) {
180 sep
= xmlreader::Span(RTL_CONSTASCII_STRINGPARAM(" "));
182 sep
= xmlreader::Span(separator
.getStr(), separator
.getLength());
184 if (text
.length
!= 0) {
185 for (xmlreader::Span
t(text
);;) {
186 sal_Int32 i
= rtl_str_indexOfStr_WithLength(
187 t
.begin
, t
.length
, sep
.begin
, sep
.length
);
190 xmlreader::Span(t
.begin
, i
== -1 ? t
.length
: i
), &val
))
192 throw css::uno::RuntimeException("invalid value");
198 t
.begin
+= i
+ sep
.length
;
199 t
.length
-= i
+ sep
.length
;
202 return css::uno::Any(comphelper::containerToSequence(seq
));
205 css::uno::Any
parseValue(
206 OString
const & separator
, xmlreader::Span
const & text
, Type type
)
210 throw css::uno::RuntimeException("invalid value of type any");
212 return parseSingleValue
< sal_Bool
>(text
);
214 return parseSingleValue
< sal_Int16
>(text
);
216 return parseSingleValue
< sal_Int32
>(text
);
218 return parseSingleValue
< sal_Int64
>(text
);
220 return parseSingleValue
< double >(text
);
222 return parseSingleValue
< OUString
>(text
);
224 return parseSingleValue
< css::uno::Sequence
< sal_Int8
> >(text
);
225 case TYPE_BOOLEAN_LIST
:
226 return parseListValue
< sal_Bool
>(separator
, text
);
227 case TYPE_SHORT_LIST
:
228 return parseListValue
< sal_Int16
>(separator
, text
);
230 return parseListValue
< sal_Int32
>(separator
, text
);
232 return parseListValue
< sal_Int64
>(separator
, text
);
233 case TYPE_DOUBLE_LIST
:
234 return parseListValue
< double >(separator
, text
);
235 case TYPE_STRING_LIST
:
236 return parseListValue
< OUString
>(separator
, text
);
237 case TYPE_HEXBINARY_LIST
:
238 return parseListValue
< css::uno::Sequence
< sal_Int8
> >(
242 throw css::uno::RuntimeException("this cannot happen");
248 ValueParser::ValueParser(int layer
): type_(TYPE_ERROR
), layer_(layer
), state_() {}
250 ValueParser::~ValueParser() {}
252 xmlreader::XmlReader::Text
ValueParser::getTextMode() const {
256 if (!items_
.empty()) {
262 (type_
== TYPE_STRING
|| type_
== TYPE_STRING_LIST
||
263 !separator_
.isEmpty())
264 ? xmlreader::XmlReader::Text::Raw
265 : xmlreader::XmlReader::Text::Normalized
;
270 return xmlreader::XmlReader::Text::NONE
;
273 bool ValueParser::startElement(
274 xmlreader::XmlReader
& reader
, int nsId
, xmlreader::Span
const & name
)
281 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&& name
== "it" &&
282 isListType(type_
) && separator_
.isEmpty())
285 // before first <it>, characters are not ignored; assume they
286 // are only whitespace
292 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
294 (type_
== TYPE_STRING
|| type_
== TYPE_STRING_LIST
))
296 sal_Int32 scalar
= -1;
299 xmlreader::Span attrLn
;
300 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
303 if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
306 if (!parseValue(reader
.getAttributeValue(true), &scalar
)) {
312 if (scalar
>= 0 && scalar
< 0x20 && scalar
!= 0x09 &&
313 scalar
!= 0x0A && scalar
!= 0x0D)
315 char c
= static_cast< char >(scalar
);
317 } else if (scalar
== 0xFFFE) {
318 pad_
.add(RTL_CONSTASCII_STRINGPARAM("\xEF\xBF\xBE"));
319 } else if (scalar
== 0xFFFF) {
320 pad_
.add(RTL_CONSTASCII_STRINGPARAM("\xEF\xBF\xBF"));
322 throw css::uno::RuntimeException(
323 "bad unicode scalar attribute in " + reader
.getUrl());
325 state_
= state_
== State::Text
? State::TextUnicode
: State::ITUnicode
;
332 throw css::uno::RuntimeException(
333 "bad member <" + name
.convertFromUtf8() + "> in " + reader
.getUrl());
336 bool ValueParser::endElement() {
343 css::uno::Any
*pValue
= nullptr;
345 switch (node_
->kind()) {
346 case Node::KIND_PROPERTY
:
347 pValue
= static_cast< PropertyNode
* >(node_
.get())->getValuePtr(layer_
);
349 case Node::KIND_LOCALIZED_PROPERTY
:
351 NodeMap
& members
= node_
->getMembers();
352 NodeMap::iterator
i(members
.find(localizedName_
));
353 LocalizedValueNode
*pLVNode
;
354 if (i
== members
.end()) {
355 pLVNode
= new LocalizedValueNode(layer_
);
357 NodeMap::value_type(localizedName_
, pLVNode
));
359 pLVNode
= static_cast< LocalizedValueNode
* >(i
->second
.get());
361 pValue
= pLVNode
->getValuePtr(layer_
);
365 assert(false); // this cannot happen
369 if (items_
.empty()) {
370 *pValue
= parseValue(separator_
, pad_
.get(), type_
);
374 case TYPE_BOOLEAN_LIST
:
375 *pValue
= convertItems
< sal_Bool
>();
377 case TYPE_SHORT_LIST
:
378 *pValue
= convertItems
< sal_Int16
>();
381 *pValue
= convertItems
< sal_Int32
>();
384 *pValue
= convertItems
< sal_Int64
>();
386 case TYPE_DOUBLE_LIST
:
387 *pValue
= convertItems
< double >();
389 case TYPE_STRING_LIST
:
390 *pValue
= convertItems
< OUString
>();
392 case TYPE_HEXBINARY_LIST
:
393 *pValue
= convertItems
< css::uno::Sequence
< sal_Int8
> >();
396 assert(false); // this cannot happen
405 case State::TextUnicode
:
406 state_
= State::Text
;
408 case State::ITUnicode
:
413 parseValue(OString(), pad_
.get(), elementType(type_
)));
415 state_
= State::Text
;
421 void ValueParser::characters(xmlreader::Span
const & text
) {
423 assert(state_
== State::Text
|| state_
== State::IT
);
424 pad_
.add(text
.begin
, text
.length
);
428 void ValueParser::start(
429 rtl::Reference
< Node
> const & node
, OUString
const & localizedName
)
431 assert(node
.is() && !node_
.is());
433 localizedName_
= localizedName
;
434 state_
= State::Text
;
438 template< typename T
> css::uno::Any
ValueParser::convertItems() {
439 css::uno::Sequence
< T
> seq(items_
.size());
440 for (sal_Int32 i
= 0; i
< seq
.getLength(); ++i
) {
441 bool ok
= (items_
[i
] >>= seq
[i
]);
443 (void) ok
; // avoid warnings
445 return css::uno::Any(seq
);
450 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */