[jdom-interest] ValidityException

Elliotte Rusty Harold elharo at metalab.unc.edu
Sun Jun 1 13:46:09 PDT 2003


I've attached a patch for several existing files and two new classes for 
org.jdom.input that together enable JDOM to throw a ValidityException 
when a validity error is detected instead of a generic JDOMParseException.

Be careful when applying these. I had some trouble syncing my tree to 
the latest CVS. I think it's all there now, but do a sanity check first.

--
Elliotte
-------------- next part --------------
? build
? src/java/org/jdom/input/SAXValidityException.java
? src/java/org/jdom/input/ValidityException.java
Index: src/java/org/jdom/Document.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/Document.java,v
retrieving revision 1.71
diff -r1.71 Document.java
131c131
<      * @throws IllegalAddException if the given docType object
---
>      * @throws IllegalAddException if the given DocType object
578,580c578,580
<      * @param ob <code>Object</code> to compare to.
<      * @return <code>boolean</code> - whether the <code>Document</code> is
<      *         equal to the supplied <code>Object</code>.
---
>      * @param ob <code>Object</code> to compare to
>      * @return <code>boolean</code> whether the <code>Document</code> is
>      *         equal to the supplied <code>Object</code>
589c589
<      * @return <code>int</code> - hash code.
---
>      * @return <code>int</code> hash code
598c598
<      * @return <code>Object</code> - clone of this <code>Document</code>.
---
>      * @return <code>Object</code> clone of this <code>Document</code>
Index: src/java/org/jdom/Verifier.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/Verifier.java,v
retrieving revision 1.47
diff -r1.47 Verifier.java
611c611
<      *
---
>      * 
633,634c633
<                        if (!isHexDigit(firstDigit) ||
<                            !isHexDigit(secondDigit)) {
---
>                        if (!isHexDigit(firstDigit) || !isHexDigit(secondDigit)) {
636c635
<                             + "exactly two hexadecimal digits.";
---
>                             + "exactly two hexadecimal digits.";    
638c637
< 
---
>           
642c641
<                         + "exactly two hexadecimal digits.";
---
>                         + "exactly two hexadecimal digits.";    
712c711
< 
---
>     
718c717
<      * @param c <code>char</code> to check for XML compliance.
---
>      * @param c <code>char</code> to check for XML compliance
720c719
<      *                                false otherwise.
---
>      *                                false otherwise
1177,1178c1176,1177
<      * @param c <code>char</code> to check for XML digit compliance.
<      * @return <code>boolean</code> true if it's a digit, false otherwise.
---
>      * @param c <code>char</code> to check for XML digit compliance
>      * @return <code>boolean</code> true if it's a digit, false otherwise
Index: src/java/org/jdom/input/BuilderErrorHandler.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/input/BuilderErrorHandler.java,v
retrieving revision 1.11
diff -r1.11 BuilderErrorHandler.java
72a73,78
>     private boolean validating;  
>       
>     public BuilderErrorHandler(boolean reportValidityErrors) {
>         this.validating = reportValidityErrors;
>     }
>      
96c102,107
<         throw exception;
---
>         if (validating) {
>             throw new SAXValidityException(exception.getMessage(), 
>               exception.getPublicId(), exception.getSystemId(),
>               exception.getLineNumber(), exception.getColumnNumber());
>         }
>         else throw exception;
Index: src/java/org/jdom/input/SAXBuilder.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/input/SAXBuilder.java,v
retrieving revision 1.78
diff -r1.78 SAXBuilder.java
410,412c410,412
<      * @param in <code>InputSource</code> to read from.
<      * @return <code>Document</code> - resultant Document object.
<      * @throws JDOMException when errors occur in parsing.
---
>      * @param in <code>InputSource</code> to read from
>      * @return <code>Document</code> resultant Document object
>      * @throws JDOMException when errors occur in parsing
414c414
<      *         from being fully parsed.
---
>      *         from being fully parsed
451c451
<                 // Reset content handler as SAXHandler instances can not
---
>                 // Reset content handler as SAXHandler instances cannot
460a461,472
>         catch (SAXValidityException e) {
>             String systemId = e.getSystemId();
>             if (systemId != null) {
>                 throw new ValidityException("Validity error on line " + 
>                     e.getLineNumber() + " of document " + systemId,
>                     e, contentHandler.getDocument());
>             } else {
>                 throw new ValidityException("Validity error on line " +
>                     e.getLineNumber(), e,
>                     contentHandler.getDocument());
>             }
>         }
621c633
<              parser.setErrorHandler(new BuilderErrorHandler());
---
>              parser.setErrorHandler(new BuilderErrorHandler(validate));
763,765c775,779
<      * @param in <code>InputStream</code> to read from.
<      * @return <code>Document</code> - resultant Document object.
<      * @throws JDOMException when errors occur in parsing.
---
>      * @param in <code>InputStream</code> to read from
>      * @return <code>Document</code> resultant Document object
>      * @throws JDOMException when errors occur in parsing
>      * @throws ValidityException when the document is invalid and this 
>      *         builder has been configured to validate
780,782c794,798
<      * @param file <code>File</code> to read from.
<      * @return <code>Document</code> - resultant Document object.
<      * @throws JDOMException when errors occur in parsing.
---
>      * @param file <code>File</code> to read from
>      * @return <code>Document</code> resultant Document object
>      * @throws JDOMException when errors occur in parsing
>      * @throws ValidityException when the document is invalid and this 
>      *     builder has been configured to validate
784c800
<      *         from being fully parsed.
---
>      *         from being fully parsed
804c820,822
<      * @throws JDOMException when errors occur in parsing.
---
>      * @throws JDOMException when errors occur in parsing
>      * @throws ValidityException when the document is invalid and this 
>      *     builder has been configured to validate
822,823c840,843
<      * @return <code>Document</code> - resultant Document object.
<      * @throws JDOMException when errors occur in parsing.
---
>      * @return <code>Document</code> resultant Document object
>      * @throws JDOMException when errors occur in parsing
>      * @throws ValidityException when the document is invalid and this 
>      *     builder has been configured to validate
825c845
<      *         from being fully parsed.
---
>      *         from being fully parsed
843,845c863,867
<      * @param characterStream <code>Reader</code> to read from.
<      * @return <code>Document</code> - resultant Document object.
<      * @throws JDOMException when errors occur in parsing.
---
>      * @param characterStream <code>Reader</code> to read from
>      * @return <code>Document</code> resultant Document object
>      * @throws JDOMException when errors occur in parsing
>      * @throws ValidityException when the document is invalid and this 
>      *     builder has been configured to validate
847c869
<      *         from being fully parsed.
---
>      *         from being fully parsed
864,865c886,889
<      * @return <code>Document</code> - resultant Document object.
<      * @throws JDOMException when errors occur in parsing.
---
>      * @return <code>Document</code> resultant Document object
>      * @throws JDOMException when errors occur in parsing
>      * @throws ValidityException when the document is invalid and this 
>      *     builder has been configured to validate
867c891
<      *         from being fully parsed.
---
>      *         from being fully parsed
883,884c907,910
<      * @return <code>Document</code> - resultant Document object.
<      * @throws JDOMException when errors occur in parsing.
---
>      * @return <code>Document</code> resultant Document object
>      * @throws JDOMException when errors occur in parsing
>      * @throws ValidityException when the document is invalid and this 
>      *     builder has been configured to validate
886c912
<      *         from being fully parsed.
---
>      *         from being fully parsed
-------------- next part --------------
/*-- 

 $Id: ValidityException.java,v 1.00 2003/05/26 09:55:12 jhunter Exp $

 Copyright (C) 2000 Jason Hunter & Brett McLaughlin.
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 
 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions, and the following disclaimer.
 
 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions, and the disclaimer that follows 
    these conditions in the documentation and/or other materials 
    provided with the distribution.

 3. The name "JDOM" must not be used to endorse or promote products
    derived from this software without prior written permission.  For
    written permission, please contact <request_AT_jdom_DOT_org>.
 
 4. Products derived from this software may not be called "JDOM", nor
    may "JDOM" appear in their name, without prior written permission
    from the JDOM Project Management <request_AT_jdom_DOT_org>.
 
 In addition, we request (but do not require) that you include in the 
 end-user documentation provided with the redistribution and/or in the 
 software itself an acknowledgement equivalent to the following:
     "This product includes software developed by the
      JDOM Project (http://www.jdom.org/)."
 Alternatively, the acknowledgment may be graphical using the logos 
 available at http://www.jdom.org/images/logos.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED.  IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

 This software consists of voluntary contributions made by many 
 individuals on behalf of the JDOM Project and was originally 
 created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
 Brett McLaughlin <brett_AT_jdom_DOT_org>.  For more information
 on the JDOM Project, please see <http://www.jdom.org/>.
 
 */

package org.jdom.input;

import org.jdom.Document;

/**
 * An exception that indicates a validity error while building
 * a document. This will only be thrown if the SAXBuilder
 * has explictly been asked to validate. Validation is not
 * perfomred by default.
 *
 * @version $Revision: 1.00 $, $Date: 2003/05/26 09:55:12 $
 * @author  Elliotte Rusty Harold
 */
public class ValidityException extends JDOMParseException {

    /**
     * This will create a <code>ValidityException</code> with the given message
     * and wrap another <code>Exception</code>, probably a 
     * <code>SAXParseException</code>. This is useful when
     * the originating <code>Exception</code> should be held on to.
     *
     * @param message <code>String</code> message indicating
     *                the problem that occurred
     * @param cause <code>Throwable</code> that caused this
     *                  to be thrown
     */    
    public ValidityException(String message, Throwable cause)  {
        super(message, cause);
    }    

    /**
     * This will create a <code>ValidityException</code> with the given message
     * and wrap another <code>Exception</code>, probably a 
     * <code>SAXParseException</code>. This is useful when
     * the originating <code>Exception</code> should be held on to.
     *
     * @param message <code>String</code> message indicating
     *                the problem that occurred
     * @param cause <code>Throwable</code> that caused this
     *                  to be thrown
     * @param partialDocument <code>Document</code> up to the first validity error
     */    
    public ValidityException(String message, Throwable cause, Document partialDocument)  {
        super(message, cause, partialDocument);
    }    

}
-------------- next part --------------
/*-- 

 $Id: SAXValidityException.java,v 1.00 2003/05/26 09:55:12 jhunter Exp $

 Copyright (C) 2000 Jason Hunter & Brett McLaughlin.
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 
 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions, and the following disclaimer.
 
 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions, and the disclaimer that follows 
    these conditions in the documentation and/or other materials 
    provided with the distribution.

 3. The name "JDOM" must not be used to endorse or promote products
    derived from this software without prior written permission.  For
    written permission, please contact <request_AT_jdom_DOT_org>.
 
 4. Products derived from this software may not be called "JDOM", nor
    may "JDOM" appear in their name, without prior written permission
    from the JDOM Project Management <request_AT_jdom_DOT_org>.
 
 In addition, we request (but do not require) that you include in the 
 end-user documentation provided with the redistribution and/or in the 
 software itself an acknowledgement equivalent to the following:
     "This product includes software developed by the
      JDOM Project (http://www.jdom.org/)."
 Alternatively, the acknowledgment may be graphical using the logos 
 available at http://www.jdom.org/images/logos.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED.  IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

 This software consists of voluntary contributions made by many 
 individuals on behalf of the JDOM Project and was originally 
 created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
 Brett McLaughlin <brett_AT_jdom_DOT_org>.  For more information
 on the JDOM Project, please see <http://www.jdom.org/>.
 
 */

package org.jdom.input;

import org.xml.sax.SAXParseException;

/**
 * An exception used to signal a validity error from the ErrorHandler
 * to the builder.
 *
 * @version $Revision: 1.00 $, $Date: 2003/05/26 09:55:12 $
 * @author  Elliotte Rusty Harold
 */
public class SAXValidityException extends SAXParseException {

    private Throwable cause;
    
    /**
     * This will create a <code>SAXValidityException</code> with the given message.
     *
     * @param message <code>String</code> message indicating
     *                the problem occurred
     * @param publicID <code>publicID</code> of the entity where
     *                the problem occurred
     * @param systemID <code>systemID</code> (URL) of the entity where
     *                the problem occurred
     * @param line line number where the problem occurred
     * @param column column number where the problem occurred
     */    
    public SAXValidityException(String message, String publicID, String systemID, int line, int column)  {
        super(message, publicID, systemID, line, column);
    }

    /**
     * This will create a <code>ValidityException</code> with the given message
     * and wrap another <code>Exception</code>, probably a 
     * <code>SAXParseException</code>. This is useful when
     * the originating <code>Exception</code> should be held on to.
     *
     * @param message <code>String</code> message indicating
     *                the problem that occurred.
     * @param publicID <code>publicID</code> of the entity where
     *                the problem occurred
     * @param systemID <code>systemID</code> (URL) of the entity where
     *                the problem occurred
     * @param line line number where the problem occurred
     * @param column column number where the problem occurred
     * @param cause <code>Throwable</code> that caused this
     *              to be thrown.
     */    
    public SAXValidityException(String message, String publicID, String systemID, int line, int column, Exception cause)  {
        super(message, publicID, systemID, line, column, cause);
    }    

}


More information about the jdom-interest mailing list