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

TypeInfoProvider.getElementTypeInfo() does not throw IllegalStateException

XMLWordPrintable

    • 1.3
    • generic
    • generic

        Name: eaR10174 Date: 12/17/2003


        The method

             javax.xml.validation.TypeInfoProvider.getElementTypeInfo()

        does not throw IllegalStateException in case when the method is called from ContentHandler
        methods that differ from the method startElement(). The method getElementTypeInfo() should
        throw IllegalStateException according to the javadoc:

            public abstract org.w3c.dom.TypeInfo getElementTypeInfo()
            ...
            The method may only be called by the startElement event of the ContentHandler that the
            application sets to the ValidatorHandler.
            ...
            Throws:
             java.lang.IllegalStateException - If this method is called from other ContentHandler
             methods.

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

          api/javax_xml/validation/TypeInfoProvider/index.html#GetElementTypeInfo[GetElementTypeInfo002]
          api/javax_xml/validation/TypeInfoProvider/index.html#GetElementTypeInfo[GetElementTypeInfo003]
          api/javax_xml/validation/TypeInfoProvider/index.html#GetElementTypeInfo[GetElementTypeInfo004]
          api/javax_xml/validation/TypeInfoProvider/index.html#GetElementTypeInfo[GetElementTypeInfo005]
          api/javax_xml/validation/TypeInfoProvider/index.html#GetElementTypeInfo[GetElementTypeInfo006]

        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.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' type='string'/>\n"
                        + "</schema>\n";

             public static final String XML = "<?xml version='1.0'?>\n"
                     + "<ns:test xmlns:ns='jaxp13_test'>1234abc</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.out.println(e.toString());
                     System.exit(1);
                 }
             }

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

                 DefaultHandler handler = new DefaultHandler() {
                     public void characters(char[] ch, int start, int length)
                               throws SAXException {
                         TypeInfoProvider infoProvider = null;
                         synchronized (validatorHandler) {
                             infoProvider = validatorHandler.getTypeInfoProvider();
                         }
                         if (infoProvider == null) {
                             throw new SAXException("Can't obtain TypeInfoProvider object.");
                         }

                         try {
                             infoProvider.getElementTypeInfo();
                             throw new SAXException("IllegalStateException was not thrown.");
                         } catch (IllegalStateException e) {
                             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)

        org.xml.sax.SAXException: IllegalStateException was not thrown.
        --------------------------------------------------------------------------

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

              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: