Ignore:
Timestamp:
2009-01-30T16:58:40+13:00 (15 years ago)
Author:
max
Message:
  1. Provides a new DOM method that allows a document to be created with a specified DOCTYPE.
  2. Reusable ErrorHandler class for parsing, that will deal with warnings, fatal and regular errors that may occur when parsing XML.
  3. XMLConverter's parse object has an ErrorHandler attached to it when initialised.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/util/XMLConverter.java

    r18307 r18434  
    2020
    2121// XML classes
     22import org.w3c.dom.DOMImplementation;
    2223import org.w3c.dom.Document;
    2324import org.w3c.dom.DocumentType;
     
    2829import org.xml.sax.InputSource;
    2930import org.xml.sax.EntityResolver;
     31import org.xml.sax.ErrorHandler;
     32import org.xml.sax.SAXParseException;
    3033import org.apache.xerces.parsers.DOMParser;
    31 import org.apache.xerces.dom.DocumentImpl; // for new Documents
     34import org.apache.xerces.dom.*; // for new Documents
    3235
    3336// other java classes
     
    7073        // http://www.sosnoski.com/opensrc/xmlbench/conclusions.html
    7174        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());
    7278    } catch (Exception e) {
    7379        logger.error(e.getMessage());
     
    163169    return doc;
    164170    }
     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    }
    165197
    166198    /** returns the Node as a String */
     
    199231        Document xmlDocNode = (Document)xmlNode;
    200232       
    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) {
    206238        DocumentType dt = xmlDocNode.getDoctype();
    207239       
     
    378410    return type;
    379411    }
     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    }
    380465}
Note: See TracChangeset for help on using the changeset viewer.