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 "Michael Glavassevich (JIRA)" <xe...@xml.apache.org> on 2005/10/24 06:00:03 UTC

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

     [ http://issues.apache.org/jira/browse/XERCESJ-941?page=all ]

Michael Glavassevich updated XERCESJ-941:
-----------------------------------------

    Bugzilla Id:   (was: 28225)
      Component: DOM (Level 2 Events)
                     (was: DOM (Level 3 Core))
    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>

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

    Environment: 
Operating System: Windows XP
Platform: PC

  was:
Operating System: Windows XP
Platform: PC

      Assign To:     (was: Xerces-J Developers Mailing List)
       Priority: Minor

> DOM Events Module: event notification of "indirect" insertions
> --------------------------------------------------------------
>
>          Key: XERCESJ-941
>          URL: http://issues.apache.org/jira/browse/XERCESJ-941
>      Project: Xerces2-J
>         Type: Bug
>   Components: DOM (Level 2 Events)
>     Versions: 2.6.2
>  Environment: Operating System: Windows XP
> Platform: PC
>     Reporter: Martin Bernauer
>     Priority: Minor
>  Attachments: DocumentImpl.java.patch, diff.txt
>
> 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>

-- 
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: j-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-dev-help@xerces.apache.org