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

SchemaFactoryImpl

public class SchemaFactoryImpl extends SchemaFactory
{@link SchemaFactory} for XML Schema.
author
Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)

Fields Summary
private final XMLSchemaLoader
loader
private static XSMessageFormatter
messageFormatter
private ErrorHandler
errorHandler
User-specified ErrorHandler. can be null.
private LSResourceResolver
resourceResolver
private SAXParseException
lastException
private final SecurityManager
secureProcessing
private boolean
enableSP
Constructors Summary
public SchemaFactoryImpl()

    
      
        secureProcessing = new SecurityManager();
        // intercept error report and remember the last thrown exception.
        loader.setErrorHandler(new ErrorHandlerWrapper(new ErrorHandler() {
            public void warning(SAXParseException exception) throws SAXException {
                if( errorHandler!=null )    errorHandler.warning(exception);
            }
            
            public void error(SAXParseException exception) throws SAXException {
                lastException = exception;
                if( errorHandler!=null )    errorHandler.error(exception);
                else    throw exception;
            }
            
            public void fatalError(SAXParseException exception) throws SAXException {
                lastException = exception;
                if( errorHandler!=null )    errorHandler.fatalError(exception);
                else    throw exception;
            }
        }));
    
Methods Summary
public org.xml.sax.ErrorHandlergetErrorHandler()

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

        if(name==null) throw new NullPointerException(SAXMessageFormatter.formatMessage(Locale.getDefault(),
        "nullparameter",new Object[] {"getFeature(String)"}));
        if(name.equals(Constants.FEATURE_SECURE_PROCESSING))
            return enableSP;
        else throw new SAXNotRecognizedException(SAXMessageFormatter.formatMessage(Locale.getDefault(),
        "feature-not-supported", new Object [] {name}));
    
public org.w3c.dom.ls.LSResourceResolvergetResourceResolver()

        return resourceResolver;
    
public booleanisSchemaLanguageSupported(java.lang.String schemaLanguage)

Is specified schema supported by this SchemaFactory?

param
schemaLanguage Specifies the schema language which the returned SchemaFactory will understand. schemaLanguage must specify a valid schema language.
return
true if SchemaFactory supports schemaLanguage, else false.
throws
NullPointerException If schemaLanguage is null.
throws
IllegalArgumentException If schemaLanguage.length() == 0 or schemaLanguage does not specify a valid schema language.

        
        if (schemaLanguage == null) {
            throw new NullPointerException(
            messageFormatter.formatMessage(Locale.getDefault(),
            "SchemaLanguageSupportedErrorWhenNull",
            new Object [] {this.getClass().getName()}));
        }
        
        if (schemaLanguage.length() == 0) {
            throw new IllegalArgumentException(
            messageFormatter.formatMessage(Locale.getDefault(),
            "SchemaLanguageSupportedErrorWhenLength",
            new Object [] {this.getClass().getName()}));
        }
        
        // understand W3C Schema and RELAX NG
        if (schemaLanguage.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)
        || schemaLanguage.equals(XMLConstants.RELAXNG_NS_URI)) {
            return true;
        }
        
        // don't know how to validate anything else
        return false;
    
public javax.xml.validation.SchemanewSchema(javax.xml.transform.Source[] schemas)

        
        lastException = null;
        
        // this will let the loader store parsed Grammars into the pool.
        XMLGrammarPool pool = new XMLGrammarPoolImpl();
        loader.setProperty(XercesConstants.XMLGRAMMAR_POOL,pool);
        loader.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING,true);
        if(enableSP)
            loader.setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY,secureProcessing);
        else
            loader.setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY,null);
        
        for( int i=0; i<schemas.length; i++ ) {
            try {
                loader.loadGrammar(schemas[i]);
            } catch (XNIException e) {
                // this should have been reported to users already.
                throw Util.toSAXException(e);
            } catch (IOException e) {
                // this hasn't been reported, so do so now.
                SAXParseException se = new SAXParseException(e.getMessage(),null,e);
                errorHandler.error(se);
                throw se; // and we must throw it.
            }
        }
        
        // if any error had been reported, throw it.
        if( lastException!=null )
            throw lastException;
        
        // make sure no further grammars are added by making it read-only.
        return new SchemaImpl(new ReadonlyGrammarPool(pool),true);
    
public javax.xml.validation.SchemanewSchema()

        // use a pool that uses the system id as the equality source.
        return new SchemaImpl(new XMLGrammarPoolImpl() {
            public boolean equals(XMLGrammarDescription desc1, XMLGrammarDescription desc2) {
                String sid1 = desc1.getExpandedSystemId();
                String sid2 = desc2.getExpandedSystemId();
                if( sid1!=null && sid2!=null )
                    return sid1.equals(sid2);
                if( sid1==null && sid2==null )
                    return true;
                return false;
            }
            public int hashCode(XMLGrammarDescription desc) {
                String s = desc.getExpandedSystemId();
                if(s!=null)     return s.hashCode();
                return 0;
            }
        }, false);
    
public voidsetErrorHandler(org.xml.sax.ErrorHandler errorHandler)

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

        if(name==null) throw new NullPointerException(SAXMessageFormatter.formatMessage(Locale.getDefault(),
        "nullparameter",new Object[] {"setFeature(String,boolean)"}));
        if(name.equals(Constants.FEATURE_SECURE_PROCESSING)){
            enableSP = value;
        }else throw new SAXNotRecognizedException(SAXMessageFormatter.formatMessage(Locale.getDefault(),
        "feature-not-supported", new Object [] {name}));
        
    
public voidsetResourceResolver(org.w3c.dom.ls.LSResourceResolver resourceResolver)

        this.resourceResolver = resourceResolver;
        loader.setEntityResolver(new DOMEntityResolverWrapper(resourceResolver));