Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Invalid
-
2.9.1
-
None
-
None
-
Java 1.6.0_10 Windows Vista Business
Description
Test code:
import java.io.File;
import java.io.IOException;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.apache.xerces.xs.ElementPSVI;
import org.apache.xerces.xs.XSElementDeclaration;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
class ValidationTest {
public static final String namespace = "http://www.example.org/schema/";
private Schema schema;
private Document document;
private Validator validator;
private Element item;
private Element foo;
public static void main(String[] args)
{ System.out.println("ValidationTest start"); ValidationTest testClass = new ValidationTest(); testClass.doTest(); } private void doTest() {
createSchema();
buildDocument();
try
catch (SAXException e)
{ e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } addItemNode();
addFooNode();
}
private void addFooNode() {
foo = document.createElementNS(namespace, "foo");
item.appendChild(foo);
ElementPSVI elementPSVI;
try
catch (SAXException e)
{ e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } elementPSVI=(ElementPSVI) foo;
XSElementDeclaration declaration = elementPSVI.getElementDeclaration();
if(declaration==null)
try
{ validator.validate(new DOMSource(item), new DOMResult(item)); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }elementPSVI=(ElementPSVI) foo;
declaration = elementPSVI.getElementDeclaration();
if(declaration==null){ System.out.println("declaration==null //4"); }
try { validator.validate(new DOMSource(document.getDocumentElement()), new DOMResult(document.getDocumentElement())); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
elementPSVI=(ElementPSVI) foo;
declaration = elementPSVI.getElementDeclaration();
if(declaration==null){ System.out.println("declaration==null //5"); }
}
private void addItemNode() {
item = document.createElementNS(namespace, "item");
document.getDocumentElement().appendChild(item);
ElementPSVI elementPSVI;
try { validator.validate(new DOMSource(item), new DOMResult(item)); }
catch (SAXException e)
{ e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } elementPSVI=(ElementPSVI) item;
XSElementDeclaration declaration = elementPSVI.getElementDeclaration();
if(declaration==null)
try
{ validator.validate(new DOMSource(document.getDocumentElement()), new DOMResult(document.getDocumentElement())); }catch (SAXException e)
{ e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } elementPSVI=(ElementPSVI) item;
declaration = elementPSVI.getElementDeclaration();
if(declaration==null)
}
private void createSchema() {
SchemaFactory factory = SchemaFactory
.newInstance("http://www.w3.org/2001/XMLSchema");
try {
schema = factory.newSchema(new File("schema.xsd"));
validator = schema.newValidator();
validator.setErrorHandler(new ErrorHandler() {
@Override
public void error(SAXParseException exception) throws SAXException {
}
@Override
public void fatalError(SAXParseException exception)
throws SAXException {
}
@Override
public void warning(SAXParseException exception)
throws SAXException {
}
});
} catch (SAXException e)
}
private void buildDocument() {
System.setProperty(DOMImplementationRegistry.PROPERTY,
"org.apache.xerces.dom.DOMXSImplementationSourceImpl");
DOMImplementationRegistry registry;
try { registry = DOMImplementationRegistry.newInstance(); DOMImplementation impl = (DOMImplementation) registry .getDOMImplementation("psvi 1.0"); document = impl.createDocument(namespace, "root", null); } catch (ClassCastException e) { e.printStackTrace(); }
catch (ClassNotFoundException e)
{ e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); }catch (IllegalAccessException e)
{ e.printStackTrace(); } }
}
Loaded schema ,file schema.xsd :
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns="http://www.example.org/schema/"
targetNamespace="http://www.example.org/schema/">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element name="foo" type="xs:string"></xs:element>
<xs:element name="bar" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Compile and run commands:
javac -cp .;./xercesImpl.jar ValidationTest.java
java -cp .;./xercesImpl.jar ValidationTest
Result:
ValidationTest start
declaration==null //1
declaration==null //3
declaration==null //4
When the element is other than document element there is no information about this element declaration. Using document or documentElement to construct DOMSource and DOMResult makes it works fine.