bump product version to 6.3.0.0.beta1
[LibreOffice.git] / configmgr / source / xcsparser.cxx
blobe7bed9cc02e4de5247faf4e2611eae467cf7b29c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
22 #include <cassert>
23 #include <cstddef>
24 #include <set>
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>
37 #include "data.hxx"
38 #include "localizedpropertynode.hxx"
39 #include "groupnode.hxx"
40 #include "node.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"
48 namespace configmgr {
50 namespace {
52 // Conservatively merge a template or component (and its recursive parts) into
53 // an existing instance:
54 void merge(
55 rtl::Reference< Node > const & original,
56 rtl::Reference< Node > const & update)
58 assert(
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);
85 break;
86 case Node::KIND_SET:
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);
104 break;
105 case Node::KIND_ROOT:
106 assert(false); // this cannot happen
107 break;
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)) {
129 return true;
131 if (state_ == STATE_START) {
132 if (nsId == ParseManager::NAMESPACE_OOR &&
133 name.equals("component-schema"))
135 handleComponentSchema(reader);
136 state_ = STATE_COMPONENT_SCHEMA;
137 ignoring_ = 0;
138 return true;
140 } else {
141 //TODO: ignoring component-schema import, component-schema uses, and
142 // prop constraints; accepting all four at illegal places (and with
143 // illegal content):
144 if (ignoring_ > 0 ||
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);
150 ++ignoring_;
151 return true;
153 switch (state_) {
154 case STATE_COMPONENT_SCHEMA:
155 if (nsId == xmlreader::XmlReader::NAMESPACE_NONE &&
156 name.equals("templates"))
158 state_ = STATE_TEMPLATES;
159 return true;
161 [[fallthrough]];
162 case STATE_TEMPLATES_DONE:
163 if (nsId == xmlreader::XmlReader::NAMESPACE_NONE &&
164 name.equals("component"))
166 state_ = STATE_COMPONENT;
167 assert(elements_.empty());
168 elements_.push(
169 Element(
170 new GroupNode(valueParser_.getLayer(), false, ""),
171 componentName_));
172 return true;
174 break;
175 case STATE_TEMPLATES:
176 if (elements_.empty()) {
177 if (nsId == xmlreader::XmlReader::NAMESPACE_NONE &&
178 name.equals("group"))
180 handleGroup(reader, true);
181 return true;
183 if (nsId == xmlreader::XmlReader::NAMESPACE_NONE &&
184 name.equals("set"))
186 handleSet(reader, true);
187 return true;
189 break;
191 [[fallthrough]];
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);
201 return true;
203 break;
204 case Node::KIND_GROUP:
205 if (nsId == xmlreader::XmlReader::NAMESPACE_NONE &&
206 name.equals("prop"))
208 handleProp(reader);
209 return true;
211 if (nsId == xmlreader::XmlReader::NAMESPACE_NONE &&
212 name.equals("node-ref"))
214 handleNodeRef(reader);
215 return true;
217 if (nsId == xmlreader::XmlReader::NAMESPACE_NONE &&
218 name.equals("group"))
220 handleGroup(reader, false);
221 return true;
223 if (nsId == xmlreader::XmlReader::NAMESPACE_NONE &&
224 name.equals("set"))
226 handleSet(reader, false);
227 return true;
229 break;
230 case Node::KIND_SET:
231 if (nsId == xmlreader::XmlReader::NAMESPACE_NONE &&
232 name.equals("item"))
234 handleSetItem(
235 reader,
236 static_cast< SetNode * >(elements_.top().node.get()));
237 return true;
239 break;
240 default: // Node::KIND_LOCALIZED_VALUE
241 assert(false); // this cannot happen
242 break;
244 break;
245 case STATE_COMPONENT_DONE:
246 break;
247 default: // STATE_START
248 assert(false); // this cannot happen
249 break;
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()) {
258 return;
260 if (ignoring_ > 0) {
261 --ignoring_;
262 } else if (!elements_.empty()) {
263 Element top(elements_.top());
264 elements_.pop();
265 if (top.node.is()) {
266 if (elements_.empty()) {
267 switch (state_) {
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);
275 break;
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;
285 break;
286 default:
287 assert(false);
288 throw css::uno::RuntimeException(
289 "this cannot happen");
291 } else {
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());
300 } else {
301 switch (state_) {
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;
306 break;
307 case STATE_TEMPLATES:
308 state_ = STATE_TEMPLATES_DONE;
309 break;
310 case STATE_TEMPLATES_DONE:
311 throw css::uno::RuntimeException(
312 "no component element in " + reader.getUrl());
313 case STATE_COMPONENT_DONE:
314 break;
315 default:
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
327 OStringBuffer buf;
328 buf.append('.');
329 bool hasPackage = false;
330 bool hasName = false;
331 for (;;) {
332 int attrNsId;
333 xmlreader::Span attrLn;
334 if (!reader.nextAttribute(&attrNsId, &attrLn)) {
335 break;
337 if (attrNsId == ParseManager::NAMESPACE_OOR && attrLn.equals("package"))
339 if (hasPackage) {
340 throw css::uno::RuntimeException(
341 "multiple component-schema package attributes in " +
342 reader.getUrl());
344 hasPackage = true;
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"))
350 if (hasName) {
351 throw css::uno::RuntimeException(
352 "multiple component-schema name attributes in " +
353 reader.getUrl());
355 hasName = true;
356 xmlreader::Span s(reader.getAttributeValue(false));
357 buf.append(s.begin, s.length);
360 if (!hasPackage) {
361 throw css::uno::RuntimeException(
362 "no component-schema package attribute in " + reader.getUrl());
364 if (!hasName) {
365 throw css::uno::RuntimeException(
366 "no component-schema name attribute in " + reader.getUrl());
368 componentName_ = xmlreader::Span(buf.getStr(), buf.getLength()).
369 convertFromUtf8();
372 void XcsParser::handleNodeRef(xmlreader::XmlReader & reader) {
373 bool hasName = false;
374 OUString name;
375 OUString component(componentName_);
376 bool hasNodeType = false;
377 OUString nodeType;
378 for (;;) {
379 int attrNsId;
380 xmlreader::Span attrLn;
381 if (!reader.nextAttribute(&attrNsId, &attrLn)) {
382 break;
384 if (attrNsId == ParseManager::NAMESPACE_OOR && attrLn.equals("name")) {
385 hasName = true;
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"))
394 hasNodeType = true;
395 nodeType = reader.getAttributeValue(false).convertFromUtf8();
398 if (!hasName) {
399 throw css::uno::RuntimeException(
400 "no node-ref name attribute in " + reader.getUrl());
402 rtl::Reference< Node > tmpl(
403 data_.getTemplate(
404 valueParser_.getLayer(),
405 xmldata::parseTemplateReference(
406 component, hasNodeType, nodeType, nullptr)));
407 if (!tmpl.is()) {
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;
420 OUString name;
421 valueParser_.type_ = TYPE_ERROR;
422 bool localized = false;
423 bool nillable = true;
424 for (;;) {
425 int attrNsId;
426 xmlreader::Span attrLn;
427 if (!reader.nextAttribute(&attrNsId, &attrLn)) {
428 break;
430 if (attrNsId == ParseManager::NAMESPACE_OOR && attrLn.equals("name")) {
431 hasName = true;
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));
448 if (!hasName) {
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());
456 elements_.push(
457 Element(
458 (localized
459 ? rtl::Reference< Node >(
460 new LocalizedPropertyNode(
461 valueParser_.getLayer(), valueParser_.type_, nillable))
462 : rtl::Reference< Node >(
463 new PropertyNode(
464 valueParser_.getLayer(), valueParser_.type_, nillable,
465 css::uno::Any(), false))),
466 name));
469 void XcsParser::handlePropValue(
470 xmlreader::XmlReader & reader, rtl::Reference< Node > const & property)
472 xmlreader::Span attrSeparator;
473 for (;;) {
474 int attrNsId;
475 xmlreader::Span attrLn;
476 if (!reader.nextAttribute(&attrNsId, &attrLn)) {
477 break;
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;
496 OUString name;
497 bool extensible = false;
498 for (;;) {
499 int attrNsId;
500 xmlreader::Span attrLn;
501 if (!reader.nextAttribute(&attrNsId, &attrLn)) {
502 break;
504 if (attrNsId == ParseManager::NAMESPACE_OOR && attrLn.equals("name")) {
505 hasName = true;
506 name = reader.getAttributeValue(false).convertFromUtf8();
507 } else if (attrNsId == ParseManager::NAMESPACE_OOR &&
508 attrLn.equals("extensible"))
510 extensible = xmldata::parseBoolean(reader.getAttributeValue(true));
513 if (!hasName) {
514 throw css::uno::RuntimeException(
515 "no group name attribute in " + reader.getUrl());
517 if (isTemplate) {
518 name = Data::fullTemplateName(componentName_, name);
520 elements_.push(
521 Element(
522 new GroupNode(
523 valueParser_.getLayer(), extensible,
524 isTemplate ? name : OUString()),
525 name));
528 void XcsParser::handleSet(xmlreader::XmlReader & reader, bool isTemplate) {
529 bool hasName = false;
530 OUString name;
531 OUString component(componentName_);
532 bool hasNodeType = false;
533 OUString nodeType;
534 for (;;) {
535 int attrNsId;
536 xmlreader::Span attrLn;
537 if (!reader.nextAttribute(&attrNsId, &attrLn)) {
538 break;
540 if (attrNsId == ParseManager::NAMESPACE_OOR && attrLn.equals("name")) {
541 hasName = true;
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"))
550 hasNodeType = true;
551 nodeType = reader.getAttributeValue(false).convertFromUtf8();
554 if (!hasName) {
555 throw css::uno::RuntimeException(
556 "no set name attribute in " + reader.getUrl());
558 if (isTemplate) {
559 name = Data::fullTemplateName(componentName_, name);
561 elements_.push(
562 Element(
563 new SetNode(
564 valueParser_.getLayer(),
565 xmldata::parseTemplateReference(
566 component, hasNodeType, nodeType, nullptr),
567 isTemplate ? name : OUString()),
568 name));
571 void XcsParser::handleSetItem(xmlreader::XmlReader & reader, SetNode * set) {
572 OUString component(componentName_);
573 bool hasNodeType = false;
574 OUString nodeType;
575 for (;;) {
576 int attrNsId;
577 xmlreader::Span attrLn;
578 if (!reader.nextAttribute(&attrNsId, &attrLn)) {
579 break;
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"))
588 hasNodeType = true;
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: */