1 16 package org.apache.commons.jxpath.xml; 17 18 import java.io.InputStream ; 19 import java.net.URL ; 20 import java.util.HashMap ; 21 22 import org.apache.commons.jxpath.Container; 23 import org.apache.commons.jxpath.JXPathException; 24 25 38 public class DocumentContainer extends XMLParser2 implements Container { 39 40 public static final String MODEL_DOM = "DOM"; 41 public static final String MODEL_JDOM = "JDOM"; 42 43 private Object document; 44 private URL xmlURL; 45 private String model; 46 47 private static HashMap parserClasses = new HashMap (); 48 static { 49 parserClasses.put(MODEL_DOM, 50 "org.apache.commons.jxpath.xml.DOMParser"); 51 parserClasses.put(MODEL_JDOM, 52 "org.apache.commons.jxpath.xml.JDOMParser"); 53 } 54 55 private static HashMap parsers = new HashMap (); 56 57 61 public static void registerXMLParser(String model, XMLParser parser) { 62 parsers.put(model, parser); 63 } 64 65 69 public static void registerXMLParser(String model, String parserClassName) { 70 parserClasses.put(model, parserClassName); 71 } 72 73 80 public DocumentContainer(URL xmlURL) { 81 this(xmlURL, MODEL_DOM); 82 } 83 84 91 public DocumentContainer(URL xmlURL, String model) { 92 this.xmlURL = xmlURL; 93 if (xmlURL == null) { 94 throw new JXPathException("XML URL is null"); 95 } 96 this.model = model; 97 } 98 99 102 public Object getValue() { 103 if (document == null) { 104 try { 105 InputStream stream = null; 106 try { 107 if (xmlURL != null) { 108 stream = xmlURL.openStream(); 109 } 110 document = parseXML(stream); 111 } 112 finally { 113 if (stream != null) { 114 stream.close(); 115 } 116 } 117 } 118 catch (Exception ex) { 119 throw new JXPathException( 120 "Cannot read XML from: " + xmlURL.toString(), 121 ex); 122 } 123 } 124 return document; 125 } 126 127 130 public Object parseXML(InputStream stream) { 131 XMLParser parser = getParser(model); 132 if (parser instanceof XMLParser2) { 133 XMLParser2 parser2 = (XMLParser2) parser; 134 parser2.setValidating(isValidating()); 135 parser2.setNamespaceAware(isNamespaceAware()); 136 parser2.setIgnoringElementContentWhitespace( 137 isIgnoringElementContentWhitespace()); 138 parser2.setExpandEntityReferences(isExpandEntityReferences()); 139 parser2.setIgnoringComments(isIgnoringComments()); 140 parser2.setCoalescing(isCoalescing()); 141 } 142 return parser.parseXML(stream); 143 } 144 145 148 public void setValue(Object value) { 149 throw new UnsupportedOperationException (); 150 } 151 152 155 private static final XMLParser getParser(String model) { 156 XMLParser parser = (XMLParser) parsers.get(model); 157 if (parser == null) { 158 String className = (String ) parserClasses.get(model); 159 if (className == null) { 160 throw new JXPathException("Unsupported XML model: " + model); 161 } 162 try { 163 Class clazz = Class.forName(className); 164 parser = (XMLParser) clazz.newInstance(); 165 } 166 catch (Exception ex) { 167 throw new JXPathException( 168 "Cannot allocate XMLParser: " + className); 169 } 170 parsers.put(model, parser); 171 } 172 return parser; 173 } 174 } | Popular Tags |