- From: Andy Davidson <andy_davidson@apple.com>
- Date: Tue, 4 Jan 2011 13:09:29 -0800
- To: xmlschema-dev@w3.org
- Message-Id: <C69FFCA4-D09D-4868-919F-3CDCB616B9D6@apple.com>
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:10:33 UTC