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