Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Invalid
-
2.3.0
-
None
-
None
-
Microsoft Windows XP [Version 5.1.2600]
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode)
Description
Xerces incorrectly reports ERROR: cvc-complex-type.2.2: Element 'soap-header' must have no element [children], and the value must be valid.
for a valid XML document. The problem appears only when you first get a DOM from XML and validate it with Validator.validate. If you validate the XML directly from StreamSource
the error message is not there. Please have a look at the provided source to reproduce the problem. If you comment the passage // 1 and uncomment the passage // 2 the
validation will pass fine.
import java.io.IOException;
import java.io.StringBufferInputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class Parse {
private static final String XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<ejb-jar version=\"2.1\" xmlns=\"http://java.sun.com/xml/ns/j2ee\" xmlns:soap-header_ns__=\"http://extra-header.org\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd\">"
+ "<display-name>Ejb1</display-name>"
+ "<enterprise-beans>"
+ "<session>"
+ "<ejb-name>com_sun_ts_tests_common_vehicle_ejb_EJBVehicle</ejb-name>"
+ "<home>com.sun.ts.tests.common.vehicle.ejb.EJBVehicleHome</home>"
+ "<remote>com.sun.ts.tests.common.vehicle.ejb.EJBVehicleRemote</remote>"
+ "<ejb-class>com.sun.ts.tests.common.vehicle.ejb.EJBVehicle</ejb-class>"
+ "<session-type>Stateful</session-type>"
+ "<transaction-type>Container</transaction-type>"
+ "<service-ref>"
+ "<service-ref-name>service/SimpleTest</service-ref-name>"
+ "<service-interface>javax.xml.rpc.Service</service-interface>"
+ "<wsdl-file>META-INF/wsdl/SimpleTest.wsdl</wsdl-file>"
+ "<jaxrpc-mapping-file>SimpleTest.xml</jaxrpc-mapping-file>"
+ "<port-component-ref>"
+ "<service-endpoint-interface>com.sun.ts.tests.jaxrpc.wsi.w2j.rpc.literal.R2753.SimpleEndpoint</service-endpoint-interface>"
+ "</port-component-ref>"
+ "<handler>"
+ "<handler-name>Handler1</handler-name>"
+ "<handler-class>com.sun.ts.tests.jaxrpc.wsi.w2j.rpc.literal.R2753.R2753Handler</handler-class>"
+ "<soap-header xmlns:soap-header_ns_=\"http://extra-header.org\">soap-header_ns_:extra-header</soap-header>"
+ "<soap-role>extra-header-actor</soap-role>"
+ "</handler>"
+ "</service-ref>"
+ "<security-identity>"
+ "<use-caller-identity/>"
+ "</security-identity>"
+ "</session>"
+ "</enterprise-beans>"
+ "<assembly-descriptor>"
+ "<container-transaction>"
+ "<method>"
+ "<ejb-name>com_sun_ts_tests_common_vehicle_ejb_EJBVehicle</ejb-name>"
+ "<method-intf>Remote</method-intf>"
+ "<method-name>runTest</method-name>"
+ "</method>"
+ "<trans-attribute>Required</trans-attribute>"
+ "</container-transaction>" + "</assembly-descriptor>" + "</ejb-jar>";
private static HashMap XERCES_NONVALIDATING_ATTR = new LinkedHashMap();
public static void main(String[] args) throws SAXException, IOException,
ParserConfigurationException {
System.setProperty("http.proxyHost", "proxy");
System.setProperty("http.proxyPort", "8080");
SchemaFactory sf = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema ejb21 = sf.newSchema(new StreamSource(
"http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"));
Validator validator = ejb21.newValidator();
validator.setErrorHandler(new ErrorHandler() {
public void warning(SAXParseException exception) throws SAXException
public void error(SAXParseException exception) throws SAXException
{ System.out.println("ERROR: " + exception.getMessage()); }public void fatalError(SAXParseException exception) throws SAXException
{ System.out.println("FATAL_ERROR: " + exception.getMessage()); }});
// 1 first get DOM, then validate
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(false);
dbf.setAttribute("http://xml.org/sax/features/validation", Boolean.FALSE);
dbf.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", Boolean.FALSE);
dbf.setAttribute("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", Boolean.FALSE);
dbf.setAttribute("http://apache.org/xml/features/dom/defer-node-expansion", Boolean.FALSE);
dbf.setAttribute("http://apache.org/xml/features/dom/include-ignorable-whitespace", Boolean.FALSE);
dbf.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", Boolean.FALSE);
dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
DocumentBuilder nonValidating = dbf.newDocumentBuilder();
Document doc = nonValidating.parse(new InputSource(new StringBufferInputStream(XML)));
validator.validate(new DOMSource(doc));
// 2 directly validate
// validator.validate(new StreamSource(new StringBufferInputStream(XML)));
}
}