- Timestamp:
- 2014-04-10T14:27:43+12:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/XMLConverter.java
r28849 r28962 31 31 import org.xml.sax.ErrorHandler; 32 32 import org.xml.sax.SAXParseException; 33 import org.xml.sax.SAXNotRecognizedException; 34 import org.xml.sax.SAXNotSupportedException; 33 35 import org.apache.xerces.parsers.DOMParser; 34 import org.apache.xerces.dom.*; // for new Documents 36 import org.apache.xerces.dom.DocumentImpl; // for new Documents 37 import org.apache.xerces.dom.DocumentTypeImpl; 35 38 36 39 // other java classes … … 58 61 * XMLConverter - utility class for greenstone 59 62 * 60 * parses XML Strings into Documents, converts Nodes to Strings different 61 * parsers have different behaviour - can experiment in here now we only use 62 * xerces 63 * 63 * generates new Documents 64 * parses XML Strings into Documents, converts Nodes to Strings 65 * different parsers have different behaviour - can experiment in here 66 * at the moment we only use xerces 67 * all xerces specific code is in here 64 68 */ 65 69 public class XMLConverter … … 68 72 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.XMLConverter.class.getName()); 69 73 70 protected EntityResolver resolver = null; 71 72 /** xerces parser */ 73 protected DOMParser parser = null; 74 //protected EntityResolver resolver = null; 74 75 75 76 private static boolean outputEscaping = true; … … 78 79 public XMLConverter() 79 80 { 80 try 81 { 82 this.parser = new DOMParser(); 83 this.parser.setFeature("http://xml.org/sax/features/validation", false); 84 // don't try and load external DTD - no need if we are not validating, and may cause connection errors if a proxy is not set up. 85 this.parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 86 // a performance test showed that having this on lead to increased 87 // memory use for small-medium docs, and not much gain for large 88 // docs. 89 // http://www.sosnoski.com/opensrc/xmlbench/conclusions.html 90 this.parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false); 91 // add an errorhandler to the parser which will store useful a error message on encountering fatal errors, errors and warnings when parsing 92 // this errormessage can then be converted to xhtml and displayed in a browser. 93 this.parser.setErrorHandler(new ParseErrorHandler()); 94 } 95 catch (Exception e) 96 { 97 logger.error(e.getMessage()); 98 } 99 } 100 101 /** sets the entity resolver. pass in null to unset it */ 102 public void setEntityResolver(EntityResolver er) 103 { 104 this.resolver = er; 105 this.parser.setEntityResolver(er); 106 } 107 108 /** 109 * Given a Node representing an Element or Document, will return the 110 * Element/docroot Element. Returns null if the Node was not an element. 111 */ 112 public static Element nodeToElement(Node node) 113 { 114 if (node == null) 115 { 116 return null; 117 } 118 short nodeType = node.getNodeType(); 119 120 if (nodeType == Node.DOCUMENT_NODE) 121 { 122 Document docNode = (Document) node; 123 return docNode.getDocumentElement(); 124 } 125 else if (nodeType == Node.ELEMENT_NODE) 126 { 127 return (Element) node; 128 } 129 else 130 { 131 String message = "Expecting Document or Element node type but got " + node.getNodeName() + "\nReturning null"; 132 System.err.println(message); 133 logger.warn(message); 134 return null; 135 } 136 } 81 82 } 83 84 // /** sets the entity resolver. pass in null to unset it */ 85 // public void setEntityResolver(EntityResolver er) 86 // { 87 // this.resolver = er; 88 // } 137 89 138 90 /** returns a DOM Document */ 139 public Document getDOM(String in)91 public static Document getDOM(String in) 140 92 { 141 93 … … 144 96 Reader reader = new StringReader(in); 145 97 InputSource xml_source = new InputSource(reader); 146 147 DOMParser parser = new DOMParser(); 148 parser.setFeature("http://xml.org/sax/features/validation", false); 149 parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 150 parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false); 151 if (resolver != null) 152 { 153 parser.setEntityResolver(this.resolver); 154 } 155 parser.setErrorHandler(new ParseErrorHandler()); 156 parser.parse(xml_source); 157 158 Document doc = parser.getDocument(); 159 98 Document doc = getDOM(xml_source, null); 160 99 reader.close(); 161 100 return doc; … … 170 109 171 110 /** returns a DOM Document */ 172 public Document getDOM(String in, String encoding)111 public static Document getDOM(String in, String encoding) 173 112 { 174 113 try … … 176 115 InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(in.getBytes(encoding)), encoding); 177 116 InputSource xml_source = new InputSource(reader); 178 179 DOMParser parser = new DOMParser(); 180 parser.setFeature("http://xml.org/sax/features/validation", false); 181 parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 182 parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false); 183 if (resolver != null) 184 { 185 parser.setEntityResolver(this.resolver); 186 } 187 parser.setErrorHandler(new ParseErrorHandler()); 188 parser.parse(xml_source); 189 190 Document doc = parser.getDocument(); 191 117 Document doc = getDOM(xml_source, null); 192 118 reader.close(); 193 119 return doc; … … 202 128 203 129 /** returns a DOM Document */ 204 public Document getDOM(File in) 205 { 130 public static Document getDOM(File in) { 206 131 try 207 132 { 208 133 FileReader reader = new FileReader(in); 209 134 InputSource xml_source = new InputSource(reader); 210 211 DOMParser parser = new DOMParser(); 212 parser.setFeature("http://xml.org/sax/features/validation", false); 213 parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 214 parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false); 215 if (resolver != null) 216 { 217 parser.setEntityResolver(this.resolver); 218 } 219 parser.setErrorHandler(new ParseErrorHandler()); 220 parser.parse(xml_source); 221 222 Document doc = parser.getDocument(); 223 135 Document doc = getDOM(xml_source, null); 224 136 reader.close(); 225 137 return doc; … … 234 146 } 235 147 148 public static Document getDOM(File in, String encoding) { 149 return getDOM(in, encoding, null); 150 } 151 236 152 /** returns a DOM document */ 237 public Document getDOM(File in, String encoding) 238 { 239 try 240 { 241 242 InputStreamReader isr = new InputStreamReader(new FileInputStream(in), encoding); 243 InputSource xml_source = new InputSource(isr); 244 245 DOMParser parser = new DOMParser(); 246 parser.setFeature("http://xml.org/sax/features/validation", false); 247 parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 248 parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false); 249 if (resolver != null) 250 { 251 parser.setEntityResolver(this.resolver); 252 } 253 parser.setErrorHandler(new ParseErrorHandler()); 254 parser.parse(xml_source); 255 256 Document doc = parser.getDocument(); 257 258 isr.close(); 259 return doc; 260 261 } 262 catch (Exception e) 263 { 264 logger.error(e.getMessage()); 265 } 266 return null; 267 } 153 public static Document getDOM(File in, String encoding, EntityResolver er) { 154 155 try { 156 157 158 InputStreamReader isr = new InputStreamReader(new FileInputStream(in), encoding); 159 InputSource xml_source = new InputSource(isr); 160 Document doc = getDOM(xml_source, er); 161 isr.close(); 162 return doc; 163 164 } 165 catch (Exception e) 166 { 167 logger.error(e.getMessage()); 168 } 169 return null; 170 } 171 172 public static Document getDOM(InputSource source, EntityResolver er) { 173 174 try { 175 DOMParser parser = new DOMParser(); 176 parser.setFeature("http://xml.org/sax/features/validation", false); 177 // don't try and load external DTD - no need if we are not validating, and may cause connection errors if a proxy is not set up. 178 parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 179 // a performance test showed that having this on lead to increased 180 // memory use for small-medium docs, and not much gain for large 181 // docs. 182 // http://www.sosnoski.com/opensrc/xmlbench/conclusions.html 183 parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false); 184 // add an errorhandler to the parser which will output messages on encountering fatal errors, errors and warnings when parsing 185 parser.setErrorHandler(new ParseErrorHandler()); 186 if (er != null) { 187 parser.setEntityResolver(er); 188 } 189 parser.parse(source); 190 191 Document doc = parser.getDocument(); 192 return doc; 193 194 } catch (Exception e) { 195 196 logger.error(e.getMessage()); 197 } 198 return null; 199 200 } 268 201 269 202 /** creates a new empty DOM Document */ 270 public Document newDOM()203 public static Document newDOM() 271 204 { 272 205 Document doc = new DocumentImpl(); … … 563 496 564 497 // returns null if there no error occurred during parsing, or else returns the error message 565 public String getParseErrorMessage() 566 { 567 ParseErrorHandler errorHandler = (ParseErrorHandler) this.parser.getErrorHandler(); 568 return errorHandler.getErrorMessage(); 569 } 498 499 // public String getParseErrorMessage() 500 // { 501 // ParseErrorHandler errorHandler = (ParseErrorHandler) this.parser.getErrorHandler(); 502 // return errorHandler.getErrorMessage(); 503 // } 570 504 571 505 // Errorhandler for SAXParseExceptions that are errors, fatal errors or warnings. This class can be used to … … 626 560 } 627 561 628 public boolean writeDOM(Element elem, File file) {562 public static boolean writeDOM(Element elem, File file) { 629 563 630 564 BufferedWriter writer = null;
Note:
See TracChangeset
for help on using the changeset viewer.