W3C home > Mailing lists > Public > xmlschema-dev@w3.org > January 2011

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

From: Michael Glavassevich <mrglavas@ca.ibm.com>
Date: Tue, 4 Jan 2011 16:36:35 -0500
To: xmlschema-dev@w3.org
Message-ID: <OF92AA4C7C.52445D01-ON8525780E.0075F937-8525780E.0076B5D1@ca.ibm.com>
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: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

Andy Davidson <andy_davidson@apple.com> 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;
> }
Received on Tuesday, 4 January 2011 21:37:26 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Tuesday, 11 January 2011 00:15:30 GMT