KickJava   Java API By Example, From Geeks To Geeks.

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


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: SparseTextNodeEditor.java,v 1.2 2005/01/26 08:29:25 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.perf;
25
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.TreeMap JavaDoc;
28
29 import org.w3c.dom.Document JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.NodeList JavaDoc;
32
33 /**
34  * Edits text nodes. May be set up to edit every every single node,
35  * every other node, every fifth node, etc...
36  *
37  * @see sparseOffsetList
38  */

39 public class SparseTextNodeEditor implements DOMEditor {
40
41     /**
42      * If true, text nodes will be searched and then edited.
43      * This is know to be slow with the lazydom.
44      * <p>
45      * If false, accessor methods of type {setTextTest[0-9]+} will
46      * be called to directly edit text nodes. This requires
47      * that the DOM have been run through the DOM converter.
48      *
49      * @see DOMConverter
50      */

51     private final boolean SEARCH_EDIT = false;
52
53     /**
54      * Configure this to your test needs. A test is run for each
55      * entry in this list. The value of an entry indicates how 'sparse'
56      * the editing function is to be (i.e. 1 means edit every single
57      * node, 2 means every other, 10 means every tenth, etc...)
58      */

59     private final int [] sparseOffsetList = new int[] {1, 2, 3, 4, 5, 10};
60     private int sparseOffsetIndex = 0;
61     private int count;
62     private int editCount;
63
64     /**
65      * @return the current sparse offset value.
66      */

67     private int getSparseOffset() {
68     return sparseOffsetList[sparseOffsetIndex];
69     }
70
71     /**
72      * @return true if the current node should be skipped
73      * (i.e. not edited)
74      */

75     private boolean skip(int count) {
76     int offset = getSparseOffset();
77     if ((count % offset) == 0) {
78         return false;
79     }
80     return true;
81     }
82
83     /**
84      * Edits the document.
85      *
86      * @return the number of nodes edited.
87      */

88     public int edit(Document JavaDoc doc) throws Exception JavaDoc {
89     count = 1;
90     editCount = 0;
91     if (!SEARCH_EDIT) {
92         edit(doc, getTextEditMethods(doc));
93     } else {
94         edit(doc.getDocumentElement());
95     }
96     return editCount;
97     }
98
99     /**
100      * Returns all the methods for editing text nodes.
101      * The list is sorted.
102      */

103     private Object JavaDoc[] getTextEditMethods(Document JavaDoc doc) throws Exception JavaDoc {
104     Method JavaDoc [] allMethods = doc.getClass().getMethods();
105     TreeMap JavaDoc map = new TreeMap JavaDoc();
106     for (int i=0; i<allMethods.length; i++) {
107         if (allMethods[i].getName().indexOf("setTextTest") > -1) {
108         map.put(allMethods[i].getName(), allMethods[i]);
109         
110         }
111     }
112     return map.values().toArray();
113     }
114     
115     /**
116      * Edits text nodes by calling direct access methods to
117      * those nodes.
118      *
119      * Some TEXT nodes are skipped based on the current sparse edit
120      * setting.
121      *
122      * @see sparseOffsetList
123      */

124     public void edit(Document JavaDoc doc, Object JavaDoc [] methods) throws Exception JavaDoc {
125     String JavaDoc [] args = new String JavaDoc[] {"hello"};
126     for (count=0; count<methods.length; count++) {
127         if (!skip(count)) {
128         ((Method JavaDoc)methods[count]).invoke(doc, (Object JavaDoc[])args);
129         editCount++;
130         }
131     }
132     }
133
134     /**
135      * Recusively edits all decendant TEXT nodes of the current node.
136      * Some TEXT nodes are skipped based on the current sparse edit
137      * setting.
138      *
139      * @see sparseOffsetList
140      */

141     public void edit(Node JavaDoc node) throws Exception JavaDoc {
142     if (node.getNodeType() == Node.TEXT_NODE) {
143         if (!skip(count)) {
144         node.setNodeValue("hello");
145         editCount++;
146         }
147         count++;
148     } else {
149         NodeList JavaDoc children = node.getChildNodes();
150         for (int i=0; i<children.getLength(); i++) {
151         edit(children.item(i));
152         }
153     }
154     }
155
156     /**
157      * String rep of editor.
158      */

159     public String JavaDoc toString() {
160     int offset = getSparseOffset();
161     String JavaDoc offsetDesc = null;
162     switch (offset) {
163     case 0:
164         offsetDesc = "0th";
165         break;
166     case 1:
167         offsetDesc = "single";
168         break;
169     case 2:
170         offsetDesc = "2nd";
171         break;
172     case 3:
173         offsetDesc = "3rd";
174         break;
175     default:
176         offsetDesc = offset + "th";
177         break;
178     }
179     return "SparseTextNodeEditor: edits every "
180         + offsetDesc + " text node in the DOM tree.";
181     }
182
183     /**
184      * The edit function can be called as long as this method returns true.
185      * The editor may contain multiple configurations, this method cycles
186      * through each configuration.
187      *
188      * @return true if there is another editor configuration that should be run.
189      */

190     public boolean next() {
191     sparseOffsetIndex++;
192     if (sparseOffsetIndex >= sparseOffsetList.length) {
193         sparseOffsetIndex = sparseOffsetList.length;
194         return false;
195     }
196     return true;
197     }
198
199     /**
200      * Reset the editor configuration queue.
201      */

202     public void reset() {
203     sparseOffsetIndex = 0;
204     }
205
206 }
207
208
209
210
211
212
213
214
Popular Tags