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 ji...@apache.org on 2004/04/14 23:22:43 UTC

[jira] Updated: (XERCESJ-941) DOM Events Module: event notification of "indirect" insertions

The following issue has been updated:

    Updater: nddelima (mailto:nddelima@ca.ibm.com)
       Date: Wed, 14 Apr 2004 2:20 PM
    Comment:
Seems like a DOMNodeInsertedIntoDocument event is not being fired for child node trees appended to nodes that do not have registered EventListeners but whose parent nodes have registered event listeners.

A solution may be to traverse this nodes parent node (and its parent) till a parent with a registered event listener is found.
    Changes:
             Attachment changed to DocumentImpl.java.patch
    ---------------------------------------------------------------------
For a full history of the issue, see:

  http://issues.apache.org/jira/browse/XERCESJ-941?page=history

---------------------------------------------------------------------
View the issue:
  http://issues.apache.org/jira/browse/XERCESJ-941

Here is an overview of the issue:
---------------------------------------------------------------------
        Key: XERCESJ-941
    Summary: DOM Events Module: event notification of "indirect" insertions
       Type: Bug

     Status: Open

    Project: Xerces2-J
 Components: 
             DOM
   Versions:
             2.6.2

   Assignee: Xerces-J Developers Mailing List
   Reporter: Martin Bernauer

    Created: Tue, 6 Apr 2004 7:59 AM
    Updated: Wed, 14 Apr 2004 2:20 PM
Environment: Operating System: Windows XP
Platform: PC

Description:
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>


---------------------------------------------------------------------
JIRA INFORMATION:
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

If you want more information on JIRA, or have a bug to report see:
   http://www.atlassian.com/software/jira


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org