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 Verifier are hit heavily.
43
* Doesn't appear to help to add children after child is appended.
46
* @author Elliotte Rusty Harold
50
class FastReproducer {
52
public static void main(String[] args) {
54
if (args.length <= 0) {
56
"Usage: java nu.xom.benchmarks.Reproducer URL"
61
FastReproducer iterator = new FastReproducer();
62
Builder parser = new Builder();
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]);
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)
77
catch (IOException ex) {
78
System.out.println(ex);
80
catch (ParsingException ex) {
81
System.out.println(ex);
86
private Document copy(Document doc)
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);
104
private Element copy(Element original, Element parent) {
106
Element copy = new Element(original.getQualifiedName(),
107
new String(original.getNamespaceURI()));
108
if (parent != null) parent.appendChild(copy);
109
for (int i = original.getAttributeCount()-1; i >= 0; i--) {
110
Attribute att = original.getAttribute(i);
111
copy.addAttribute(copy(att));
113
// Weird; need to find just the additional namespaces????
114
/* for (int i = original.getNamespaceDeclarationCount()-1; i >= 0; i--) {
115
copy.addNamespaceDeclaration(original.);
117
for (int i = 0; i < original.getChildCount(); i++) {
118
Node child = original.getChild(i);
119
if (child instanceof Element) {
120
copy((Element) child, copy);
123
Node node = copy(child);
124
copy.appendChild(node);
132
private Node copy(Node node) {
134
if (node instanceof Text) {
135
return copy((Text) node);
137
else if (node instanceof Comment) {
138
return copy((Comment) node);
140
else if (node instanceof ProcessingInstruction) {
141
return copy((ProcessingInstruction) node);
143
else if (node instanceof DocType) {
144
return copy((DocType) node);
151
private Node copy(Text text) {
152
return new Text(text.getValue());
156
private Node copy(Comment comment) {
157
return new Comment(comment.getValue());
161
private Node copy(ProcessingInstruction pi) {
162
return new ProcessingInstruction(pi.getTarget(), pi.getValue());
166
private Node copy(DocType doctype) {
168
doctype.getRootElementName(),
169
doctype.getPublicID(),
170
doctype.getSystemID());
174
private Attribute copy(Attribute original) {
176
Attribute copy = new Attribute(original.getQualifiedName(),
177
original.getNamespaceURI(),
b'\\ No newline at end of file'