You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by "Sieger, Nicholas" <ns...@netperceptions.com> on 2000/06/27 00:37:17 UTC

BUG: SAXParser endElement reports incorrect local name w/ namespa ces

I found a problem with the 1.1.* versions of the SAXParser where
the following example XML would not fire correct endElement events - the
local names reported still had the namespace prefixes attached for
elements a and b.  This same test run with version 1.0.4 works correctly.
Source code reproducing the problem follows.

<?xml version="1.0" standalone="yes"?>
<test:a xmlns:test="/dev/null">
 <test:b>
  <test:c></test:c>
 </test:b> <!-- endElement reports the local name to be "test:b" -->
</test:a>  <!-- endElement reports the local name to be "test:a" -->

This XML fires events properly, including reporting the correct
namespaces:

<?xml version="1.0" standalone="yes"?>
<a xmlns="/dev/null">
 <b>
  <c></c>
 </b>
</a>

Can anyone verify this as a bug, or is the test malformed?

Nick Sieger
nsieger@netperceptions.com

--- begin XercesTest.java ---

import java.io.StringReader;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class XercesTest
{
    private static String testNS = "/dev/null";
    private static String testXML1 =
        "<?xml version=\"1.0\" standalone=\"yes\"?>"
        + " <a xmlns=\"" + testNS + "\">\n"
        + "  <b>\n"
        + "   <c></c>\n"
        + "  </b>\n"
        + " </a>\n";
    private static String testXML2 =
        "<?xml version=\"1.0\" standalone=\"yes\"?>"
        + " <test:a xmlns:test=\"" + testNS + "\">\n"
        + "  <test:b>\n"
        + "   <test:c></test:c>\n"
        + "  </test:b>\n"
        + " </test:a>\n";

    private int elementAStart;
    private int elementBStart;
    private int elementCStart;
    private int elementAEnd;
    private int elementBEnd;
    private int elementCEnd;

    public XercesTest () {
        elementAStart = 0;
        elementBStart = 0;
        elementCStart = 0;
        elementAEnd   = 0;
        elementBEnd   = 0;
        elementCEnd   = 0;
    }

    public class MyHandler extends DefaultHandler
    {
        public void startElement (String namespaceURI, String name,
                                  String qName, Attributes attrs)
            throws SAXException {

            if (namespaceURI.equals (testNS)) {

                if (name.equals ("a")) {
                    elementAStart++;
                }
                else if (name.equals ("b")) {
                    elementBStart++;
                }
                else if (name.equals ("c")) {
                    elementCStart++;
                }
            }
        }
        
        public void endElement (String namespaceURI, String name,
                                String qName)
            throws SAXException {

            if (namespaceURI.equals (testNS)) {

                if (name.equals ("a")) {
                    elementAEnd++;
                }
                else if (name.equals ("b")) {
                    elementBEnd++;
                }
                else if (name.equals ("c")) {
                    elementCEnd++;
                }
            }
        }
    }

    public static class Assert
    {
        public static void equals (int a, int b) throws Exception {
            equals (new Integer (a), new Integer (b));
        }
        public static void equals (Object a, Object b) throws Exception {
            if (a.equals (b))
                return;
            throw new Exception ("assertion failed: expected <" + a.toString
()
                                 + "> but was <" + b.toString () + ">");
        }
    }

    public static void runTest1 () throws Exception {

        XMLReader reader = XMLReaderFactory.createXMLReader
            ("org.apache.xerces.parsers.SAXParser");

        reader.setFeature ("http://xml.org/sax/features/namespaces", true);

        XercesTest test = new XercesTest ();
        reader.setContentHandler (test.new MyHandler ());
        reader.parse (new InputSource (new StringReader (testXML1)));
                    
        Assert.equals (1, test.elementAStart);
        Assert.equals (1, test.elementBStart);
        Assert.equals (1, test.elementCStart);
        Assert.equals (1, test.elementCEnd);
        Assert.equals (1, test.elementBEnd);
        Assert.equals (1, test.elementAEnd);
    }

    public static void runTest2 () throws Exception {

        XMLReader reader = XMLReaderFactory.createXMLReader
            ("org.apache.xerces.parsers.SAXParser");

        reader.setFeature ("http://xml.org/sax/features/namespaces", true);

        XercesTest test = new XercesTest ();
        reader.setContentHandler (test.new MyHandler ());
        reader.parse (new InputSource (new StringReader (testXML2)));
                    
        Assert.equals (1, test.elementAStart);
        Assert.equals (1, test.elementBStart);
        Assert.equals (1, test.elementCStart);
        Assert.equals (1, test.elementCEnd);
        Assert.equals (1, test.elementBEnd);
        Assert.equals (1, test.elementAEnd);
    }

    public static void main (String[] args) {

        int exitStatus = 0;
        try {
            runTest1 ();
            runTest2 ();
        }
        catch (Exception e) {
            e.printStackTrace ();
            exitStatus = 1;
        }
        System.exit (exitStatus);
    }
}

--- end XercesTest.java