Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8175245

bug when validating xml from schema when xml is DomSource

XMLWordPrintable

      FULL PRODUCT VERSION :
      openjdk version "1.8.0_102"
      OpenJDK Runtime Environment (build 1.8.0_102-8u102-b14.1-2-b14)
      OpenJDK 64-Bit Server VM (build 25.102-b14, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Linux 4.8.0-2-amd64 #1 SMP Debian 4.8.11-1 (2016-12-02) x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      A java.xml.validation.schema.newValiator() won't validate when the given XML has been built from a DomSource

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See attached script

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      attached script should output

      validation OK from String Resource
      validation OK from DOM Resource
      ACTUAL -
      attached script outputs:

      validation OK from String Resource
      validation NOK from DOM Resource reason:org.xml.sax.SAXParseException; cvc-elt.1 : Déclaration de l'élément 'per:person' introuvable.


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.IOException;
      import java.io.StringReader;

      import javax.xml.XMLConstants;
      import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;
      import javax.xml.parsers.ParserConfigurationException;
      import javax.xml.transform.Source;
      import javax.xml.transform.dom.DOMSource;
      import javax.xml.transform.stream.StreamSource;
      import javax.xml.validation.*;

      import org.w3c.dom.Document;
      import org.xml.sax.InputSource;
      import org.xml.sax.SAXException;

      public class MainValTest
      {

      public static String XMLSTR = "<per:person xmlns:per=\"http://www.tbrc.org/models/person\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" RID=\"P1331\" status=\"released\" xsi:schemaLocation=\"http://www.tbrc.org/models/person person.xsd\"/>";
      public static String XSDSTR = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
      +"<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
      +" targetNamespace=\"http://www.tbrc.org/models/person\" xmlns:per=\"http://www.tbrc.org/models/person\""
      +" xmlns=\"http://www.tbrc.org/models/person\""
      +" elementFormDefault=\"qualified\" attributeFormDefault=\"unqualified\">"
      +" <xsd:element name=\"person\" type=\"Person\"/>"
      +" <xsd:complexType name=\"Person\">"
      +" <xsd:attribute name=\"RID\" type=\"xsd:string\" use=\"required\"/>"
      +" <xsd:attribute name=\"status\" type=\"xsd:string\" default=\"editing\"/>"
      +" </xsd:complexType>"
      +"</xsd:schema>";

      public static void main(String[] args)
      {
      Source xsdSource = new StreamSource(new StringReader(XSDSTR));

      Source xmlSourceFromString = new StreamSource(new StringReader(XMLSTR));

      // build xmlSourceFromDom
      DocumentBuilder parser;
      try {
      parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      } catch (ParserConfigurationException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      return;
      }
      Document documenttest;
      try {
      documenttest = parser.parse(new InputSource( new StringReader( XMLSTR )));
      } catch (SAXException | IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      return;
      }
      Source xmlSourceFromDom = new DOMSource(documenttest);

      // build validator
      SchemaFactory schemaFactory = SchemaFactory
                .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
      Schema schema;
      try {
      schema = schemaFactory.newSchema(xsdSource);
      } catch (SAXException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      return;
      }
      Validator validator = schema.newValidator();

      // validate
      try {
      validator.validate(xmlSourceFromString);
      System.out.println("validation OK from String Resource");
      } catch (SAXException e) {
      System.out.println("validation NOK from String Resource reason:" + e);
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      try {
      validator.validate(xmlSourceFromDom);
      System.out.println("validation OK from DOM Resource");
      } catch (SAXException e) {
      System.out.println("validation NOK from DOM Resource reason:" + e);
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      }
      }
      ---------- END SOURCE ----------

            joehw Joe Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: