SchemaFactory.newSchema can't be called more than one with the same sources

I am using Java 5 or Java 6. Did you already observed such behavior? Seems a bug but I didn't find a Bug ID match.

// Create an array of sources

final InputStream xsd =new FileInputStream("src/xml.xsd");

StreamSource[] sources =new StreamSource[1];

sources[0] =new StreamSource(xsd);

// Create the Schema Factory

final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

[code]

// Create a schema

Schema schema1 = schemaFactory.newSchema(sources);

// Create a second schema, it will throw an Excepion

Schema schema2 = schemaFactory.newSchema(sources);

If you are creating a new array of sources before to create the second schema, it works fine. It seems that SchemaFactory.newSchema is making the passed sources as not "reusable"

Thrown Exception :

Exception in thread "main" org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'null', 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>.

at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:236)

at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:172)

at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:382)

at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:316)

at [/code]com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaError(XSDHandler.java:2245)

at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getSchema(XSDHandler.java:1590)

at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:438)

at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:556)

at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:523)

at com.sun.org.apache.xerces.internal.jaxp.validation.xs.SchemaFactoryImpl.newSchema(SchemaFactoryImpl.java:206)

at javaapplication5.Main.main(Main.java:55)

[2610 byte] By [jfdenisea] at [2007-11-26 18:35:23]
# 1

It looks like a bug to me too. If you do not insist on using java.io.FileInputStream, I suggest using java.io.File instead. So if you replace this line:final InputStream xsd = new FileInputStream("src/xml.xsd");

with this:final File xsd = new File("src/xml.xsd");

there will be no problem.

prgguya at 2007-7-9 6:09:28 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
I can't use File. I am loading xchemas from a jar file. File doesn't know how to deal with URI of files contained in a jar.I am going to log a bug.
jfdenisea at 2007-7-9 6:09:28 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

jfdenise,

The problem here is that in general an InputStream cannot be reused once it has been consumed. In your example, in particular, the FileInputStream is fully consumed in the first called to newSchema(Source[]), and most likely closed too. You need to create new instances of FileInputStream for your second call to work. Hope this helps.

spericasa at 2007-7-9 6:09:28 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

I've looked further into the matter and the problem is that the newSchema() method closes the underlying input stream, as a result of which there is no way to reuse the same source object if it is based on an input stream. There's another alternative: if it is not necessary for you to provide the schemas in an array, you can use the "newSchema(URL schema)" method. The advantage of using an URL object is that it can hold a reference to files inside a jar file.

prgguya at 2007-7-9 6:09:28 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...