Yes and it is described in xerces howtos...

Based on howtos the following code was created, I believe you can easily adopt it to your need.

    public void init(File xs, File xml) throws SAXException, SAXNotSupportedException, IOException
    {
        String xsModelNameSpace = null;
        XSModel xsModel = null;

        SymbolTable sym = new SymbolTable(BIG_PRIME);
        XMLGrammarPreparser preparser = new XMLGrammarPreparser(sym);
        XMLGrammarPoolImpl grammarPool = new XMLGrammarPoolImpl();
        preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);

        preparser.setProperty(GRAMMAR_POOL, grammarPool);
        preparser.setFeature(NAMESPACES_FEATURE_ID, true);
        preparser.setFeature(VALIDATION_FEATURE_ID, true);
        // note we can set schema features just in case...
        preparser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
        preparser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, true);

        if (xs != null)
        {
            System.out.println("Started parsing schema " + xs.getAbsolutePath());
            long t0 = System.currentTimeMillis();

            try
            {
                Grammar g = preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA,
                                        stringToXIS(xs.getAbsolutePath()));
                XSGrammar xsGrammar = (XSGrammar) g;
                xsModelNameSpace = g.getGrammarDescription().getNamespace();
                xsModel = xsGrammar.toXSModel();
            }
            catch (Exception e)
            {
                e.printStackTrace();
                System.exit(1);
            }

            System.out.println("Finished parsing schema " + xs.getAbsolutePath() + " (" + (System.currentTimeMillis() - t0) + " ms)");
        }

        XMLParserConfiguration parserConfiguration = new IntegratedParserConfiguration(sym, grammarPool);

        try
        {
            parserConfiguration.setFeature(NAMESPACES_FEATURE_ID, true);
            parserConfiguration.setFeature(VALIDATION_FEATURE_ID, true);
            // now we can still do schema features just in case,
            // so long as it's our configuraiton......
            parserConfiguration.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
            parserConfiguration.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, true);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.exit(1);
        }

        org.w3c.dom.Element root = null;

        if (xml != null)
        {
            System.out.println("Started parsing document " + xml.getAbsolutePath());
            long t0 = System.currentTimeMillis();

            DOMParser parser = new DOMParser(parserConfiguration);
            // Perform namespace processing: prefixes will be stripped off element and attribute names and
            // replaced with the corresponding namespace URIs. By default, the two will simply be concatenated,
            // but the namespace-sep core property allows the application to specify a delimiter string for
            // separating the URI part and the local part.
            parser.setFeature("http://xml.org/sax/features/namespaces", true);

            // Validate the document and report validity errors.
            parser.setFeature("http://xml.org/sax/features/validation", true);

            // Turn on XML Schema validation by inserting XML Schema validator in the pipeline.
            parser.setFeature("http://apache.org/xml/features/validation/schema", true);

            // Enable full schema grammar constraint checking, including checking which may be time-consuming or
            // memory intensive. Currently, particle unique attribution constraint checking and
            // particle derivation resriction checking are controlled by this option.
//                parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);

            // The fully qualified name of the class implementing the org.w3c.dom.Document interface.
            // The implementation used must have a zero argument constructor.
            //  org.apache.xerces.dom.PSVIDocumentImpl - Apache document implementation,
            //      which knows how to create an element with PSVI information.
            //  PSVI - post schema validation infoset
            parser.setProperty("http://apache.org/xml/properties/dom/document-class-name",
                    "org.apache.xerces.dom.PSVIDocumentImpl");

            parser.parse(xml.getAbsolutePath());
            System.out.println("Finished parsing document " + xml.getAbsolutePath() + " (" + (System.currentTimeMillis() - t0) + " ms)");

            Document document = parser.getDocument();
            root = document.getDocumentElement();

            // retrieve PSVI for the root element
            ElementPSVI rootPSVI;
            rootPSVI = (ElementPSVI)root;

            // retrieve the schema used in validation of this document
            XSModel schema = rootPSVI.getSchemaInformation();
            if (schema == null)
                throw new IllegalArgumentException("Schema file could not be located");

            String docNamespace = root.getAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns");
            if (xsModel == null)
                xsModel = schema;
            else if (!docNamespace.equals(xsModelNameSpace))
                throw new IllegalArgumentException("Schema and document have different namespaces:\n\t" +
                        xsModelNameSpace + "\n\t" + docNamespace + "\n respectively");
        }

    }

    private XMLInputSource stringToXIS(String uri)
    {
        return new XMLInputSource(null, uri, null);
    }

--

Maksym Kovalenko
Software Engineer
Marketswitch Corporation
http://www.marketswitch.com
108 Powers Court, Suite 225
Dulles, VA 20166
Phone: +1 (703) 444-6750 ext. 302
Fax: +1 (703) 444-6812

Reply via email to