org.custommonkey.xmlunit
Class Validator

java.lang.Object
  extended byorg.xml.sax.helpers.DefaultHandler
      extended byorg.custommonkey.xmlunit.Validator
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler

public class Validator
extends org.xml.sax.helpers.DefaultHandler
implements org.xml.sax.ErrorHandler

Validates XML against its internal or external DOCTYPE, or a completely different DOCTYPE. Usage:


Examples and more at xmlunit.sourceforge.net


Constructor Summary
  Validator(org.w3c.dom.Document document, java.lang.String systemID, java.lang.String doctype)
          DOM-style constructor: allows Document validation post-manipulation of the DOM tree's contents.
protected Validator(org.xml.sax.InputSource inputSource, boolean usingDoctypeReader)
          Baseline constructor: called by all others
  Validator(java.io.Reader readerForValidation)
          Basic constructor.
  Validator(java.io.Reader readerForValidation, java.lang.String systemID)
          Extended constructor.
  Validator(java.io.Reader readerForValidation, java.lang.String systemID, java.lang.String doctype)
          Full constructor.
 
Method Summary
 void assertIsValid()
          Assert that a document is valid.
 void error(org.xml.sax.SAXParseException exception)
          ErrorHandler interface method
 void fatalError(org.xml.sax.SAXParseException exception)
          ErrorHandler interface method
 boolean isValid()
          Perform the validation of the source against DTD
 org.xml.sax.InputSource resolveEntity(java.lang.String publicId, java.lang.String systemId)
          Entity Resolver method: allows us to override an existing systemID referenced in the markup DOCTYPE instruction
 java.lang.String toString()
           
 void useXMLSchema(boolean use)
          Turn on XML Schema validation.
 void warning(org.xml.sax.SAXParseException exception)
          ErrorHandler interface method
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
characters, endDocument, endElement, endPrefixMapping, ignorableWhitespace, notationDecl, processingInstruction, setDocumentLocator, skippedEntity, startDocument, startElement, startPrefixMapping, unparsedEntityDecl
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Validator

protected Validator(org.xml.sax.InputSource inputSource,
                    boolean usingDoctypeReader)
             throws javax.xml.parsers.ParserConfigurationException,
                    org.xml.sax.SAXException
Baseline constructor: called by all others

Parameters:
inputSource -
usingDoctypeReader -
Throws:
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXException

Validator

public Validator(org.w3c.dom.Document document,
                 java.lang.String systemID,
                 java.lang.String doctype)
          throws javax.xml.parsers.ParserConfigurationException,
                 org.xml.sax.SAXException
DOM-style constructor: allows Document validation post-manipulation of the DOM tree's contents. This takes a fairly tortuous route to validation as DOM level 2 does not allow creation of Doctype nodes. The supplied systemId and doctype name will replace any Doctype settings in the Document.

Parameters:
document -
systemID -
doctype -
Throws:
javax.xml.parsers.ParserConfigurationException - if unable to turn validation feature on in JAXP factory
org.xml.sax.SAXException - if unable to obtain new Sax parser via JAXP factory

Validator

public Validator(java.io.Reader readerForValidation)
          throws javax.xml.parsers.ParserConfigurationException,
                 org.xml.sax.SAXException
Basic constructor. Validates the contents of the Reader using the DTD or schema referenced by those contents.

Parameters:
readerForValidation -
Throws:
javax.xml.parsers.ParserConfigurationException - if unable to turn validation feature on in JAXP factory
org.xml.sax.SAXException - if unable to obtain new Sax parser via JAXP factory

Validator

public Validator(java.io.Reader readerForValidation,
                 java.lang.String systemID)
          throws javax.xml.parsers.ParserConfigurationException,
                 org.xml.sax.SAXException
Extended constructor. Validates the contents of the Reader using the DTD specified with the systemID. There must be DOCTYPE instruction in the markup that references the DTD or else the markup will be considered invalid: if there is no DOCTYPE in the markup use the 3-argument constructor

Parameters:
readerForValidation -
systemID -
Throws:
javax.xml.parsers.ParserConfigurationException - if unable to turn validation feature on in JAXP factory
org.xml.sax.SAXException - if unable to obtain new Sax parser via JAXP factory

Validator

public Validator(java.io.Reader readerForValidation,
                 java.lang.String systemID,
                 java.lang.String doctype)
          throws javax.xml.parsers.ParserConfigurationException,
                 org.xml.sax.SAXException
Full constructor. Validates the contents of the Reader using the DTD specified with the systemID and named with the doctype name.

Parameters:
readerForValidation -
systemID -
doctype -
Throws:
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXException
Method Detail

useXMLSchema

public void useXMLSchema(boolean use)
                  throws org.xml.sax.SAXException
Turn on XML Schema validation. Calling this method sets the featues http://apache.org/xml/features/validation/schema & http://apache.org/xml/features/validation/dynamic to true. Currently this feature only works with the Xerces 2 XML parser

Parameters:
use - indicate that XML Schema should be used to validate documents.
Throws:
org.xml.sax.SAXException

isValid

public boolean isValid()
Perform the validation of the source against DTD

Returns:
true if the input supplied to the constructor passes validation, false otherwise

assertIsValid

public void assertIsValid()
Assert that a document is valid.


toString

public java.lang.String toString()
Returns:
class name appended with validation messages

warning

public void warning(org.xml.sax.SAXParseException exception)
             throws org.xml.sax.SAXException
ErrorHandler interface method

Specified by:
warning in interface org.xml.sax.ErrorHandler
Parameters:
exception -
Throws:
org.xml.sax.SAXException

error

public void error(org.xml.sax.SAXParseException exception)
           throws org.xml.sax.SAXException
ErrorHandler interface method

Specified by:
error in interface org.xml.sax.ErrorHandler
Parameters:
exception -
Throws:
org.xml.sax.SAXException

fatalError

public void fatalError(org.xml.sax.SAXParseException exception)
                throws org.xml.sax.SAXException
ErrorHandler interface method

Specified by:
fatalError in interface org.xml.sax.ErrorHandler
Parameters:
exception -
Throws:
org.xml.sax.SAXException

resolveEntity

public org.xml.sax.InputSource resolveEntity(java.lang.String publicId,
                                             java.lang.String systemId)
Entity Resolver method: allows us to override an existing systemID referenced in the markup DOCTYPE instruction

Specified by:
resolveEntity in interface org.xml.sax.EntityResolver
Parameters:
publicId -
systemId -
Returns:
the sax InputSource that points to the overridden systemID

XMLUnit is hosted by sourceforge.net