~ubuntu-branches/ubuntu/karmic/libxerces2-java/karmic

« back to all changes in this revision

Viewing changes to samples/dom/Counter.java

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2004-06-06 18:00:26 UTC
  • Revision ID: james.westby@ubuntu.com-20040606180026-a3vh56uc95hjbyfh
Tags: upstream-2.6.2
ImportĀ upstreamĀ versionĀ 2.6.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The Apache Software License, Version 1.1
 
3
 *
 
4
 *
 
5
 * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
 
6
 * reserved.
 
7
 *
 
8
 * Redistribution and use in source and binary forms, with or without
 
9
 * modification, are permitted provided that the following conditions
 
10
 * are met:
 
11
 *
 
12
 * 1. Redistributions of source code must retain the above copyright
 
13
 *    notice, this list of conditions and the following disclaimer.
 
14
 *
 
15
 * 2. Redistributions in binary form must reproduce the above copyright
 
16
 *    notice, this list of conditions and the following disclaimer in
 
17
 *    the documentation and/or other materials provided with the
 
18
 *    distribution.
 
19
 *
 
20
 * 3. The end-user documentation included with the redistribution,
 
21
 *    if any, must include the following acknowledgment:
 
22
 *       "This product includes software developed by the
 
23
 *        Apache Software Foundation (http://www.apache.org/)."
 
24
 *    Alternately, this acknowledgment may appear in the software itself,
 
25
 *    if and wherever such third-party acknowledgments normally appear.
 
26
 *
 
27
 * 4. The names "Xerces" and "Apache Software Foundation" must
 
28
 *    not be used to endorse or promote products derived from this
 
29
 *    software without prior written permission. For written
 
30
 *    permission, please contact apache@apache.org.
 
31
 *
 
32
 * 5. Products derived from this software may not be called "Apache",
 
33
 *    nor may "Apache" appear in their name, without prior written
 
34
 *    permission of the Apache Software Foundation.
 
35
 *
 
36
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 
37
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
38
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
39
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 
40
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
41
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
42
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 
43
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
44
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
45
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 
46
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
47
 * SUCH DAMAGE.
 
48
 * ====================================================================
 
49
 *
 
50
 * This software consists of voluntary contributions made by many
 
51
 * individuals on behalf of the Apache Software Foundation and was
 
52
 * originally based on software copyright (c) 1999, International
 
53
 * Business Machines, Inc., http://www.apache.org.  For more
 
54
 * information on the Apache Software Foundation, please see
 
55
 * <http://www.apache.org/>.
 
56
 */
 
57
 
 
58
package dom;
 
59
 
 
60
import java.io.PrintWriter;
 
61
 
 
62
import org.w3c.dom.Document;
 
63
import org.w3c.dom.NamedNodeMap;
 
64
import org.w3c.dom.Node;
 
65
import org.w3c.dom.Text;
 
66
 
 
67
import org.xml.sax.SAXException;
 
68
import org.xml.sax.SAXParseException;
 
69
 
 
70
/**
 
71
 * A sample DOM counter. This sample program illustrates how to
 
72
 * traverse a DOM tree in order to get information about the document.
 
73
 * The output of this program shows the time and count of elements,
 
74
 * attributes, ignorable whitespaces, and characters appearing in
 
75
 * the document. Three times are shown: the parse time, the first
 
76
 * traversal of the document, and the second traversal of the tree.
 
77
 * <p>
 
78
 * This class is useful as a "poor-man's" performance tester to
 
79
 * compare the speed and accuracy of various DOM parsers. However,
 
80
 * it is important to note that the first parse time of a parser
 
81
 * will include both VM class load time and parser initialization
 
82
 * that would not be present in subsequent parses with the same
 
83
 * file.
 
84
 * <p>
 
85
 * <strong>Note:</strong> The results produced by this program
 
86
 * should never be accepted as true performance measurements.
 
87
 *
 
88
 * @author Andy Clark, IBM
 
89
 *
 
90
 * @version $Id: Counter.java,v 1.9 2003/11/29 18:07:34 mrglavas Exp $
 
91
 */
 
92
public class Counter {
 
93
 
 
94
    //
 
95
    // Constants
 
96
    //
 
97
 
 
98
    // feature ids
 
99
 
 
100
    /** Namespaces feature id (http://xml.org/sax/features/namespaces). */
 
101
    protected static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces";
 
102
 
 
103
    /** Validation feature id (http://xml.org/sax/features/validation). */
 
104
    protected static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation";
 
105
 
 
106
    /** Schema validation feature id (http://apache.org/xml/features/validation/schema). */
 
107
    protected static final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema";
 
108
 
 
109
    /** Schema full checking feature id (http://apache.org/xml/features/validation/schema-full-checking). */
 
110
    protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking";
 
111
    
 
112
    /** Dynamic validation feature id (http://apache.org/xml/features/validation/dynamic). */
 
113
    protected static final String DYNAMIC_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/dynamic";
 
114
 
 
115
    // default settings
 
116
 
 
117
    /** Default parser name (dom.wrappers.Xerces). */
 
118
    protected static final String DEFAULT_PARSER_NAME = "dom.wrappers.Xerces";
 
119
 
 
120
    /** Default repetition (1). */
 
121
    protected static final int DEFAULT_REPETITION = 1;
 
122
 
 
123
    /** Default namespaces support (true). */
 
124
    protected static final boolean DEFAULT_NAMESPACES = true;
 
125
 
 
126
    /** Default validation support (false). */
 
127
    protected static final boolean DEFAULT_VALIDATION = false;
 
128
 
 
129
    /** Default Schema validation support (false). */
 
130
    protected static final boolean DEFAULT_SCHEMA_VALIDATION = false;
 
131
 
 
132
    /** Default Schema full checking support (false). */
 
133
    protected static final boolean DEFAULT_SCHEMA_FULL_CHECKING = false;
 
134
 
 
135
    /** Default dynamic validation support (false). */
 
136
    protected static final boolean DEFAULT_DYNAMIC_VALIDATION = false;
 
137
 
 
138
    //
 
139
    // Data
 
140
    //
 
141
 
 
142
    /** Number of elements. */
 
143
    protected long fElements;
 
144
 
 
145
    /** Number of attributes. */
 
146
    protected long fAttributes;
 
147
 
 
148
    /** Number of characters. */
 
149
    protected long fCharacters;
 
150
 
 
151
    /** Number of ignorable whitespace characters. */
 
152
    protected long fIgnorableWhitespace;
 
153
 
 
154
    /** Document information. */
 
155
    protected ParserWrapper.DocumentInfo fDocumentInfo;
 
156
 
 
157
    //
 
158
    // Public methods
 
159
    //
 
160
 
 
161
    /** Sets the parser wrapper. */
 
162
    public void setDocumentInfo(ParserWrapper.DocumentInfo documentInfo) {
 
163
        fDocumentInfo = documentInfo;
 
164
    } // setDocumentInfo(ParserWrapper.DocumentInfo)
 
165
 
 
166
    /** Traverses the specified node, recursively. */
 
167
    public void count(Node node) {
 
168
 
 
169
        // is there anything to do?
 
170
        if (node == null) {
 
171
            return;
 
172
        }
 
173
 
 
174
        int type = node.getNodeType();
 
175
        switch (type) {
 
176
            case Node.DOCUMENT_NODE: {
 
177
                fElements = 0;
 
178
                fAttributes = 0;
 
179
                fCharacters = 0;
 
180
                fIgnorableWhitespace = 0;
 
181
                Document document = (Document)node;
 
182
                count(document.getDocumentElement());
 
183
                break;
 
184
            }
 
185
 
 
186
            case Node.ELEMENT_NODE: {
 
187
                fElements++;
 
188
                NamedNodeMap attrs = node.getAttributes();
 
189
                if (attrs != null) {
 
190
                    fAttributes += attrs.getLength();
 
191
                }
 
192
                // drop through to entity reference
 
193
            }
 
194
 
 
195
            case Node.ENTITY_REFERENCE_NODE: {
 
196
                Node child = node.getFirstChild();
 
197
                while (child != null) {
 
198
                    count(child);
 
199
                    child = child.getNextSibling();
 
200
                }
 
201
                break;
 
202
            }
 
203
 
 
204
            case Node.CDATA_SECTION_NODE: {
 
205
                fCharacters += ((Text)node).getLength();
 
206
                break;
 
207
            }
 
208
 
 
209
            case Node.TEXT_NODE: {
 
210
                if (fDocumentInfo != null) {
 
211
                    Text text = (Text)node;
 
212
                    int length = text.getLength();
 
213
                    if (fDocumentInfo.isIgnorableWhitespace(text)) {
 
214
                        fIgnorableWhitespace += length;
 
215
                    }
 
216
                    else {
 
217
                        fCharacters += length;
 
218
                    }
 
219
                }
 
220
                break;
 
221
            }
 
222
        }
 
223
 
 
224
    } // count(Node)
 
225
 
 
226
    /** Prints the results. */
 
227
    public void printResults(PrintWriter out, String uri,
 
228
                             long parse, long traverse1, long traverse2,
 
229
                             int repetition) {
 
230
 
 
231
        // filename.xml: 631/200/100 ms (4 elems, 0 attrs, 78 spaces, 0 chars)
 
232
        out.print(uri);
 
233
        out.print(": ");
 
234
        if (repetition == 1) {
 
235
            out.print(parse);
 
236
        }
 
237
        else {
 
238
            out.print(parse);
 
239
            out.print('/');
 
240
            out.print(repetition);
 
241
            out.print('=');
 
242
            out.print(parse/repetition);
 
243
        }
 
244
        out.print(';');
 
245
        out.print(traverse1);
 
246
        out.print(';');
 
247
        out.print(traverse2);
 
248
        out.print(" ms (");
 
249
        out.print(fElements);
 
250
        out.print(" elems, ");
 
251
        out.print(fAttributes);
 
252
        out.print(" attrs, ");
 
253
        out.print(fIgnorableWhitespace);
 
254
        out.print(" spaces, ");
 
255
        out.print(fCharacters);
 
256
        out.print(" chars)");
 
257
        out.println();
 
258
        out.flush();
 
259
 
 
260
    } // printResults(PrintWriter,String,long,long,long)
 
261
 
 
262
    //
 
263
    // MAIN
 
264
    //
 
265
 
 
266
    /** Main program entry point. */
 
267
    public static void main(String argv[]) {
 
268
 
 
269
        // is there anything to do?
 
270
        if (argv.length == 0) {
 
271
            printUsage();
 
272
            System.exit(1);
 
273
        }
 
274
 
 
275
        // variables
 
276
        Counter counter = new Counter();
 
277
        PrintWriter out = new PrintWriter(System.out);
 
278
        ParserWrapper parser = null;
 
279
        int repetition = DEFAULT_REPETITION;
 
280
        boolean namespaces = DEFAULT_NAMESPACES;
 
281
        boolean validation = DEFAULT_VALIDATION;
 
282
        boolean schemaValidation = DEFAULT_SCHEMA_VALIDATION;
 
283
        boolean schemaFullChecking = DEFAULT_SCHEMA_FULL_CHECKING;
 
284
        boolean dynamicValidation = DEFAULT_DYNAMIC_VALIDATION;
 
285
 
 
286
        // process arguments
 
287
        for (int i = 0; i < argv.length; i++) {
 
288
            String arg = argv[i];
 
289
            if (arg.startsWith("-")) {
 
290
                String option = arg.substring(1);
 
291
                if (option.equals("p")) {
 
292
                    // get parser name
 
293
                    if (++i == argv.length) {
 
294
                        System.err.println("error: Missing argument to -p option.");
 
295
                    }
 
296
                    String parserName = argv[i];
 
297
 
 
298
                    // create parser
 
299
                    try {
 
300
                        parser = (ParserWrapper)Class.forName(parserName).newInstance();
 
301
                    }
 
302
                    catch (Exception e) {
 
303
                        parser = null;
 
304
                        System.err.println("error: Unable to instantiate parser ("+parserName+")");
 
305
                    }
 
306
                    continue;
 
307
                }
 
308
                if (option.equals("x")) {
 
309
                    if (++i == argv.length) {
 
310
                        System.err.println("error: Missing argument to -x option.");
 
311
                        continue;
 
312
                    }
 
313
                    String number = argv[i];
 
314
                    try {
 
315
                        int value = Integer.parseInt(number);
 
316
                        if (value < 1) {
 
317
                            System.err.println("error: Repetition must be at least 1.");
 
318
                            continue;
 
319
                        }
 
320
                        repetition = value;
 
321
                    }
 
322
                    catch (NumberFormatException e) {
 
323
                        System.err.println("error: invalid number ("+number+").");
 
324
                    }
 
325
                    continue;
 
326
                }
 
327
                if (option.equalsIgnoreCase("n")) {
 
328
                    namespaces = option.equals("n");
 
329
                    continue;
 
330
                }
 
331
                if (option.equalsIgnoreCase("v")) {
 
332
                    validation = option.equals("v");
 
333
                    continue;
 
334
                }
 
335
                if (option.equalsIgnoreCase("s")) {
 
336
                    schemaValidation = option.equals("s");
 
337
                    continue;
 
338
                }
 
339
                if (option.equalsIgnoreCase("f")) {
 
340
                    schemaFullChecking = option.equals("f");
 
341
                    continue;
 
342
                }
 
343
                if (option.equalsIgnoreCase("dv")) {
 
344
                    dynamicValidation = option.equals("dv");
 
345
                    continue;
 
346
                }
 
347
                if (option.equals("h")) {
 
348
                    printUsage();
 
349
                    continue;
 
350
                }
 
351
            }
 
352
 
 
353
            // use default parser?
 
354
            if (parser == null) {
 
355
 
 
356
                // create parser
 
357
                try {
 
358
                    parser = (ParserWrapper)Class.forName(DEFAULT_PARSER_NAME).newInstance();
 
359
                }
 
360
                catch (Exception e) {
 
361
                    System.err.println("error: Unable to instantiate parser ("+DEFAULT_PARSER_NAME+")");
 
362
                    continue;
 
363
                }
 
364
            }
 
365
 
 
366
            // set parser features
 
367
            try {
 
368
                parser.setFeature(NAMESPACES_FEATURE_ID, namespaces);
 
369
            }
 
370
            catch (SAXException e) {
 
371
                System.err.println("warning: Parser does not support feature ("+NAMESPACES_FEATURE_ID+")");
 
372
            }
 
373
            try {
 
374
                parser.setFeature(VALIDATION_FEATURE_ID, validation);
 
375
            }
 
376
            catch (SAXException e) {
 
377
                System.err.println("warning: Parser does not support feature ("+VALIDATION_FEATURE_ID+")");
 
378
            }
 
379
            try {
 
380
                parser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, schemaValidation);
 
381
            }
 
382
            catch (SAXException e) {
 
383
                System.err.println("warning: Parser does not support feature ("+SCHEMA_VALIDATION_FEATURE_ID+")");
 
384
            }
 
385
            try {
 
386
                parser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking);
 
387
            }
 
388
            catch (SAXException e) {
 
389
                System.err.println("warning: Parser does not support feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")");
 
390
            }
 
391
            try {
 
392
                parser.setFeature(DYNAMIC_VALIDATION_FEATURE_ID, dynamicValidation);
 
393
            }
 
394
            catch (SAXException e) {
 
395
                System.err.println("warning: Parser does not support feature ("+DYNAMIC_VALIDATION_FEATURE_ID+")");
 
396
            }
 
397
 
 
398
            // parse file
 
399
            try {
 
400
                long beforeParse = System.currentTimeMillis();
 
401
                Document document = null;
 
402
                for (int j = 0; j < repetition; j++) {
 
403
                    document = parser.parse(arg);
 
404
                }
 
405
                long afterParse = System.currentTimeMillis();
 
406
                long parse = afterParse - beforeParse;
 
407
 
 
408
                ParserWrapper.DocumentInfo documentInfo = parser.getDocumentInfo();
 
409
                counter.setDocumentInfo(documentInfo);
 
410
 
 
411
                long beforeTraverse1 = System.currentTimeMillis();
 
412
                counter.count(document);
 
413
                long afterTraverse1 = System.currentTimeMillis();
 
414
                long traverse1 = afterTraverse1 - beforeTraverse1;
 
415
 
 
416
                long beforeTraverse2 = System.currentTimeMillis();
 
417
                counter.count(document);
 
418
                long afterTraverse2 = System.currentTimeMillis();
 
419
                long traverse2 = afterTraverse2 - beforeTraverse2;
 
420
                counter.printResults(out, arg, parse, traverse1, traverse2,
 
421
                                     repetition);
 
422
            }
 
423
            catch (SAXParseException e) {
 
424
                // ignore
 
425
            }
 
426
            catch (Exception e) {
 
427
                System.err.println("error: Parse error occurred - "+e.getMessage());
 
428
                Exception se = e;
 
429
                if (e instanceof SAXException) {
 
430
                    se = ((SAXException)e).getException();
 
431
                }
 
432
                if (se != null)
 
433
                  se.printStackTrace(System.err);
 
434
                else
 
435
                  e.printStackTrace(System.err);
 
436
            }
 
437
        }
 
438
 
 
439
    } // main(String[])
 
440
 
 
441
    //
 
442
    // Private static methods
 
443
    //
 
444
 
 
445
    /** Prints the usage. */
 
446
    private static void printUsage() {
 
447
 
 
448
        System.err.println("usage: java dom.Counter (options) uri ...");
 
449
        System.err.println();
 
450
 
 
451
        System.err.println("options:");
 
452
        System.err.println("  -p name     Select parser by name.");
 
453
        System.err.println("  -x number   Select number of repetitions.");
 
454
        System.err.println("  -n  | -N    Turn on/off namespace processing.");
 
455
        System.err.println("  -v  | -V    Turn on/off validation.");
 
456
        System.err.println("  -s  | -S    Turn on/off Schema validation support.");
 
457
        System.err.println("              NOTE: Not supported by all parsers.");
 
458
        System.err.println("  -f  | -F    Turn on/off Schema full checking.");
 
459
        System.err.println("              NOTE: Requires use of -s and not supported by all parsers.");
 
460
        System.err.println("  -dv | -DV   Turn on/off dynamic validation.");
 
461
        System.err.println("              NOTE: Not supported by all parsers.");
 
462
        System.err.println("  -h          This help screen.");
 
463
        System.err.println();
 
464
 
 
465
        System.err.println("defaults:");
 
466
        System.err.println("  Parser:     "+DEFAULT_PARSER_NAME);
 
467
        System.err.println("  Repetition: "+DEFAULT_REPETITION);
 
468
        System.err.print("  Namespaces: ");
 
469
        System.err.println(DEFAULT_NAMESPACES ? "on" : "off");
 
470
        System.err.print("  Validation: ");
 
471
        System.err.println(DEFAULT_VALIDATION ? "on" : "off");
 
472
        System.err.print("  Schema:     ");
 
473
        System.err.println(DEFAULT_SCHEMA_VALIDATION ? "on" : "off");
 
474
        System.err.print("  Schema full checking:     ");
 
475
        System.err.println(DEFAULT_SCHEMA_FULL_CHECKING ? "on" : "off");
 
476
        System.err.print("  Dynamic:    ");
 
477
        System.err.println(DEFAULT_DYNAMIC_VALIDATION ? "on" : "off");
 
478
 
 
479
    } // printUsage()
 
480
 
 
481
} // class DOMCount