001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.jxpath;
018
019import java.net.URL;
020
021import javax.xml.transform.Source;
022import javax.xml.transform.Transformer;
023import javax.xml.transform.TransformerFactory;
024import javax.xml.transform.dom.DOMResult;
025
026import org.apache.commons.jxpath.xml.DocumentContainer;
027
028/**
029 * An XML document container reads and parses XML only when it is
030 * accessed.  JXPath traverses Containers transparently -
031 * you use the same paths to access objects in containers as you
032 * do to access those objects directly.  You can create
033 * XMLDocumentContainers for various XML documents that may or
034 * may not be accessed by XPaths.  If they are, they will be automatically
035 * read, parsed and traversed. If they are not - they won't be
036 * read at all.
037 *
038 * @deprecated 1.1 Please use {@link DocumentContainer}
039 *
040 * @author Dmitri Plotnikov
041 * @version $Revision: 1234255 $ $Date: 2012-01-21 04:11:46 +0100 (Sa, 21 Jan 2012) $
042 */
043public class XMLDocumentContainer implements Container {
044
045    private DocumentContainer delegate;
046    private Object document;
047    private URL xmlURL;
048    private Source source;
049
050    /**
051     * Create a new XMLDocumentContainer.
052     * @param xmlURL a URL for an XML file. Use getClass().getResource(resourceName)
053     *               to load XML from a resource file.
054     */
055    public XMLDocumentContainer(URL xmlURL) {
056        this.xmlURL = xmlURL;
057        delegate = new DocumentContainer(xmlURL);
058    }
059
060    /**
061     * Create a new XMLDocumentContainer.
062     * @param source XML source
063     */
064    public XMLDocumentContainer(Source source) {
065        this.source = source;
066        if (source == null) {
067            throw new RuntimeException("Source is null");
068        }
069    }
070
071    /**
072     * Reads XML, caches it internally and returns the Document.
073     * @return Object value
074     */
075    public Object getValue() {
076        if (document == null) {
077            try {
078                if (source != null) {
079                    DOMResult result = new DOMResult();
080                    Transformer trans =
081                        TransformerFactory.newInstance().newTransformer();
082                    trans.transform(source, result);
083                    document = result.getNode();
084                }
085                else {
086                    document = delegate.getValue();
087                }
088            }
089            catch (Exception ex) {
090                throw new JXPathException(
091                    "Cannot read XML from: "
092                        + (xmlURL != null
093                            ? xmlURL.toString()
094                            : (source != null
095                                ? source.getSystemId()
096                                : "<<undefined source>>")),
097                    ex);
098            }
099        }
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}