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())
61 switch (original
->kind()) {
62 case Node::KIND_PROPERTY
:
63 case Node::KIND_LOCALIZED_PROPERTY
:
64 case Node::KIND_LOCALIZED_VALUE
:
65 break; //TODO: merge certain parts?
66 case Node::KIND_GROUP
:
67 for (auto const& updateMember
: update
->getMembers())
69 NodeMap
& members
= original
->getMembers();
70 NodeMap::iterator
i1(members
.find(updateMember
.first
));
71 if (i1
== members
.end()) {
72 if (updateMember
.second
->kind() == Node::KIND_PROPERTY
&&
73 static_cast< GroupNode
* >(
74 original
.get())->isExtensible())
76 members
.insert(updateMember
);
78 } else if (updateMember
.second
->kind() == i1
->second
->kind()) {
79 merge(i1
->second
, updateMember
.second
);
84 for (auto const& updateMember
: update
->getMembers())
86 NodeMap
& members
= original
->getMembers();
87 NodeMap::iterator
i1(members
.find(updateMember
.first
));
88 if (i1
== members
.end()) {
89 if (static_cast< SetNode
* >(original
.get())->
90 isValidTemplate(updateMember
.second
->getTemplateName()))
92 members
.insert(updateMember
);
94 } else if (updateMember
.second
->kind() == i1
->second
->kind() &&
95 (updateMember
.second
->getTemplateName() ==
96 i1
->second
->getTemplateName()))
98 merge(i1
->second
, updateMember
.second
);
102 case Node::KIND_ROOT
:
103 assert(false); // this cannot happen
111 XcsParser::XcsParser(int layer
, Data
& data
):
112 valueParser_(layer
), data_(data
), state_(STATE_START
), ignoring_()
115 XcsParser::~XcsParser() {}
117 xmlreader::XmlReader::Text
XcsParser::getTextMode() {
118 return valueParser_
.getTextMode();
121 bool XcsParser::startElement(
122 xmlreader::XmlReader
& reader
, int nsId
, xmlreader::Span
const & name
,
123 std::set
< OUString
> const * /*existingDependencies*/)
125 if (valueParser_
.startElement(reader
, nsId
, name
)) {
128 if (state_
== STATE_START
) {
129 if (nsId
== ParseManager::NAMESPACE_OOR
&&
130 name
== "component-schema")
132 handleComponentSchema(reader
);
133 state_
= STATE_COMPONENT_SCHEMA
;
138 //TODO: ignoring component-schema import, component-schema uses, and
139 // prop constraints; accepting all four at illegal places (and with
142 (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
143 (name
== "info" || name
== "import" ||
144 name
== "uses" || name
== "constraints")))
146 assert(ignoring_
< LONG_MAX
);
151 case STATE_COMPONENT_SCHEMA
:
152 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
155 state_
= STATE_TEMPLATES
;
159 case STATE_TEMPLATES_DONE
:
160 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
163 state_
= STATE_COMPONENT
;
164 assert(elements_
.empty());
167 new GroupNode(valueParser_
.getLayer(), false, ""),
172 case STATE_TEMPLATES
:
173 if (elements_
.empty()) {
174 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
177 handleGroup(reader
, true);
180 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
183 handleSet(reader
, true);
189 case STATE_COMPONENT
:
190 assert(!elements_
.empty());
191 switch (elements_
.top().node
->kind()) {
192 case Node::KIND_PROPERTY
:
193 case Node::KIND_LOCALIZED_PROPERTY
:
194 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
197 handlePropValue(reader
, elements_
.top().node
);
201 case Node::KIND_GROUP
:
202 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
208 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
211 handleNodeRef(reader
);
214 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
217 handleGroup(reader
, false);
220 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
223 handleSet(reader
, false);
228 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
233 static_cast< SetNode
* >(elements_
.top().node
.get()));
237 default: // Node::KIND_LOCALIZED_VALUE
238 assert(false); // this cannot happen
242 case STATE_COMPONENT_DONE
:
244 default: // STATE_START
245 assert(false); // this cannot happen
249 throw css::uno::RuntimeException(
250 "bad member <" + name
.convertFromUtf8() + "> in " + reader
.getUrl());
253 void XcsParser::endElement(xmlreader::XmlReader
const & reader
) {
254 if (valueParser_
.endElement()) {
259 } else if (!elements_
.empty()) {
260 Element
top(std::move(elements_
.top()));
263 if (elements_
.empty()) {
265 case STATE_TEMPLATES
:
267 auto itPair
= data_
.templates
.insert({top
.name
, top
.node
});
268 if (!itPair
.second
) {
269 merge(itPair
.first
->second
, top
.node
);
273 case STATE_COMPONENT
:
275 NodeMap
& components
= data_
.getComponents();
276 auto itPair
= components
.insert({top
.name
, top
.node
});
277 if (!itPair
.second
) {
278 merge(itPair
.first
->second
, top
.node
);
280 state_
= STATE_COMPONENT_DONE
;
285 throw css::uno::RuntimeException(
286 "this cannot happen");
289 if (!elements_
.top().node
->getMembers().insert(
290 NodeMap::value_type(top
.name
, top
.node
)).second
)
292 throw css::uno::RuntimeException(
293 "duplicate " + top
.name
+ " in " + reader
.getUrl());
299 case STATE_COMPONENT_SCHEMA
:
300 // To support old, broken extensions with .xcs files that contain
301 // empty <component-schema> elements:
302 state_
= STATE_COMPONENT_DONE
;
304 case STATE_TEMPLATES
:
305 state_
= STATE_TEMPLATES_DONE
;
307 case STATE_TEMPLATES_DONE
:
308 throw css::uno::RuntimeException(
309 "no component element in " + reader
.getUrl());
310 case STATE_COMPONENT_DONE
:
313 assert(false); // this cannot happen
318 void XcsParser::characters(xmlreader::Span
const & text
) {
319 valueParser_
.characters(text
);
322 void XcsParser::handleComponentSchema(xmlreader::XmlReader
& reader
) {
323 //TODO: oor:version, xml:lang attributes
324 OStringBuffer
buf(256);
326 bool hasPackage
= false;
327 bool hasName
= false;
330 xmlreader::Span attrLn
;
331 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
334 if (attrNsId
== ParseManager::NAMESPACE_OOR
&& attrLn
== "package")
337 throw css::uno::RuntimeException(
338 "multiple component-schema package attributes in " +
342 xmlreader::Span
s(reader
.getAttributeValue(false));
343 buf
.insert(0, s
.begin
, s
.length
);
344 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
348 throw css::uno::RuntimeException(
349 "multiple component-schema name attributes in " +
353 xmlreader::Span
s(reader
.getAttributeValue(false));
354 buf
.append(s
.begin
, s
.length
);
358 throw css::uno::RuntimeException(
359 "no component-schema package attribute in " + reader
.getUrl());
362 throw css::uno::RuntimeException(
363 "no component-schema name attribute in " + reader
.getUrl());
365 componentName_
= xmlreader::Span(buf
.getStr(), buf
.getLength()).
369 void XcsParser::handleNodeRef(xmlreader::XmlReader
& reader
) {
370 bool hasName
= false;
372 OUString
component(componentName_
);
373 bool hasNodeType
= false;
377 xmlreader::Span attrLn
;
378 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
381 if (attrNsId
== ParseManager::NAMESPACE_OOR
&& attrLn
== "name") {
383 name
= reader
.getAttributeValue(false).convertFromUtf8();
384 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
385 attrLn
== "component")
387 component
= reader
.getAttributeValue(false).convertFromUtf8();
388 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
389 attrLn
== "node-type")
392 nodeType
= reader
.getAttributeValue(false).convertFromUtf8();
396 throw css::uno::RuntimeException(
397 "no node-ref name attribute in " + reader
.getUrl());
399 rtl::Reference
< Node
> tmpl(
401 valueParser_
.getLayer(),
402 xmldata::parseTemplateReference(
403 component
, hasNodeType
, nodeType
, nullptr)));
405 //TODO: this can erroneously happen as long as import/uses attributes
406 // are not correctly processed
407 throw css::uno::RuntimeException(
408 "unknown node-ref " + name
+ " in " + reader
.getUrl());
410 rtl::Reference
< Node
> node(tmpl
->clone(false));
411 node
->setLayer(valueParser_
.getLayer());
412 elements_
.push(Element(node
, name
));
415 void XcsParser::handleProp(xmlreader::XmlReader
& reader
) {
416 bool hasName
= false;
418 valueParser_
.type_
= TYPE_ERROR
;
419 bool localized
= false;
420 bool nillable
= true;
423 xmlreader::Span attrLn
;
424 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
427 if (attrNsId
== ParseManager::NAMESPACE_OOR
&& attrLn
== "name") {
429 name
= reader
.getAttributeValue(false).convertFromUtf8();
430 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
433 valueParser_
.type_
= xmldata::parseType(
434 reader
, reader
.getAttributeValue(true));
435 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
436 attrLn
== "localized")
438 localized
= xmldata::parseBoolean(reader
.getAttributeValue(true));
439 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
440 attrLn
== "nillable")
442 nillable
= xmldata::parseBoolean(reader
.getAttributeValue(true));
446 throw css::uno::RuntimeException(
447 "no prop name attribute in " + reader
.getUrl());
449 if (valueParser_
.type_
== TYPE_ERROR
) {
450 throw css::uno::RuntimeException(
451 "no prop type attribute in " + reader
.getUrl());
456 ? rtl::Reference
< Node
>(
457 new LocalizedPropertyNode(
458 valueParser_
.getLayer(), valueParser_
.type_
, nillable
))
459 : rtl::Reference
< Node
>(
461 valueParser_
.getLayer(), valueParser_
.type_
, nillable
,
462 css::uno::Any(), false))),
466 void XcsParser::handlePropValue(
467 xmlreader::XmlReader
& reader
, rtl::Reference
< Node
> const & property
)
469 xmlreader::Span attrSeparator
;
472 xmlreader::Span attrLn
;
473 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
476 if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
477 attrLn
== "separator")
479 attrSeparator
= reader
.getAttributeValue(false);
480 if (attrSeparator
.length
== 0) {
481 throw css::uno::RuntimeException(
482 "bad oor:separator attribute in " + reader
.getUrl());
486 valueParser_
.separator_
= OString(
487 attrSeparator
.begin
, attrSeparator
.length
);
488 valueParser_
.start(property
);
491 void XcsParser::handleGroup(xmlreader::XmlReader
& reader
, bool isTemplate
) {
492 bool hasName
= false;
494 bool extensible
= false;
497 xmlreader::Span attrLn
;
498 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
501 if (attrNsId
== ParseManager::NAMESPACE_OOR
&& attrLn
== "name") {
503 name
= reader
.getAttributeValue(false).convertFromUtf8();
504 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
505 attrLn
== "extensible")
507 extensible
= xmldata::parseBoolean(reader
.getAttributeValue(true));
511 throw css::uno::RuntimeException(
512 "no group name attribute in " + reader
.getUrl());
515 name
= Data::fullTemplateName(componentName_
, name
);
520 valueParser_
.getLayer(), extensible
,
521 isTemplate
? name
: OUString()),
525 void XcsParser::handleSet(xmlreader::XmlReader
& reader
, bool isTemplate
) {
526 bool hasName
= false;
528 OUString
component(componentName_
);
529 bool hasNodeType
= false;
533 xmlreader::Span attrLn
;
534 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
537 if (attrNsId
== ParseManager::NAMESPACE_OOR
&& attrLn
== "name") {
539 name
= reader
.getAttributeValue(false).convertFromUtf8();
540 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
541 attrLn
== "component")
543 component
= reader
.getAttributeValue(false).convertFromUtf8();
544 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
545 attrLn
== "node-type")
548 nodeType
= reader
.getAttributeValue(false).convertFromUtf8();
552 throw css::uno::RuntimeException(
553 "no set name attribute in " + reader
.getUrl());
556 name
= Data::fullTemplateName(componentName_
, name
);
561 valueParser_
.getLayer(),
562 xmldata::parseTemplateReference(
563 component
, hasNodeType
, nodeType
, nullptr),
564 isTemplate
? name
: OUString()),
568 void XcsParser::handleSetItem(xmlreader::XmlReader
& reader
, SetNode
* set
) {
569 OUString
component(componentName_
);
570 bool hasNodeType
= false;
574 xmlreader::Span attrLn
;
575 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
578 if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
579 attrLn
== "component")
581 component
= reader
.getAttributeValue(false).convertFromUtf8();
582 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
583 attrLn
== "node-type")
586 nodeType
= reader
.getAttributeValue(false).convertFromUtf8();
589 set
->getAdditionalTemplateNames().push_back(
590 xmldata::parseTemplateReference(component
, hasNodeType
, nodeType
, nullptr));
591 elements_
.push(Element(rtl::Reference
< Node
>(), ""));
596 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */