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 package org.apache.commons.configuration2;
18
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import javax.xml.parsers.DocumentBuilder;
24 import javax.xml.parsers.DocumentBuilderFactory;
25 import javax.xml.parsers.ParserConfigurationException;
26 import javax.xml.transform.Result;
27 import javax.xml.transform.Source;
28 import javax.xml.transform.Transformer;
29 import javax.xml.transform.TransformerConfigurationException;
30 import javax.xml.transform.TransformerException;
31 import javax.xml.transform.TransformerFactory;
32 import javax.xml.transform.dom.DOMResult;
33 import javax.xml.transform.dom.DOMSource;
34
35 import org.apache.commons.configuration2.ex.ConfigurationException;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38 import org.w3c.dom.Node;
39 import org.w3c.dom.NodeList;
40
41 /**
42 * <p>
43 * An internally used helper class for dealing with XML documents.
44 * </p>
45 * <p>
46 * This class is used by {@link XMLConfiguration}. It provides some basic functionality for processing DOM documents and
47 * dealing with elements. The main idea is that an instance holds the XML document associated with a XML configuration
48 * object. When the configuration is to be saved the document has to be manipulated according to the changes made on the
49 * configuration. To ensure that this is possible even under concurrent access, a new temporary instance is created as a
50 * copy of the original instance. Then, on this copy, the changes of the configuration are applied. The resulting
51 * document can then be serialized.
52 * </p>
53 * <p>
54 * Nodes of an {@code XMLConfiguration} that was read from a file are associated with the XML elements they represent.
55 * In order to apply changes on the copied document, it is necessary to establish a mapping between the elements of the
56 * old document and the elements of the copied document. This is also handled by this class.
57 * </p>
58 *
59 * @since 2.0
60 */
61 final class XMLDocumentHelper {
62
63 /**
64 * Creates a copy of the specified document.
65 *
66 * @param doc the {@code Document}
67 * @return the copy of this document
68 * @throws ConfigurationException if an error occurs
69 */
70 private static Document copyDocument(final Document doc) throws ConfigurationException {
71 final Transformer transformer = createTransformer();
72 final DOMSource source = new DOMSource(doc);
73 final DOMResult result = new DOMResult();
74 transform(transformer, source, result);
75
76 return (Document) result.getNode();
77 }
78
79 /**
80 * Creates a new {@code DocumentBuilder} using the specified factory. Exceptions are rethrown as
81 * {@code ConfigurationException} exceptions.
82 *
83 * @param factory the {@code DocumentBuilderFactory}
84 * @return the newly created {@code DocumentBuilder}
85 * @throws ConfigurationException if an error occurs
86 */
87 static DocumentBuilder createDocumentBuilder(final DocumentBuilderFactory factory) throws ConfigurationException {
88 try {
89 return factory.newDocumentBuilder();
90 } catch (final ParserConfigurationException pcex) {
91 throw new ConfigurationException(pcex);
92 }
93 }
94
95 /**
96 * Creates a new {@code DocumentBuilderFactory} instance.
97 *
98 * @return the new factory object
99 */
100 private static DocumentBuilderFactory createDocumentBuilderFactory() {
101 return DocumentBuilderFactory.newInstance();
102 }
103
104 /**
105 * Creates the element mapping for the specified documents. For each node in the source document an entry is created
106 * pointing to the corresponding node in the destination object.
107 *
108 * @param doc1 the source document
109 * @param doc2 the destination document
110 * @return the element mapping
111 */
112 private static Map<Node, Node> createElementMapping(final Document doc1, final Document doc2) {
113 final Map<Node, Node> mapping = new HashMap<>();
114 createElementMappingForNodes(doc1.getDocumentElement(), doc2.getDocumentElement(), mapping);
115 return mapping;
116 }
117
118 /**
119 * Creates the element mapping for the specified nodes and all their child nodes.
120 *
121 * @param n1 node 1
122 * @param n2 node 2
123 * @param mapping the mapping to be filled
124 */
125 private static void createElementMappingForNodes(final Node n1, final Node n2, final Map<Node, Node> mapping) {
126 mapping.put(n1, n2);
127 final NodeList childNodes1 = n1.getChildNodes();
128 final NodeList childNodes2 = n2.getChildNodes();
129 final int count = Math.min(childNodes1.getLength(), childNodes2.getLength());
130 for (int i = 0; i < count; i++) {
131 createElementMappingForNodes(childNodes1.item(i), childNodes2.item(i), mapping);
132 }
133 }
134
135 /**
136 * Creates a new {@code Transformer} object. No initializations are performed on the new instance.
137 *
138 * @return the new {@code Transformer}
139 * @throws ConfigurationException if the {@code Transformer} could not be created
140 */
141 public static Transformer createTransformer() throws ConfigurationException {
142 return createTransformer(createTransformerFactory());
143 }
144
145 /**
146 * Creates a {@code Transformer} using the specified factory.
147 *
148 * @param factory the {@code TransformerFactory}
149 * @return the newly created {@code Transformer}
150 * @throws ConfigurationException if an error occurs
151 */
152 static Transformer createTransformer(final TransformerFactory factory) throws ConfigurationException {
153 try {
154 return factory.newTransformer();
155 } catch (final TransformerConfigurationException tex) {
156 throw new ConfigurationException(tex);
157 }
158 }
159
160 /**
161 * Creates a new {@code TransformerFactory}.
162 *
163 * @return the {@code TransformerFactory}
164 */
165 static TransformerFactory createTransformerFactory() {
166 return TransformerFactory.newInstance();
167 }
168
169 /**
170 * Creates an empty element mapping.
171 *
172 * @return the empty mapping
173 */
174 private static Map<Node, Node> emptyElementMapping() {
175 return Collections.emptyMap();
176 }
177
178 /**
179 * Creates a new instance of {@code XMLDocumentHelper} and initializes it with a newly created, empty {@code Document}.
180 * The new document has a root element with the given element name. This element has no further child nodes.
181 *
182 * @param rootElementName the name of the root element
183 * @return the newly created instance
184 * @throws ConfigurationException if an error occurs when creating the document
185 */
186 public static XMLDocumentHelper forNewDocument(final String rootElementName) throws ConfigurationException {
187 final Document doc = createDocumentBuilder(createDocumentBuilderFactory()).newDocument();
188 final Element rootElem = doc.createElement(rootElementName);
189 doc.appendChild(rootElem);
190 return new XMLDocumentHelper(doc, emptyElementMapping(), null, null);
191 }
192
193 /**
194 * Creates a new instance of {@code XMLDocumentHelper} and initializes it with a source document. This is a document
195 * created from a configuration file. It is kept in memory so that the configuration can be saved with the same format.
196 * Note that already a copy of this document is created. This is done for the following reasons:
197 * <ul>
198 * <li>It is a defensive copy.</li>
199 * <li>An identity transformation on a document may change certain nodes, for example CDATA sections. When later on again
200 * copies of this document are created it has to be ensured that these copies have the same structure than the original
201 * document stored in this instance.</li>
202 * </ul>
203 *
204 * @param srcDoc the source document
205 * @return the newly created instance
206 * @throws ConfigurationException if an error occurs
207 */
208 public static XMLDocumentHelper forSourceDocument(final Document srcDoc) throws ConfigurationException {
209 final String pubID;
210 final String sysID;
211 if (srcDoc.getDoctype() != null) {
212 pubID = srcDoc.getDoctype().getPublicId();
213 sysID = srcDoc.getDoctype().getSystemId();
214 } else {
215 pubID = null;
216 sysID = null;
217 }
218
219 return new XMLDocumentHelper(copyDocument(srcDoc), emptyElementMapping(), pubID, sysID);
220 }
221
222 /**
223 * Performs an XSL transformation on the passed in operands. All possible exceptions are caught and redirected as
224 * {@code ConfigurationException} exceptions.
225 *
226 * @param transformer the transformer
227 * @param source the source
228 * @param result the result
229 * @throws ConfigurationException if an error occurs
230 */
231 public static void transform(final Transformer transformer, final Source source, final Result result) throws ConfigurationException {
232 try {
233 transformer.transform(source, result);
234 } catch (final TransformerException tex) {
235 throw new ConfigurationException(tex);
236 }
237 }
238
239 /** Stores the document managed by this instance. */
240 private final Document document;
241
242 /** The element mapping to the source document. */
243 private final Map<Node, Node> elementMapping;
244
245 /** Stores the public ID of the source document. */
246 private final String sourcePublicID;
247
248 /** Stores the system ID of the source document. */
249 private final String sourceSystemID;
250
251 /**
252 * Creates a new instance of {@code XMLDocumentHelper} and initializes it with the given XML document. Note: This
253 * constructor is package private only for testing purposes. Instances should be created using the static factory
254 * methods.
255 *
256 * @param doc the {@code Document}
257 * @param elemMap the element mapping
258 * @param pubID the public ID of the source document
259 * @param sysID the system ID of the source document
260 */
261 XMLDocumentHelper(final Document doc, final Map<Node, Node> elemMap, final String pubID, final String sysID) {
262 document = doc;
263 elementMapping = elemMap;
264 sourcePublicID = pubID;
265 sourceSystemID = sysID;
266 }
267
268 /**
269 * Creates a copy of this object. This copy contains a copy of the document and an element mapping which allows mapping
270 * elements from the source document to elements of the copied document.
271 *
272 * @return the copy
273 * @throws ConfigurationException if an error occurs
274 */
275 public XMLDocumentHelper createCopy() throws ConfigurationException {
276 final Document docCopy = copyDocument(getDocument());
277 return new XMLDocumentHelper(docCopy, createElementMapping(getDocument(), docCopy), getSourcePublicID(), getSourceSystemID());
278 }
279
280 /**
281 * Gets the {@code Document} managed by this helper.
282 *
283 * @return the wrapped {@code Document}
284 */
285 public Document getDocument() {
286 return document;
287 }
288
289 /**
290 * Gets the element mapping to the source document. This map can be used to obtain elements in the managed document
291 * which correspond to elements in the source document. If this instance has not been created from a source document,
292 * the mapping is empty.
293 *
294 * @return the element mapping to the source document
295 */
296 public Map<Node, Node> getElementMapping() {
297 return elementMapping;
298 }
299
300 /**
301 * Gets the public ID of the source document.
302 *
303 * @return the public ID of the source document
304 */
305 public String getSourcePublicID() {
306 return sourcePublicID;
307 }
308
309 /**
310 * Gets the system ID of the source document.
311 *
312 * @return the system ID of the source document
313 */
314 public String getSourceSystemID() {
315 return sourceSystemID;
316 }
317 }