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>
26 #include <com/sun/star/uno/Any.hxx>
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <com/sun/star/uno/RuntimeException.hpp>
29 #include <com/sun/star/uno/XInterface.hpp>
30 #include <rtl/ref.hxx>
31 #include <rtl/strbuf.hxx>
32 #include <rtl/string.hxx>
33 #include <rtl/ustring.hxx>
34 #include <xmlreader/span.hxx>
35 #include <xmlreader/xmlreader.hxx>
38 #include "localizedpropertynode.hxx"
39 #include "groupnode.hxx"
41 #include "nodemap.hxx"
42 #include "parsemanager.hxx"
43 #include "propertynode.hxx"
44 #include "setnode.hxx"
45 #include "xcsparser.hxx"
46 #include "xmldata.hxx"
52 // Conservatively merge a template or component (and its recursive parts) into
53 // an existing instance:
55 rtl::Reference
< Node
> const & original
,
56 rtl::Reference
< Node
> const & update
)
59 original
.is() && update
.is() && original
->kind() == update
->kind() &&
60 update
->getFinalized() == Data::NO_LAYER
);
61 if (update
->getLayer() >= original
->getLayer() &&
62 update
->getLayer() <= original
->getFinalized())
64 switch (original
->kind()) {
65 case Node::KIND_PROPERTY
:
66 case Node::KIND_LOCALIZED_PROPERTY
:
67 case Node::KIND_LOCALIZED_VALUE
:
68 break; //TODO: merge certain parts?
69 case Node::KIND_GROUP
:
70 for (auto const& updateMember
: update
->getMembers())
72 NodeMap
& members
= original
->getMembers();
73 NodeMap::iterator
i1(members
.find(updateMember
.first
));
74 if (i1
== members
.end()) {
75 if (updateMember
.second
->kind() == Node::KIND_PROPERTY
&&
76 static_cast< GroupNode
* >(
77 original
.get())->isExtensible())
79 members
.insert(updateMember
);
81 } else if (updateMember
.second
->kind() == i1
->second
->kind()) {
82 merge(i1
->second
, updateMember
.second
);
87 for (auto const& updateMember
: update
->getMembers())
89 NodeMap
& members
= original
->getMembers();
90 NodeMap::iterator
i1(members
.find(updateMember
.first
));
91 if (i1
== members
.end()) {
92 if (static_cast< SetNode
* >(original
.get())->
93 isValidTemplate(updateMember
.second
->getTemplateName()))
95 members
.insert(updateMember
);
97 } else if (updateMember
.second
->kind() == i1
->second
->kind() &&
98 (updateMember
.second
->getTemplateName() ==
99 i1
->second
->getTemplateName()))
101 merge(i1
->second
, updateMember
.second
);
105 case Node::KIND_ROOT
:
106 assert(false); // this cannot happen
114 XcsParser::XcsParser(int layer
, Data
& data
):
115 valueParser_(layer
), data_(data
), state_(STATE_START
), ignoring_()
118 XcsParser::~XcsParser() {}
120 xmlreader::XmlReader::Text
XcsParser::getTextMode() {
121 return valueParser_
.getTextMode();
124 bool XcsParser::startElement(
125 xmlreader::XmlReader
& reader
, int nsId
, xmlreader::Span
const & name
,
126 std::set
< OUString
> const * /*existingDependencies*/)
128 if (valueParser_
.startElement(reader
, nsId
, name
)) {
131 if (state_
== STATE_START
) {
132 if (nsId
== ParseManager::NAMESPACE_OOR
&&
133 name
.equals("component-schema"))
135 handleComponentSchema(reader
);
136 state_
= STATE_COMPONENT_SCHEMA
;
141 //TODO: ignoring component-schema import, component-schema uses, and
142 // prop constraints; accepting all four at illegal places (and with
145 (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
146 (name
.equals("info") || name
.equals("import") ||
147 name
.equals("uses") || name
.equals("constraints"))))
149 assert(ignoring_
< LONG_MAX
);
154 case STATE_COMPONENT_SCHEMA
:
155 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
156 name
.equals("templates"))
158 state_
= STATE_TEMPLATES
;
162 case STATE_TEMPLATES_DONE
:
163 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
164 name
.equals("component"))
166 state_
= STATE_COMPONENT
;
167 assert(elements_
.empty());
170 new GroupNode(valueParser_
.getLayer(), false, ""),
175 case STATE_TEMPLATES
:
176 if (elements_
.empty()) {
177 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
178 name
.equals("group"))
180 handleGroup(reader
, true);
183 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
186 handleSet(reader
, true);
192 case STATE_COMPONENT
:
193 assert(!elements_
.empty());
194 switch (elements_
.top().node
->kind()) {
195 case Node::KIND_PROPERTY
:
196 case Node::KIND_LOCALIZED_PROPERTY
:
197 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
198 name
.equals("value"))
200 handlePropValue(reader
, elements_
.top().node
);
204 case Node::KIND_GROUP
:
205 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
211 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
212 name
.equals("node-ref"))
214 handleNodeRef(reader
);
217 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
218 name
.equals("group"))
220 handleGroup(reader
, false);
223 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
226 handleSet(reader
, false);
231 if (nsId
== xmlreader::XmlReader::NAMESPACE_NONE
&&
236 static_cast< SetNode
* >(elements_
.top().node
.get()));
240 default: // Node::KIND_LOCALIZED_VALUE
241 assert(false); // this cannot happen
245 case STATE_COMPONENT_DONE
:
247 default: // STATE_START
248 assert(false); // this cannot happen
252 throw css::uno::RuntimeException(
253 "bad member <" + name
.convertFromUtf8() + "> in " + reader
.getUrl());
256 void XcsParser::endElement(xmlreader::XmlReader
const & reader
) {
257 if (valueParser_
.endElement()) {
262 } else if (!elements_
.empty()) {
263 Element
top(elements_
.top());
266 if (elements_
.empty()) {
268 case STATE_TEMPLATES
:
270 auto itPair
= data_
.templates
.insert({top
.name
, top
.node
});
271 if (!itPair
.second
) {
272 merge(itPair
.first
->second
, top
.node
);
276 case STATE_COMPONENT
:
278 NodeMap
& components
= data_
.getComponents();
279 auto itPair
= components
.insert({top
.name
, top
.node
});
280 if (!itPair
.second
) {
281 merge(itPair
.first
->second
, top
.node
);
283 state_
= STATE_COMPONENT_DONE
;
288 throw css::uno::RuntimeException(
289 "this cannot happen");
292 if (!elements_
.top().node
->getMembers().insert(
293 NodeMap::value_type(top
.name
, top
.node
)).second
)
295 throw css::uno::RuntimeException(
296 "duplicate " + top
.name
+ " in " + reader
.getUrl());
302 case STATE_COMPONENT_SCHEMA
:
303 // To support old, broken extensions with .xcs files that contain
304 // empty <component-schema> elements:
305 state_
= STATE_COMPONENT_DONE
;
307 case STATE_TEMPLATES
:
308 state_
= STATE_TEMPLATES_DONE
;
310 case STATE_TEMPLATES_DONE
:
311 throw css::uno::RuntimeException(
312 "no component element in " + reader
.getUrl());
313 case STATE_COMPONENT_DONE
:
316 assert(false); // this cannot happen
321 void XcsParser::characters(xmlreader::Span
const & text
) {
322 valueParser_
.characters(text
);
325 void XcsParser::handleComponentSchema(xmlreader::XmlReader
& reader
) {
326 //TODO: oor:version, xml:lang attributes
329 bool hasPackage
= false;
330 bool hasName
= false;
333 xmlreader::Span attrLn
;
334 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
337 if (attrNsId
== ParseManager::NAMESPACE_OOR
&& attrLn
.equals("package"))
340 throw css::uno::RuntimeException(
341 "multiple component-schema package attributes in " +
345 xmlreader::Span
s(reader
.getAttributeValue(false));
346 buf
.insert(0, s
.begin
, s
.length
);
347 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
348 attrLn
.equals("name"))
351 throw css::uno::RuntimeException(
352 "multiple component-schema name attributes in " +
356 xmlreader::Span
s(reader
.getAttributeValue(false));
357 buf
.append(s
.begin
, s
.length
);
361 throw css::uno::RuntimeException(
362 "no component-schema package attribute in " + reader
.getUrl());
365 throw css::uno::RuntimeException(
366 "no component-schema name attribute in " + reader
.getUrl());
368 componentName_
= xmlreader::Span(buf
.getStr(), buf
.getLength()).
372 void XcsParser::handleNodeRef(xmlreader::XmlReader
& reader
) {
373 bool hasName
= false;
375 OUString
component(componentName_
);
376 bool hasNodeType
= false;
380 xmlreader::Span attrLn
;
381 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
384 if (attrNsId
== ParseManager::NAMESPACE_OOR
&& attrLn
.equals("name")) {
386 name
= reader
.getAttributeValue(false).convertFromUtf8();
387 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
388 attrLn
.equals("component"))
390 component
= reader
.getAttributeValue(false).convertFromUtf8();
391 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
392 attrLn
.equals("node-type"))
395 nodeType
= reader
.getAttributeValue(false).convertFromUtf8();
399 throw css::uno::RuntimeException(
400 "no node-ref name attribute in " + reader
.getUrl());
402 rtl::Reference
< Node
> tmpl(
404 valueParser_
.getLayer(),
405 xmldata::parseTemplateReference(
406 component
, hasNodeType
, nodeType
, nullptr)));
408 //TODO: this can erroneously happen as long as import/uses attributes
409 // are not correctly processed
410 throw css::uno::RuntimeException(
411 "unknown node-ref " + name
+ " in " + reader
.getUrl());
413 rtl::Reference
< Node
> node(tmpl
->clone(false));
414 node
->setLayer(valueParser_
.getLayer());
415 elements_
.push(Element(node
, name
));
418 void XcsParser::handleProp(xmlreader::XmlReader
& reader
) {
419 bool hasName
= false;
421 valueParser_
.type_
= TYPE_ERROR
;
422 bool localized
= false;
423 bool nillable
= true;
426 xmlreader::Span attrLn
;
427 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
430 if (attrNsId
== ParseManager::NAMESPACE_OOR
&& attrLn
.equals("name")) {
432 name
= reader
.getAttributeValue(false).convertFromUtf8();
433 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
434 attrLn
.equals("type"))
436 valueParser_
.type_
= xmldata::parseType(
437 reader
, reader
.getAttributeValue(true));
438 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
439 attrLn
.equals("localized"))
441 localized
= xmldata::parseBoolean(reader
.getAttributeValue(true));
442 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
443 attrLn
.equals("nillable"))
445 nillable
= xmldata::parseBoolean(reader
.getAttributeValue(true));
449 throw css::uno::RuntimeException(
450 "no prop name attribute in " + reader
.getUrl());
452 if (valueParser_
.type_
== TYPE_ERROR
) {
453 throw css::uno::RuntimeException(
454 "no prop type attribute in " + reader
.getUrl());
459 ? rtl::Reference
< Node
>(
460 new LocalizedPropertyNode(
461 valueParser_
.getLayer(), valueParser_
.type_
, nillable
))
462 : rtl::Reference
< Node
>(
464 valueParser_
.getLayer(), valueParser_
.type_
, nillable
,
465 css::uno::Any(), false))),
469 void XcsParser::handlePropValue(
470 xmlreader::XmlReader
& reader
, rtl::Reference
< Node
> const & property
)
472 xmlreader::Span attrSeparator
;
475 xmlreader::Span attrLn
;
476 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
479 if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
480 attrLn
.equals("separator"))
482 attrSeparator
= reader
.getAttributeValue(false);
483 if (attrSeparator
.length
== 0) {
484 throw css::uno::RuntimeException(
485 "bad oor:separator attribute in " + reader
.getUrl());
489 valueParser_
.separator_
= OString(
490 attrSeparator
.begin
, attrSeparator
.length
);
491 valueParser_
.start(property
);
494 void XcsParser::handleGroup(xmlreader::XmlReader
& reader
, bool isTemplate
) {
495 bool hasName
= false;
497 bool extensible
= false;
500 xmlreader::Span attrLn
;
501 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
504 if (attrNsId
== ParseManager::NAMESPACE_OOR
&& attrLn
.equals("name")) {
506 name
= reader
.getAttributeValue(false).convertFromUtf8();
507 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
508 attrLn
.equals("extensible"))
510 extensible
= xmldata::parseBoolean(reader
.getAttributeValue(true));
514 throw css::uno::RuntimeException(
515 "no group name attribute in " + reader
.getUrl());
518 name
= Data::fullTemplateName(componentName_
, name
);
523 valueParser_
.getLayer(), extensible
,
524 isTemplate
? name
: OUString()),
528 void XcsParser::handleSet(xmlreader::XmlReader
& reader
, bool isTemplate
) {
529 bool hasName
= false;
531 OUString
component(componentName_
);
532 bool hasNodeType
= false;
536 xmlreader::Span attrLn
;
537 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
540 if (attrNsId
== ParseManager::NAMESPACE_OOR
&& attrLn
.equals("name")) {
542 name
= reader
.getAttributeValue(false).convertFromUtf8();
543 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
544 attrLn
.equals("component"))
546 component
= reader
.getAttributeValue(false).convertFromUtf8();
547 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
548 attrLn
.equals("node-type"))
551 nodeType
= reader
.getAttributeValue(false).convertFromUtf8();
555 throw css::uno::RuntimeException(
556 "no set name attribute in " + reader
.getUrl());
559 name
= Data::fullTemplateName(componentName_
, name
);
564 valueParser_
.getLayer(),
565 xmldata::parseTemplateReference(
566 component
, hasNodeType
, nodeType
, nullptr),
567 isTemplate
? name
: OUString()),
571 void XcsParser::handleSetItem(xmlreader::XmlReader
& reader
, SetNode
* set
) {
572 OUString
component(componentName_
);
573 bool hasNodeType
= false;
577 xmlreader::Span attrLn
;
578 if (!reader
.nextAttribute(&attrNsId
, &attrLn
)) {
581 if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
582 attrLn
.equals("component"))
584 component
= reader
.getAttributeValue(false).convertFromUtf8();
585 } else if (attrNsId
== ParseManager::NAMESPACE_OOR
&&
586 attrLn
.equals("node-type"))
589 nodeType
= reader
.getAttributeValue(false).convertFromUtf8();
592 set
->getAdditionalTemplateNames().push_back(
593 xmldata::parseTemplateReference(component
, hasNodeType
, nodeType
, nullptr));
594 elements_
.push(Element(rtl::Reference
< Node
>(), ""));
599 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */