[Biojava-l] unexpected behavior in StAX, StringElementHandlerBase

Michael L. Heuer heuermh@acm.org
Tue, 18 Sep 2001 10:50:13 -0400 (EDT)


All,

Perhaps I'm making an incorrect assumption, but shouldn't a String
element handler return the string value of just the current element?  I'm
seeing a concatenation of all the values:

 [java] There was 1 failure:
 [java] 1) testStringElementHandlerBase(ConfusedStAXHandlerTest) "expected:<d> but was:<abcd>"


   michael


---

import java.io.*;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import org.apache.xerces.parsers.*;

import org.biojava.utils.stax.*;

import junit.framework.*;

public class ConfusedStAXHandlerTest
    extends TestCase {

    public ConfusedStAXHandlerTest(String name) {
        super(name);
    }
    public void testStringElementHandlerBase() {
        try {
            String xml = "<?xml version=\"1.0\"?>" +

"<root><stringElement>a</stringElement><stringElement>b</stringElement>" +

"<stringElement>c</stringElement><stringElement>d</stringElement></root>";

            SAXParser parser = new SAXParser();
            RootStAXContentHandler rootHandler = new
RootStAXContentHandler();
            parser.setContentHandler(new SAX2StAXAdaptor(rootHandler));
            InputSource is = new InputSource(new StringReader(xml));
            parser.parse(is);

            assertEquals("d", rootHandler.getStringValue());
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }

    class RootStAXContentHandler extends StAXContentHandlerBase {
        private String stringValue = null;

        private StAXContentHandler stringHandler = new
StringElementHandlerBase() {
            public void setStringValue(String s) {
                stringValue = s;
            }
        };

        public void startElement(String nsURI,
                                 String localName,
                                 String qName,
                                 Attributes attrs,
                                 DelegationManager dm) throws SAXException
{

            if (localName.equals("stringElement"))
                dm.delegate(stringHandler);
        }
        public String getStringValue() {
            return stringValue;
        }
    }
}