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