1 23 24 package org.enhydra.xml.xmlc.perf; 25 26 import java.lang.reflect.Method ; 27 import java.util.TreeMap ; 28 29 import org.w3c.dom.Document ; 30 import org.w3c.dom.Node ; 31 import org.w3c.dom.NodeList ; 32 33 39 public class SparseTextNodeEditor implements DOMEditor { 40 41 51 private final boolean SEARCH_EDIT = false; 52 53 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 67 private int getSparseOffset() { 68 return sparseOffsetList[sparseOffsetIndex]; 69 } 70 71 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 88 public int edit(Document doc) throws Exception { 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 103 private Object [] getTextEditMethods(Document doc) throws Exception { 104 Method [] allMethods = doc.getClass().getMethods(); 105 TreeMap map = new TreeMap (); 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 124 public void edit(Document doc, Object [] methods) throws Exception { 125 String [] args = new String [] {"hello"}; 126 for (count=0; count<methods.length; count++) { 127 if (!skip(count)) { 128 ((Method )methods[count]).invoke(doc, (Object [])args); 129 editCount++; 130 } 131 } 132 } 133 134 141 public void edit(Node node) throws Exception { 142 if (node.getNodeType() == Node.TEXT_NODE) { 143 if (!skip(count)) { 144 node.setNodeValue("hello"); 145 editCount++; 146 } 147 count++; 148 } else { 149 NodeList children = node.getChildNodes(); 150 for (int i=0; i<children.getLength(); i++) { 151 edit(children.item(i)); 152 } 153 } 154 } 155 156 159 public String toString() { 160 int offset = getSparseOffset(); 161 String 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 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 202 public void reset() { 203 sparseOffsetIndex = 0; 204 } 205 206 } 207 208 209 210 211 212 213 214 | Popular Tags |