KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > perf > TextNodeEditor


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: TextNodeEditor.java,v 1.2 2005/01/26 08:29:25 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.perf;
25
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29 import org.w3c.dom.NodeList JavaDoc;
30 ;
31
32 /**
33  * Editor that edits all text nodes. Performs a depth-frist traversl
34  * of the DOM tree.
35  */

36 public class TextNodeEditor implements DOMEditor {
37     /** Has this been run yet? */
38     private boolean fHasRun = false;
39
40     public int edit(Document JavaDoc doc) throws Exception JavaDoc {
41     Element JavaDoc e = doc.getDocumentElement();
42     return edit(e);
43     }
44
45     public int edit(Node JavaDoc node) throws Exception JavaDoc {
46         int cnt = 0;
47     if (node.getNodeType() == Node.TEXT_NODE) {
48         node.setNodeValue("hello");
49             cnt++;
50     } else {
51         NodeList JavaDoc children = node.getChildNodes();
52         for (int i=0; i<children.getLength(); i++) {
53         cnt += edit(children.item(i));
54         }
55     }
56         return cnt;
57     }
58
59     /** Next function, allows support only one edit configuration */
60     public boolean next() {
61         if (fHasRun) {
62             return false;
63         } else {
64             fHasRun = true;
65             return true;
66         }
67     }
68
69
70     /** Reset the editor queue. */
71     public void reset() {
72         fHasRun = false;
73     }
74
75     /** Get a description */
76     public String JavaDoc toString() {
77     return "TextNodeEditor: edits all text nodes in the DOM tree.";
78     }
79     
80 }
81
82
83
84
85
86
87
Popular Tags