1
/* Copyright 2002-2005 Elliotte Rusty Harold
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.
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.
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
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/
22
package nu.xom.benchmarks;
24
import java.io.IOException;
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;
33
import nu.xom.ParsingException;
34
import nu.xom.ProcessingInstruction;
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 <code>Verifier</code> are hit heavily.
45
* @author Elliotte Rusty Harold
51
public static void main(String[] args) {
53
if (args.length <= 0) {
55
"Usage: java nu.xom.benchmarks.Reproducer URL"
60
Reproducer iterator = new Reproducer();
61
Builder parser = new Builder();
63
// Separate out the basic I/O by parsing document,
64
// and then serializing into a byte array. This caches
65
// the document and removes any dependence on the DTD.
66
Document document = parser.build(args[0]);
68
long prewalk = System.currentTimeMillis();
69
// Process it starting at the root
70
iterator.copy(document);
71
long postwalk = System.currentTimeMillis();
72
System.out.println((postwalk - prewalk)
73
+ "ms to build tree");
76
catch (IOException ex) {
77
System.out.println(ex);
79
catch (ParsingException ex) {
80
System.out.println(ex);
85
private Document copy(Document doc)
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);
103
private Element copy(Element original) {
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));
111
// Weird; need to find just the additional namespaces????
112
/* for (int i = original.getNamespaceDeclarationCount()-1; i >= 0; i--) {
113
copy.addNamespaceDeclaration(original.);
115
for (int i = 0; i < original.getChildCount(); i++) {
116
Node node = copy(original.getChild(i));
117
copy.appendChild(node);
124
private Node copy(Node node) {
126
if (node instanceof Text) {
127
return copy((Text) node);
129
else if (node instanceof Element) {
130
return copy((Element) node);
132
else if (node instanceof Comment) {
133
return copy((Comment) node);
135
else if (node instanceof ProcessingInstruction) {
136
return copy((ProcessingInstruction) node);
138
else if (node instanceof DocType) {
139
return copy((DocType) node);
146
private Node copy(Text text) {
147
return new Text(text.getValue());
151
private Node copy(Comment comment) {
152
return new Comment(comment.getValue());
156
private Node copy(ProcessingInstruction pi) {
157
return new ProcessingInstruction(pi.getTarget(), pi.getValue());
161
private Node copy(DocType doctype) {
163
doctype.getRootElementName(),
164
doctype.getPublicID(),
165
doctype.getSystemID());
169
private Attribute copy(Attribute original) {
171
Attribute copy = new Attribute(original.getQualifiedName(),
172
original.getNamespaceURI(),
b'\\ No newline at end of file'