Adding listeners affects the number of events propagated
--------------------------------------------------------

         Key: XERCESJ-1162
         URL: http://issues.apache.org/jira/browse/XERCESJ-1162
     Project: Xerces2-J
        Type: Bug

  Components: DOM (Level 2 Events)  
    Versions: 2.6.2    
 Environment: Windows XP, JDK 1.5.0_6 (using the in-built Xerces implementation)
    Reporter: Doug Satchwell


At the bottom of this report is a document and some code that adds listeners to 
it. The code simply removes the child node 'Row' from its parent 'Rows'. 
When the code is executed, the output is the following 5 lines:

1147448668625:/Foo/Rows/Row:AT_TARGET:DOMNodeRemoved:[Row: null]:[Row: null]:
1147448668625:/Foo/Rows:BUBBLING_PHASE:DOMNodeRemoved:[Rows: null]:[Row: null]:
1147448668625:/Foo/Rows/Row:AT_TARGET:DOMNodeRemovedFromDocument:[Row: 
null]:[Row: null]:
1147448668625:/Foo/Rows/Row/Display:AT_TARGET:DOMNodeRemovedFromDocument:[Display:
 null]:[Display: null]:
1147448668625:/Foo/Rows:AT_TARGET:DOMSubtreeModified:[Rows: null]:[Rows: null]:

As you can see, the listener at /Foo/Rows is fired twice. However, if this 
listener is removed from the code (by commenting out the first call to 
addListeners), instead of 3 lines of output we just get the one: 

1147448846390:/Foo/Rows/Row:AT_TARGET:DOMNodeRemoved:[Row: null]:[Row: null]:

In other words, adding the listener at /Foo/Rows affects the number of events 
that are being to the other listeners! 


-----------------------------



<?xml version="1.0" encoding="UTF-8"?>
<Foo>
        <Rows>
                <Row>
                        <Id>1</Id>
                        <Display>A</Display>
                </Row>
                <Row>
                        <Id>2</Id>
                        <Display>B</Display>
                </Row>
        </Rows>
</Foo>


public class DOMListenerTest2 {
        
        private static final String[] EVENT_TYPES = new 
String[]{"DOMSubtreeModified","DOMNodeInserted","DOMNodeRemoved","DOMAttrModified","DOMNodeRemovedFromDocument","DOMNodeInsertedIntoDocument","DOMCharacterDataModified"};

        public DOMListenerTest2() 
        {}
        
        public void run() throws JaxenException, ParserConfigurationException, 
SAXException, IOException
        {
        Document doc = 
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(DOMListenerTest2.class.getResourceAsStream("/test.xml"));
        
        
        Node rowsNode = (Node)new DOMXPath("/Foo/Rows").selectSingleNode(doc);
        Node rowNode = (Node)new 
DOMXPath("/Foo/Rows/Row").selectSingleNode(doc);
        Node rowDisplayNode = (Node)new 
DOMXPath("/Foo/Rows/Row/Display").selectSingleNode(doc);

        addListeners((EventTarget)rowsNode, new 
AllListener("/Foo/Rows"),EVENT_TYPES,false);
        addListeners((EventTarget)rowNode, new 
AllListener("/Foo/Rows/Row"),EVENT_TYPES,false);
        addListeners((EventTarget)rowDisplayNode, new 
AllListener("/Foo/Rows/Row/Display"),EVENT_TYPES,false);
        
        rowsNode.removeChild(rowNode);
        }

        private void addListeners(EventTarget etarget, EventListener listener, 
String[] types, boolean capture)
        {
                for (int i = 0; i < types.length; i++) {
                        etarget.addEventListener(types[i],listener,capture);
                }
        }
        
        public static void main(String[] args) 
        {
                try 
                {
                        DOMListenerTest2 test = new DOMListenerTest2();
                        test.run();
                } 
                catch (Throwable e) 
                {
                        e.printStackTrace();
                }
        }
    
        private static class AllListener implements EventListener
        {
                private String xpath;

                public AllListener(String xpath)
                {
                        this.xpath = xpath;
                }
                
                public void handleEvent(Event evt) 
                {
                        StringBuffer sb = new StringBuffer();
                        sb.append(evt.getTimeStamp()).append(":");
                        sb.append(xpath).append(":");
                        sb.append(phaseText(evt.getEventPhase())).append(":");
                        sb.append(evt.getType()).append(":");
                        sb.append(evt.getCurrentTarget()).append(":");
                        sb.append(evt.getTarget()).append(":");
                        System.out.println(sb);
                }
                
                private String phaseText(short phase)
                {
                        switch(phase)
                        {
                                case Event.AT_TARGET:
                                        return "AT_TARGET";
                                case Event.BUBBLING_PHASE:
                                        return "BUBBLING_PHASE";
                                case Event.CAPTURING_PHASE:
                                        return "CAPTURING_PHASE";
                        }
                        return null;
                }
        }
}

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to