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

ValidatorHandlerImpl

public final class ValidatorHandlerImpl extends ValidatorHandler
{@link ValidatorHandler} implementation that wraps {@link InsulatedValidatorComponent}.

This class implements all the SAX {@link org.xml.sax.ContentHandler} methods and turn SAX events into XNI events.

This class also implements {@link XMLComponentManager} to host a validator.

author
Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)

Fields Summary
private final InsulatedValidatorComponent
validator
The actual validator.
private final XMLDocumentFilter
validatorFilter
validator.getValidator().
private final XNI2SAXEx
xni2sax
Used to adopt the output from a validtor to the input of the user specified {@link ContentHandler}.
private final SymbolTable
symbolTable
private final NamespaceSupport
nsContext
private final ValidationManager
validationManager
private final XMLEntityManager
entityManager
private final XMLErrorReporter
errorReporter
error reporter is used to format error messages.
private ErrorHandler
errorHandler
User-specified error handler. Maybe null.
private boolean
inStartElement
This flag is set to true while we are processing the startElement event.
private boolean
namespacePrefixesFeature
The value of the http://xml.org/sax/features/namespace-prefixes feature.
private final ErrorHandlerAdaptor
xercesErrorHandler
Used by {@link XMLDTDValidator} to report errors.
private LSResourceResolver
resourceResolver
User-specified entity resolver. Maybe null.
private final XMLComponentManager
manager
private Locator
locator
private final TypeInfoProvider
typeInfoProvider
{@link TypeInfoProvider} implementation. REVISIT: I'm not sure if this code should belong here.
private final XMLAttributes
xa
only one instance of XMLAttributes is used.
Constructors Summary
ValidatorHandlerImpl(InsulatedValidatorComponent validator)

    
    
    
    
        
        this.validator = validator;
        this.validatorFilter = validator.getValidator();

        // format error message with Schema aware formatter
        errorReporter.putMessageFormatter(
                XSMessageFormatter.SCHEMA_DOMAIN,
                new XSMessageFormatter());
    
Methods Summary
public voidcharacters(char[] buf, int offset, int len)

        try {
            validatorFilter.characters(new XMLString(buf,offset,len),null);
        } catch( WrappedSAXException e ) {
            throw e.exception;
        }
    
private com.sun.org.apache.xerces.internal.xni.XMLAttributescreateAttributes(org.xml.sax.Attributes att)
Creates an XMLAttributes object.

    
         
        
        xa.removeAllAttributes();
        int len = att.getLength();
        for (int i = 0; i < len; i++) {
            int idx = xa.addAttribute(
            createQName(att.getURI(i), att.getLocalName(i), att.getQName(i)),
            att.getType(i),
            att.getValue(i));
            // attributes present in the original SAX event streams
            // are considered as "specified".
            xa.setSpecified(idx,true);
        }
        return xa;
    
private com.sun.org.apache.xerces.internal.xni.QNamecreateQName(java.lang.String uri, java.lang.String local, java.lang.String raw)
Creates a QName object.

        
        if( local.length()==0 ) {
            // if naemspace processing is turned off, local could be "".
            // in that case, treat everything to be in the no namespace.
            uri = "";
            local = raw;
        }
        
        int idx = raw.indexOf(':");
        String prefix;
        if (idx < 0)
            prefix = null;
        else
            prefix = raw.substring(0, idx);
        
        if (uri != null && uri.length() == 0)
            uri = null; // XNI uses null whereas SAX uses the empty string
        
        return new QName(symbolize(prefix), symbolize(local), symbolize(raw), symbolize(uri));
    
private com.sun.org.apache.xerces.internal.xni.XMLStringcreateXMLString(java.lang.String str)

        // with my patch
        // return new XMLString(str);
        
        // for now
        return new XMLString(str.toCharArray(), 0, str.length());
    
public voidendDocument()

        try {
            validatorFilter.endDocument(null);
        } catch( WrappedSAXException e ) {
            throw e.exception;
        }
    
public voidendElement(java.lang.String uri, java.lang.String local, java.lang.String qname)

        try {
            validatorFilter.endElement(createQName(uri,local,qname),null);
        } catch( WrappedSAXException e ) {
            throw e.exception;
        }
    
public voidendPrefixMapping(java.lang.String prefix)

        nsContext.popContext();
    
public final org.xml.sax.ContentHandlergetContentHandler()

        return xni2sax.getContentHandler();
    
private final com.sun.org.apache.xerces.internal.xni.XMLAttributesgetCurrentAttributes()
Obtains the current attributes.

throws
IllegalStateException

        return xni2sax.getCurrentAttributes();
    
private final com.sun.org.apache.xerces.internal.xni.AugmentationsgetCurrentAugmentation()
Obtains the current augmentation.

used for {@link javax.xml.validation.TypeInfoProvider}.

return
may return null.

        return xni2sax.getCurrentAugmentation();
    
public org.xml.sax.ErrorHandlergetErrorHandler()

        return errorHandler;
    
public booleangetFeature(java.lang.String name)

        if( name.equals("http://xml.org/sax/features/namespace-prefixes") )
            return namespacePrefixesFeature;
        return super.getFeature(name);
    
public org.w3c.dom.ls.LSResourceResolvergetResourceResolver()

        return resourceResolver;
    
public javax.xml.validation.TypeInfoProvidergetTypeInfoProvider()

        return typeInfoProvider;
    
public voidignorableWhitespace(char[] buf, int offset, int len)

        try {
            validatorFilter.ignorableWhitespace(new XMLString(buf,offset,len),null);
        } catch( WrappedSAXException e ) {
            throw e.exception;
        }
    
public booleanisValidSoFar()

        return !xercesErrorHandler.hadError();
    
public voidprocessingInstruction(java.lang.String target, java.lang.String data)

        try {
            validatorFilter.processingInstruction(
            symbolize(target),createXMLString(data),null);
        } catch( WrappedSAXException e ) {
            throw e.exception;
        }
    
public voidreset()
Resets this handler.

Meaning resets all the user-specified configurations to the initial state, then also resets all the components.

        resetComponents();
        errorHandler = null;
        resourceResolver = null;
    
private voidresetComponents()
Resets the components we use internally.

        // reset the error flag when we start a new validation.
        xercesErrorHandler.reset();
        nsContext.reset();
        errorReporter.reset(manager);
        validator.reset(manager);
    
public final voidsetContentHandler(org.xml.sax.ContentHandler result)

        xni2sax.setContentHandler(result);
        if(result==null)    validatorFilter.setDocumentHandler(null);
        else                validatorFilter.setDocumentHandler(xni2sax);
    
public voidsetDocumentLocator(org.xml.sax.Locator _loc)

        this.locator = _loc;
    
public voidsetErrorHandler(org.xml.sax.ErrorHandler errorHandler)

        this.errorHandler = errorHandler;
    
public voidsetFeature(java.lang.String name, boolean value)

        if( name.equals("http://xml.org/sax/features/namespace-prefixes") ) {
            namespacePrefixesFeature = value;
            return;
        }
        super.setFeature(name, value);
    
public voidsetResourceResolver(org.w3c.dom.ls.LSResourceResolver entityResolver)

        this.resourceResolver = entityResolver;
    
public voidskippedEntity(java.lang.String name)

        // there seems to be no corresponding method on XMLDocumentFilter.
        // just pass it down to the output, if any.
        ContentHandler handler = getContentHandler();
        if( handler!=null )
            skippedEntity(name);
    
public voidstartDocument()

    
    //
    //
    // ContentHandler implementation
    //
    //
         
        try {
            resetComponents();
            
            XMLLocator xmlLocator = (locator==null)?null:new LocatorWrapper(locator);
            
            // set the locator to the error reporter
            errorReporter.setDocumentLocator(xmlLocator);
            
            validatorFilter.startDocument(
            xmlLocator,
            null,
            nsContext,
            null);
        } catch( WrappedSAXException e ) {
            throw e.exception;
        }
    
public voidstartElement(java.lang.String uri, java.lang.String local, java.lang.String qname, org.xml.sax.Attributes att)

        try {
            inStartElement = true;
            validatorFilter.startElement(createQName(uri,local,qname),createAttributes(att),null);
        } catch( WrappedSAXException e ) {
            throw e.exception;
        } finally {
            inStartElement = false;
        }
    
public voidstartPrefixMapping(java.lang.String prefix, java.lang.String uri)

        nsContext.pushContext();
        nsContext.declarePrefix(prefix,uri);
    
private java.lang.Stringsymbolize(java.lang.String s)
Symbolizes the specified string.

    
    
    
    //
    //
    // helper methods
    //
    //
         
        
        if (s == null)
            return null;
        else
            return symbolTable.addSymbol(s);