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