001: /*
002: * Copyright (c) 2000-2001 Sosnoski Software Solutions, Inc.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
019: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
020: * IN THE SOFTWARE.
021: */
022:
023: package com.sosnoski.xmlbench;
024:
025: import java.io.*;
026: import java.util.*;
027:
028: import javax.xml.parsers.*;
029:
030: import org.apache.xerces.dom.*;
031: import org.apache.xerces.parsers.*;
032: import org.apache.xml.serialize.*;
033:
034: import org.w3c.dom.*;
035:
036: import org.xml.sax.*;
037:
038: /**
039: * Abstract base class for benchmarks measuring performance of the Xerces2 DOM
040: * document representation. This base class implementation can be customized
041: * by subclasses to experiment with options for the representation, in
042: * particular for trying the deferred node expansion feature of Xerces. The
043: * code used for Xerces2 DOM is identical to that used for Xerces1, but is
044: * compiled with a different classpath.
045: *
046: * @author Dennis M. Sosnoski
047: * @version 1.2
048: */
049:
050: public abstract class BenchXerces2 extends BenchDOM {
051: /** Flag for using deferred node expansion. */
052: private boolean m_deferExpansion;
053:
054: /** DOM parser used within a test run. */
055: private DOMParser m_parser;
056:
057: /** XML output serializer used within a test run. */
058: private XMLSerializer m_serializer;
059:
060: /**
061: * Constructor.
062: *
063: * @param config test configuration name
064: * @param defer defer node expansion flag
065: */
066:
067: protected BenchXerces2(String config, boolean defer) {
068: super (config);
069: m_deferExpansion = defer;
070: }
071:
072: /**
073: * Set deferred node expansion mode.
074: *
075: * @param defer defer node expansion flag
076: */
077:
078: protected void setDeferExpansion(boolean defer) {
079: m_deferExpansion = defer;
080: }
081:
082: /**
083: * Build document representation by parsing XML. This implementation
084: * creates a DOM parser if one does not already exist, then reuses
085: * that parser for the duration of a test run..
086: *
087: * @param in XML document input stream
088: * @return document representation
089: */
090:
091: protected Object build(InputStream in) {
092: if (m_parser == null) {
093: m_parser = new DOMParser();
094: try {
095: m_parser
096: .setFeature(
097: "http://xml.org/sax/features/validation",
098: false);
099: m_parser
100: .setFeature(
101: "http://apache.org/xml/features/dom/defer-node-expansion",
102: m_deferExpansion);
103: m_parser.setFeature(
104: "http://xml.org/sax/features/namespaces", true);
105: } catch (Exception ex) {
106: ex.printStackTrace(System.err);
107: System.exit(0);
108: }
109: }
110: Object doc = null;
111: try {
112: m_parser.parse(new InputSource(in));
113: doc = m_parser.getDocument();
114: m_parser.reset();
115: } catch (Exception ex) {
116: ex.printStackTrace(System.err);
117: System.exit(0);
118: }
119: return doc;
120: }
121:
122: /**
123: * Output a document as XML text. This method uses the method defined
124: * by the Xerces DOM to output a text representation of the document.
125: *
126: * @param doc document representation to be output
127: * @param out XML document output stream
128: */
129:
130: protected void output(Object doc, OutputStream out) {
131: if (m_serializer == null) {
132: OutputFormat format = new OutputFormat((Document) doc);
133: m_serializer = new XMLSerializer(format);
134: }
135: try {
136: m_serializer.reset();
137: m_serializer.setOutputByteStream(out);
138: m_serializer.serialize(((Document) doc)
139: .getDocumentElement());
140: } catch (Exception ex) {
141: ex.printStackTrace(System.err);
142: System.exit(0);
143: }
144: }
145:
146: /**
147: * Reset test class instance. This discards the parser used
148: * within a test pass.
149: */
150:
151: protected void reset() {
152: m_parser = null;
153: m_serializer = null;
154: }
155: }
|