FileDocCategorySizeDatePackage
XMLSchemaFactory.javaAPI DocApache Xerces 3.0.120144Fri Sep 14 20:33:56 BST 2007org.apache.xerces.jaxp.validation

XMLSchemaFactory

public final class XMLSchemaFactory extends SchemaFactory
{@link SchemaFactory} for XML Schema.
author
Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
version
$Id: XMLSchemaFactory.java 542520 2007-05-29 13:55:53Z mrglavas $

Fields Summary
private static final String
SCHEMA_FULL_CHECKING
Feature identifier: schema full checking.
private static final String
USE_GRAMMAR_POOL_ONLY
Feature identifier: use grammar pool only.
private static final String
XMLGRAMMAR_POOL
Property identifier: grammar pool.
private static final String
SECURITY_MANAGER
Property identifier: SecurityManager.
private final org.apache.xerces.impl.xs.XMLSchemaLoader
fXMLSchemaLoader
The XMLSchemaLoader
private ErrorHandler
fErrorHandler
User-specified ErrorHandler; can be null.
private LSResourceResolver
fLSResourceResolver
The LSResrouceResolver
private final org.apache.xerces.util.DOMEntityResolverWrapper
fDOMEntityResolverWrapper
The DOMEntityResolverWrapper
private final org.apache.xerces.util.ErrorHandlerWrapper
fErrorHandlerWrapper
The ErrorHandlerWrapper
private org.apache.xerces.util.SecurityManager
fSecurityManager
The SecurityManager.
private final XMLGrammarPoolWrapper
fXMLGrammarPoolWrapper
The container for the real grammar pool.
private boolean
fUseGrammarPoolOnly
Whether or not to allow new schemas to be added to the grammar pool
Constructors Summary
public XMLSchemaFactory()

    
      
        fErrorHandlerWrapper = new ErrorHandlerWrapper(DraconianErrorHandler.getInstance());
        fDOMEntityResolverWrapper = new DOMEntityResolverWrapper();
        fXMLGrammarPoolWrapper = new XMLGrammarPoolWrapper();
        fXMLSchemaLoader.setFeature(SCHEMA_FULL_CHECKING, true);
        fXMLSchemaLoader.setProperty(XMLGRAMMAR_POOL, fXMLGrammarPoolWrapper);
        fXMLSchemaLoader.setEntityResolver(fDOMEntityResolverWrapper);
        fXMLSchemaLoader.setErrorHandler(fErrorHandlerWrapper);
        fUseGrammarPoolOnly = true;
    
Methods Summary
public org.xml.sax.ErrorHandlergetErrorHandler()

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

        if (name == null) {
            throw new NullPointerException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 
                    "FeatureNameNull", null));
        }
        if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
            return (fSecurityManager != null);
        }
        else if (name.equals(USE_GRAMMAR_POOL_ONLY)) {
            return fUseGrammarPoolOnly;
        }
        try {
            return fXMLSchemaLoader.getFeature(name);
        }
        catch (XMLConfigurationException e) {
            String identifier = e.getIdentifier();
            if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
                throw new SAXNotRecognizedException(
                        SAXMessageFormatter.formatMessage(Locale.getDefault(), 
                        "feature-not-recognized", new Object [] {identifier}));
            }
            else {
                throw new SAXNotSupportedException(
                        SAXMessageFormatter.formatMessage(Locale.getDefault(), 
                        "feature-not-supported", new Object [] {identifier}));
            }
        }
    
public java.lang.ObjectgetProperty(java.lang.String name)

        if (name == null) {
            throw new NullPointerException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 
                    "ProperyNameNull", null));
        }
        if (name.equals(SECURITY_MANAGER)) {
            return fSecurityManager;
        }
        else if (name.equals(XMLGRAMMAR_POOL)) {
            throw new SAXNotSupportedException(
                    SAXMessageFormatter.formatMessage(Locale.getDefault(), 
                    "property-not-supported", new Object [] {name}));
        }
        try {
            return fXMLSchemaLoader.getProperty(name);
        }
        catch (XMLConfigurationException e) {
            String identifier = e.getIdentifier();
            if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
                throw new SAXNotRecognizedException(
                        SAXMessageFormatter.formatMessage(Locale.getDefault(), 
                        "property-not-recognized", new Object [] {identifier}));
            }
            else {
                throw new SAXNotSupportedException(
                        SAXMessageFormatter.formatMessage(Locale.getDefault(), 
                        "property-not-supported", new Object [] {identifier}));
            }
        }
    
public org.w3c.dom.ls.LSResourceResolvergetResourceResolver()

        return fLSResourceResolver;
    
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(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 
                    "SchemaLanguageNull", null));
        }
        if (schemaLanguage.length() == 0) {
            throw new IllegalArgumentException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 
                    "SchemaLanguageLengthZero", null));
        }
        // only W3C XML Schema 1.0 is supported 
        return schemaLanguage.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    
public javax.xml.validation.SchemanewSchema(javax.xml.transform.Source[] schemas)

        
        // this will let the loader store parsed Grammars into the pool.
        XMLGrammarPoolImplExtension pool = new XMLGrammarPoolImplExtension();
        fXMLGrammarPoolWrapper.setGrammarPool(pool);
        
        XMLInputSource[] xmlInputSources = new XMLInputSource[schemas.length];
        InputStream inputStream;
        Reader reader;
        for( int i=0; i<schemas.length; i++ ) {
            Source source = schemas[i];
            if (source instanceof StreamSource) {
                StreamSource streamSource = (StreamSource) source;
                String publicId = streamSource.getPublicId();
                String systemId = streamSource.getSystemId();
                inputStream = streamSource.getInputStream();
                reader = streamSource.getReader();
                xmlInputSources[i] = new XMLInputSource(publicId, systemId, null);
                xmlInputSources[i].setByteStream(inputStream);
                xmlInputSources[i].setCharacterStream(reader);
            }
            else if (source instanceof SAXSource) {
                SAXSource saxSource = (SAXSource) source;
                InputSource inputSource = saxSource.getInputSource();
                if (inputSource == null) {
                    throw new SAXException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 
                            "SAXSourceNullInputSource", null));
                }
                xmlInputSources[i] = new SAXInputSource(saxSource.getXMLReader(), inputSource);
            }
            else if (source instanceof DOMSource) {
                DOMSource domSource = (DOMSource) source;
                Node node = domSource.getNode();
                String systemID = domSource.getSystemId();          
                xmlInputSources[i] = new DOMInputSource(node, systemID);
            }
            else if (source == null) {
                throw new NullPointerException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 
                        "SchemaSourceArrayMemberNull", null));
            }
            else {
                throw new IllegalArgumentException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 
                        "SchemaFactorySourceUnrecognized", 
                        new Object [] {source.getClass().getName()}));
            }
        }
        
        try {
            fXMLSchemaLoader.loadGrammar(xmlInputSources);
        } 
        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);
            if (fErrorHandler != null) {
                fErrorHandler.error(se);
            }
            throw se; // and we must throw it.
        }
        
        // Clear reference to grammar pool.
        fXMLGrammarPoolWrapper.setGrammarPool(null);
        
        // Select Schema implementation based on grammar count.
        final int grammarCount = pool.getGrammarCount();
        AbstractXMLSchema schema = null;
        if (fUseGrammarPoolOnly) {
            if (grammarCount > 1) {
                schema = new XMLSchema(new ReadOnlyGrammarPool(pool));
            }
            else if (grammarCount == 1) {
                Grammar[] grammars = pool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
                schema = new SimpleXMLSchema(grammars[0]);
            }
            else {
                schema = new EmptyXMLSchema();
            }
        }
        else {
            schema = new XMLSchema(new ReadOnlyGrammarPool(pool), false);
        }
        propagateFeatures(schema);
        return schema;
    
public javax.xml.validation.SchemanewSchema()

        /*
         * It would make sense to return an EmptyXMLSchema object here, if
         * fUseGrammarPoolOnly is set to true. However, because the default
         * value of this feature is true, doing so would change the default
         * behaviour of this method. Thus, we return a WeakReferenceXMLSchema
         * regardless of the value of fUseGrammarPoolOnly. -PM
         */
        
        // Use a Schema that uses the system id as the equality source.
        AbstractXMLSchema schema = new WeakReferenceXMLSchema();
        propagateFeatures(schema);
        return schema;
    
private voidpropagateFeatures(AbstractXMLSchema schema)

        schema.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, fSecurityManager != null);
        String[] features = fXMLSchemaLoader.getRecognizedFeatures();
        for (int i = 0; i < features.length; ++i) {
            boolean state = fXMLSchemaLoader.getFeature(features[i]);
            schema.setFeature(features[i], state);
        }
    
public voidsetErrorHandler(org.xml.sax.ErrorHandler errorHandler)

        fErrorHandler = errorHandler;
        fErrorHandlerWrapper.setErrorHandler(errorHandler != null ? errorHandler : DraconianErrorHandler.getInstance());
        fXMLSchemaLoader.setErrorHandler(fErrorHandlerWrapper);
    
public voidsetFeature(java.lang.String name, boolean value)

        if (name == null) {
            throw new NullPointerException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 
                    "FeatureNameNull", null));
        }
        if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
            fSecurityManager = value ? new SecurityManager() : null;
            fXMLSchemaLoader.setProperty(SECURITY_MANAGER, fSecurityManager);
            return;
        }
        else if (name.equals(USE_GRAMMAR_POOL_ONLY)) {
            fUseGrammarPoolOnly = value;
            return;
        }
        try {
            fXMLSchemaLoader.setFeature(name, value);
        }
        catch (XMLConfigurationException e) {
            String identifier = e.getIdentifier();
            if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
                throw new SAXNotRecognizedException(
                        SAXMessageFormatter.formatMessage(Locale.getDefault(), 
                        "feature-not-recognized", new Object [] {identifier}));
            }
            else {
                throw new SAXNotSupportedException(
                        SAXMessageFormatter.formatMessage(Locale.getDefault(), 
                        "feature-not-supported", new Object [] {identifier}));
            }
        }
    
public voidsetProperty(java.lang.String name, java.lang.Object object)

        if (name == null) {
            throw new NullPointerException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 
                    "ProperyNameNull", null));
        }
        if (name.equals(SECURITY_MANAGER)) {
            fSecurityManager = (SecurityManager) object;
            fXMLSchemaLoader.setProperty(SECURITY_MANAGER, fSecurityManager);
            return;
        }
        else if (name.equals(XMLGRAMMAR_POOL)) {
            throw new SAXNotSupportedException(
                    SAXMessageFormatter.formatMessage(Locale.getDefault(), 
                    "property-not-supported", new Object [] {name}));
        }
        try {
            fXMLSchemaLoader.setProperty(name, object);
        }
        catch (XMLConfigurationException e) {
            String identifier = e.getIdentifier();
            if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
                throw new SAXNotRecognizedException(
                        SAXMessageFormatter.formatMessage(Locale.getDefault(), 
                        "property-not-recognized", new Object [] {identifier}));
            }
            else {
                throw new SAXNotSupportedException(
                        SAXMessageFormatter.formatMessage(Locale.getDefault(), 
                        "property-not-supported", new Object [] {identifier}));
            }
        }
    
public voidsetResourceResolver(org.w3c.dom.ls.LSResourceResolver resourceResolver)

        fLSResourceResolver = resourceResolver;
        fDOMEntityResolverWrapper.setEntityResolver(resourceResolver);
        fXMLSchemaLoader.setEntityResolver(fDOMEntityResolverWrapper);