You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Simon Vogensen <si...@lakesite.dk> on 2006/10/30 15:36:13 UTC

Chained schemas loaded via array of inputstream not supported?

Hi

Im using Xerces 2.8.0.
Because of compatibility issues I need to validate my xml by schemas via
DocumentBuilder.parse()
My root schema imports other schemas in multiple levels.
2 of the schemas import each other, so there is a circular dependency
between them.

My problem is this: how do I load these schemas from jar resources?

If I do this:

        InputStream schemaStream = XmlUtil.class.getResourceAsStream
("/root.xsd");
        documentBuilderFactory.setAttribute(SCHEMA_SOURCE, schemaStream);

only the root.xsd is loaded from the classpath (the jar). The others are
only found if I add them to the runtime dir.
It seems like Xerces don't look in classpath when parsing the schemaLocation
hint on <import> ?

If I add all my schemas to the SCHEMA_SOURCE attribute:

       documentBuilderFactory.setAttribute(SCHEMA_SOURCE, new
InputStream[]{schemaStream1, schemaStream2});

then it fails because of the circular import? (the circular import is not a
problem when schemas is loaded as local files)

I know these problems disappear if I use the jaxp.validation api, but thats
not an option.

Am I doing something wrong here?

Kind regards
Simon Vogensen
Lake IT

Re: Chained schemas loaded via array of inputstream not supported?

Posted by Simon Vogensen <vo...@gmail.com>.
Fantastic! Thanks for the quick answer..

I just implemented the following EntityResolver, and that fixed my problem..

            EntityResolver entityresolver = new EntityResolver() {
                public InputSource resolveEntity(String publicId, String
systemId) throws SAXException, IOException {
                    if (systemId.indexOf("wsu.xsd") != -1) {
                        InputStream schemaStream =
XmlUtil.class.getResourceAsStream("/wsu.xsd");
                        return new InputSource(schemaStream);
...
                    } else {
                        // use the default behaviour
                        return null;
                    }
                }

            };
            documentBuilder.setEntityResolver(entityresolver);

Cheers
Simon Vogensen

On 10/30/06, Stanimir Stamenkov <st...@myrealbox.com> wrote:
>
> /Simon Vogensen/:
>
> > My problem is this: how do I load these schemas from jar resources?
>
> I guess because the jar: URIs are opaque [1], relative resources
> can't be resolved automatically so you would need to assign a custom
> entity resolver to handle those.
>
> [1] <http://java.sun.com/j2se/1.5.0/docs/api/java/net/URI.html>:
>
> > An opaque URI is an absolute URI whose scheme-specific part does not
> > begin with a slash character ('/'). Opaque URIs are not subject to
> > further parsing.
>
> --
> Stanimir
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org
>
>

Re: Chained schemas loaded via array of inputstream not supported?

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Stanimir Stamenkov <st...@myrealbox.com> wrote on 10/30/2006 10:53:12 AM:

> /Simon Vogensen/:
> 
> > My problem is this: how do I load these schemas from jar resources?
> 
> I guess because the jar: URIs are opaque [1], relative resources 
> can't be resolved automatically so you would need to assign a custom 
> entity resolver to handle those.

Actually the issue is with passing the parser an InputStream as the schema 
source. The schema loader will fallback to using the current working 
directory (the value of the system property user.dir) as the base URI for 
resolution since it has no idea where your document is located. If you 
wrap the InputStream in an InputSource and set the system ID on this 
object it should work.

> [1] <http://java.sun.com/j2se/1.5.0/docs/api/java/net/URI.html>:
> 
> > An opaque URI is an absolute URI whose scheme-specific part does not 
> > begin with a slash character ('/'). Opaque URIs are not subject to 
> > further parsing.
> 
> -- 
> Stanimir
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Re: Chained schemas loaded via array of inputstream not supported?

Posted by Stanimir Stamenkov <st...@myrealbox.com>.
/Simon Vogensen/:

> My problem is this: how do I load these schemas from jar resources?

I guess because the jar: URIs are opaque [1], relative resources 
can't be resolved automatically so you would need to assign a custom 
entity resolver to handle those.

[1] <http://java.sun.com/j2se/1.5.0/docs/api/java/net/URI.html>:

> An opaque URI is an absolute URI whose scheme-specific part does not 
> begin with a slash character ('/'). Opaque URIs are not subject to 
> further parsing.

-- 
Stanimir

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org