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

« back to all changes in this revision

Viewing changes to src/nu/xom/benchmarks/Reproducer.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 <code>Verifier</code> are hit heavily. 
 
43
 * </p>
 
44
 * 
 
45
 * @author Elliotte Rusty Harold
 
46
 * @version 1.1d5
 
47
 *
 
48
 */
 
49
class Reproducer {
 
50
 
 
51
    public static void main(String[] args) {
 
52
     
 
53
        if (args.length <= 0) {
 
54
          System.out.println(
 
55
            "Usage: java nu.xom.benchmarks.Reproducer URL"
 
56
          );
 
57
          return; 
 
58
        }
 
59
         
 
60
        Reproducer iterator = new Reproducer();
 
61
        Builder parser = new Builder();
 
62
        try {
 
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]);
 
67
 
 
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");
 
74
 
 
75
        }
 
76
        catch (IOException ex) { 
 
77
          System.out.println(ex); 
 
78
        }
 
79
        catch (ParsingException ex) { 
 
80
          System.out.println(ex); 
 
81
        }
 
82
  
 
83
    }
 
84
 
 
85
    private Document copy(Document doc)
 
86
      throws IOException {
 
87
 
 
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);
 
97
        }
 
98
        return copy;
 
99
        
 
100
    }
 
101
 
 
102
    
 
103
    private Element copy(Element original) {
 
104
 
 
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));
 
110
        }
 
111
        // Weird; need to find just the additional namespaces????
 
112
        /* for (int i = original.getNamespaceDeclarationCount()-1; i >= 0; i--) {
 
113
             copy.addNamespaceDeclaration(original.);
 
114
        } */
 
115
        for (int i = 0; i < original.getChildCount(); i++) {
 
116
            Node node = copy(original.getChild(i));
 
117
            copy.appendChild(node);
 
118
        }
 
119
        return copy;
 
120
        
 
121
    }
 
122
 
 
123
    
 
124
    private Node copy(Node node) {
 
125
 
 
126
        if (node instanceof Text) {
 
127
            return copy((Text) node);
 
128
        }
 
129
        else if (node instanceof Element) {
 
130
            return copy((Element) node);
 
131
        }
 
132
        else if (node instanceof Comment) {
 
133
            return copy((Comment) node);
 
134
        }
 
135
        else if (node instanceof ProcessingInstruction) {
 
136
            return copy((ProcessingInstruction) node);
 
137
        }
 
138
        else if (node instanceof DocType) {
 
139
            return copy((DocType) node);
 
140
        }
 
141
        return null;
 
142
        
 
143
    }
 
144
 
 
145
    
 
146
    private Node copy(Text text) {
 
147
        return new Text(text.getValue());
 
148
    }
 
149
 
 
150
    
 
151
    private Node copy(Comment comment) {
 
152
        return new Comment(comment.getValue());
 
153
    }
 
154
 
 
155
    
 
156
    private Node copy(ProcessingInstruction pi) {
 
157
        return new ProcessingInstruction(pi.getTarget(), pi.getValue());
 
158
    }
 
159
 
 
160
    
 
161
    private Node copy(DocType doctype) {
 
162
        return new DocType(
 
163
          doctype.getRootElementName(), 
 
164
          doctype.getPublicID(), 
 
165
          doctype.getSystemID());
 
166
    }
 
167
 
 
168
    
 
169
    private Attribute copy(Attribute original) {
 
170
        
 
171
        Attribute copy = new Attribute(original.getQualifiedName(), 
 
172
          original.getNamespaceURI(), 
 
173
          original.getValue(), 
 
174
          original.getType());
 
175
        return copy;
 
176
        
 
177
    }
 
178
 
 
179
 
 
180
}
 
 
b'\\ No newline at end of file'