~ubuntu-branches/ubuntu/precise/xom/precise

« back to all changes in this revision

Viewing changes to src/nu/xom/benchmarks/FastReproducer.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-11-25 15:50:40 UTC
  • Revision ID: james.westby@ubuntu.com-20071125155040-r75ikcqf1vu0cei7
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2002-2005 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;
 
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.1d5
 
48
 *
 
49
 */
 
50
class FastReproducer {
 
51
 
 
52
    public static void main(String[] 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 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 {
 
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(), 
 
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));
 
112
        }
 
113
        // Weird; need to find just the additional namespaces????
 
114
        /* for (int i = original.getNamespaceDeclarationCount()-1; i >= 0; i--) {
 
115
             copy.addNamespaceDeclaration(original.);
 
116
        } */
 
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);
 
121
            }
 
122
            else {
 
123
                Node node = copy(child);
 
124
                copy.appendChild(node);
 
125
            }
 
126
        }
 
127
        return copy;
 
128
        
 
129
    }
 
130
 
 
131
    
 
132
    private Node copy(Node node) {
 
133
 
 
134
        if (node instanceof Text) {
 
135
            return copy((Text) node);
 
136
        }
 
137
        else if (node instanceof Comment) {
 
138
            return copy((Comment) node);
 
139
        }
 
140
        else if (node instanceof ProcessingInstruction) {
 
141
            return copy((ProcessingInstruction) node);
 
142
        }
 
143
        else if (node instanceof DocType) {
 
144
            return copy((DocType) node);
 
145
        }
 
146
        return null;
 
147
        
 
148
    }
 
149
 
 
150
    
 
151
    private Node copy(Text text) {
 
152
        return new Text(text.getValue());
 
153
    }
 
154
 
 
155
    
 
156
    private Node copy(Comment comment) {
 
157
        return new Comment(comment.getValue());
 
158
    }
 
159
 
 
160
    
 
161
    private Node copy(ProcessingInstruction pi) {
 
162
        return new ProcessingInstruction(pi.getTarget(), pi.getValue());
 
163
    }
 
164
 
 
165
    
 
166
    private Node copy(DocType doctype) {
 
167
        return new DocType(
 
168
          doctype.getRootElementName(), 
 
169
          doctype.getPublicID(), 
 
170
          doctype.getSystemID());
 
171
    }
 
172
 
 
173
    
 
174
    private Attribute copy(Attribute original) {
 
175
        
 
176
        Attribute copy = new Attribute(original.getQualifiedName(), 
 
177
          original.getNamespaceURI(), 
 
178
          original.getValue(), 
 
179
          original.getType());
 
180
        return copy;
 
181
        
 
182
    }
 
183
 
 
184
 
 
185
}
 
 
b'\\ No newline at end of file'