1 21 22 package nu.xom.benchmarks; 23 24 import java.io.IOException ; 25 26 import nu.xom.Attribute; 27 import nu.xom.Builder; 28 import nu.xom.Comment; 29 import nu.xom.DocType; 30 import nu.xom.Document; 31 import nu.xom.Element; 32 import nu.xom.Node; 33 import nu.xom.ParsingException; 34 import nu.xom.ProcessingInstruction; 35 import nu.xom.Text; 36 37 50 class FastReproducer { 51 52 public static void main(String [] args) { 53 54 if (args.length <= 0) { 55 System.out.println( 56 "Usage: java nu.xom.benchmarks.Reproducer URL" 57 ); 58 return; 59 } 60 61 FastReproducer iterator = new FastReproducer(); 62 Builder parser = new Builder(); 63 try { 64 Document document = parser.build(args[0]); 68 69 long prewalk = System.currentTimeMillis(); 70 iterator.copy(document); 72 long postwalk = System.currentTimeMillis(); 73 System.out.println((postwalk - prewalk) 74 + "ms to walk tree"); 75 76 } 77 catch (IOException ex) { 78 System.out.println(ex); 79 } 80 catch (ParsingException ex) { 81 System.out.println(ex); 82 } 83 84 } 85 86 private Document copy(Document doc) 87 throws IOException { 88 89 Element originalRoot = doc.getRootElement(); 90 Element root = copy(originalRoot, null); 91 Document copy = new Document(root); 92 copy.setBaseURI(doc.getBaseURI()); 93 for (int i = 0; i < doc.getChildCount(); i++) { 94 Node child = doc.getChild(i); 95 if (child == originalRoot) continue; 96 Node node = copy(child); 97 copy.insertChild(node, i); 98 } 99 return copy; 100 101 } 102 103 104 private Element copy(Element original, Element parent) { 105 106 Element copy = new Element(original.getQualifiedName(), new String (original.getNamespaceURI())); 107 if (parent != null) parent.appendChild(copy); 108 for (int i = original.getAttributeCount()-1; i >= 0; i--) { 109 Attribute att = original.getAttribute(i); 110 copy.addAttribute(copy(att)); 111 } 112 116 for (int i = 0; i < original.getChildCount(); i++) { 117 Node child = original.getChild(i); 118 if (child instanceof Element) { 119 copy((Element) child, copy); 120 } 121 else { 122 Node node = copy(child); 123 copy.appendChild(node); 124 } 125 } 126 return copy; 127 128 } 129 130 131 private Node copy(Node node) { 132 133 if (node instanceof Text) { 134 return copy((Text) node); 135 } 136 else if (node instanceof Element) { 137 return copy((Element) node); 138 } 139 else if (node instanceof Comment) { 140 return copy((Comment) node); 141 } 142 else if (node instanceof ProcessingInstruction) { 143 return copy((ProcessingInstruction) node); 144 } 145 else if (node instanceof DocType) { 146 return copy((DocType) node); 147 } 148 return null; 149 150 } 151 152 153 private Node copy(Text text) { 154 return new Text(text.getValue()); 155 } 156 157 158 private Node copy(Comment comment) { 159 return new Comment(comment.getValue()); 160 } 161 162 163 private Node copy(ProcessingInstruction pi) { 164 return new ProcessingInstruction(pi.getTarget(), pi.getValue()); 165 } 166 167 168 private Node copy(DocType doctype) { 169 return new DocType(doctype.getRootElementName(), doctype.getPublicID(), doctype.getSystemID()); 170 } 171 172 173 private Attribute copy(Attribute original) { 174 175 Attribute copy = new Attribute(original.getQualifiedName(), 176 original.getNamespaceURI(), 177 original.getValue(), 178 original.getType()); 179 return copy; 180 181 } 182 183 184 private void warmup(Document doc, int numPasses) 185 throws IOException , ParsingException { 186 187 for (int i = 0; i < numPasses; i++) { 188 copy(doc); 189 } 190 191 } 192 193 194 195 private void followNode(Node node) throws IOException { 197 198 for (int i = 0; i < node.getChildCount(); i++) { 199 followNode(node.getChild(i)); 200 } 201 202 } 203 204 } | Popular Tags |