KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dom > events > DeferredDOM


1 /* $Id: DeferredDOM.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.enhydra.apache.xerces.dom.TextImpl;
61 import org.w3c.dom.Document JavaDoc;
62 import org.w3c.dom.NamedNodeMap JavaDoc;
63 import org.w3c.dom.Node JavaDoc;
64 import org.w3c.dom.NodeList JavaDoc;
65 import org.w3c.dom.events.EventTarget JavaDoc;
66
67 import util.Arguments;
68 import dom.DOMParserWrapper;
69
70 /*
71  * This program is actually meant to test that we DO NOT fire any mutation
72  * events while the Deferred DOM is being synchronized.
73  */

74 public class DeferredDOM
75 {
76     //
77
// Constants
78
//
79

80     /** Default parser name. */
81     private static final String JavaDoc
82     DEFAULT_PARSER_NAME = "dom.wrappers.DOMParser";
83
84     private static boolean setValidation = false; //defaults
85
private static boolean setNameSpaces = true;
86     private static boolean setSchemaSupport = true;
87     private static boolean setDeferredDOM = true;
88
89
90
91     //
92
// Data
93
//
94

95     /** Elements. */
96     private long elements;
97
98     /** Attributes. */
99     private long attributes;
100
101     /** Characters. */
102     private long characters;
103
104     /** Ignorable whitespace. */
105     private long ignorableWhitespace;
106
107
108     //
109
// Public static methods
110
//
111

112     /** Traverse the resulting document tree to trigger the synchronization. */
113     public static void traverse(String JavaDoc parserWrapperName, String JavaDoc uri) {
114
115         try {
116             DOMParserWrapper parser =
117             (DOMParserWrapper)Class.forName(parserWrapperName).newInstance();
118             DeferredDOM test = new DeferredDOM();
119
120             parser.setFeature( "http://apache.org/xml/features/dom/defer-node-expansion",
121
122                                setDeferredDOM );
123             parser.setFeature( "http://xml.org/sax/features/validation",
124                                setValidation );
125             parser.setFeature( "http://xml.org/sax/features/namespaces",
126                                setNameSpaces );
127             parser.setFeature( "http://apache.org/xml/features/validation/schema",
128                                setSchemaSupport );
129
130             Document JavaDoc document = parser.parse(uri);
131             test.reportAllMutations(document);
132             test.traverse(document);
133         } catch (org.xml.sax.SAXParseException JavaDoc spe) {
134         } catch (org.xml.sax.SAXNotRecognizedException JavaDoc ex ){
135         } catch (org.xml.sax.SAXNotSupportedException JavaDoc ex ){
136         } catch (org.xml.sax.SAXException JavaDoc se) {
137             if (se.getException() != null)
138                 se.getException().printStackTrace(System.err);
139             else
140                 se.printStackTrace(System.err);
141         } catch (Exception JavaDoc e) {
142             e.printStackTrace(System.err);
143         }
144
145     } // print(String,String,boolean)
146

147     //
148
// Public methods
149
//
150

151     /** Traverses the specified node, recursively. */
152     public void traverse(Node JavaDoc node) {
153
154         // is there anything to do?
155
if (node == null) {
156             return;
157         }
158
159         int type = node.getNodeType();
160         switch (type) {
161         // print document
162
case Node.DOCUMENT_NODE: {
163                 elements = 0;
164                 attributes = 0;
165                 characters = 0;
166                 ignorableWhitespace = 0;
167                 traverse(((Document JavaDoc)node).getDocumentElement());
168                 break;
169             }
170
171             // print element with attributes
172
case Node.ELEMENT_NODE: {
173                 elements++;
174                 NamedNodeMap JavaDoc attrs = node.getAttributes();
175                 if (attrs != null) {
176                     attributes += attrs.getLength();
177                 }
178                 NodeList JavaDoc children = node.getChildNodes();
179                 if (children != null) {
180                     int len = children.getLength();
181                     for (int i = 0; i < len; i++) {
182                         traverse(children.item(i));
183                     }
184                 }
185                 break;
186             }
187
188             // handle entity reference nodes
189
case Node.ENTITY_REFERENCE_NODE: {
190                 NodeList JavaDoc children = node.getChildNodes();
191                 if (children != null) {
192                     int len = children.getLength();
193                     for (int i = 0; i < len; i++) {
194                         traverse(children.item(i));
195                     }
196                 }
197                 break;
198             }
199
200             // print text
201
case Node.CDATA_SECTION_NODE: {
202                 characters += node.getNodeValue().length();
203                 break;
204             }
205         case Node.TEXT_NODE: {
206                 if (node instanceof TextImpl) {
207                     if (((TextImpl)node).isIgnorableWhitespace())
208                         ignorableWhitespace += node.getNodeValue().length();
209                     else
210                         characters += node.getNodeValue().length();
211                 } else
212                     characters += node.getNodeValue().length();
213                 break;
214             }
215         }
216
217     } // traverse(Node)
218

219     //
220
// Main
221
//
222

223     /** Main program entry point. */
224     public static void main(String JavaDoc argv[]) {
225
226         Arguments argopt = new Arguments();
227         argopt.setUsage( new String JavaDoc[] {
228                              "usage: java dom.events.DeferredDOM (options) uri ...",
229                              "",
230                              "options:",
231                              " -p name Specify DOM parser wrapper by name.",
232                              " -n | -N Turn on/off namespace [default=on]",
233                              " -v | -V Turn on/off validation [default=off]",
234                              " -s | -S Turn on/off Schema support [default=on]",
235                              " -d | -D Turn on/off deferred DOM [default=on]",
236                              " -h This help screen."} );
237
238
239         // is there anything to do?
240
if (argv.length == 0) {
241             argopt.printUsage();
242             System.exit(1);
243         }
244
245         // vars
246
String JavaDoc parserName = DEFAULT_PARSER_NAME;
247
248         argopt.parseArgumentTokens(argv , new char[] { 'p'} );
249
250         int c;
251         String JavaDoc arg = null;
252         while ( ( arg = argopt.getlistFiles() ) != null ) {
253 outer:
254             while ( (c = argopt.getArguments()) != -1 ){
255                 switch (c) {
256                 case 'v':
257                     setValidation = true;
258                     //System.out.println( "v" );
259
break;
260                 case 'V':
261                     setValidation = false;
262                     //System.out.println( "V" );
263
break;
264                 case 'N':
265                     setNameSpaces = false;
266                     break;
267                 case 'n':
268                     setNameSpaces = true;
269                     break;
270                 case 'p':
271                     //System.out.println('p');
272
parserName = argopt.getStringParameter();
273                     //System.out.println( "parserName = " + parserName );
274
break;
275                 case 'd':
276                     setDeferredDOM = true;
277                     break;
278                 case 'D':
279                     setDeferredDOM = false;
280                     break;
281                 case 's':
282                     //System.out.println("s" );
283
setSchemaSupport = true;
284                     break;
285                 case 'S':
286                     //System.out.println("S" );
287
setSchemaSupport = false;
288                     break;
289                 case '?':
290                 case 'h':
291                 case '-':
292                     argopt.printUsage();
293                     System.exit(1);
294                     break;
295                 case -1:
296                     //System.out.println( "-1" );
297
break outer;
298                 default:
299                     
300                     break;
301                 }
302             }
303
304             traverse(parserName, arg ); // parse and traverse uri
305
}
306
307     } // main(String[])
308

309
310     EventReporter sharedReporter=new EventReporter();
311     
312     void reportAllMutations(Node JavaDoc n)
313     {
314         String JavaDoc[] evtNames={
315             "DOMSubtreeModified","DOMAttrModified","DOMCharacterDataModified",
316             "DOMNodeInserted","DOMNodeRemoved",
317             "DOMNodeInsertedIntoDocument","DOMNodeRemovedFromDocument",
318             };
319             
320         EventTarget JavaDoc t=(EventTarget JavaDoc)n;
321         
322         for(int i=evtNames.length-1;
323             i>=0;
324             --i)
325         {
326             t.addEventListener(evtNames[i], sharedReporter, true);
327             t.addEventListener(evtNames[i], sharedReporter, false);
328         }
329
330     }
331 }
332
Popular Tags