DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=28225>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=28225

DOM Events Module: event notification of "indirect" insertions

           Summary: DOM Events Module: event notification of "indirect"
                    insertions
           Product: Xerces2-J
           Version: 2.6.2
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: DOM
        AssignedTo: [EMAIL PROTECTED]
        ReportedBy: [EMAIL PROTECTED]


When inserting a subtree, e.g., <b><c>444</c></b> as child of document element
<a>, and registering an event listener for event type
DOMNodeInsertedIntoDocument, one would expect the DOM implementation to report
the insertions of element b, element c, and the text node containing 444.

In my above defined environment, this only happens if the event listener is
registered on the document element. When registered on the document itself, the
indirect insertions of element c and text node containing 444 are not reported.

I have enclosed a sample application and its ouput below.

Regards,
Martin

---------- sample application ----------


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.events.MutationEvent;

public class DOMEventTest 
        implements EventListener
{
        /**
         * 
         * @param args
         * @throws Exception
         */
        public static void main(String[] args)
                throws Exception
        {
                DOMEventTest test = new DOMEventTest();
        }
        
        /**
         * 
         * @throws Exception
         */
        public DOMEventTest()
                throws Exception
        {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                
                // Initialize document
                Document doc = builder.newDocument();
                doc.appendChild(doc.createElement("a"));
                
                // Alternative 1: register event listener on the document itself
                ((EventTarget) doc).addEventListener("DOMCharacterDataModified", this, 
true);
                ((EventTarget) doc).addEventListener("DOMNodeInserted", this, true);
                ((EventTarget) doc).addEventListener("DOMNodeInsertedIntoDocument", 
this, true);
                
                // Manipulate document
                manipulateDocument(doc);
                
                System.out.println("\n----------\n");
                
                // Initialize document
                doc = builder.newDocument();
                doc.appendChild(doc.createElement("a"));
                
                // Alternative 2: register event listener on the document element
                Element elt = doc.getDocumentElement(); 
                ((EventTarget) elt).addEventListener("DOMCharacterDataModified", this, 
true);
                ((EventTarget) elt).addEventListener("DOMNodeInserted", this, true);
                ((EventTarget) elt).addEventListener("DOMNodeInsertedIntoDocument", 
this, true);
                
                // Manipulate document
                manipulateDocument(doc);
                
                System.out.println("\n----------\n");
                
                OutputFormat format = new OutputFormat(doc);
                XMLSerializer output = new XMLSerializer(System.out, format);
                output.serialize(doc);
        }

        void manipulateDocument(Document doc)
        {
                Element elt = doc.getDocumentElement();
                /*
                elt.appendChild(doc.createTextNode("111"));
                Text txt = (Text) elt.appendChild(doc.createTextNode("222"));
                txt.setData("333");
                */
                
                elt = doc.createElement("b");
                
elt.appendChild(doc.createElement("c")).appendChild(doc.createTextNode("444"));
                doc.getDocumentElement().appendChild(elt);
        }

        /**
         * 
         */
        public void handleEvent(org.w3c.dom.events.Event inEvt)
        {
                if (!(inEvt instanceof MutationEvent))
                        return;
                MutationEvent evt = (MutationEvent) inEvt;
                
                StringBuffer res = new StringBuffer();
                res.append("[" + evt.getType() + "]\n");

                res.append("  prevValue:" + evt.getPrevValue() + "\n");
                res.append("  newValue:" + evt.getNewValue() + "\n");
                Node targetNode = (Node) evt.getTarget();
                if (targetNode != null)
                {
                        res.append("  targetNodeType:");
                        switch (targetNode.getNodeType())
                        {
                                case Node.ELEMENT_NODE: res.append("ELEMENT_NODE"); 
break;
                                case Node.ATTRIBUTE_NODE: 
res.append("ATTRIBUTE_NODE"); break;
                                case Node.TEXT_NODE: res.append("TEXT_NODE"); break;
                                default: res.append("##OTHER");
                        }
                        res.append("\n");
                        res.append("  targetNodeName:" + targetNode.getNodeName() + 
"\n");
                }
                Node relatedNode = evt.getRelatedNode();
                if (relatedNode != null)
                        res.append("  relatedNodeName:" + relatedNode.getNodeName() + 
"\n");

                System.out.print(res.toString());       
        }
}

---------- Output ----------

[DOMNodeInserted]
  prevValue:null
  newValue:null
  targetNodeType:ELEMENT_NODE
  targetNodeName:b
  relatedNodeName:a

----------

[DOMNodeInserted]
  prevValue:null
  newValue:null
  targetNodeType:ELEMENT_NODE
  targetNodeName:b
  relatedNodeName:a
[DOMNodeInsertedIntoDocument]
  prevValue:null
  newValue:null
  targetNodeType:ELEMENT_NODE
  targetNodeName:b
[DOMNodeInsertedIntoDocument]
  prevValue:null
  newValue:null
  targetNodeType:ELEMENT_NODE
  targetNodeName:c
[DOMNodeInsertedIntoDocument]
  prevValue:null
  newValue:null
  targetNodeType:TEXT_NODE
  targetNodeName:#text

----------

<?xml version="1.0" encoding="UTF-8"?>
<a><b><c>444</c></b></a>

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

Reply via email to