KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > benchmarks > FastReproducer


1 /* Copyright 2002-2004 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.benchmarks;
23
24 import java.io.IOException JavaDoc;
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 /**
38  *
39  * <p>
40  * Benchmarks building a tree in memory by copying an existing document
41  * without using copy. Thus everything is reverified so that
42  * constructors and Verifier are hit heavily.
43  * Doesn't appear to help to add children after child is appended.
44  * </p>
45  *
46  * @author Elliotte Rusty Harold
47  * @version 1.0
48  *
49  */

50 class FastReproducer {
51
52     public static void main(String JavaDoc[] 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             // Separate out the basic I/O by parsing document,
65
// and then serializing into a byte array. This caches
66
// the document and removes any dependence on the DTD.
67
Document document = parser.build(args[0]);
68
69             long prewalk = System.currentTimeMillis();
70             // Process it starting at the root
71
iterator.copy(document);
72             long postwalk = System.currentTimeMillis();
73             System.out.println((postwalk - prewalk)
74               + "ms to walk tree");
75
76         }
77         catch (IOException JavaDoc 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 JavaDoc {
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 JavaDoc(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         // Weird; need to find just the additional namespaces????
113
/* for (int i = original.getNamespaceDeclarationCount()-1; i >= 0; i--) {
114              copy.addNamespaceDeclaration(original.);
115         } */

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 JavaDoc, ParsingException {
186         
187         for (int i = 0; i < numPasses; i++) {
188             copy(doc);
189         }
190         
191     }
192     
193     
194
195     // note use of recursion
196
private void followNode(Node node) throws IOException JavaDoc {
197     
198         for (int i = 0; i < node.getChildCount(); i++) {
199             followNode(node.getChild(i));
200         }
201     
202     }
203
204 }
Popular Tags