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 Gottfried Szing <go...@szing.at> on 2002/04/10 20:50:42 UTC

Re: How to find non local grammer?

On Thu, 11 Apr 2002, Doug Helton wrote:

> Hi,
>
> 	I am using an InputSource as input into my DOMparser,
> and trying to validate files with schemas and DTDs that are
> not local to my xml file.  I can't specify a relative path in
> my file because I may not know where my file will be located,
> I am at a loss.  I tried setting the systemID on the
> InputSource but that doesn't seem to be what I need to do or
> I am somehow using it wrong.  Does anyone have any
> suggestions?

maybe the feature for external-schema locations help you?

http://xml.apache.org/xerces2-j/features.html


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


RE: How to find non local grammer?

Posted by Rob Outar <ro...@ideorlando.org>.
Is there no way to specify where to search for the DTD or XML Schema,
besides setting user.dir?

Thanks,

Rob

-----Original Message-----
From: Brad Buckingham [mailto:bbuckingham@lucent.com]
Sent: Wednesday, April 10, 2002 3:28 PM
To: xerces-j-user@xml.apache.org
Subject: Re: How to find non local grammer?


Doug,

If I understand your issue correctly, it
sounds like something I also encountered
a while back.

Assuming that your DTD and XML file are on
the same system, you may add the directory
location of your DTD to the CLASSPATH,
implement the EntityResolver class overriding
the resolveEntity() method to locate
the DTD using the CLASSPATH.  Then set
this as your parser's entity resolver
prior to parsing the XML document.

Below are a couple of code snippets:

Setting resolver:

DOMParser parser = new DOMParser();
parser.setEntityResolver(new STAEntityResolver());


Class that implements EntityResolver:

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import java.io.*;
import java.net.URL;
import java.util.HashMap;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.apache.xerces.parsers.DOMParser;

//
//  Class Name: STAEntityResolver
//
//  Description:  This class will locate the entity (e.g. DTD or XML)
//  that has been encountered by searching the user's CLASSPATH.
//
public class STAEntityResolver implements EntityResolver
{
	/**
	 * This attempts to resolve the entity associated with the specified
	 * public and system ids. If the systemId is empty, then we use the
	 * publicId to locate the URL of the cataloged DTD file.
	 */
	public InputSource resolveEntity(String publicId, String systemId)
		throws SAXException, IOException {

		String dtd = getUrl(systemId);

		//
		// uses classpath to locate the dtd
		//
		return new InputSource(getClass().getResourceAsStream("/"+dtd));
	}

	/**
	 * Return the URL of the DTD corresponding to the systemId.
	 */
	private static final String getUrl(String systemId) {
		File file = new File(systemId);
		String name = file.getName();
		return name;
	}
}


Doug Helton wrote:
>
> UnFortunately no, I don't want it to overide the name of the schema or
dtd,
> What I realy need is to somehow be able to tell it where to look for that
> grammer.
>
> Thanks,
> Doug Helton
>
> -----Original Message-----
> From: Gottfried Szing [mailto:gottfried@szing.at]
> Sent: Wednesday, April 10, 2002 2:51 PM
> To: Xerces-J-User
> Subject: Re: How to find non local grammer?
>
> On Thu, 11 Apr 2002, Doug Helton wrote:
>
> > Hi,
> >
> >       I am using an InputSource as input into my DOMparser,
> > and trying to validate files with schemas and DTDs that are
> > not local to my xml file.  I can't specify a relative path in
> > my file because I may not know where my file will be located,
> > I am at a loss.  I tried setting the systemID on the
> > InputSource but that doesn't seem to be what I need to do or
> > I am somehow using it wrong.  Does anyone have any
> > suggestions?
>
> maybe the feature for external-schema locations help you?
>
> http://xml.apache.org/xerces2-j/features.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org


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


RE: How to find non local grammer?

Posted by Doug Helton <dh...@ideorlando.org>.
I was able to get it to work for both my schemas and the dtds Thank you very
much!

Doug Helton

-----Original Message-----
From: Brad Buckingham [mailto:bbuckingham@lucent.com]
Sent: Wednesday, April 10, 2002 3:28 PM
To: xerces-j-user@xml.apache.org
Subject: Re: How to find non local grammer?


Doug,

If I understand your issue correctly, it
sounds like something I also encountered
a while back.

Assuming that your DTD and XML file are on
the same system, you may add the directory
location of your DTD to the CLASSPATH,
implement the EntityResolver class overriding
the resolveEntity() method to locate
the DTD using the CLASSPATH.  Then set
this as your parser's entity resolver
prior to parsing the XML document.

Below are a couple of code snippets:

Setting resolver:

DOMParser parser = new DOMParser();
parser.setEntityResolver(new STAEntityResolver());


Class that implements EntityResolver:

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import java.io.*;
import java.net.URL;
import java.util.HashMap;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.apache.xerces.parsers.DOMParser;

//
//  Class Name: STAEntityResolver
//
//  Description:  This class will locate the entity (e.g. DTD or XML)
//  that has been encountered by searching the user's CLASSPATH.
//
public class STAEntityResolver implements EntityResolver
{
	/**
	 * This attempts to resolve the entity associated with the specified
	 * public and system ids. If the systemId is empty, then we use the
	 * publicId to locate the URL of the cataloged DTD file.
	 */
	public InputSource resolveEntity(String publicId, String systemId)
		throws SAXException, IOException {

		String dtd = getUrl(systemId);

		//
		// uses classpath to locate the dtd
		//
		return new InputSource(getClass().getResourceAsStream("/"+dtd));
	}

	/**
	 * Return the URL of the DTD corresponding to the systemId.
	 */
	private static final String getUrl(String systemId) {
		File file = new File(systemId);
		String name = file.getName();
		return name;
	}
}


Doug Helton wrote:
>
> UnFortunately no, I don't want it to overide the name of the schema or
dtd,
> What I realy need is to somehow be able to tell it where to look for that
> grammer.
>
> Thanks,
> Doug Helton
>
> -----Original Message-----
> From: Gottfried Szing [mailto:gottfried@szing.at]
> Sent: Wednesday, April 10, 2002 2:51 PM
> To: Xerces-J-User
> Subject: Re: How to find non local grammer?
>
> On Thu, 11 Apr 2002, Doug Helton wrote:
>
> > Hi,
> >
> >       I am using an InputSource as input into my DOMparser,
> > and trying to validate files with schemas and DTDs that are
> > not local to my xml file.  I can't specify a relative path in
> > my file because I may not know where my file will be located,
> > I am at a loss.  I tried setting the systemID on the
> > InputSource but that doesn't seem to be what I need to do or
> > I am somehow using it wrong.  Does anyone have any
> > suggestions?
>
> maybe the feature for external-schema locations help you?
>
> http://xml.apache.org/xerces2-j/features.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org


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


Re: How to find non local grammer?

Posted by go...@szing.at.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, 10 Apr 2002, Brad Buckingham wrote:

first, thanks to all. some of the answer has also answered a few
question of me. :)

> Assuming that your DTD and XML file are on the same system,
> you may add the directory location of your DTD to the
> CLASSPATH, implement the EntityResolver class overriding the
> resolveEntity() method to locate the DTD using the CLASSPATH.
> Then set this as your parser's entity resolver prior to
> parsing the XML document.

is the entityresolver also called if a schema is wanted? or is
this a general-purpose file loader?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.4 (GNU/Linux)
Comment: pgpenvelope 2.10.2 - http://pgpenvelope.sourceforge.net/

iD8DBQE8tJYXrMda10jkyDYRAlMRAJ92u0G5uVtjTP9V85yZ1R5agF2hAACguqwA
OpDHfL/LVY6X97/oMjx+F8Q=
=KsUz
-----END PGP SIGNATURE-----


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


Re: How to find non local grammer?

Posted by Brad Buckingham <bb...@lucent.com>.
Doug,

If I understand your issue correctly, it
sounds like something I also encountered 
a while back.

Assuming that your DTD and XML file are on
the same system, you may add the directory
location of your DTD to the CLASSPATH,
implement the EntityResolver class overriding
the resolveEntity() method to locate
the DTD using the CLASSPATH.  Then set
this as your parser's entity resolver
prior to parsing the XML document.

Below are a couple of code snippets:

Setting resolver: 

DOMParser parser = new DOMParser(); 
parser.setEntityResolver(new STAEntityResolver());


Class that implements EntityResolver:

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import java.io.*;
import java.net.URL;
import java.util.HashMap;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.apache.xerces.parsers.DOMParser;

//
//  Class Name: STAEntityResolver
//
//  Description:  This class will locate the entity (e.g. DTD or XML)
//  that has been encountered by searching the user's CLASSPATH.
//
public class STAEntityResolver implements EntityResolver
{
	/**
	 * This attempts to resolve the entity associated with the specified
	 * public and system ids. If the systemId is empty, then we use the
	 * publicId to locate the URL of the cataloged DTD file.
	 */
	public InputSource resolveEntity(String publicId, String systemId)
		throws SAXException, IOException {

		String dtd = getUrl(systemId);

		//
		// uses classpath to locate the dtd 
		//
		return new InputSource(getClass().getResourceAsStream("/"+dtd));
	}

	/**
	 * Return the URL of the DTD corresponding to the systemId.
	 */
	private static final String getUrl(String systemId) {
		File file = new File(systemId);
		String name = file.getName();
		return name;
	}
}


Doug Helton wrote:
> 
> UnFortunately no, I don't want it to overide the name of the schema or dtd,
> What I realy need is to somehow be able to tell it where to look for that
> grammer.
> 
> Thanks,
> Doug Helton
> 
> -----Original Message-----
> From: Gottfried Szing [mailto:gottfried@szing.at]
> Sent: Wednesday, April 10, 2002 2:51 PM
> To: Xerces-J-User
> Subject: Re: How to find non local grammer?
> 
> On Thu, 11 Apr 2002, Doug Helton wrote:
> 
> > Hi,
> >
> >       I am using an InputSource as input into my DOMparser,
> > and trying to validate files with schemas and DTDs that are
> > not local to my xml file.  I can't specify a relative path in
> > my file because I may not know where my file will be located,
> > I am at a loss.  I tried setting the systemID on the
> > InputSource but that doesn't seem to be what I need to do or
> > I am somehow using it wrong.  Does anyone have any
> > suggestions?
> 
> maybe the feature for external-schema locations help you?
> 
> http://xml.apache.org/xerces2-j/features.html
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org

RE: How to find non local grammer?

Posted by Gottfried Szing <go...@szing.at>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Thu, 11 Apr 2002, Doug Helton wrote:

> UnFortunately no, I don't want it to overide the name of the
> schema or dtd, What I realy need is to somehow be able to
> tell it where to look for that grammer.

i think this is an possible solution. in out xmls every namespace
referes to a "symbolic" location. e.g.

<a:test xmlns:a="http://xxx/test">
	blabla
</a:test>

and in the xml schema exists an attribute targetnamespace with
the same value. e.g

<schema targetNamespace="http://xxx/test">
	blabla
</schema>


and the i specify the external schema location with

"http://xxx/test file:///local/path/to/schema.xsd"

this works fine for me. but this has been only tested with some
formats and only with schemas (dtds also working?). and there is
no change in the xml or schema - except that the xml has to
follow some conventions like schema location aso.

cu
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.4 (GNU/Linux)
Comment: pgpenvelope 2.10.2 - http://pgpenvelope.sourceforge.net/

iD8DBQE8tJUkrMda10jkyDYRAme6AJ9ocFQOv3L6Bbuc+e35Wh+o0xMVNACggLkG
/9OTGhy9muJcekRZBBFXPqI=
=F2Ya
-----END PGP SIGNATURE-----


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


RE: How to find non local grammer?

Posted by Doug Helton <dh...@ideorlando.org>.
UnFortunately no, I don't want it to overide the name of the schema or dtd,
What I realy need is to somehow be able to tell it where to look for that
grammer.

Thanks,
Doug Helton

-----Original Message-----
From: Gottfried Szing [mailto:gottfried@szing.at]
Sent: Wednesday, April 10, 2002 2:51 PM
To: Xerces-J-User
Subject: Re: How to find non local grammer?


On Thu, 11 Apr 2002, Doug Helton wrote:

> Hi,
>
> 	I am using an InputSource as input into my DOMparser,
> and trying to validate files with schemas and DTDs that are
> not local to my xml file.  I can't specify a relative path in
> my file because I may not know where my file will be located,
> I am at a loss.  I tried setting the systemID on the
> InputSource but that doesn't seem to be what I need to do or
> I am somehow using it wrong.  Does anyone have any
> suggestions?

maybe the feature for external-schema locations help you?

http://xml.apache.org/xerces2-j/features.html


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


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