FileDocCategorySizeDatePackage
ValidatorImpl.javaAPI DocJava SE 5 API11450Fri Aug 26 14:55:54 BST 2005com.sun.org.apache.xerces.internal.jaxp.validation

ValidatorImpl

public class ValidatorImpl extends Validator
(For Implementors) Default implementation of {@link Validator}.

This class is intended to be used in conjunction with {@link AbstractSchemaImpl} to promote consistent behaviors among {@link Schema} implementations.

This class wraps a {@link javax.xml.validation.ValidatorHandler} object and implements the {@link Validator} semantics.

author
Kohsuke Kawaguchi
version
$Revision: 1.5 $, $Date: 2004/07/12 20:38:39 $
since
1.5

Fields Summary
private final ValidatorHandlerImpl
handler
The actual validation will be done by this object.
private Transformer
identityTransformer1
Lazily created identity transformer.
private TransformerHandler
identityTransformer2
private final ErrorHandler
errorForwarder
Forwards the error to the {@link ValidatorHandler}. If the {@link ValidatorHandler} doesn't have its own {@link ErrorHandler}, behave draconian.
private final EntityResolver
resolutionForwarder
Forwards the entity resolution to the {@link ValidatorHandler}. If the {@link ValidatorHandler} doesn't have its own {@link DOMResourceResolver}, let the parser do the resolution.
Constructors Summary
ValidatorImpl(ValidatorHandlerImpl _handler)

    
        
        this.handler = _handler;
    
Methods Summary
public org.xml.sax.ErrorHandlergetErrorHandler()

        return handler.getErrorHandler();
    
public org.w3c.dom.ls.LSResourceResolvergetResourceResolver()

        return handler.getResourceResolver();
    
private voidprocess(javax.xml.transform.sax.SAXSource source, javax.xml.transform.sax.SAXResult result)
Parses a {@link SAXSource} potentially to a {@link SAXResult}.

        if( result!=null ) {
            handler.setContentHandler(result.getHandler());
        }
        
        try {
            XMLReader reader = source.getXMLReader();
            if( reader==null ) {
                // create one now
                SAXParserFactory spf = SAXParserFactory.newInstance();
                spf.setNamespaceAware(true);
                try {
                    reader = spf.newSAXParser().getXMLReader();
                } catch( Exception e ) {
                    // this is impossible, but better safe than sorry
                    throw new FactoryConfigurationError(e);
                }
            }
            
            reader.setErrorHandler(errorForwarder);
            reader.setEntityResolver(resolutionForwarder);
            reader.setContentHandler(handler);
            
            InputSource is = source.getInputSource();
            reader.parse(is);
        } finally {
            // release the reference to user's handler ASAP
            handler.setContentHandler(null);
        }
    
private voidprocess(javax.xml.transform.dom.DOMSource source, javax.xml.transform.dom.DOMResult result)
Parses a {@link DOMSource} potentially to a {@link DOMResult}.

        if( identityTransformer1==null ) {
            try {
                SAXTransformerFactory tf = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
                identityTransformer1 = tf.newTransformer();
                identityTransformer2 = tf.newTransformerHandler();
            } catch (TransformerConfigurationException e) {
                // this is impossible, but again better safe than sorry
                throw new TransformerFactoryConfigurationError(e);
            }
        }

        if( result!=null ) {
            handler.setContentHandler(identityTransformer2);
            identityTransformer2.setResult(result);
        }
        
        try {
            identityTransformer1.transform( source, new SAXResult(handler) );
        } catch (TransformerException e) {
            if( e.getException() instanceof SAXException )
                throw (SAXException)e.getException();
            throw new SAXException(e);
        } finally {
            handler.setContentHandler(null);
        }
    
public voidreset()

    
       
        handler.reset();
        
        // I don't think this is necessary, but I don't think it hurts either.
        // so reset just for the kick.
        if(identityTransformer1!=null) {
            identityTransformer1.reset();
        }
    
public voidsetErrorHandler(org.xml.sax.ErrorHandler errorHandler)

        handler.setErrorHandler(errorHandler);
    
public voidsetResourceResolver(org.w3c.dom.ls.LSResourceResolver resolver)

        handler.setResourceResolver(resolver);
    
public voidvalidate(javax.xml.transform.Source source, javax.xml.transform.Result result)

        if( source instanceof DOMSource ) {
            if( result!=null && !(result instanceof DOMResult) )
                throw new IllegalArgumentException(result.getClass().getName());
            process( (DOMSource)source, (DOMResult)result );
            return;
        }
        if( source instanceof SAXSource ) {
            if( result!=null && !(result instanceof SAXResult) )
                throw new IllegalArgumentException(result.getClass().getName());
            process( (SAXSource)source, (SAXResult)result );
            return;
        }
        if( source instanceof StreamSource ) {
            if( result!=null )
                throw new IllegalArgumentException(result.getClass().getName());
            StreamSource ss = (StreamSource)source;
            InputSource is = new InputSource();
            is.setByteStream(ss.getInputStream());
            is.setCharacterStream(ss.getReader());
            is.setPublicId(ss.getPublicId());
            is.setSystemId(ss.getSystemId());
            process( new SAXSource(is), null );
            return;
        }
        throw new IllegalArgumentException(source.getClass().getName());