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

ValidationHandler: xmlns* are not listed in Attributes object

    Details

    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description



      Name: eaR10174 Date: 02/20/2004


         The element attributes 'xmlns*' are not listed in the Attributes object when the feature
      "http://xml.org/sax/features/namespace-prefixes" is set to true. They should be listed according
      to the javadoc:

            public abstract class ValidatorHandler
            ...

            This spec defines the following feature that must be recognized by all ValidatorHandler implementations.
            http://xml.org/sax/features/namespace-prefixes
            ...
            Additionally, when this feature is set to true, it must also make sure that the user's ContentHandler
            will see the corresponding xmlns* attribute in the Attributes object of the
            ContentHandler.startElement(String,String,String,Attributes) callback.

      The bug affects the new JCK1.5-beta1 test:

          api/javax_xml/validation/ValidatorHandler/index.html#GetSetFeature[GetSetFeature008]


      This test has been in the known failures list with the bug #4970400.

      The sample is provided below; it fails on JDK 1.5.0-beta-b39.
      ------------------------------------test.java-----------------------------
      import java.io.ByteArrayInputStream;
      import java.io.InputStreamReader;
      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.ValidatorHandler;
      import org.xml.sax.Attributes;
      import org.xml.sax.InputSource;
      import org.xml.sax.SAXException;
      import org.xml.sax.XMLReader;
      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'/>\n"
                   + " </complexType>\n"
                   + " </element>\n"
                   + "</schema>\n";

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

           private XMLReader createXMLReader() throws Exception {
               SAXParserFactory parserFactory = SAXParserFactory.newInstance();
               parserFactory.setNamespaceAware(true);

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

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

               InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(xsd.getBytes()));
               StreamSource xsdSource = new StreamSource(reader);

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

           private void parse(XMLReader xmlReader, String xml) throws Exception {
               InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(xml.getBytes()));
               InputSource inSource = new InputSource(reader);

               xmlReader.parse(inSource);
           }

           public static void main(String argv[]) {
               try {
                   new test().run();
               } catch (Exception e) {
                   System.out.println(e.toString());
                   System.exit(1);
               }
           }

           public void run() throws Exception {
               ValidatorHandler validatorHandler = createValidatorHandler(XSD);
               DefaultHandler contentHandler = new DefaultHandler() {
                   public void startElement(String uri,
                                    String localName,
                                    String qName,
                                    Attributes attributes)
                             throws SAXException {
                       if (!"ns:test".equals(qName)) {
                           return;
                       }

                       int index = attributes.getIndex("xmlns:ns");
                       if (index == -1) {
                           throw new SAXException("The attribute 'xmlns:ns' is not in the list.");
                       }
                       if (!"jaxp13_test".equals(attributes.getValue(index))) {
                           throw new SAXException("Wrong value of the attribute 'xmlns:ns'.");
                       }

                       System.out.println("OK");
                   }
               };
               validatorHandler.setContentHandler(contentHandler);
               validatorHandler.setErrorHandler(contentHandler);
               validatorHandler.setFeature("http://xml.org/sax/features/namespace-prefixes", true);

               XMLReader xmlReader = createXMLReader();
               xmlReader.setContentHandler(validatorHandler);
               parse(xmlReader, XML);
           }
      }
      --------------------------------------------------------------------------
      % java -showversion test
      java version "1.5.0-beta2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b39)
      Java HotSpot(TM) Client VM (build 1.5.0-beta2-b39, mixed mode)

      org.xml.sax.SAXException: The attribute 'xmlns:ns' is not in the list.
      --------------------------------------------------------------------------

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

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                evgsunw Evg Evg (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: