xmlschemaloader.java

来自「JAVA 所有包」· Java 代码 · 共 1,324 行 · 第 1/4 页

JAVA
1,324
字号
                LocationArray la = ((LocationArray)locations.get(namespace));                if(la == null) {                    la = new LocationArray();                    locations.put(namespace, la);                }                la.addLocation(location);            }        }        return true;    } // tokenizeSchemaLocation(String, Hashtable):  boolean        /**     * Translate the various JAXP SchemaSource property types to XNI     * XMLInputSource.  Valid types are: String, org.xml.sax.InputSource,     * InputStream, File, or Object[] of any of previous types.     * REVISIT:  the JAXP 1.2 spec is less than clear as to whether this property     * should be available to imported schemas.  I have assumed     * that it should.  - NG     * Note: all JAXP schema files will be checked for full-schema validity if the feature was set up     *      */    private void processJAXPSchemaSource(Hashtable locationPairs) throws IOException {        fJAXPProcessed = true;        if (fJAXPSource == null) {            return;        }                Class componentType = fJAXPSource.getClass().getComponentType();        XMLInputSource xis = null;        String sid = null;        if (componentType == null) {            // Not an array            if(fJAXPSource instanceof InputStream ||                    fJAXPSource instanceof InputSource) {                SchemaGrammar g = (SchemaGrammar)fJAXPCache.get(fJAXPSource);                if(g != null) {                    fGrammarBucket.putGrammar(g);                    return;                }            }            fXSDDescription.reset();            xis = xsdToXMLInputSource(fJAXPSource);            sid = xis.getSystemId();            fXSDDescription.fContextType = XSDDescription.CONTEXT_PREPARSE;            if (sid != null) {                fXSDDescription.setBaseSystemId(xis.getBaseSystemId());                fXSDDescription.setLiteralSystemId(sid);                fXSDDescription.setExpandedSystemId(sid);                fXSDDescription.fLocationHints = new String[]{sid};            }            SchemaGrammar g = loadSchema(fXSDDescription, xis, locationPairs);            // it is possible that we won't be able to resolve JAXP schema-source location            if (g != null){                if(fJAXPSource instanceof InputStream ||                        fJAXPSource instanceof InputSource) {                    fJAXPCache.put(fJAXPSource, g);                    if(fIsCheckedFully) {                        XSConstraints.fullSchemaChecking(fGrammarBucket, fSubGroupHandler, fCMBuilder, fErrorReporter);                    }                }                fGrammarBucket.putGrammar(g);            }            return ;        } else if ( (componentType != Object.class) &&                (componentType != String.class) &&                (componentType != File.class) &&                (componentType != InputStream.class) &&                (componentType != InputSource.class)        ) {            // Not an Object[], String[], File[], InputStream[], InputSource[]            throw new XMLConfigurationException(                    XMLConfigurationException.NOT_SUPPORTED, "\""+JAXP_SCHEMA_SOURCE+                    "\" property cannot have an array of type {"+componentType.getName()+                    "}. Possible types of the array supported are Object, String, File, "+            "InputStream, InputSource.");        }                // JAXP spec. allow []s of type String, File, InputStream,        // InputSource also, apart from [] of type Object.        Object[] objArr = (Object[]) fJAXPSource;        //make local vector for storing targetn namespaces of schemasources specified in object arrays.        Vector jaxpSchemaSourceNamespaces = new Vector() ;        for (int i = 0; i < objArr.length; i++) {            if(objArr[i] instanceof InputStream ||                    objArr[i] instanceof InputSource) {                SchemaGrammar g = (SchemaGrammar)fJAXPCache.get(objArr[i]);                if (g != null) {                    fGrammarBucket.putGrammar(g);                    continue;                }            }            fXSDDescription.reset();            xis = xsdToXMLInputSource(objArr[i]);            sid = xis.getSystemId();            fXSDDescription.fContextType = XSDDescription.CONTEXT_PREPARSE;            if (sid != null) {                fXSDDescription.setBaseSystemId(xis.getBaseSystemId());                fXSDDescription.setLiteralSystemId(sid);                fXSDDescription.setExpandedSystemId(sid);                fXSDDescription.fLocationHints = new String[]{sid};            }            String targetNamespace = null ;            // load schema            SchemaGrammar grammar = fSchemaHandler.parseSchema(xis,fXSDDescription, locationPairs);                        if(fIsCheckedFully) {                XSConstraints.fullSchemaChecking(fGrammarBucket, fSubGroupHandler, fCMBuilder, fErrorReporter);            }                                               if(grammar != null){                targetNamespace = grammar.getTargetNamespace() ;                if(jaxpSchemaSourceNamespaces.contains(targetNamespace)){                    //when an array of objects is passed it is illegal to have two schemas that share same namespace.                    throw new java.lang.IllegalArgumentException(                            " When using array of Objects as the value of SCHEMA_SOURCE property , " +                    "no two Schemas should share the same targetNamespace. " );                }                else{                    jaxpSchemaSourceNamespaces.add(targetNamespace) ;                }                if(objArr[i] instanceof InputStream ||                        objArr[i] instanceof InputSource) {                    fJAXPCache.put(objArr[i], grammar);                }                fGrammarBucket.putGrammar(grammar);            }            else{                //REVISIT: What should be the acutal behavior if grammar can't be loaded as specified in schema source?            }        }    }//processJAXPSchemaSource        private XMLInputSource xsdToXMLInputSource(            Object val)    {        if (val instanceof String) {            // String value is treated as a URI that is passed through the            // EntityResolver            String loc = (String) val;                      fXSDDescription.reset();            fXSDDescription.setValues(null, loc, null, null);            XMLInputSource xis = null;            try {                xis = fEntityManager.resolveEntity(fXSDDescription);            } catch (IOException ex) {                fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN,                        "schema_reference.4",                        new Object[] { loc }, XMLErrorReporter.SEVERITY_ERROR);            }            if (xis == null) {                // REVISIT: can this happen?                // Treat value as a URI and pass in as systemId                return new XMLInputSource(null, loc, null);            }            return xis;        } else if (val instanceof InputSource) {            return saxToXMLInputSource((InputSource) val);        } else if (val instanceof InputStream) {            return new XMLInputSource(null, null, null,                    (InputStream) val, null);        } else if (val instanceof File) {            File file = (File) val;            InputStream is = null;            try {                is = new BufferedInputStream(new FileInputStream(file));            } catch (FileNotFoundException ex) {                fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN,                        "schema_reference.4", new Object[] { file.toString() },                        XMLErrorReporter.SEVERITY_ERROR);            }            return new XMLInputSource(null, null, null, is, null);        }        throw new XMLConfigurationException(                XMLConfigurationException.NOT_SUPPORTED, "\""+JAXP_SCHEMA_SOURCE+                "\" property cannot have a value of type {"+val.getClass().getName()+                "}. Possible types of the value supported are String, File, InputStream, "+        "InputSource OR an array of these types.");    }            //Convert a SAX InputSource to an equivalent XNI XMLInputSource        private static XMLInputSource saxToXMLInputSource(InputSource sis) {        String publicId = sis.getPublicId();        String systemId = sis.getSystemId();                Reader charStream = sis.getCharacterStream();        if (charStream != null) {            return new XMLInputSource(publicId, systemId, null, charStream,                    null);        }                InputStream byteStream = sis.getByteStream();        if (byteStream != null) {            return new XMLInputSource(publicId, systemId, null, byteStream,                    sis.getEncoding());        }                return new XMLInputSource(publicId, systemId, null);    }        static class LocationArray{                int length ;        String [] locations = new String[2];                public void resize(int oldLength , int newLength){            String [] temp = new String[newLength] ;            System.arraycopy(locations, 0, temp, 0, Math.min(oldLength, newLength));            locations = temp ;            length = Math.min(oldLength, newLength);        }                public void addLocation(String location){            if(length >= locations.length ){                resize(length, Math.max(1, length*2));            }            locations[length++] = location;        }//setLocation()                public String [] getLocationArray(){            if(length < locations.length ){                resize(locations.length, length);            }            return locations;        }//getLocationArray()                public String getFirstLocation(){            return length > 0 ? locations[0] : null;        }                public int getLength(){            return length ;        }            } //locationArray        /* (non-Javadoc)     * @see com.sun.org.apache.xerces.internal.xni.parser.XMLComponent#getFeatureDefault(java.lang.String)     */    public Boolean getFeatureDefault(String featureId) {        if (featureId.equals(AUGMENT_PSVI)){            return Boolean.TRUE;        }        return null;    }        /* (non-Javadoc)     * @see com.sun.org.apache.xerces.internal.xni.parser.XMLComponent#getPropertyDefault(java.lang.String)     */    public Object getPropertyDefault(String propertyId) {        // TODO Auto-generated method stub        return null;    }        /* (non-Javadoc)     * @see com.sun.org.apache.xerces.internal.xni.parser.XMLComponent#reset(com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager)     */    public void reset(XMLComponentManager componentManager) throws XMLConfigurationException {                fGrammarBucket.reset();                fSubGroupHandler.reset();		                boolean parser_settings;        try {            parser_settings = componentManager.getFeature(PARSER_SETTINGS);             }        catch (XMLConfigurationException e){            parser_settings = true;        }        if (!parser_settings || !fSettingsChanged){                        // reinitialize grammar bucket            initGrammarBucket();            return;                   }                 // get registered entity manager to be able to resolve JAXP schema-source property:        // Note: in case XMLSchemaValidator has created the loader,         // the entity manager property is null        fEntityManager = (XMLEntityManager)componentManager.getProperty(ENTITY_MANAGER);                      // get the error reporter        fErrorReporter = (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER);                boolean psvi = true;        try {            psvi = componentManager.getFeature(AUGMENT_PSVI);        } catch (XMLConfigurationException e) {            psvi = false;        }                if (!psvi) {            fDeclPool.reset();            fCMBuilder.setDeclPool(fDeclPool);            fSchemaHandler.setDeclPool(fDeclPool);        } else {            fCMBuilder.setDeclPool(null);            fSchemaHandler.setDeclPool(null);        }                // get schema location properties        try {            fExternalSchemas = (String) componentManager.getProperty(SCHEMA_LOCATION);            fExternalNoNSSchema =                (String) componentManager.getProperty(SCHEMA_NONS_LOCATION);        } catch (XMLConfigurationException e) {            fExternalSchemas = null;            fExternalNoNSSchema = null;        }        // get JAXP sources if available        try {            fJAXPSource = componentManager.getProperty(JAXP_SCHEMA_SOURCE);            fJAXPProcessed = false;                    } catch (XMLConfigurationException e) {            fJAXPSource = null;            fJAXPProcessed = false;        }                // clear grammars, and put the one for schema namespace there        try {            fGrammarPool = (XMLGrammarPool) componentManager.getProperty(XMLGRAMMAR_POOL);        } catch (XMLConfigurationException e) {            fGrammarPool = null;        }        initGrammarBucket();        // get continue-after-fatal-error feature        try {            boolean fatalError = componentManager.getFeature(CONTINUE_AFTER_FATAL_ERROR);            fErrorReporter.setFeature(CONTINUE_AFTER_FATAL_ERROR, fatalError);        } catch (XMLConfigurationException e) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?