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

TypeInfoProvider.isSpecified returns false for attribute existing in document

XMLWordPrintable

    • b38
    • generic
    • generic
    • Verified

      Name: eaR10174 Date: 12/19/2003


      The method

           javax.xml.validation.TypeInfoProvider.isSpecified(int index)

      returns false in case of an attribute that is originally present in the document.
      The method should return true according to the javadoc:

         public abstract boolean isSpecified(int index)

           Returns false if the attribute was added by the validator.

           ...

           A general guideline for validators is to return true if the attribute was
           originally present in the pipeline, and false if it was added by the validator.


      The bug affects a new JCK1.5 test (not integrated yet):

        api/javax_xml/validation/TypeInfoProvider/index.html#IsSpecified[IsSpecified001]

      The following test fails running on JDK 1.5.0-beta-b31.
      See below the test source and the execution log:
      ------------------------------------test.java-----------------------------
      import java.io.StringReader;
      import java.io.IOException;
      import javax.xml.parsers.ParserConfigurationException;
      import javax.xml.parsers.SAXParserFactory;
      import javax.xml.transform.stream.StreamSource;
      import javax.xml.validation.Schema;
      import javax.xml.validation.SchemaFactory;
      import javax.xml.validation.TypeInfoProvider;
      import javax.xml.validation.ValidatorHandler;
      import org.xml.sax.InputSource;
      import org.xml.sax.SAXException;
      import org.xml.sax.XMLReader;
      import org.xml.sax.Attributes;
      import org.xml.sax.helpers.DefaultHandler;

      public class test {
           public static final String XSD = "<?xml version='1.0'?>\n"
                   + "<schema xmlns='http://www.w3.org/2001/XMLSchema&#39;\n"
                   + " xmlns:test='jaxp13_test'\n"
                   + " targetNamespace='jaxp13_test'\n"
                   + " elementFormDefault='qualified'>\n"
                   + " <element name='test'>\n"
                   + " <complexType>\n"
                   + " <sequence>\n"
                   + " <element name='child' type='string'/>\n"
                   + " </sequence>\n"
                   + " <attribute name='id' type='ID'/>\n"
                   + " <attribute name='date' type='date' default='2003-12-04'/>\n"
                   + " </complexType>\n"
                   + " </element>\n"
                   + "</schema>\n";

           public static final String XML = "<?xml version='1.0'?>\n"
                   + "<ns:test xmlns:ns='jaxp13_test' id='i001'>\n"
                   + " <ns:child>123abc</ns:child>\n"
                   + "</ns:test>\n";

           private ValidatorHandler createValidatorHandler(String xsd)
                   throws SAXException {
               SchemaFactory schemaFactory =
                   SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

               StringReader reader = new StringReader(xsd);
               StreamSource xsdSource = new StreamSource(reader);

               Schema schema = schemaFactory.newSchema(xsdSource);
               return schema.newValidatorHandler();
           }

           private XMLReader createXMLReader() throws ParserConfigurationException, SAXException {
               SAXParserFactory parserFactory = SAXParserFactory.newInstance();
               if (!parserFactory.isNamespaceAware()) {
                   parserFactory.setNamespaceAware(true);
               }

               return parserFactory.newSAXParser().getXMLReader();
           }

           private void parse(XMLReader xmlReader, String xml) throws SAXException, IOException {
               StringReader reader = new StringReader(xml);
               InputSource inSource = new InputSource(reader);

               xmlReader.parse(inSource);
           }

           public static void main(String argv[]) {
               try {
                   new test().run();
               } catch (Exception e) {
                   System.err.print("Failed. ");
                   System.err.println(e.toString());
               }
           }

           public void run() throws Exception {
               XMLReader xmlReader = createXMLReader();
               final ValidatorHandler validatorHandler = createValidatorHandler(XSD);
               xmlReader.setContentHandler(validatorHandler);

               DefaultHandler handler = new DefaultHandler() {
                   public void startElement(String uri, String localName,
                                    String qName, Attributes attributes)
                             throws SAXException {
                       if (!"ns:test".equals(qName)) {
                           return;
                       }

                       TypeInfoProvider infoProvider = validatorHandler.getTypeInfoProvider();
                       if (infoProvider == null) {
                           throw new SAXException("Can't obtain TypeInfoProvider object.");
                       }

                       int index = attributes.getIndex("id");
                       if (index == -1) {
                           throw new SAXException("The attribute 'id' is not in the list.");
                       }

                       if (!infoProvider.isSpecified(index)) {
                           throw new SAXException("Returned false, expected true.");
                       }

                       System.out.println("OK");
                   }
               };
               validatorHandler.setContentHandler(handler);

               parse(xmlReader, XML);
           }
      }
      --------------------------------------------------------------------------
      % java -showversion test
      java version "1.5.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b31)
      Java HotSpot(TM) Client VM (build 1.5.0-beta-b31, mixed mode)

      Failed. org.xml.sax.SAXException: Returned false, expected true.
      --------------------------------------------------------------------------

      ======================================================================

            kkawagucsunw Kohsuke Kawaguchi (Inactive)
            evgsunw Evg Evg (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: