Changeset 18434
- Timestamp:
- 2009-01-30T16:58:40+13:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
greenstone3/trunk/src/java/org/greenstone/gsdl3/util/XMLConverter.java
r18307 r18434 20 20 21 21 // XML classes 22 import org.w3c.dom.DOMImplementation; 22 23 import org.w3c.dom.Document; 23 24 import org.w3c.dom.DocumentType; … … 28 29 import org.xml.sax.InputSource; 29 30 import org.xml.sax.EntityResolver; 31 import org.xml.sax.ErrorHandler; 32 import org.xml.sax.SAXParseException; 30 33 import org.apache.xerces.parsers.DOMParser; 31 import org.apache.xerces.dom. DocumentImpl; // for new Documents34 import org.apache.xerces.dom.*; // for new Documents 32 35 33 36 // other java classes … … 70 73 // http://www.sosnoski.com/opensrc/xmlbench/conclusions.html 71 74 this.parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false); 75 // add an errorhandler to the parser which will store useful a error message on encountering fatal errors, errors and warnings when parsing 76 // this errormessage can then be converted to xhtml and displayed in a browser. 77 this.parser.setErrorHandler(new ParseErrorHandler()); 72 78 } catch (Exception e) { 73 79 logger.error(e.getMessage()); … … 163 169 return doc; 164 170 } 171 172 /** 173 * This method's parameters represent the parts of the Doctype of this 174 * Document that is to be created. 175 * For more info see 176 * http://xerces.apache.org/xerces-j/apiDocs/org/apache/xerces/dom/DocumentTypeImpl.html#DocumentTypeImpl(org.apache.xerces.dom.CoreDocumentImpl,%20java.lang.String) 177 * 178 * */ 179 public static Document newDOM(String qualifiedName, String publicID, String systemID) { 180 // create empty DOM document 181 DocumentImpl docImpl = new DocumentImpl(); 182 183 // Need to use the document to create the docType for it 184 DocumentType myDocType = new DocumentTypeImpl(docImpl, qualifiedName, publicID, systemID); 185 186 // Although we have created the docType using the document, we need to still 187 // put it into the empty document we just created 188 try{ 189 docImpl.appendChild(myDocType); 190 } catch(Exception e) { 191 System.out.println("Could not append docType because: " + e) ; 192 } 193 194 // return the document containing a DocType 195 return docImpl; 196 } 165 197 166 198 /** returns the Node as a String */ … … 199 231 Document xmlDocNode = (Document)xmlNode; 200 232 201 if (xmlDocNode.getDoctype() == null) {202 System.err.println("Doctype is null.");203 }204 else {205 233 //if (xmlDocNode.getDoctype() == null) { 234 //System.err.println("Doctype is null."); 235 //} 236 //else { 237 if (xmlDocNode.getDoctype() != null) { 206 238 DocumentType dt = xmlDocNode.getDoctype(); 207 239 … … 378 410 return type; 379 411 } 412 413 // returns null if there no error occurred during parsing, or else returns the error message 414 public String getParseErrorMessage() { 415 ParseErrorHandler errorHandler = (ParseErrorHandler)this.parser.getErrorHandler(); 416 return errorHandler.getErrorMessage(); 417 } 418 419 // Errorhandler for SAXParseExceptions that are errors, fatal errors or warnings. This class can be used to 420 // register a handler for any fatal errors, errors and warnings that may occur when parsing an xml file. The 421 // errors are printed both to the greenstone.log and to the tomcat console (System.err), and the error message 422 // is stored in the errorMessage variable so that it can be retrieved and be used to generate an xhtml error page. 423 static public class ParseErrorHandler implements ErrorHandler { 424 protected String errorMessage = null; 425 426 // Receive notification of a recoverable error. 427 public void error(SAXParseException exception) { 428 handleError("Error:\n", exception); 429 } 430 // Receive notification of a non-recoverable error. 431 public void fatalError(SAXParseException exception) { 432 handleError("Fatal Error:\n", exception); 433 } 434 // Receive notification of a warning. 435 public void warning(SAXParseException exception) { 436 handleError("Warning:\n", exception); 437 } 438 439 public String toString(SAXParseException e) { 440 String msg = e.getMessage(); 441 msg += "\nOn line(column): " + e.getLineNumber() + "(" + e.getColumnNumber() + ")"; 442 msg += (e.getPublicId() != null) ? ("\npublic ID: " + e.getPublicId()) : "\nNo public ID"; 443 msg += (e.getSystemId() != null) ? ("\nsystem ID: " + e.getSystemId()) : "\nNo system ID"; 444 445 return msg; 446 } 447 448 // clears the errorPage variable after first call to this method 449 public String getErrorMessage() { 450 String errMsg = this.errorMessage; 451 if(this.errorMessage != null) { 452 this.errorMessage = null; 453 } 454 return errMsg; 455 } 456 457 // sets the errorMessage member variable to the data stored in the exception 458 // and writes the errorMessage to the logger and tomcat's System.err 459 protected void handleError(String errorType, SAXParseException exception) { 460 this.errorMessage = errorType + toString(exception); 461 System.err.println("\n****Error parsing xml:\n" + this.errorMessage + "\n****\n"); 462 logger.error(this.errorMessage); 463 } 464 } 380 465 }
Note:
See TracChangeset
for help on using the changeset viewer.