1 16 package org.apache.commons.jxpath; 17 18 import java.net.URL ; 19 20 import javax.xml.transform.Source ; 21 import javax.xml.transform.Transformer ; 22 import javax.xml.transform.TransformerFactory ; 23 import javax.xml.transform.dom.DOMResult ; 24 25 import org.apache.commons.jxpath.xml.DocumentContainer; 26 import org.w3c.dom.Document ; 27 28 43 public class XMLDocumentContainer implements Container { 44 45 private DocumentContainer delegate; 46 private Object document; 47 private URL xmlURL; 48 private Source source; 49 private String parser; 50 51 55 public XMLDocumentContainer(URL xmlURL) { 56 delegate = new DocumentContainer(xmlURL); 57 } 58 59 public XMLDocumentContainer(Source source) { 60 this.source = source; 61 if (source == null) { 62 throw new RuntimeException ("Source is null"); 63 } 64 } 65 66 69 public Object getValue() { 70 if (document == null) { 71 try { 72 if (source != null) { 73 DOMResult result = new DOMResult (); 74 Transformer trans = 75 TransformerFactory.newInstance().newTransformer(); 76 trans.transform(source, result); 77 document = (Document ) result.getNode(); 78 } 79 else { 80 document = delegate.getValue(); 81 } 82 } 83 catch (Exception ex) { 84 throw new JXPathException( 85 "Cannot read XML from: " 86 + (xmlURL != null 87 ? xmlURL.toString() 88 : (source != null 89 ? source.getSystemId() 90 : "<<undefined source>>")), 91 ex); 92 } 93 } 94 return document; 95 } 96 97 100 public void setValue(Object value) { 101 throw new UnsupportedOperationException (); 102 } 103 } | Popular Tags |