Re: trouble creating javax.xml.validation.Validator from locally cached version of XMLSchema.xsd

Michael Glavassevich <[email protected]>
Newsgroups gmane.text.xml.schema.devel
Message-ID <OF16C1857A.778B8AED-ON8525780E.008315D2-8525780E.00837628@ca.ibm.com>
Hi Andy,

I was referring to the DOCTYPE at the top of XMLSchema.xsd:

<?xml version='1.0' encoding='UTF-8'?>
<!-- XML Schema schema for XML Schemas: Part 1: Structures -->
<!-- Note this schema is NOT the normative structures schema. -->
<!-- The prose copy in the structures REC is the normative -->
<!-- version (which shouldn't differ from this one except for -->
<!-- this comment and entity expansions, but just in case -->
<!DOCTYPE xs:schema PUBLIC "-//W3C//DTD XMLSCHEMA 200102//EN"
"XMLSchema.dtd" [

...

You do see that, right?

Thanks.

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: [email protected]
E-mail: [email protected]

Andy Davidson <[email protected]> wrote on 01/04/2011 05:49:59 PM:

> Hi Michael
>
> turns out XMLSchema.xsd is really a DTD
>
> as far as I can tell this is the only foreign reference in the DTD.
> <xs:import namespace="http://www.w3.org/XML/1998/namespace"
schemaLocation="
> http://www.w3.org/2001/xml.xsd">
>
> I create an array of Sources. with entries for XMLSchema.xsd and
> xml.xsd. I wonder if setSystemID has an effect on how things are
> resolved? I know that before I called setSystemID( "http://
> www.w3.org/2001/XMLSchema.xsd") I was not able to correctly fetch
> XMLSchema.xsd using the URL http://www.w3.org/2001/XMLSchema.xsd
>
> I have tried calling setSystemID() with the following values, but
> always get the same SAXParseException. (the exception msg prints
> contains what ever value I passed to setSystemID()
>
> http://www.w3.org/2001/XMLSchema
> http://www.w3.org/2001/XMLSchema.xsd
> XMLSchema.xsd
> null
> ./cache/www.w3.org/2001/XMLSchema.xsd
>
> Any ideas how I can debug this?
>
> thanks
>
> Andy
>
> On Jan 4, 2011, at 1:36 PM, Michael Glavassevich wrote:
>
> Andy,
>
> XMLSchema.xsd has a DTD. Did you also download it (i.e.
> XMLSchema.dtd) and everything else it references?
>
> The most likely reason why this is failing is because the parser
> can't find the DTD relative to wherever you put XMLSchema.xsd in
> your local file system.
>
> Thanks.
>
> Michael Glavassevich
> XML Parser Development
> IBM Toronto Lab
> E-mail: [email protected]
> E-mail: [email protected]
>
> Andy Davidson <[email protected]> wrote on 01/04/2011 04:09:29 PM:
>
> > Hi
> >
> > I need to write a code generation tool that takes in XSD files as
> > input. The first thing I want to do is make sure the XSD input file
> > is valid. My code works fine if create the
> > javax.xml.transform.Source using the URL http://www.w3.org/2001/
> XMLSchema.xsd
> > .  Some times the tool must be used offline. In this case I want to
> > load XMLSchema.xsd from the local file system. For unknown reason I
> > can not get this to work. I keep getting a SAXParseException
> >
> > [ERROR] Parse error line:-1 col:-1schema_reference.4: Failed to read
> > schema document './cache/www.w3.org/2001/XMLSchema.xsd', because 1)
> > could not find the document; 2) the document could not be read; 3)
> > the root element of the document is not <xsd:schema>.
> >
> > I checked the current working and directory and know that the file
> > it is complaining about exists.
> >
> > Any idea what I am doing wrong?
> >
> > thanks
> >
> > Andy
> >
> > import static java.lang.System.err;
> >
> > import java.io.File;
> > import java.io.FileInputStream;
> > import java.io.FileNotFoundException;
> > import java.io.IOException;
> > import java.io.InputStream;
> >
> > import java.net.HttpURLConnection;
> > import java.net.URL;
> >
> > import javax.xml.transform.Source;
> > import javax.xml.transform.stream.StreamSource;
> > import javax.xml.validation.Schema;
> > import javax.xml.validation.SchemaFactory;
> > import javax.xml.validation.Validator;
> >
> > import org.xml.sax.SAXException;
> > import org.xml.sax.SAXParseException;
> >
> > import pojo2pooco.Settings;
> >
> > public class ValidateSchema {
> > static Validator validator;
> >
> > static {
> > //
> > // 1. Lookup a factory for the W3C XML Schema language
> > SchemaFactory factory = SchemaFactory.newInstance("http://
> > www.w3.org/2001/XMLSchema");
> >
> > Source sources[] = ValidateSchema.getSources();
> >
> > // 2. Compile the schema.
> > Schema schema = null;
> > try {
> > schema = factory.newSchema(sources);
> >
> > } catch (SAXParseException spe) {
> > [ERROR] Parse error line:-1 col:-1schema_reference.4: Failed to read
> > schema document 'http://www.w3.org/2001/XMLSchema.xsd', because 1)
> > could not find the document; 2) the document could not be read; 3)
> > the root element of the document is not <xsd:schema>.
> >
> > ValidateSchema.printSAXParseError(spe);
> > System.exit(1);
> >
> > } catch (SAXException e) {
> > err.println( "[ERROR] " + e);
> > e.printStackTrace();
> > System.exit(1);
> > }
> >
> > // 3. Get a validator from the schema.
> > validator = schema.newValidator();
> > }
>
> > private static Source[] getSources() {
> > Source ret[] = null;
> > // try to use version from w3.org
> > String schemaLocation = new String
("http://www.w3.org/2001/XMLSchema.xsd");
> >
> > /**
> > * <xs:import namespace="http://www.w3.org/XML/1998/namespace"
> schemaLocation="
> > http://www.w3.org/2001/xml.xsd">
> > */
> >
> > String namespaceLocation = new String
("http://www.w3.org/2001/xml.xsd");
> >
> > if (isURLContentAvalibleFromServer(schemaLocation) &&
> > isURLContentAvalibleFromServer(namespaceLocation)) {
> > ret = new Source[1];
> > ret[0] = getSourceFromServerForURL(schemaLocation);
> > // we do not need namespaceLocation
> >
> > } else {
> > err.println("[INFO] could not get files from www.w3.org");
> > err.println("[INFO] will try to use cache versions from " + Settings.
> > localCachePath);
> > ret = new Source[2];
> > err.println( "******* AEDWIP pwd = " + System.getProperty("user.dir"));
> > String fileName = Settings.localCachePath + "/www.w3.org/2001/
> XMLSchema.xsd";
> > // I tried different systemID values
> > ret[0] = getSourceFromLocalCacheFor(fileName, fileName); //null
> > //"XMLSchema.xsd" //schemaLocation
> > fileName = Settings.localCachePath + "/www.w3.org/2001/xml.xsd";
> > ret[1] = getSourceFromLocalCacheFor(fileName, fileName); //null //
> > "xml.xsd" //namespaceLocation
> > }
> >
> > return ret;
> > }
> >
> > private static Source getSourceFromLocalCacheFor(String fileNameStr,
> > String systemID) {
> >
> > Source ret = null;
> >         File file = new java.io.File(fileNameStr);
> > InputStream input = null;
> > try {
> > input = new FileInputStream(file);
> > } catch (FileNotFoundException e) {
> > err.println("[ERROR] getSourceFromLocalCacheFor()"  +
> > e.getLocalizedMessage());
> > System.exit(1);
> > }
> > ret = new StreamSource(input);
> > ret.setSystemId(systemID);
> > return ret;
> > }
> >
> > private static Source getSourceFromServerForURL(String urlStr) {
> > Source ret = null;
> > try {
> > URL url = new URL(urlStr);
> > InputStream input = url.openStream();
> > ret = new StreamSource(input);
> > ret.setSystemId(urlStr);
> > } catch (Exception ex) {
> > err.println( "[ERROR] could not create source for "
> > + urlStr
> > + " " + ex.getLocalizedMessage());
> > }
> >
> > return ret;
> > }
> >
> > private static boolean isURLContentAvalibleFromServer(String
testURLStr) {
> > boolean ret = false;
> > HttpURLConnection connection = null;
> >
> > try {
> > connection = (HttpURLConnection) new URL(testURLStr)
> > .openConnection();
> > connection.setRequestMethod("HEAD");
> > int responseCode = connection.getResponseCode();
> >
> > if (responseCode == 200) {
> > ret = true;
> > }
> > } catch (Exception ex) {
> > // content is not available
> > ret = false;
> > } finally {
> > if (connection != null) {
> > connection.disconnect();
> > }
> > }
> >
> > return ret;
> > }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.