javajunitsax

SAXParseException after migrating from java 5 to java 6


I'm facing a problem when migrating an application from Java 5 to java 6. It is some unit tests which went good on Java5.

the code of the unit test is:

public void testValidateAgainstSchemaWhenDocumentIsOk() throws ParserConfigurationException, SAXException, IOException, JAXBException, URISyntaxException {
    // Setup
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputStream stream = this.getClass().getResourceAsStream("/m6/mygale/common/valid_notification.xml");
    Document document = builder.parse(stream);
    stream.close();

    File schemaFile = new File(getClass().getResource("/m6/mygale/common/notification_schema.xsd").toURI());
    // pre assert
    assertNotNull(schemaFile);

    // Action
    JaxbHelper.validateSchema(document, schemaFile);
}

the method of validation is

public static void validateSchema(Document document, File schemaFile) throws SAXException, IOException {
    //LOGGER.debug("validation du document" + document.getTextContent() + " schemaFile=" + schemaFile);
    SchemaFactory factory = newInstance(W3C_XML_SCHEMA_NS_URI);
    Schema schema = factory.newSchema(schemaFile);
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(document));
}

The schema file

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="mygaleNotification" type="notificationXmlBean" />

    <xs:complexType name="notificationXmlBean">
        <xs:sequence>
            <xs:element name="header" type="headerXmlBean" />
            <xs:element name="flowParameters"
                type="flowParametersXmlBean" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="headerXmlBean">
        <xs:all>
            <xs:element name="sourceName" type="xs:string" minOccurs="1"
                maxOccurs="1" />
            <xs:element name="fluxId" type="xs:string" minOccurs="1"
                maxOccurs="1" />
            <xs:element name="notificationDateTime" type="xs:string"
                minOccurs="1" maxOccurs="1" />
        </xs:all>
    </xs:complexType>

    <xs:element name="audiencesMinuteParametersXmlBean"
        type="audiencesMinuteParametersXmlBean" />

    <xs:complexType name="audiencesMinuteParametersXmlBean">
        <xs:all>
            <xs:element name="audiencesBaseUri" type="xs:string"
                minOccurs="0" />
            <xs:element name="broadcastDate" type="xs:string"
                minOccurs="0" />
            <xs:element name="channels" minOccurs="0">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="channel" type="channelXmlBean"
                            maxOccurs="unbounded" minOccurs="0" />
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:all>
    </xs:complexType>

    <xs:complexType name="channelXmlBean">
        <xs:sequence>
            <xs:element name="label" type="xs:string" minOccurs="0" />
        </xs:sequence>
        <xs:attribute name="sherpaId" type="xs:int" />
    </xs:complexType>

    <xs:complexType name="flowParametersXmlBean">
        <xs:sequence>
            <xs:element name="audiencesMinuteParameters"
                type="audiencesMinuteParametersXmlBean" minOccurs="0" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="dateTimeAdapter">
        <xs:complexContent>
            <xs:extension base="xmlAdapter">
                <xs:sequence />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="xmlAdapter" abstract="true" />

    <xs:complexType name="localDateAdapter">
        <xs:complexContent>
            <xs:extension base="xmlAdapter">
                <xs:sequence />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

</xs:schema>

the file to validate

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mygaleNotification>
    <header>
        <fluxId>FROM_SHERPA_TO_PARALLELEANTENNE</fluxId>
        <notificationDateTime>2008-10-18T11:22:37.310+02:00</notificationDateTime>
        <sourceName>Sherpa</sourceName>
    </header>
    <flowParameters>
        <audiencesMinuteParameters>
            <audiencesBaseUri>http://lapplipubd3:8880/mock-sherpa</audiencesBaseUri>
            <broadcastDate>20081017</broadcastDate>
            <channels>
                <channel sherpaId="6">
                    <label>M6</label>
                </channel>
                <channel sherpaId="205">
                    <label>W9</label>
                </channel>
            </channels>
        </audiencesMinuteParameters>
    </flowParameters>
</mygaleNotification>

the stack is

org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'mygaleNotification'.

    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
    at org.apache.xerces.jaxp.validation.DOMValidatorHelper.beginNode(Unknown Source)
    at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
    at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
    at org.apache.xerces.jaxp.validation.ValidatorImpl.validate(Unknown Source)
    at javax.xml.validation.Validator.validate(Validator.java:127)
    at m6.mygale.common.JaxbHelper.validateSchema(JaxbHelper.java:108)
    at m6.mygale.common.JaxbHelperUTest.testValidateAgainstSchemaWhenDocumentIsOk(JaxbHelperUTest.java:59)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

I'm using Java 6u45 et junit 4.5, could anyone help me please?


Solution

  • The answer is that the namespaceAware of the DocumentBuilderFactory is false by default in java 6 I resolved the problem by setting it to true.