KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dom > events > Test


1 /* $Id: Test.java,v 1.2 2005/01/26 08:28:45 jkjome Exp $ */
2 /*
3  * The Apache Software License, Version 1.1
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache\@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation, and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.ibm.com . For more information
54  * on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package dom.events;
59
60 import org.w3c.dom.Attr JavaDoc;
61 import org.w3c.dom.Document JavaDoc;
62 import org.w3c.dom.Element JavaDoc;
63 import org.w3c.dom.NamedNodeMap JavaDoc;
64 import org.w3c.dom.Node JavaDoc;
65 import org.w3c.dom.events.EventTarget JavaDoc;
66
67 public class Test
68 {
69     EventReporter sharedReporter=new EventReporter();
70     
71     public static void main(String JavaDoc[] args)
72     {
73         Test met=new Test();
74         met.runTest();
75     }
76
77     void runTest()
78     {
79         Document JavaDoc doc=new org.enhydra.apache.xerces.dom.DocumentImpl();
80         reportAllMutations(doc);
81         
82         Element JavaDoc root=addNoisyElement(doc,doc,0);
83         Element JavaDoc e=null;
84         int i;
85         
86         // Individual nodes
87
e=addNoisyElement(doc,root,0);
88         Attr JavaDoc a=addNoisyAttr(doc,e,0);
89         a.setNodeValue("Updated A0 of E0, prepare to be acidulated.");
90         NamedNodeMap JavaDoc nnm=e.getAttributes();
91         nnm.removeNamedItem(a.getName());
92         nnm.setNamedItem(a);
93         
94         // InsertedInto/RemovedFrom tests.
95
// ***** These do not currently cross the Attr/Element barrier.
96
// DOM spec is pretty clear on that, but this may not be the intent.
97
System.out.println("\nAdd/remove a preconstructed tree; tests AddedToDocument\n");
98         sharedReporter.off();
99         Element JavaDoc lateAdd=doc.createElement("lateAdd");
100         reportAllMutations(lateAdd);
101         e=lateAdd;
102         for(i=0;i<2;++i)
103         {
104             e=addNoisyElement(doc,e,i);
105             addNoisyAttr(doc,e,i);
106         }
107         sharedReporter.on();
108         root.appendChild(lateAdd);
109         root.removeChild(lateAdd);
110
111         System.out.println("\nReplace a preconstructed tree; tests AddedToDocument\n");
112
113         sharedReporter.off();
114         Node JavaDoc e0=root.replaceChild(lateAdd,root.getFirstChild());
115         sharedReporter.on();
116         root.replaceChild(e0,lateAdd);
117         
118
119         System.out.println("Done");
120     }
121     
122     Element JavaDoc addNoisyElement(Document JavaDoc doc,Node JavaDoc parent,int index)
123     {
124         String JavaDoc nodeName="Root";
125         if(parent!=doc)
126             nodeName=parent.getNodeName()+"_E"+index;
127         Element JavaDoc e=doc.createElement(nodeName);
128         reportAllMutations(e);
129         parent.appendChild(e);
130         return e;
131     }
132
133     Attr JavaDoc addNoisyAttr(Document JavaDoc doc,Element JavaDoc parent,int index)
134     {
135         String JavaDoc attrName=parent.getNodeName()+"_A"+index;
136         Attr JavaDoc a=doc.createAttribute(attrName);
137         reportAllMutations(a);
138         a.setNodeValue("Initialized A"+index+" of "+parent.getNodeName());
139         parent.setAttributeNode(a);
140         return a;
141     }
142     
143     void reportAllMutations(Node JavaDoc n)
144     {
145         String JavaDoc[] evtNames={
146             "DOMSubtreeModified","DOMAttrModified","DOMCharacterDataModified",
147             "DOMNodeInserted","DOMNodeRemoved",
148             "DOMNodeInsertedIntoDocument","DOMNodeRemovedFromDocument",
149             };
150             
151         EventTarget JavaDoc t=(EventTarget JavaDoc)n;
152         
153         for(int i=evtNames.length-1;
154             i>=0;
155             --i)
156         {
157             t.addEventListener(evtNames[i], sharedReporter, true);
158             t.addEventListener(evtNames[i], sharedReporter, false);
159         }
160
161     }
162 }
163
Popular Tags