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 */ 017 package org.apache.commons.jxpath; 018 019 import java.net.URL; 020 021 import javax.xml.transform.Source; 022 import javax.xml.transform.Transformer; 023 import javax.xml.transform.TransformerFactory; 024 import javax.xml.transform.dom.DOMResult; 025 026 import org.apache.commons.jxpath.xml.DocumentContainer; 027 import org.w3c.dom.Document; 028 029 /** 030 * An XML document container reads and parses XML only when it is 031 * accessed. JXPath traverses Containers transparently - 032 * you use the same paths to access objects in containers as you 033 * do to access those objects directly. You can create 034 * XMLDocumentContainers for various XML documents that may or 035 * may not be accessed by XPaths. If they are, they will be automatically 036 * read, parsed and traversed. If they are not - they won't be 037 * read at all. 038 * 039 * @deprecated 1.1 Please use {@link DocumentContainer} 040 * 041 * @author Dmitri Plotnikov 042 * @version $Revision: 652845 $ $Date: 2008-05-02 12:46:46 -0500 (Fri, 02 May 2008) $ 043 */ 044 public class XMLDocumentContainer implements Container { 045 046 private DocumentContainer delegate; 047 private Object document; 048 private URL xmlURL; 049 private Source source; 050 051 /** 052 * Create a new XMLDocumentContainer. 053 * @param xmlURL a URL for an XML file. Use getClass().getResource(resourceName) 054 * to load XML from a resource file. 055 */ 056 public XMLDocumentContainer(URL xmlURL) { 057 this.xmlURL = xmlURL; 058 delegate = new DocumentContainer(xmlURL); 059 } 060 061 /** 062 * Create a new XMLDocumentContainer. 063 * @param source XML source 064 */ 065 public XMLDocumentContainer(Source source) { 066 this.source = source; 067 if (source == null) { 068 throw new RuntimeException("Source is null"); 069 } 070 } 071 072 /** 073 * Reads XML, caches it internally and returns the Document. 074 * @return Object value 075 */ 076 public Object getValue() { 077 if (document == null) { 078 try { 079 if (source != null) { 080 DOMResult result = new DOMResult(); 081 Transformer trans = 082 TransformerFactory.newInstance().newTransformer(); 083 trans.transform(source, result); 084 document = (Document) result.getNode(); 085 } 086 else { 087 document = delegate.getValue(); 088 } 089 } 090 catch (Exception ex) { 091 throw new JXPathException( 092 "Cannot read XML from: " 093 + (xmlURL != null 094 ? xmlURL.toString() 095 : (source != null 096 ? source.getSystemId() 097 : "<<undefined source>>")), 098 ex); 099 } 100 } 101 return document; 102 } 103 104 /** 105 * Throws an UnsupportedOperationException 106 * @param value to set 107 */ 108 public void setValue(Object value) { 109 throw new UnsupportedOperationException(); 110 } 111 }