You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Dmitri Toubelis <dt...@algorithmics.com> on 2001/08/30 16:18:30 UTC

Including schema from jar file

Does anybody knows an easy way to include schema from jar file into xml
document. It seems like java URL doesn't work for that.
 
-Dmitri

Re: Including schema from jar file

Posted by Andy Clark <an...@apache.org>.
Dmitri Toubelis wrote:
> Does anybody knows an easy way to include schema from jar file into
> xml document. It seems like java URL doesn't work for that.

Use an EntityResolver that returns an InputSource created
from getClass().getResourceAsStream("..."). Or use getClass().
getClassLoader().getSystemResourceAsStream("...") if you want
to retrieve the resource from any place in the classpath.

-- 
Andy Clark * IBM, TRL - Japan * andyc@apache.org

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


RE: Including schema from jar file

Posted by Eric Hartmann <eh...@rhinfo.com>.
Hi Dmitri,

I had the same problems and the solution I'd found is to create an
CachedEntityResolver with an InputSource from the jar. Something like
(sorry it's cut/paste from existing classes) :

            InputStream in =
getClass().getClassLoader().getResourceAsStream("resources/mySchema.xsd"
);
            CachedEntityResolver resolver = null;
            if (in != null) {
                resolver = new CachedEntityResolver(null,
"http://www.mysite.com/schema1", in);
            }
And then
            SAXParser parser = new SAXParser();
	
parser.setProperty("http://apache.org/xml/properties/schema/external-noN
amespaceSchemaLocation", externalNoNameSpaceSchemaLocation);
 
parser.setProperty("http://apache.org/xml/properties/schema/external-sch
emaLocation", externalSchemaLocation);
            ParseError err= new ParseError(log);
            parser.setErrorHandler(err);
		parser.setEntityResolver(resolver);
            
            parser.setFeature("http://xml.org/sax/features/validation",
true);
            parser.setFeature("http://xml.org/sax/features/namespaces",
true);
 
parser.setFeature("http://apache.org/xml/features/validation/schema",
true);
 
parser.setFeature("http://apache.org/xml/features/nonvalidating/load-ext
ernal-dtd", true);
 
parser.setFeature("http://apache.org/xml/features/validation/schema-full
-checking", true);
            parser.parse(in);
            
            return err.isValid();

------------------------------------------------

ParseError.java is a class implementing ErrorHandler.
And my CachedEntityResolver.java is :

package com.sharedvalue.tools.xml;

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import java.io.InputStream;

/**
 * Classe interne de gestion du controle des schemas / DTD
 */
public class CachedEntityResolver implements EntityResolver {
    
    private String publicId = null;
    private String systemId = null;
    private InputStream in = null;
    
    /** Constructeur de base
     *
     * @param publicId Id public <CODE>null</CODE> si non utilisé
     * @param systemId Id System <CODE>null</CODE> si non utilisé (à
priori celui à utiliser)
     * @param in Input source du dtd ou schéma
     */    
    public CachedEntityResolver(String publicId, String systemId,
InputStream in) {
        this.in = in;
        this.publicId = publicId;
        this.systemId = systemId; 
    }
    
    /**
     * Utiliser pour résoudre les entitées (DTD & Schéma)
     *
     * @param publicId Id public <CODE>null</CODE> si non utilisé
     * @param systemId Id service <CODE>null</CODE> si non utilisé
     * @throws SAXException Exception en cas d'erreur, ce cache ne le
lance jamais
     * @throws IOException En cas d'erreur non lancé
     * @return Le stream caché
     */
    public InputSource resolveEntity(java.lang.String publicId,
java.lang.String systemId) throws org.xml.sax.SAXException,
java.io.IOException {
        if (
            ((this.systemId == null) || ( (this.systemId != null) &&
this.systemId.equals(systemId) )) &&
            ((this.publicId == null) || ( (this.publicId != null) &&
this.publicId.equals(publicId)  ))
            ) {
            
            if (in != null) 
                return new InputSource(in);
            else 
                return null;
        } else {
            return null;
        }
    }
}

I don't know if it's the correct way to do so, but it worked with Xerces
1.4.2.

Hope this helps,

Eric

-----Original Message-----
From: Dmitri Toubelis [mailto:dtoubeli@algorithmics.com] 
Sent: jeudi 30 août 2001 16:19
To: xerces-j-dev@xml.apache.org
Subject: Including schema from jar file


Does anybody knows an easy way to include schema from jar file into xml
document. It seems like java URL doesn't work for that.

-Dmitri


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