1 21 22 package nu.xom.benchmarks; 23 24 import java.io.BufferedInputStream ; 25 import java.io.ByteArrayInputStream ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 30 import nu.xom.Attribute; 31 import nu.xom.Builder; 32 import nu.xom.DocType; 33 import nu.xom.Document; 34 import nu.xom.Element; 35 import nu.xom.Node; 36 import nu.xom.ParentNode; 37 import nu.xom.Serializer; 38 import nu.xom.Text; 39 import nu.xom.ParsingException; 40 41 42 67 class DocumentModifier { 68 69 public static void main(String [] args) { 70 71 if (args.length <= 0) { 72 System.out.println( 73 "Usage: java nu.xom.benchmarks.DocumentModifier URL" 74 ); 75 return; 76 } 77 78 DocumentModifier iterator = new DocumentModifier(); 79 Builder parser = new Builder(); 80 try { 81 Document doc = parser.build(args[0]); 85 DocType type = doc.getDocType(); 86 if (type != null) { 87 doc.removeChild(type); 88 } 89 ByteArrayOutputStream out = new ByteArrayOutputStream (); 90 Serializer serializer = new Serializer(out); 91 serializer.write(doc); 92 serializer.flush(); 93 out.close(); 94 byte[] data = out.toByteArray(); 95 96 warmup(parser, iterator, data, 5); 97 InputStream raw = new BufferedInputStream ( 98 new ByteArrayInputStream (data) 99 ); 100 101 System.gc(); System.gc(); System.gc(); 103 104 long prebuild = System.currentTimeMillis(); 105 106 Document document = parser.build(raw); 108 long postbuild = System.currentTimeMillis(); 109 110 System.out.println((postbuild - prebuild) 111 + "ms to build the document"); 112 113 long prewalk = System.currentTimeMillis(); 114 performTask(iterator, document); 115 long postwalk = System.currentTimeMillis(); 116 117 System.out.println((postwalk - prewalk) 118 + "ms to modify the document"); 119 120 } 121 catch (IOException ex) { 122 System.out.println(ex); 123 } 124 catch (ParsingException ex) { 125 System.out.println(ex); 126 } 127 128 } 130 private static void warmup(Builder builder, 131 DocumentModifier iterator, byte[] data, int numPasses) 132 throws IOException , ParsingException { 133 134 InputStream in = new BufferedInputStream ( 135 new ByteArrayInputStream (data)); 136 Document doc = builder.build(in); 137 for (int i = 0; i < numPasses; i++) { 138 performTask(iterator, doc); 139 } 140 } 141 142 private static void performTask(DocumentModifier iterator, Document document) 143 throws IOException { 144 iterator.followNode(document); 145 } 146 147 public void followNode(Node node) throws IOException { 149 150 if (node instanceof Text) { 152 if (node.getValue().trim().length() == 0) { 153 node.detach(); 154 } 155 else { 156 Element dummy = new Element("dummy"); 157 ParentNode parent = node.getParent(); 158 parent.insertChild(dummy, parent.indexOf(node)); 159 node.detach(); 160 dummy.appendChild(node); 161 } 162 return; 163 } 164 else if (node instanceof Element){ 165 Element element = (Element) node; 166 element.addAttribute(new Attribute("class", "original")); 167 for (int i = 0; i < node.getChildCount(); i++) { 168 followNode(node.getChild(i)); 169 } 170 } 171 else { 172 for (int i = 0; i < node.getChildCount(); i++) { 173 followNode(node.getChild(i)); 174 } 175 } 176 177 } 178 179 } | Popular Tags |