org.apache.cocoon.components.validation.impl
Class AbstractValidator

java.lang.Object
  extended by org.apache.cocoon.components.validation.impl.AbstractValidator
All Implemented Interfaces:
Disposable, LogEnabled, Serviceable, Validator
Direct Known Subclasses:
DefaultValidator

public abstract class AbstractValidator
extends Object
implements Validator, Serviceable, Disposable, LogEnabled

The AbstractValidator provides a generic implementation of the methods specified by the Validator interface.

Final implementations must implement three component management methods lookupParserByGrammar(String), lookupParserByName(String) and releaseParser(SchemaParser).

In addition to this, they might also override the default implementation of the getSchema(SchemaParser, Source, String) method, for example when caching Schema instances.

This implementation provides a simple grammar identification mechanism, which can be overridden by reimplementing the detectGrammar(Source) method provided by this class.


Field Summary
protected  Logger logger
          The configured Logger instance.
protected  ServiceManager manager
          The configured ServiceManager instance.
protected  SourceResolver resolver
          The configured SourceResolver instance.
 
Fields inherited from interface org.apache.cocoon.components.validation.Validator
GRAMMAR_ISO_SCHEMATRON, GRAMMAR_RELAX_CORE, GRAMMAR_RELAX_NG, GRAMMAR_RELAX_NS, GRAMMAR_SCHEMATRON, GRAMMAR_TREX, GRAMMAR_XML_DTD, GRAMMAR_XML_SCHEMA, ROLE
 
Constructor Summary
AbstractValidator()
          Create a new AbstractValidator instance.
 
Method Summary
protected  String detectGrammar(Source source)
          Attempt to detect the grammar language used by the schema identified by the specified Source.
 void dispose()
          Dispose of this component instance.
 void enableLogging(Logger logger)
          Enable logging.
protected  Schema getSchema(SchemaParser parser, Source source, String grammar)
          Return a Schema instance from the specified SchemaParser associated with the given Source and grammar language.
 ValidationHandler getValidationHandler(Source source)
          Return a ValidationHandler validating an XML document according to the schema found at the specified location.
 ValidationHandler getValidationHandler(Source source, ErrorHandler errorHandler)
          Return a ValidationHandler validating an XML document according to the schema found at the specified location.
 ValidationHandler getValidationHandler(Source source, String grammar)
          Return a ValidationHandler validating an XML document according to the schema found at the specified location.
 ValidationHandler getValidationHandler(Source source, String grammar, ErrorHandler errorHandler)
          Return a ValidationHandler validating an XML document according to the schema found at the specified location.
 ValidationHandler getValidationHandler(String uri)
          Return a ValidationHandler validating an XML document according to the schema found at the specified location.
 ValidationHandler getValidationHandler(String uri, ErrorHandler errorHandler)
          Return a ValidationHandler validating an XML document according to the schema found at the specified location.
 ValidationHandler getValidationHandler(String uri, String grammar)
          Return a ValidationHandler validating an XML document according to the schema found at the specified location.
 ValidationHandler getValidationHandler(String uri, String grammar, ErrorHandler errorHandler)
          Return a ValidationHandler validating an XML document according to the schema found at the specified location.
protected abstract  SchemaParser lookupParserByGrammar(String grammar)
          Attempt to acquire a SchemaParser interface able to understand the grammar language specified.
protected abstract  SchemaParser lookupParserByName(String name)
          Attempt to acquire a SchemaParser interface associated with the specified instance name.
protected abstract  void releaseParser(SchemaParser parser)
          Release a previously acquired SchemaParser instance back to its original component manager.
 void service(ServiceManager manager)
          Specify the ServiceManager available to this instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

manager

protected ServiceManager manager

The configured ServiceManager instance.


resolver

protected SourceResolver resolver

The configured SourceResolver instance.


logger

protected Logger logger

The configured Logger instance.

Constructor Detail

AbstractValidator

public AbstractValidator()

Create a new AbstractValidator instance.

Method Detail

enableLogging

public void enableLogging(Logger logger)

Enable logging.

Specified by:
enableLogging in interface LogEnabled

service

public void service(ServiceManager manager)
             throws ServiceException

Specify the ServiceManager available to this instance.

Specified by:
service in interface Serviceable
Throws:
ServiceException

dispose

public void dispose()

Dispose of this component instance.

Specified by:
dispose in interface Disposable

getValidationHandler

public ValidationHandler getValidationHandler(String uri)
                                       throws IOException,
                                              SAXException,
                                              ValidatorException

Return a ValidationHandler validating an XML document according to the schema found at the specified location.

The Validator will attempt to automatically detect the grammar language of the specified schema, and each error or warning occurring while validating the document will trigger a SAXException to be thrown back to the caller.

Specified by:
getValidationHandler in interface Validator
Parameters:
uri - the location of the schema to use to validate the document.
Returns:
a non null ValidationHandler able to SAX events from the original XML document to validate.
Throws:
IOException - if an I/O error occurred parsing the schema.
SAXException - if a grammar error occurred parsing the schema.
ValidatorException - if the grammar language of the specified schema could not be detected or was not supported.
See Also:
SchemaParser.parseSchema(Source, String), Schema.createValidator(ErrorHandler)

getValidationHandler

public ValidationHandler getValidationHandler(String uri,
                                              String grammar)
                                       throws IOException,
                                              SAXException,
                                              ValidatorException

Return a ValidationHandler validating an XML document according to the schema found at the specified location.

Each error or warning occurring while validating the document will trigger a SAXException to be thrown back to the caller.

Specified by:
getValidationHandler in interface Validator
Parameters:
uri - the location of the schema to use to validate the document.
grammar - the grammar language of the schema to parse.
Returns:
a non null ValidationHandler able to SAX events from the original XML document to validate.
Throws:
IOException - if an I/O error occurred parsing the schema.
SAXException - if a grammar error occurred parsing the schema.
ValidatorException - if the specified grammar language wasn't supported.
See Also:
SchemaParser.parseSchema(Source, String), Schema.createValidator(ErrorHandler)

getValidationHandler

public ValidationHandler getValidationHandler(String uri,
                                              ErrorHandler errorHandler)
                                       throws IOException,
                                              SAXException,
                                              ValidatorException

Return a ValidationHandler validating an XML document according to the schema found at the specified location.

The Validator will attempt to automatically detect the grammar language of the specified schema, while each validation error or warning will be passed to the specified ErrorHandler which will have the ability to generate and throw a SAXException back to the caller.

Specified by:
getValidationHandler in interface Validator
Parameters:
uri - the location of the schema to use to validate the document.
errorHandler - the ErrorHandler notified of validation problems.
Returns:
a non null ValidationHandler able to SAX events from the original XML document to validate.
Throws:
IOException - if an I/O error occurred parsing the schema.
SAXException - if a grammar error occurred parsing the schema.
ValidatorException - if the grammar language of the specified schema could not be detected or was not supported.
See Also:
SchemaParser.parseSchema(Source, String), Schema.createValidator(ErrorHandler)

getValidationHandler

public ValidationHandler getValidationHandler(String uri,
                                              String grammar,
                                              ErrorHandler errorHandler)
                                       throws IOException,
                                              SAXException,
                                              ValidatorException

Return a ValidationHandler validating an XML document according to the schema found at the specified location.

Each validation error or warning will be passed to the specified ErrorHandler which will have the ability to generate and throw a SAXException back to the caller.

Specified by:
getValidationHandler in interface Validator
Parameters:
uri - the location of the schema to use to validate the document.
grammar - the grammar language of the schema to parse.
errorHandler - the ErrorHandler notified of validation problems.
Returns:
a non null ValidationHandler able to SAX events from the original XML document to validate.
Throws:
IOException - if an I/O error occurred parsing the schema.
SAXException - if a grammar error occurred parsing the schema.
ValidatorException - if the specified grammar language wasn't supported.
See Also:
SchemaParser.parseSchema(Source, String), Schema.createValidator(ErrorHandler)

getValidationHandler

public ValidationHandler getValidationHandler(Source source)
                                       throws IOException,
                                              SAXException,
                                              ValidatorException

Return a ValidationHandler validating an XML document according to the schema found at the specified location.

The Validator will attempt to automatically detect the grammar language of the specified schema, and each error or warning occurring while validating the document will trigger a SAXException to be thrown back to the caller.

Specified by:
getValidationHandler in interface Validator
Parameters:
source - the Source identifying the schema to use for validation.
Returns:
a non null ValidationHandler able to SAX events from the original XML document to validate.
Throws:
IOException - if an I/O error occurred parsing the schema.
SAXException - if a grammar error occurred parsing the schema.
ValidatorException - if the grammar language of the specified schema could not be detected or was not supported.
See Also:
SchemaParser.parseSchema(Source, String), Schema.createValidator(ErrorHandler)

getValidationHandler

public ValidationHandler getValidationHandler(Source source,
                                              String grammar)
                                       throws IOException,
                                              SAXException,
                                              ValidatorException

Return a ValidationHandler validating an XML document according to the schema found at the specified location.

Each error or warning occurring while validating the document will trigger a SAXException to be thrown back to the caller.

Specified by:
getValidationHandler in interface Validator
Parameters:
source - the Source identifying the schema to use for validation.
grammar - the grammar language of the schema to parse.
Returns:
a non null ValidationHandler able to SAX events from the original XML document to validate.
Throws:
IOException - if an I/O error occurred parsing the schema.
SAXException - if a grammar error occurred parsing the schema.
ValidatorException - if the specified grammar language wasn't supported.
See Also:
SchemaParser.parseSchema(Source, String), Schema.createValidator(ErrorHandler)

getValidationHandler

public ValidationHandler getValidationHandler(Source source,
                                              ErrorHandler errorHandler)
                                       throws IOException,
                                              SAXException,
                                              ValidatorException

Return a ValidationHandler validating an XML document according to the schema found at the specified location.

The Validator will attempt to automatically detect the grammar language of the specified schema, while each validation error or warning will be passed to the specified ErrorHandler which will have the ability to generate and throw a SAXException back to the caller.

Specified by:
getValidationHandler in interface Validator
Parameters:
source - the Source identifying the schema to use for validation.
errorHandler - the ErrorHandler notified of validation problems.
Returns:
a non null ValidationHandler able to SAX events from the original XML document to validate.
Throws:
IOException - if an I/O error occurred parsing the schema.
SAXException - if a grammar error occurred parsing the schema.
ValidatorException - if the grammar language of the specified schema could not be detected or was not supported.
See Also:
SchemaParser.parseSchema(Source, String), Schema.createValidator(ErrorHandler)

getValidationHandler

public ValidationHandler getValidationHandler(Source source,
                                              String grammar,
                                              ErrorHandler errorHandler)
                                       throws IOException,
                                              SAXException,
                                              ValidatorException

Return a ValidationHandler validating an XML document according to the schema found at the specified location.

Each validation error or warning will be passed to the specified ErrorHandler which will have the ability to generate and throw a SAXException back to the caller.

Specified by:
getValidationHandler in interface Validator
Parameters:
source - the Source identifying the schema to use for validation.
grammar - the grammar language of the schema to parse.
errorHandler - the ErrorHandler notified of validation problems.
Returns:
a non null ValidationHandler able to SAX events from the original XML document to validate.
Throws:
IOException - if an I/O error occurred parsing the schema.
SAXException - if a grammar error occurred parsing the schema.
ValidatorException - if the specified grammar language wasn't supported.
See Also:
SchemaParser.parseSchema(Source, String), Schema.createValidator(ErrorHandler)

lookupParserByGrammar

protected abstract SchemaParser lookupParserByGrammar(String grammar)

Attempt to acquire a SchemaParser interface able to understand the grammar language specified.

Parameters:
grammar - the grammar language that must be understood by the returned SchemaParser
Returns:
a SchemaParser instance or null if none was found able to understand the specified grammar language.

lookupParserByName

protected abstract SchemaParser lookupParserByName(String name)

Attempt to acquire a SchemaParser interface associated with the specified instance name.

Parameters:
name - the name associated with the SchemaParser to be returned.
Returns:
a SchemaParser instance or null if none was found.

releaseParser

protected abstract void releaseParser(SchemaParser parser)

Release a previously acquired SchemaParser instance back to its original component manager.

This method is supplied in case solid implementations of this class relied on the ServiceManager to manage SchemaParsers instances.

Parameters:
parser - the SchemaParser whose instance is to be released.

getSchema

protected Schema getSchema(SchemaParser parser,
                           Source source,
                           String grammar)
                    throws IOException,
                           SAXException

Return a Schema instance from the specified SchemaParser associated with the given Source and grammar language.

This method simply implements resolution returning the Schema instance acquired calling parser.getSchema(source,grammar).

Parameters:
parser - the SchemaParser producing the Schema.
source - the Source associated with the Schema to return.
grammar - the grammar language of the schema to produce.
Throws:
SAXException - if a grammar error occurred parsing the schema.
IOException - if an I/O error occurred parsing the schema.

detectGrammar

protected String detectGrammar(Source source)
                        throws IOException,
                               SAXException,
                               ValidatorException

Attempt to detect the grammar language used by the schema identified by the specified Source.

Parameters:
source - a Source instance pointing to the schema to be analyzed.
Throws:
IOException - if an I/O error occurred accessing the schema.
SAXException - if an error occurred parsing the schema.
ValidatorException - if the language of the schema could not be guessed.


Copyright © 1999-2010 The Apache Software Foundation. All Rights Reserved.