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 * http://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.jxpath;
19
20 import java.net.URL;
21 import java.util.Objects;
22
23 import javax.xml.transform.Source;
24 import javax.xml.transform.Transformer;
25 import javax.xml.transform.TransformerFactory;
26 import javax.xml.transform.dom.DOMResult;
27
28 import org.apache.commons.jxpath.xml.DocumentContainer;
29
30 /**
31 * An XML document container reads and parses XML only when it is accessed. JXPath traverses Containers transparently - you use the same paths to access objects
32 * in containers as you do to access those objects directly. You can create XMLDocumentContainers for various XML documents that may or may not be accessed by
33 * XPaths. If they are, they will be automatically read, parsed and traversed. If they are not - they won't be read at all.
34 *
35 * @deprecated 1.1 Please use {@link DocumentContainer}
36 */
37 @Deprecated
38 public class XMLDocumentContainer implements Container {
39
40 private static final long serialVersionUID = 1L;
41
42 /** The delegate document container. */
43 private DocumentContainer delegate;
44
45 /** The DOM document. */
46 private Object document;
47
48 /**
49 * The document source.
50 */
51 private URL xmlURL;
52
53 /**
54 * The transformation source.
55 */
56 private Source source;
57
58 /**
59 * Constructs a new XMLDocumentContainer.
60 *
61 * @param source XML source
62 */
63 public XMLDocumentContainer(final Source source) {
64 this.source = Objects.requireNonNull(source);
65 }
66
67 /**
68 * Constructs a new XMLDocumentContainer.
69 *
70 * @param xmlURL a URL for an XML file. Use getClass().getResource(resourceName) to load XML from a resource file.
71 */
72 public XMLDocumentContainer(final URL xmlURL) {
73 this.xmlURL = xmlURL;
74 delegate = new DocumentContainer(xmlURL);
75 }
76
77 /**
78 * Reads XML, caches it internally and returns the Document.
79 *
80 * @return Object value
81 */
82 @Override
83 public Object getValue() {
84 if (document == null) {
85 try {
86 if (source != null) {
87 final DOMResult result = new DOMResult();
88 final Transformer trans = TransformerFactory.newInstance().newTransformer();
89 trans.transform(source, result);
90 document = result.getNode();
91 } else {
92 document = delegate.getValue();
93 }
94 } catch (final Exception ex) {
95 throw new JXPathException(
96 "Cannot read XML from: " + (xmlURL != null ? xmlURL.toString() : source != null ? source.getSystemId() : "<<undefined source>>"), ex);
97 }
98 }
99 return document;
100 }
101
102 /**
103 * Throws an UnsupportedOperationException
104 *
105 * @param value to set
106 */
107 @Override
108 public void setValue(final Object value) {
109 throw new UnsupportedOperationException();
110 }
111 }