Package org.custommonkey.xmlunit
Class Validator
java.lang.Object
org.xml.sax.helpers.DefaultHandler
org.custommonkey.xmlunit.Validator
- All Implemented Interfaces:
ContentHandler
,DTDHandler
,EntityResolver
,ErrorHandler
Validates XML against its internal or external DOCTYPE, or a completely
different DOCTYPE.
Usage:
Examples and more at xmlunit.sourceforge.net
new Validator(readerForXML);
to validate some XML that contains or references an accessible DTD or schemanew Validator(readerForXML, systemIdForValidation);
to validate some XML that references a DTD but using a local systemId to perform the validationnew Validator(readerForXML, systemIdForValidation, doctypeName);
to validate some XML against a completely different DTD
Examples and more at xmlunit.sourceforge.net
-
Constructor Summary
ConstructorsModifierConstructorDescriptionBasic constructor.Extended constructor.Full constructor.Basic constructor.Extended constructor.DOM-style constructor: allows Document validation post-manipulation of the DOM tree's contents.Validator
(InputSource sourceForValidation) Basic constructor.protected
Validator
(InputSource inputSource, boolean usingDoctypeReader) Deprecated.Use the protected three arg constructor instead.Validator
(InputSource sourceForValidation, String systemID) Extended constructor.protected
Validator
(InputSource inputSource, String systemId, boolean usingDoctypeReader) Baseline constructor: called by all othersValidator
(InputSource sourceForValidation, String systemID, String doctype) Full constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Assert that a document is valid.void
error
(SAXParseException exception) ErrorHandler interface method.void
fatalError
(SAXParseException exception) ErrorHandler interface method.boolean
isValid()
Perform the validation of the source against DTD / Schema.resolveEntity
(String publicId, String systemId) Entity Resolver method: allows us to override an existing systemID referenced in the markup DOCTYPE instruction.void
setJAXP12SchemaSource
(Object schemaSource) As per JAXP 1.2 changes, which introduced a standard way for parsers to support schema validation.toString()
void
useXMLSchema
(boolean use) Turn on XML Schema validation.void
warning
(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
Methods inherited from interface org.xml.sax.ContentHandler
declaration
-
Constructor Details
-
Validator
protected Validator(InputSource inputSource, boolean usingDoctypeReader) throws SAXException, ConfigurationException Deprecated.Use the protected three arg constructor instead.Kept for backwards compatibility.- Throws:
SAXException
ConfigurationException
-
Validator
protected Validator(InputSource inputSource, String systemId, boolean usingDoctypeReader) throws SAXException, ConfigurationException Baseline constructor: called by all others- Parameters:
inputSource
-systemId
-usingDoctypeReader
-- Throws:
SAXException
ConfigurationException
- if validation could not be turned on
-
Validator
public Validator(Document document, String systemID, String doctype) throws SAXException, ConfigurationException 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:
SAXException
- if unable to obtain new Sax parser via JAXP factoryConfigurationException
- if validation could not be turned on
-
Validator
Basic constructor. Validates the contents of the Reader using the DTD or schema referenced by those contents.- Parameters:
readerForValidation
-- Throws:
SAXException
- if unable to obtain new Sax parser via JAXP factoryConfigurationException
- if validation could not be turned on
-
Validator
Basic constructor. Validates the contents of the String using the DTD or schema referenced by those contents.- Parameters:
stringForValidation
-- Throws:
SAXException
- if unable to obtain new Sax parser via JAXP factoryConfigurationException
- if validation could not be turned on
-
Validator
Basic constructor. Validates the contents of the InputSource using the DTD or schema referenced by those contents.- Parameters:
readerForValidation
-- Throws:
SAXException
- if unable to obtain new Sax parser via JAXP factoryConfigurationException
- if validation could not be turned on
-
Validator
public Validator(Reader readerForValidation, String systemID) throws SAXException, ConfigurationException 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:
SAXException
- if unable to obtain new Sax parser via JAXP factoryConfigurationException
- if validation could not be turned on
-
Validator
public Validator(String stringForValidation, String systemID) throws SAXException, ConfigurationException Extended constructor. Validates the contents of the String 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:
stringForValidation
-systemID
-- Throws:
SAXException
- if unable to obtain new Sax parser via JAXP factoryConfigurationException
- if validation could not be turned on
-
Validator
public Validator(InputSource sourceForValidation, String systemID) throws SAXException, ConfigurationException Extended constructor. Validates the contents of the InputSource 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:
sourceForValidation
-systemID
-- Throws:
SAXException
- if unable to obtain new Sax parser via JAXP factoryConfigurationException
- if validation could not be turned on
-
Validator
public Validator(InputSource sourceForValidation, String systemID, String doctype) throws SAXException, ConfigurationException Full constructor. Validates the contents of the InputSource using the DTD specified with the systemID and named with the doctype name.- Parameters:
sourceForValidation
-systemID
-doctype
-- Throws:
SAXException
ConfigurationException
- if validation could not be turned on
-
Validator
public Validator(Reader readerForValidation, String systemID, String doctype) throws SAXException, ConfigurationException 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:
SAXException
ConfigurationException
- if validation could not be turned on
-
-
Method Details
-
useXMLSchema
Turn on XML Schema validation.This feature should work with any XML parser that is JAXP 1.2 compliant and supports XML Schema validation.
For a fully JAXP 1.2 compliant parser the property
invalid reference
http://java.sun.com/xml/jaxp/properties/schemaLanguage
- Parameters:
use
- indicate that XML Schema should be used to validate documents.- Throws:
SAXException
- See Also:
-
isValid
public boolean isValid()Perform the validation of the source against DTD / Schema.- Returns:
- true if the input supplied to the constructor passes validation, false otherwise
-
assertIsValid
public void assertIsValid()Assert that a document is valid. -
toString
-
warning
ErrorHandler interface method.- Specified by:
warning
in interfaceErrorHandler
- Overrides:
warning
in classDefaultHandler
- Parameters:
exception
-- Throws:
SAXException
-
error
ErrorHandler interface method.- Specified by:
error
in interfaceErrorHandler
- Overrides:
error
in classDefaultHandler
- Parameters:
exception
-- Throws:
SAXException
-
fatalError
ErrorHandler interface method.- Specified by:
fatalError
in interfaceErrorHandler
- Overrides:
fatalError
in classDefaultHandler
- Parameters:
exception
-- Throws:
SAXException
-
resolveEntity
Entity Resolver method: allows us to override an existing systemID referenced in the markup DOCTYPE instruction.- Specified by:
resolveEntity
in interfaceEntityResolver
- Overrides:
resolveEntity
in classDefaultHandler
- Parameters:
publicId
-systemId
-- Returns:
- the sax InputSource that points to the overridden systemID
-
setJAXP12SchemaSource
As per JAXP 1.2 changes, which introduced a standard way for parsers to support schema validation. Since only W3C Schema support was included in JAXP 1.2, this is the only mechanism currently supported by this method.- Parameters:
schemaSource
- This can be one of the following:- String that points to the URI of the schema
- InputStream with the contents of the schema
- SAX InputSource
- File
- an array of Objects with the contents being one of the types defined above. An array of Objects can be used only when the schema language has the ability to assemble a schema at runtime. When an array of Objects is passed it is illegal to have two schemas that share the same namespace.
- Throws:
SAXException
- if this method of validating isn't supported.
-