1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.configuration2;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.Reader;
23 import java.io.StringReader;
24 import java.io.StringWriter;
25 import java.io.Writer;
26 import java.net.URL;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.Map;
33
34 import javax.xml.parsers.DocumentBuilder;
35 import javax.xml.parsers.DocumentBuilderFactory;
36 import javax.xml.parsers.ParserConfigurationException;
37 import javax.xml.transform.OutputKeys;
38 import javax.xml.transform.Result;
39 import javax.xml.transform.Source;
40 import javax.xml.transform.Transformer;
41 import javax.xml.transform.dom.DOMSource;
42 import javax.xml.transform.stream.StreamResult;
43
44 import org.apache.commons.configuration2.convert.ListDelimiterHandler;
45 import org.apache.commons.configuration2.ex.ConfigurationException;
46 import org.apache.commons.configuration2.io.ConfigurationLogger;
47 import org.apache.commons.configuration2.io.FileLocator;
48 import org.apache.commons.configuration2.io.FileLocatorAware;
49 import org.apache.commons.configuration2.io.InputStreamSupport;
50 import org.apache.commons.configuration2.resolver.DefaultEntityResolver;
51 import org.apache.commons.configuration2.tree.ImmutableNode;
52 import org.apache.commons.configuration2.tree.NodeTreeWalker;
53 import org.apache.commons.configuration2.tree.ReferenceNodeHandler;
54 import org.apache.commons.lang3.StringUtils;
55 import org.apache.commons.lang3.mutable.MutableObject;
56 import org.w3c.dom.Attr;
57 import org.w3c.dom.CDATASection;
58 import org.w3c.dom.Document;
59 import org.w3c.dom.Element;
60 import org.w3c.dom.NamedNodeMap;
61 import org.w3c.dom.Node;
62 import org.w3c.dom.NodeList;
63 import org.w3c.dom.Text;
64 import org.xml.sax.EntityResolver;
65 import org.xml.sax.InputSource;
66 import org.xml.sax.SAXException;
67 import org.xml.sax.SAXParseException;
68 import org.xml.sax.helpers.DefaultHandler;
69
70 /**
71 * <p>
72 * A specialized hierarchical configuration class that is able to parse XML documents.
73 * </p>
74 * <p>
75 * The parsed document will be stored keeping its structure. The class also tries to preserve as much information from
76 * the loaded XML document as possible, including comments and processing instructions. These will be contained in
77 * documents created by the {@code save()} methods, too.
78 * </p>
79 * <p>
80 * Like other file based configuration classes this class maintains the name and path to the loaded configuration file.
81 * These properties can be altered using several setter methods, but they are not modified by {@code save()} and
82 * {@code load()} methods. If XML documents contain relative paths to other documents (for example to a DTD), these references
83 * are resolved based on the path set for this configuration.
84 * </p>
85 * <p>
86 * By inheriting from {@link AbstractConfiguration} this class provides some extended functionality, for example interpolation
87 * of property values. Like in {@link PropertiesConfiguration} property values can contain delimiter characters (the
88 * comma ',' per default) and are then split into multiple values. This works for XML attributes and text content of
89 * elements as well. The delimiter can be escaped by a backslash. As an example consider the following XML fragment:
90 * </p>
91 *
92 * <pre>
93 * <config>
94 * <array>10,20,30,40</array>
95 * <scalar>3\,1415</scalar>
96 * <cite text="To be or not to be\, this is the question!"/>
97 * </config>
98 * </pre>
99 *
100 * <p>
101 * Here the content of the {@code array} element will be split at the commas, so the {@code array} key will be assigned
102 * 4 values. In the {@code scalar} property and the {@code text} attribute of the {@code cite} element the comma is
103 * escaped, so that no splitting is performed.
104 * </p>
105 * <p>
106 * The configuration API allows setting multiple values for a single attribute, for example something like the following is
107 * legal (assuming that the default expression engine is used):
108 * </p>
109 *
110 * <pre>
111 * XMLConfiguration config = new XMLConfiguration();
112 * config.addProperty("test.dir[@name]", "C:\\Temp\\");
113 * config.addProperty("test.dir[@name]", "D:\\Data\\");
114 * </pre>
115 *
116 * <p>
117 * However, in XML such a constellation is not supported; an attribute can appear only once for a single element.
118 * Therefore, an attempt to save a configuration which violates this condition will throw an exception.
119 * </p>
120 * <p>
121 * Like other {@code Configuration} implementations, {@code XMLConfiguration} uses a {@link ListDelimiterHandler} object
122 * for controlling list split operations. Per default, a list delimiter handler object is set which disables this
123 * feature. XML has a built-in support for complex structures including list properties; therefore, list splitting is
124 * not that relevant for this configuration type. Nevertheless, by setting an alternative {@code ListDelimiterHandler}
125 * implementation, this feature can be enabled. It works as for any other concrete {@code Configuration} implementation.
126 * </p>
127 * <p>
128 * Whitespace in the content of XML documents is trimmed per default. In most cases this is desired. However, sometimes
129 * whitespace is indeed important and should be treated as part of the value of a property as in the following example:
130 * </p>
131 *
132 * <pre>
133 * <indent> </indent>
134 * </pre>
135 *
136 * <p>
137 * Per default the spaces in the {@code indent} element will be trimmed resulting in an empty element. To tell
138 * {@code XMLConfiguration} that spaces are relevant the {@code xml:space} attribute can be used, which is defined in
139 * the <a href="https://www.w3.org/TR/REC-xml/#sec-white-space">XML specification</a>. This will look as follows:
140 * </p>
141 *
142 * <pre>
143 * <indent <strong>xml:space="preserve"</strong>> </indent>
144 * </pre>
145 *
146 * <p>
147 * The value of the {@code indent} property will now contain the spaces.
148 * </p>
149 * <p>
150 * {@code XMLConfiguration} implements the {@link FileBasedConfiguration} interface and thus can be used together with a
151 * file-based builder to load XML configuration files from various sources like files, URLs, or streams.
152 * </p>
153 * <p>
154 * Like other {@code Configuration} implementations, this class uses a {@code Synchronizer} object to control concurrent
155 * access. By choosing a suitable implementation of the {@code Synchronizer} interface, an instance can be made
156 * thread-safe or not. Note that access to most of the properties typically set through a builder is not protected by
157 * the {@code Synchronizer}. The intended usage is that these properties are set once at construction time through the
158 * builder and after that remain constant. If you wish to change such properties during life time of an instance, you
159 * have to use the {@code lock()} and {@code unlock()} methods manually to ensure that other threads see your changes.
160 * </p>
161 * <p>
162 * More information about the basic functionality supported by {@code XMLConfiguration} can be found at the user's guide
163 * at <a href="https://commons.apache.org/proper/commons-configuration/userguide/howto_basicfeatures.html"> Basic
164 * features and AbstractConfiguration</a>. There is also a separate chapter dealing with
165 * <a href="commons.apache.org/proper/commons-configuration/userguide/howto_xml.html"> XML Configurations</a> in
166 * special.
167 * </p>
168 *
169 * @since 1.0
170 */
171 public class XMLConfiguration extends BaseHierarchicalConfiguration implements FileBasedConfiguration, FileLocatorAware, InputStreamSupport {
172 /**
173 * A concrete {@code BuilderVisitor} that can construct XML documents.
174 */
175 static class XMLBuilderVisitor extends BuilderVisitor {
176 /**
177 * Removes all attributes of the given element.
178 *
179 * @param elem the element
180 */
181 private static void clearAttributes(final Element elem) {
182 final NamedNodeMap attributes = elem.getAttributes();
183 for (int i = 0; i < attributes.getLength(); i++) {
184 elem.removeAttribute(attributes.item(i).getNodeName());
185 }
186 }
187
188 /**
189 * Returns the only text node of an element for update. This method is called when the element's text changes. Then all
190 * text nodes except for the first are removed. A reference to the first is returned or <strong>null</strong> if there is no text
191 * node at all.
192 *
193 * @param elem the element
194 * @return the first and only text node
195 */
196 private static Text findTextNodeForUpdate(final Element elem) {
197 Text result = null;
198 // Find all Text nodes
199 final NodeList children = elem.getChildNodes();
200 final Collection<Node> textNodes = new ArrayList<>();
201 for (int i = 0; i < children.getLength(); i++) {
202 final Node nd = children.item(i);
203 if (nd instanceof Text) {
204 if (result == null) {
205 result = (Text) nd;
206 } else {
207 textNodes.add(nd);
208 }
209 }
210 }
211
212 // We don't want CDATAs
213 if (result instanceof CDATASection) {
214 textNodes.add(result);
215 result = null;
216 }
217
218 // Remove all but the first Text node
219 textNodes.forEach(elem::removeChild);
220 return result;
221 }
222
223 /**
224 * Helper method for updating the values of all attributes of the specified node.
225 *
226 * @param node the affected node
227 * @param elem the element that is associated with this node
228 */
229 private static void updateAttributes(final ImmutableNode node, final Element elem) {
230 if (node != null && elem != null) {
231 clearAttributes(elem);
232 node.getAttributes().forEach((k, v) -> {
233 if (v != null) {
234 elem.setAttribute(k, v.toString());
235 }
236 });
237 }
238 }
239
240 /** Stores the document to be constructed. */
241 private final Document document;
242
243 /** The element mapping. */
244 private final Map<Node, Node> elementMapping;
245
246 /** A mapping for the references for new nodes. */
247 private final Map<ImmutableNode, Element> newElements;
248
249 /** Stores the list delimiter handler . */
250 private final ListDelimiterHandler listDelimiterHandler;
251
252 /**
253 * Creates a new instance of {@code XMLBuilderVisitor}.
254 *
255 * @param docHelper the document helper
256 * @param handler the delimiter handler for properties with multiple values
257 */
258 public XMLBuilderVisitor(final XMLDocumentHelper docHelper, final ListDelimiterHandler handler) {
259 document = docHelper.getDocument();
260 elementMapping = docHelper.getElementMapping();
261 listDelimiterHandler = handler;
262 newElements = new HashMap<>();
263 }
264
265 /**
266 * Helper method for accessing the element of the specified node.
267 *
268 * @param node the node
269 * @param refHandler the {@code ReferenceNodeHandler}
270 * @return the element of this node
271 */
272 private Element getElement(final ImmutableNode node, final ReferenceNodeHandler refHandler) {
273 final Element elementNew = newElements.get(node);
274 if (elementNew != null) {
275 return elementNew;
276 }
277
278 // special treatment for root node of the hierarchy
279 final Object reference = refHandler.getReference(node);
280 final Node element;
281 if (reference instanceof XMLDocumentHelper) {
282 element = ((XMLDocumentHelper) reference).getDocument().getDocumentElement();
283 } else if (reference instanceof XMLListReference) {
284 element = ((XMLListReference) reference).getElement();
285 } else {
286 element = (Node) reference;
287 }
288 return element != null ? (Element) elementMapping.get(element) : document.getDocumentElement();
289 }
290
291 /**
292 * Updates the current XML document regarding removed nodes. The elements associated with removed nodes are removed from
293 * the document.
294 *
295 * @param refHandler the {@code ReferenceNodeHandler}
296 */
297 public void handleRemovedNodes(final ReferenceNodeHandler refHandler) {
298 refHandler.removedReferences().stream().filter(Node.class::isInstance).forEach(ref -> removeReference(elementMapping.get(ref)));
299 }
300
301 /**
302 * {@inheritDoc} This implementation ensures that the correct XML element is created and inserted between the given
303 * siblings.
304 */
305 @Override
306 protected void insert(final ImmutableNode newNode, final ImmutableNode parent, final ImmutableNode sibling1, final ImmutableNode sibling2,
307 final ReferenceNodeHandler refHandler) {
308 if (XMLListReference.isListNode(newNode, refHandler)) {
309 return;
310 }
311
312 final Element elem = document.createElement(newNode.getNodeName());
313 newElements.put(newNode, elem);
314 updateAttributes(newNode, elem);
315 if (newNode.getValue() != null) {
316 final String txt = String.valueOf(listDelimiterHandler.escape(newNode.getValue(), ListDelimiterHandler.NOOP_TRANSFORMER));
317 elem.appendChild(document.createTextNode(txt));
318 }
319 if (sibling2 == null) {
320 getElement(parent, refHandler).appendChild(elem);
321 } else if (sibling1 != null) {
322 getElement(parent, refHandler).insertBefore(elem, getElement(sibling1, refHandler).getNextSibling());
323 } else {
324 getElement(parent, refHandler).insertBefore(elem, getElement(parent, refHandler).getFirstChild());
325 }
326 }
327
328 /**
329 * Processes the specified document, updates element values, and adds new nodes to the hierarchy.
330 *
331 * @param refHandler the {@code ReferenceNodeHandler}
332 */
333 public void processDocument(final ReferenceNodeHandler refHandler) {
334 updateAttributes(refHandler.getRootNode(), document.getDocumentElement());
335 NodeTreeWalker.INSTANCE.walkDFS(refHandler.getRootNode(), this, refHandler);
336 }
337
338 /**
339 * Updates the associated XML elements when a node is removed.
340 *
341 * @param element the element to be removed
342 */
343 private void removeReference(final Node element) {
344 final Node parentElem = element.getParentNode();
345 if (parentElem != null) {
346 parentElem.removeChild(element);
347 }
348 }
349
350 /**
351 * {@inheritDoc} This implementation determines the XML element associated with the given node. Then this element's
352 * value and attributes are set accordingly.
353 */
354 @Override
355 protected void update(final ImmutableNode node, final Object reference, final ReferenceNodeHandler refHandler) {
356 if (XMLListReference.isListNode(node, refHandler)) {
357 if (XMLListReference.isFirstListItem(node, refHandler)) {
358 final String value = XMLListReference.listValue(node, refHandler, listDelimiterHandler);
359 updateElement(node, refHandler, value);
360 }
361 } else {
362 final Object value = listDelimiterHandler.escape(refHandler.getValue(node), ListDelimiterHandler.NOOP_TRANSFORMER);
363 updateElement(node, refHandler, value);
364 }
365 }
366
367 /**
368 * Updates the node's value if it represents an element node.
369 *
370 * @param element the element
371 * @param value the new value
372 */
373 private void updateElement(final Element element, final Object value) {
374 Text txtNode = findTextNodeForUpdate(element);
375 if (value == null) {
376 // remove text
377 if (txtNode != null) {
378 element.removeChild(txtNode);
379 }
380 } else {
381 final String newValue = String.valueOf(value);
382 if (txtNode == null) {
383 txtNode = document.createTextNode(newValue);
384 if (element.getFirstChild() != null) {
385 element.insertBefore(txtNode, element.getFirstChild());
386 } else {
387 element.appendChild(txtNode);
388 }
389 } else {
390 txtNode.setNodeValue(newValue);
391 }
392 }
393 }
394
395 private void updateElement(final ImmutableNode node, final ReferenceNodeHandler refHandler, final Object value) {
396 final Element element = getElement(node, refHandler);
397 updateElement(element, value);
398 updateAttributes(node, element);
399 }
400 }
401
402 /** Constant for the default indent size. */
403 static final int DEFAULT_INDENT_SIZE = 2;
404
405 /** Constant for output property name used on a transformer to specify the indent amount. */
406 static final String INDENT_AMOUNT_PROPERTY = "{http://xml.apache.org/xslt}indent-amount";
407
408 /** Constant for the default root element name. */
409 private static final String DEFAULT_ROOT_NAME = "configuration";
410
411 /** Constant for the name of the space attribute. */
412 private static final String ATTR_SPACE = "xml:space";
413
414 /** Constant for an internally used space attribute. */
415 private static final String ATTR_SPACE_INTERNAL = "config-xml:space";
416
417 /** Constant for the xml:space value for preserving whitespace. */
418 private static final String VALUE_PRESERVE = "preserve";
419
420 /** Schema Langauge key for the parser */
421 private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
422
423 /** Schema Language for the parser */
424 private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
425
426 /**
427 * Determines the number of child elements of this given node with the specified node name.
428 *
429 * @param parent the parent node
430 * @param name the name in question
431 * @return the number of child elements with this name
432 */
433 private static int countChildElements(final Node parent, final String name) {
434 final NodeList childNodes = parent.getChildNodes();
435 int count = 0;
436 for (int i = 0; i < childNodes.getLength(); i++) {
437 final Node item = childNodes.item(i);
438 if (item instanceof Element && name.equals(((Element) item).getTagName())) {
439 count++;
440 }
441 }
442 return count;
443 }
444
445 /**
446 * Determines the value of a configuration node. This method mainly checks whether the text value is to be trimmed or
447 * not. This is normally defined by the trim flag. However, if the node has children and its content is only whitespace,
448 * then it makes no sense to store any value; this would only scramble layout when the configuration is saved again.
449 *
450 * @param content the text content of this node
451 * @param hasChildren a flag whether the node has children
452 * @param trimFlag the trim flag
453 * @return the value to be stored for this node
454 */
455 private static String determineValue(final String content, final boolean hasChildren, final boolean trimFlag) {
456 final boolean shouldTrim = trimFlag || StringUtils.isBlank(content) && hasChildren;
457 return shouldTrim ? content.trim() : content;
458 }
459
460 /**
461 * Checks whether an element defines a complete list. If this is the case, extended list handling can be applied.
462 *
463 * @param element the element to be checked
464 * @return a flag whether this is the only element defining the list
465 */
466 private static boolean isSingleElementList(final Element element) {
467 final Node parentNode = element.getParentNode();
468 return countChildElements(parentNode, element.getTagName()) == 1;
469 }
470
471 /**
472 * Helper method for initializing the attributes of a configuration node from the given XML element.
473 *
474 * @param element the current XML element
475 * @return a map with all attribute values extracted for the current node
476 */
477 private static Map<String, String> processAttributes(final Element element) {
478 final NamedNodeMap attributes = element.getAttributes();
479 final Map<String, String> attrmap = new HashMap<>();
480
481 for (int i = 0; i < attributes.getLength(); ++i) {
482 final Node w3cNode = attributes.item(i);
483 if (w3cNode instanceof Attr) {
484 final Attr attr = (Attr) w3cNode;
485 attrmap.put(attr.getName(), attr.getValue());
486 }
487 }
488
489 return attrmap;
490 }
491
492 /**
493 * Checks whether the content of the current XML element should be trimmed. This method checks whether a
494 * {@code xml:space} attribute is present and evaluates its value. See
495 * <a href="https://www.w3.org/TR/REC-xml/#sec-white-space"> http://www.w3.org/TR/REC-xml/#sec-white-space</a> for more
496 * details.
497 *
498 * @param element the current XML element
499 * @param currentTrim the current trim flag
500 * @return a flag whether the content of this element should be trimmed
501 */
502 private static boolean shouldTrim(final Element element, final boolean currentTrim) {
503 final Attr attr = element.getAttributeNode(ATTR_SPACE);
504
505 if (attr == null) {
506 return currentTrim;
507 }
508 return !VALUE_PRESERVE.equals(attr.getValue());
509 }
510
511 /** Stores the name of the root element. */
512 private String rootElementName;
513
514 /** Stores the public ID from the DOCTYPE. */
515 private String publicID;
516
517 /** Stores the system ID from the DOCTYPE. */
518 private String systemID;
519
520 /** Stores the document builder that should be used for loading. */
521 private DocumentBuilder documentBuilder;
522
523 /** Stores a flag whether DTD or Schema validation should be performed. */
524 private boolean validating;
525
526 /** Stores a flag whether DTD or Schema validation is used */
527 private boolean schemaValidation;
528
529 /** The EntityResolver to use */
530 private EntityResolver entityResolver = new DefaultEntityResolver();
531
532 /** The current file locator. */
533 private FileLocator locator;
534
535 /**
536 * Creates a new instance of {@code XMLConfiguration}.
537 */
538 public XMLConfiguration() {
539 initLogger(new ConfigurationLogger(XMLConfiguration.class));
540 }
541
542 /**
543 * Creates a new instance of {@code XMLConfiguration} and copies the content of the passed in configuration into this
544 * object. Note that only the data of the passed in configuration will be copied. If, for instance, the other
545 * configuration is a {@code XMLConfiguration}, too, things like comments or processing instructions will be lost.
546 *
547 * @param c the configuration to copy
548 * @since 1.4
549 */
550 public XMLConfiguration(final HierarchicalConfiguration<ImmutableNode> c) {
551 super(c);
552 rootElementName = c != null ? c.getRootElementName() : null;
553 initLogger(new ConfigurationLogger(XMLConfiguration.class));
554 }
555
556 /**
557 * Helper method for building the internal storage hierarchy. The XML elements are transformed into node objects.
558 *
559 * @param node a builder for the current node
560 * @param refValue stores the text value of the element
561 * @param element the current XML element
562 * @param elemRefs a map for assigning references objects to nodes; can be <strong>null</strong>, then reference objects are
563 * irrelevant
564 * @param trim a flag whether the text content of elements should be trimmed; this controls the whitespace handling
565 * @param level the current level in the hierarchy
566 * @return a map with all attribute values extracted for the current node; this map also contains the value of the trim
567 * flag for this node under the key {@value #ATTR_SPACE}
568 */
569 private Map<String, String> constructHierarchy(final ImmutableNode.Builder node, final MutableObject<String> refValue, final Element element,
570 final Map<ImmutableNode, Object> elemRefs, final boolean trim, final int level) {
571 final boolean trimFlag = shouldTrim(element, trim);
572 final Map<String, String> attributes = processAttributes(element);
573 attributes.put(ATTR_SPACE_INTERNAL, String.valueOf(trimFlag));
574 final StringBuilder buffer = new StringBuilder();
575 final NodeList list = element.getChildNodes();
576 boolean hasChildren = false;
577
578 for (int i = 0; i < list.getLength(); i++) {
579 final Node w3cNode = list.item(i);
580 if (w3cNode instanceof Element) {
581 final Element child = (Element) w3cNode;
582 final ImmutableNode.Builder childNode = new ImmutableNode.Builder();
583 childNode.name(child.getTagName());
584 final MutableObject<String> refChildValue = new MutableObject<>();
585 final Map<String, String> attrmap = constructHierarchy(childNode, refChildValue, child, elemRefs, trimFlag, level + 1);
586 final boolean childTrim = Boolean.parseBoolean(attrmap.remove(ATTR_SPACE_INTERNAL));
587 childNode.addAttributes(attrmap);
588 final ImmutableNode newChild = createChildNodeWithValue(node, childNode, child, refChildValue.getValue(), childTrim, attrmap, elemRefs);
589 if (elemRefs != null && !elemRefs.containsKey(newChild)) {
590 elemRefs.put(newChild, child);
591 }
592 hasChildren = true;
593 } else if (w3cNode instanceof Text) {
594 final Text data = (Text) w3cNode;
595 buffer.append(data.getData());
596 }
597 }
598
599 boolean childrenFlag = false;
600 if (hasChildren || trimFlag) {
601 childrenFlag = hasChildren || attributes.size() > 1;
602 }
603 final String text = determineValue(buffer.toString(), childrenFlag, trimFlag);
604 if (!text.isEmpty() || !childrenFlag && level != 0) {
605 refValue.setValue(text);
606 }
607 return attributes;
608 }
609
610 /**
611 * Creates a new child node, assigns its value, and adds it to its parent. This method also deals with elements whose
612 * value is a list. In this case multiple child elements must be added. The return value is the first child node which
613 * was added.
614 *
615 * @param parent the builder for the parent element
616 * @param child the builder for the child element
617 * @param elem the associated XML element
618 * @param value the value of the child element
619 * @param trim flag whether texts of elements should be trimmed
620 * @param attrmap a map with the attributes of the current node
621 * @param elemRefs a map for assigning references objects to nodes; can be <strong>null</strong>, then reference objects are
622 * irrelevant
623 * @return the first child node added to the parent
624 */
625 private ImmutableNode createChildNodeWithValue(final ImmutableNode.Builder parent, final ImmutableNode.Builder child, final Element elem,
626 final String value, final boolean trim, final Map<String, String> attrmap, final Map<ImmutableNode, Object> elemRefs) {
627 final ImmutableNode addedChildNode;
628 final Collection<String> values;
629
630 if (value != null) {
631 values = getListDelimiterHandler().split(value, trim);
632 } else {
633 values = Collections.emptyList();
634 }
635
636 if (values.size() > 1) {
637 final Map<ImmutableNode, Object> refs = isSingleElementList(elem) ? elemRefs : null;
638 final Iterator<String> it = values.iterator();
639 // Create new node for the original child's first value
640 child.value(it.next());
641 addedChildNode = child.create();
642 parent.addChild(addedChildNode);
643 XMLListReference.assignListReference(refs, addedChildNode, elem);
644
645 // add multiple new children
646 while (it.hasNext()) {
647 final ImmutableNode.Builder c = new ImmutableNode.Builder();
648 c.name(addedChildNode.getNodeName());
649 c.value(it.next());
650 c.addAttributes(attrmap);
651 final ImmutableNode newChild = c.create();
652 parent.addChild(newChild);
653 XMLListReference.assignListReference(refs, newChild, null);
654 }
655 } else {
656 if (values.size() == 1) {
657 // we will have to replace the value because it might
658 // contain escaped delimiters
659 child.value(values.iterator().next());
660 }
661 addedChildNode = child.create();
662 parent.addChild(addedChildNode);
663 }
664
665 return addedChildNode;
666 }
667
668 /**
669 * Creates a DOM document from the internal tree of configuration nodes.
670 *
671 * @return the new document
672 * @throws ConfigurationException if an error occurs
673 */
674 private Document createDocument() throws ConfigurationException {
675 final ReferenceNodeHandler handler = getReferenceHandler();
676 final XMLDocumentHelper docHelper = (XMLDocumentHelper) handler.getReference(handler.getRootNode());
677 final XMLDocumentHelper newHelper = docHelper == null ? XMLDocumentHelper.forNewDocument(getRootElementName()) : docHelper.createCopy();
678
679 final XMLBuilderVisitor builder = new XMLBuilderVisitor(newHelper, getListDelimiterHandler());
680 builder.handleRemovedNodes(handler);
681 builder.processDocument(handler);
682 initRootElementText(newHelper.getDocument(), getModel().getNodeHandler().getRootNode().getValue());
683 return newHelper.getDocument();
684 }
685
686 /**
687 * Creates the {@code DocumentBuilder} to be used for loading files. This implementation checks whether a specific
688 * {@code DocumentBuilder} has been set. If this is the case, this one is used. Otherwise a default builder is created.
689 * Depending on the value of the validating flag this builder will be a validating or a non validating
690 * {@code DocumentBuilder}.
691 *
692 * @return the {@code DocumentBuilder} for loading configuration files
693 * @throws ParserConfigurationException if an error occurs
694 * @since 1.2
695 */
696 protected DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
697 if (getDocumentBuilder() != null) {
698 return getDocumentBuilder();
699 }
700 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
701 if (isValidating()) {
702 factory.setValidating(true);
703 if (isSchemaValidation()) {
704 factory.setNamespaceAware(true);
705 factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
706 }
707 }
708
709 final DocumentBuilder result = factory.newDocumentBuilder();
710 result.setEntityResolver(this.entityResolver);
711
712 if (isValidating()) {
713 // register an error handler which detects validation errors
714 result.setErrorHandler(new DefaultHandler() {
715 @Override
716 public void error(final SAXParseException ex) throws SAXException {
717 throw ex;
718 }
719 });
720 }
721 return result;
722 }
723
724 /**
725 * Creates and initializes the transformer used for save operations. This base implementation initializes all of the
726 * default settings like indentation mode and the DOCTYPE. Derived classes may overload this method if they have
727 * specific needs.
728 *
729 * @return the transformer to use for a save operation
730 * @throws ConfigurationException if an error occurs
731 * @since 1.3
732 */
733 protected Transformer createTransformer() throws ConfigurationException {
734 final Transformer transformer = XMLDocumentHelper.createTransformer();
735
736 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
737 transformer.setOutputProperty(INDENT_AMOUNT_PROPERTY, Integer.toString(DEFAULT_INDENT_SIZE));
738 if (locator != null && locator.getEncoding() != null) {
739 transformer.setOutputProperty(OutputKeys.ENCODING, locator.getEncoding());
740 }
741 if (publicID != null) {
742 transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, publicID);
743 }
744 if (systemID != null) {
745 transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemID);
746 }
747
748 return transformer;
749 }
750
751 /**
752 * Gets the XML document this configuration was loaded from. The return value is <strong>null</strong> if this configuration
753 * was not loaded from a XML document.
754 *
755 * @return the XML document this configuration was loaded from
756 */
757 public Document getDocument() {
758 final XMLDocumentHelper docHelper = getDocumentHelper();
759 return docHelper != null ? docHelper.getDocument() : null;
760 }
761
762 /**
763 * Gets the {@code DocumentBuilder} object that is used for loading documents. If no specific builder has been set,
764 * this method returns <strong>null</strong>.
765 *
766 * @return the {@code DocumentBuilder} for loading new documents
767 * @since 1.2
768 */
769 public DocumentBuilder getDocumentBuilder() {
770 return documentBuilder;
771 }
772
773 /**
774 * Gets the helper object for managing the underlying document.
775 *
776 * @return the {@code XMLDocumentHelper}
777 */
778 private XMLDocumentHelper getDocumentHelper() {
779 final ReferenceNodeHandler handler = getReferenceHandler();
780 return (XMLDocumentHelper) handler.getReference(handler.getRootNode());
781 }
782
783 /**
784 * Gets the EntityResolver.
785 *
786 * @return The EntityResolver.
787 * @since 1.7
788 */
789 public EntityResolver getEntityResolver() {
790 return this.entityResolver;
791 }
792
793 /**
794 * Gets the public ID of the DOCTYPE declaration from the loaded XML document. This is <strong>null</strong> if no document has
795 * been loaded yet or if the document does not contain a DOCTYPE declaration with a public ID.
796 *
797 * @return the public ID
798 * @since 1.3
799 */
800 public String getPublicID() {
801 return syncReadValue(publicID, false);
802 }
803
804 /**
805 * Gets the extended node handler with support for references.
806 *
807 * @return the {@code ReferenceNodeHandler}
808 */
809 private ReferenceNodeHandler getReferenceHandler() {
810 return getSubConfigurationParentModel().getReferenceNodeHandler();
811 }
812
813 /**
814 * Gets the name of the root element. If this configuration was loaded from a XML document, the name of this
815 * document's root element is returned. Otherwise it is possible to set a name for the root element that will be used
816 * when this configuration is stored.
817 *
818 * @return the name of the root element
819 */
820 @Override
821 protected String getRootElementNameInternal() {
822 final Document doc = getDocument();
823 if (doc == null) {
824 return rootElementName == null ? DEFAULT_ROOT_NAME : rootElementName;
825 }
826 return doc.getDocumentElement().getNodeName();
827 }
828
829 /**
830 * Gets the system ID of the DOCTYPE declaration from the loaded XML document. This is <strong>null</strong> if no document has
831 * been loaded yet or if the document does not contain a DOCTYPE declaration with a system ID.
832 *
833 * @return the system ID
834 * @since 1.3
835 */
836 public String getSystemID() {
837 return syncReadValue(systemID, false);
838 }
839
840 /**
841 * {@inheritDoc} Stores the passed in locator for the upcoming IO operation.
842 */
843 @Override
844 public void initFileLocator(final FileLocator loc) {
845 locator = loc;
846 }
847
848 /**
849 * Initializes this configuration from an XML document.
850 *
851 * @param docHelper the helper object with the document to be parsed
852 * @param elemRefs a flag whether references to the XML elements should be set
853 */
854 private void initProperties(final XMLDocumentHelper docHelper, final boolean elemRefs) {
855 final Document document = docHelper.getDocument();
856 setPublicID(docHelper.getSourcePublicID());
857 setSystemID(docHelper.getSourceSystemID());
858
859 final ImmutableNode.Builder rootBuilder = new ImmutableNode.Builder();
860 final MutableObject<String> rootValue = new MutableObject<>();
861 final Map<ImmutableNode, Object> elemRefMap = elemRefs ? new HashMap<>() : null;
862 final Map<String, String> attributes = constructHierarchy(rootBuilder, rootValue, document.getDocumentElement(), elemRefMap, true, 0);
863 attributes.remove(ATTR_SPACE_INTERNAL);
864 final ImmutableNode top = rootBuilder.value(rootValue.getValue()).addAttributes(attributes).create();
865 getSubConfigurationParentModel().mergeRoot(top, document.getDocumentElement().getTagName(), elemRefMap, elemRefs ? docHelper : null, this);
866 }
867
868 /**
869 * Sets the text of the root element of a newly created XML Document.
870 *
871 * @param doc the document
872 * @param value the new text to be set
873 */
874 private void initRootElementText(final Document doc, final Object value) {
875 final Element elem = doc.getDocumentElement();
876 final NodeList children = elem.getChildNodes();
877
878 // Remove all existing text nodes
879 for (int i = 0; i < children.getLength(); i++) {
880 final Node nd = children.item(i);
881 if (nd.getNodeType() == Node.TEXT_NODE) {
882 elem.removeChild(nd);
883 }
884 }
885
886 if (value != null) {
887 // Add a new text node
888 elem.appendChild(doc.createTextNode(String.valueOf(value)));
889 }
890 }
891
892 /**
893 * Returns the value of the schemaValidation flag.
894 *
895 * @return the schemaValidation flag
896 * @since 1.7
897 */
898 public boolean isSchemaValidation() {
899 return schemaValidation;
900 }
901
902 /**
903 * Returns the value of the validating flag.
904 *
905 * @return the validating flag
906 * @since 1.2
907 */
908 public boolean isValidating() {
909 return validating;
910 }
911
912 /**
913 * Loads a configuration file from the specified input source.
914 *
915 * @param source the input source
916 * @throws ConfigurationException if an error occurs
917 */
918 private void load(final InputSource source) throws ConfigurationException {
919 if (locator == null) {
920 throw new ConfigurationException(
921 "Load operation not properly initialized! Do not call read(InputStream) directly, but use a FileHandler to load a configuration.");
922 }
923
924 try {
925 final URL sourceURL = locator.getSourceURL();
926 if (sourceURL != null) {
927 source.setSystemId(sourceURL.toString());
928 }
929
930 final DocumentBuilder builder = createDocumentBuilder();
931 final Document newDocument = builder.parse(source);
932 final Document oldDocument = getDocument();
933 initProperties(XMLDocumentHelper.forSourceDocument(newDocument), oldDocument == null);
934 } catch (final SAXParseException spe) {
935 throw new ConfigurationException("Error parsing " + source.getSystemId(), spe);
936 } catch (final Exception e) {
937 getLogger().debug("Unable to load the configuration: " + e);
938 throw new ConfigurationException("Unable to load the configuration", e);
939 }
940 }
941
942 /**
943 * Loads the configuration from the given input stream. This is analogous to {@link #read(Reader)}, but data is read
944 * from a stream. Note that this method will be called most time when reading an XML configuration source. By reading
945 * XML documents directly from an input stream, the file's encoding can be correctly dealt with.
946 *
947 * @param in the input stream
948 * @throws ConfigurationException if an error occurs
949 * @throws IOException if an IO error occurs
950 */
951 @Override
952 public void read(final InputStream in) throws ConfigurationException, IOException {
953 load(new InputSource(in));
954 }
955
956 /**
957 * Loads the configuration from the given reader. Note that the {@code clear()} method is not called, so the properties
958 * contained in the loaded file will be added to the current set of properties.
959 *
960 * @param in the reader
961 * @throws ConfigurationException if an error occurs
962 * @throws IOException if an IO error occurs
963 */
964 @Override
965 public void read(final Reader in) throws ConfigurationException, IOException {
966 load(new InputSource(in));
967 }
968
969 /**
970 * Sets the {@code DocumentBuilder} object to be used for loading documents. This method makes it possible to specify
971 * the exact document builder. So an application can create a builder, configure it for its special needs, and then pass
972 * it to this method.
973 *
974 * @param documentBuilder the document builder to be used; if undefined, a default builder will be used
975 * @since 1.2
976 */
977 public void setDocumentBuilder(final DocumentBuilder documentBuilder) {
978 this.documentBuilder = documentBuilder;
979 }
980
981 /**
982 * Sets a new EntityResolver. Setting this will cause RegisterEntityId to have no effect.
983 *
984 * @param resolver The EntityResolver to use.
985 * @since 1.7
986 */
987 public void setEntityResolver(final EntityResolver resolver) {
988 this.entityResolver = resolver;
989 }
990
991 /**
992 * Sets the public ID of the DOCTYPE declaration. When this configuration is saved, a DOCTYPE declaration will be
993 * constructed that contains this public ID.
994 *
995 * @param publicID the public ID
996 * @since 1.3
997 */
998 public void setPublicID(final String publicID) {
999 syncWrite(() -> this.publicID = publicID, false);
1000 }
1001
1002 /**
1003 * Sets the name of the root element. This name is used when this configuration object is stored in an XML file. Note
1004 * that setting the name of the root element works only if this configuration has been newly created. If the
1005 * configuration was loaded from an XML file, the name cannot be changed and an {@code UnsupportedOperationException}
1006 * exception is thrown. Whether this configuration has been loaded from an XML document or not can be found out using
1007 * the {@code getDocument()} method.
1008 *
1009 * @param name the name of the root element
1010 */
1011 public void setRootElementName(final String name) {
1012 beginRead(true);
1013 try {
1014 if (getDocument() != null) {
1015 throw new UnsupportedOperationException("The name of the root element cannot be changed when loaded from an XML document!");
1016 }
1017 rootElementName = name;
1018 } finally {
1019 endRead();
1020 }
1021 }
1022
1023 /**
1024 * Sets the value of the schemaValidation flag. This flag determines whether DTD or Schema validation should be used.
1025 * This flag is evaluated only if no custom {@code DocumentBuilder} was set. If set to true the XML document must
1026 * contain a schemaLocation definition that provides resolvable hints to the required schemas.
1027 *
1028 * @param schemaValidation the validating flag
1029 * @since 1.7
1030 */
1031 public void setSchemaValidation(final boolean schemaValidation) {
1032 this.schemaValidation = schemaValidation;
1033 if (schemaValidation) {
1034 this.validating = true;
1035 }
1036 }
1037
1038 /**
1039 * Sets the system ID of the DOCTYPE declaration. When this configuration is saved, a DOCTYPE declaration will be
1040 * constructed that contains this system ID.
1041 *
1042 * @param systemID the system ID
1043 * @since 1.3
1044 */
1045 public void setSystemID(final String systemID) {
1046 syncWrite(() -> this.systemID = systemID, false);
1047 }
1048
1049 /**
1050 * Sets the value of the validating flag. This flag determines whether DTD/Schema validation should be performed when
1051 * loading XML documents. This flag is evaluated only if no custom {@code DocumentBuilder} was set.
1052 *
1053 * @param validating the validating flag
1054 * @since 1.2
1055 */
1056 public void setValidating(final boolean validating) {
1057 if (!schemaValidation) {
1058 this.validating = validating;
1059 }
1060 }
1061
1062 /**
1063 * Validate the document against the Schema.
1064 *
1065 * @throws ConfigurationException if the validation fails.
1066 */
1067 public void validate() throws ConfigurationException {
1068 syncWrite(() -> {
1069 try {
1070 final StringWriter writer = new StringWriter();
1071 final Result result = new StreamResult(writer);
1072 XMLDocumentHelper.transform(createTransformer(), new DOMSource(createDocument()), result);
1073 final Reader reader = new StringReader(writer.getBuffer().toString());
1074 createDocumentBuilder().parse(new InputSource(reader));
1075 } catch (final SAXException | IOException | ParserConfigurationException pce) {
1076 throw new ConfigurationException("Validation failed", pce);
1077 }
1078 }, false);
1079 }
1080
1081 /**
1082 * Saves the configuration to the specified writer.
1083 *
1084 * @param writer the writer used to save the configuration
1085 * @throws ConfigurationException if an error occurs
1086 * @throws IOException if an IO error occurs
1087 */
1088 @Override
1089 public void write(final Writer writer) throws ConfigurationException, IOException {
1090 write(writer, createTransformer());
1091 }
1092
1093 /**
1094 * Saves the configuration to the specified writer.
1095 *
1096 * @param writer the writer used to save the configuration.
1097 * @param transformer How to transform this configuration.
1098 * @throws ConfigurationException if an error occurs.
1099 * @since 2.7.0
1100 */
1101 public void write(final Writer writer, final Transformer transformer) throws ConfigurationException {
1102 final Source source = new DOMSource(createDocument());
1103 final Result result = new StreamResult(writer);
1104 XMLDocumentHelper.transform(transformer, source, result);
1105 }
1106 }