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>
25 #include <com/sun/star/uno/Any.hxx>
26 #include <com/sun/star/uno/RuntimeException.hpp>
27 #include <rtl/ref.hxx>
28 #include <rtl/strbuf.hxx>
29 #include <rtl/string.hxx>
30 #include <rtl/ustring.hxx>
31 #include <xmlreader/span.hxx>
32 #include <xmlreader/xmlreader.hxx>
35 #include "localizedpropertynode.hxx"
36 #include "groupnode.hxx"
38 #include "nodemap.hxx"
39 #include "parsemanager.hxx"
40 #include "propertynode.hxx"
41 #include "setnode.hxx"
42 #include "xcsparser.hxx"
43 #include "xmldata.hxx"
49 // Conservatively merge a template or component (and its recursive parts) into
50 // an existing instance:
52 rtl::Reference
< Node
> const & original
,
53 rtl::Reference
< Node
> const & update
)
56 original
.is() && update
.is() && original
->kind() == update
->kind() &&
57 update
->getFinalized() == Data::NO_LAYER
);
58 if (update
->getLayer() < original
->getLayer() ||
59 update
->getLayer() > original
->getFinalized())
62 switch (original
->kind()) {
63 case Node::KIND_PROPERTY
:
64 case Node::KIND_LOCALIZED_PROPERTY
:
65 case Node::KIND_LOCALIZED_VALUE
:
66 break; //TODO: merge certain parts?
67 case Node::KIND_GROUP
:
68 for (auto const& updateMember
: update
->getMembers())
70 NodeMap
& members
= original
->getMembers();
71 NodeMap::iterator
i1(members
.find(updateMember
.first
));
72 if (i1
== members
.end()) {
73 if (updateMember
.second
->kind() == Node::KIND_PROPERTY
&&
74 static_cast< GroupNode
* >(
75 original
.get())->isExtensible())
77 members
.insert(updateMember
);
79 } else if (updateMember
.second
->kind() == i1
->second
->kind()) {
80 merge(i1
->second
, updateMember
.second
);
85 for (auto const& updateMember
: update
->getMembers())
87 NodeMap
& members
= original
->getMembers();
88 NodeMap::iterator
i1(members
.find(updateMember
.first
));
89 if (i1
== members
.end()) {
90 if (static_cast< SetNode
* >(original
.get())->
91 isValidTemplate(updateMember
.second
->getTemplateName()))
93 members
.insert(updateMember
);
95 } else if (updateMember
.second
->kind() == i1
->second
->kind() &&
96 (updateMember
.second
->getTemplateName() ==
97 i1
->second
->getTemplateName()))
99 merge(i1
->second
, updateMember
.second
);
103 case Node::KIND_ROOT
:
104 assert(false); // this cannot happen
111 XcsParser::XcsParser(int layer
, Data
& data
):
112 valueParser_(layer
), data_(data
), state_(STATE_START
), ignoring_(), bIsParsingInfo_(false)
115 XcsParser::~XcsParser() {}
117 xmlreader::XmlReader::Text
XcsParser::getTextMode() {
119 return xmlreader::XmlReader::Text::Raw
;
120 return valueParser_
.getTextMode();
123 bool XcsParser::startElement(
124 xmlreader::XmlReader
& reader
, int nsId
, xmlreader::Span
const & name
,
125 std::set
< OUString
> const * /*existingDependencies*/)
127 //TODO: ignoring component-schema import, component-schema uses, and
128 // prop constraints; accepting all four at illegal places (and with
131 || (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
132 && (name
== "import" || name
== "uses" || name
== "constraints" || name
== "desc")))
134 assert(ignoring_
< LONG_MAX
);
141 if (valueParser_
.startElement(reader
, nsId
, name
)) {
144 if (state_
== STATE_START
) {
145 if (nsId
== ParseManager::NAMESPACE_OOR
&&
146 name
== "component-schema")
148 handleComponentSchema(reader
);
149 state_
= STATE_COMPONENT_SCHEMA
;
155 case STATE_COMPONENT_SCHEMA
:
156 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
159 state_
= STATE_TEMPLATES
;
162 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
165 bIsParsingInfo_
= true;
169 case STATE_TEMPLATES_DONE
:
170 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
173 state_
= STATE_COMPONENT
;
174 assert(elements_
.empty());
177 new GroupNode(valueParser_
.getLayer(), false, ""),
182 case STATE_TEMPLATES
:
183 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
186 bIsParsingInfo_
= true;
189 if (elements_
.empty()) {
190 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
193 handleGroup(reader
, true);
196 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
199 handleSet(reader
, true);
202 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
205 bIsParsingInfo_
= true;
211 case STATE_COMPONENT
:
212 assert(!elements_
.empty());
213 switch (elements_
.top().node
->kind()) {
214 case Node::KIND_PROPERTY
:
215 case Node::KIND_LOCALIZED_PROPERTY
:
216 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
219 handlePropValue(reader
, elements_
.top().node
);
222 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
225 bIsParsingInfo_
= true;
229 case Node::KIND_GROUP
:
230 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
236 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
239 handleNodeRef(reader
);
242 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
245 handleGroup(reader
, false);
248 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
251 handleSet(reader
, false);
254 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
257 bIsParsingInfo_
= true;
262 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
267 static_cast< SetNode
* >(elements_
.top().node
.get()));
270 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
273 bIsParsingInfo_
= true;
277 default: // Node::KIND_LOCALIZED_VALUE
278 assert(false); // this cannot happen
282 case STATE_COMPONENT_DONE
:
284 default: // STATE_START
285 assert(false); // this cannot happen
289 throw css::uno::RuntimeException(
290 "bad member <" + name
.convertFromUtf8() + "> in " + reader
.getUrl());
293 void XcsParser::endElement(xmlreader::XmlReader
const & reader
) {
300 bIsParsingInfo_
= false;
303 if (valueParser_
.endElement()) {
306 if (!elements_
.empty()) {
307 Element
top(std::move(elements_
.top()));
310 if (top
.node
->kind() == Node::KIND_PROPERTY
311 || top
.node
->kind() == Node::KIND_LOCALIZED_PROPERTY
)
313 // Remove whitespace from description_ resulting from line breaks/indentation in xml files
314 OUString
desc(description_
.makeStringAndClear());
316 while (desc
.indexOf(" ") != -1)
317 desc
= desc
.replaceAll(" ", " ");
318 top
.node
->setDescription(desc
);
320 if (elements_
.empty()) {
322 case STATE_TEMPLATES
:
324 auto itPair
= data_
.templates
.insert({top
.name
, top
.node
});
325 if (!itPair
.second
) {
326 merge(itPair
.first
->second
, top
.node
);
330 case STATE_COMPONENT
:
332 NodeMap
& components
= data_
.getComponents();
333 auto itPair
= components
.insert({top
.name
, top
.node
});
334 if (!itPair
.second
) {
335 merge(itPair
.first
->second
, top
.node
);
337 state_
= STATE_COMPONENT_DONE
;
342 throw css::uno::RuntimeException(
343 "this cannot happen");
346 if (!elements_
.top().node
->getMembers().insert(
347 NodeMap::value_type(top
.name
, top
.node
)).second
)
349 throw css::uno::RuntimeException(
350 "duplicate " + top
.name
+ " in " + reader
.getUrl());
356 case STATE_COMPONENT_SCHEMA
:
357 // To support old, broken extensions with .xcs files that contain
358 // empty <component-schema> elements:
359 state_
= STATE_COMPONENT_DONE
;
361 case STATE_TEMPLATES
:
362 state_
= STATE_TEMPLATES_DONE
;
364 case STATE_TEMPLATES_DONE
:
365 throw css::uno::RuntimeException(
366 "no component element in " + reader
.getUrl());
367 case STATE_COMPONENT_DONE
:
370 assert(false); // this cannot happen
375 void XcsParser::characters(xmlreader::Span
const & text
) {
378 description_
.append(text
.convertFromUtf8());
381 valueParser_
.characters(text
);
384 void XcsParser::handleComponentSchema(xmlreader::XmlReader
& reader
) {
385 //TODO: oor:version, xml:lang attributes
386 OStringBuffer
buf(256);
388 bool hasPackage
= false;
389 bool hasName
= false;
392 xmlreader::Span attrLn
;
393 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
396 if (attrNsId
== ParseManager::NAMESPACE_OOR
&& attrLn
== "package")
399 throw css::uno::RuntimeException(
400 "multiple component-schema package attributes in " +
404 xmlreader::Span
s(reader
.getAttributeValue(false));
405 buf
.insert(0, s
.begin
, s
.length
);
406 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
410 throw css::uno::RuntimeException(
411 "multiple component-schema name attributes in " +
415 xmlreader::Span
s(reader
.getAttributeValue(false));
416 buf
.append(s
.begin
, s
.length
);
420 throw css::uno::RuntimeException(
421 "no component-schema package attribute in " + reader
.getUrl());
424 throw css::uno::RuntimeException(
425 "no component-schema name attribute in " + reader
.getUrl());
427 componentName_
= xmlreader::Span(buf
.getStr(), buf
.getLength()).
431 void XcsParser::handleNodeRef(xmlreader::XmlReader
& reader
) {
432 bool hasName
= false;
434 OUString
component(componentName_
);
435 bool hasNodeType
= false;
439 xmlreader::Span attrLn
;
440 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
443 if (attrNsId
== ParseManager::NAMESPACE_OOR
&& attrLn
== "name") {
445 name
= reader
.getAttributeValue(false).convertFromUtf8();
446 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
447 attrLn
== "component")
449 component
= reader
.getAttributeValue(false).convertFromUtf8();
450 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
451 attrLn
== "node-type")
454 nodeType
= reader
.getAttributeValue(false).convertFromUtf8();
458 throw css::uno::RuntimeException(
459 "no node-ref name attribute in " + reader
.getUrl());
461 rtl::Reference
< Node
> tmpl(
463 valueParser_
.getLayer(),
464 xmldata::parseTemplateReference(
465 component
, hasNodeType
, nodeType
, nullptr)));
467 //TODO: this can erroneously happen as long as import/uses attributes
468 // are not correctly processed
469 throw css::uno::RuntimeException(
470 "unknown node-ref " + name
+ " in " + reader
.getUrl());
472 rtl::Reference
< Node
> node(tmpl
->clone(false));
473 node
->setLayer(valueParser_
.getLayer());
474 elements_
.push(Element(node
, name
));
477 void XcsParser::handleProp(xmlreader::XmlReader
& reader
) {
478 bool hasName
= false;
480 valueParser_
.type_
= TYPE_ERROR
;
481 bool localized
= false;
482 bool nillable
= true;
485 xmlreader::Span attrLn
;
486 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
489 if (attrNsId
== ParseManager::NAMESPACE_OOR
&& attrLn
== "name") {
491 name
= reader
.getAttributeValue(false).convertFromUtf8();
492 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
495 valueParser_
.type_
= xmldata::parseType(
496 reader
, reader
.getAttributeValue(true));
497 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
498 attrLn
== "localized")
500 localized
= xmldata::parseBoolean(reader
.getAttributeValue(true));
501 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
502 attrLn
== "nillable")
504 nillable
= xmldata::parseBoolean(reader
.getAttributeValue(true));
508 throw css::uno::RuntimeException(
509 "no prop name attribute in " + reader
.getUrl());
511 if (valueParser_
.type_
== TYPE_ERROR
) {
512 throw css::uno::RuntimeException(
513 "no prop type attribute in " + reader
.getUrl());
518 ? rtl::Reference
< Node
>(
519 new LocalizedPropertyNode(
520 valueParser_
.getLayer(), valueParser_
.type_
, nillable
))
521 : rtl::Reference
< Node
>(
523 valueParser_
.getLayer(), valueParser_
.type_
, nillable
,
524 css::uno::Any(), false))),
528 void XcsParser::handlePropValue(
529 xmlreader::XmlReader
& reader
, rtl::Reference
< Node
> const & property
)
531 xmlreader::Span attrSeparator
;
534 xmlreader::Span attrLn
;
535 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
538 if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
539 attrLn
== "separator")
541 attrSeparator
= reader
.getAttributeValue(false);
542 if (attrSeparator
.length
== 0) {
543 throw css::uno::RuntimeException(
544 "bad oor:separator attribute in " + reader
.getUrl());
548 valueParser_
.separator_
= OString(
549 attrSeparator
.begin
, attrSeparator
.length
);
550 valueParser_
.start(property
);
553 void XcsParser::handleGroup(xmlreader::XmlReader
& reader
, bool isTemplate
) {
554 bool hasName
= false;
556 bool extensible
= false;
559 xmlreader::Span attrLn
;
560 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
563 if (attrNsId
== ParseManager::NAMESPACE_OOR
&& attrLn
== "name") {
565 name
= reader
.getAttributeValue(false).convertFromUtf8();
566 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
567 attrLn
== "extensible")
569 extensible
= xmldata::parseBoolean(reader
.getAttributeValue(true));
573 throw css::uno::RuntimeException(
574 "no group name attribute in " + reader
.getUrl());
577 name
= Data::fullTemplateName(componentName_
, name
);
582 valueParser_
.getLayer(), extensible
,
583 isTemplate
? name
: OUString()),
587 void XcsParser::handleSet(xmlreader::XmlReader
& reader
, bool isTemplate
) {
588 bool hasName
= false;
590 OUString
component(componentName_
);
591 bool hasNodeType
= false;
595 xmlreader::Span attrLn
;
596 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
599 if (attrNsId
== ParseManager::NAMESPACE_OOR
&& attrLn
== "name") {
601 name
= reader
.getAttributeValue(false).convertFromUtf8();
602 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
603 attrLn
== "component")
605 component
= reader
.getAttributeValue(false).convertFromUtf8();
606 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
607 attrLn
== "node-type")
610 nodeType
= reader
.getAttributeValue(false).convertFromUtf8();
614 throw css::uno::RuntimeException(
615 "no set name attribute in " + reader
.getUrl());
618 name
= Data::fullTemplateName(componentName_
, name
);
623 valueParser_
.getLayer(),
624 xmldata::parseTemplateReference(
625 component
, hasNodeType
, nodeType
, nullptr),
626 isTemplate
? name
: OUString()),
630 void XcsParser::handleSetItem(xmlreader::XmlReader
& reader
, SetNode
* set
) {
631 OUString
component(componentName_
);
632 bool hasNodeType
= false;
636 xmlreader::Span attrLn
;
637 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
640 if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
641 attrLn
== "component")
643 component
= reader
.getAttributeValue(false).convertFromUtf8();
644 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
645 attrLn
== "node-type")
648 nodeType
= reader
.getAttributeValue(false).convertFromUtf8();
651 set
->getAdditionalTemplateNames().push_back(
652 xmldata::parseTemplateReference(component
, hasNodeType
, nodeType
, nullptr));
653 elements_
.push(Element(rtl::Reference
< Node
>(), ""));
658 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */