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 Xerces 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.
043: *
044: * @author Dennis M. Sosnoski
045: * @version 1.2
046: */
047:
048: public abstract class BenchXerces extends BenchDOM {
049: /** Flag for using deferred node expansion. */
050: private boolean m_deferExpansion;
051:
052: /** DOM parser used within a test run. */
053: private DOMParser m_parser;
054:
055: /** XML output serializer used within a test run. */
056: private XMLSerializer m_serializer;
057:
058: /**
059: * Constructor.
060: *
061: * @param config test configuration name
062: * @param defer defer node expansion flag
063: */
064:
065: protected BenchXerces(String config, boolean defer) {
066: super (config);
067: m_deferExpansion = defer;
068: }
069:
070: /**
071: * Set deferred node expansion mode.
072: *
073: * @param defer defer node expansion flag
074: */
075:
076: protected void setDeferExpansion(boolean defer) {
077: m_deferExpansion = defer;
078: }
079:
080: /**
081: * Build document representation by parsing XML. This implementation
082: * creates a DOM parser if one does not already exist, then reuses
083: * that parser for the duration of a test run..
084: *
085: * @param in XML document input stream
086: * @return document representation
087: */
088:
089: protected Object build(InputStream in) {
090: if (m_parser == null) {
091: m_parser = new DOMParser();
092: try {
093: m_parser
094: .setFeature(
095: "http://xml.org/sax/features/validation",
096: false);
097: m_parser
098: .setFeature(
099: "http://apache.org/xml/features/dom/defer-node-expansion",
100: m_deferExpansion);
101: m_parser.setFeature(
102: "http://xml.org/sax/features/namespaces", true);
103: } catch (Exception ex) {
104: ex.printStackTrace(System.err);
105: System.exit(0);
106: }
107: }
108: Object doc = null;
109: try {
110: m_parser.parse(new InputSource(in));
111: doc = m_parser.getDocument();
112: m_parser.reset();
113: } catch (Exception ex) {
114: ex.printStackTrace(System.err);
115: System.exit(0);
116: }
117: return doc;
118: }
119:
120: /**
121: * Output a document as XML text. This method uses the method defined
122: * by the Xerces DOM to output a text representation of the document.
123: *
124: * @param doc document representation to be output
125: * @param out XML document output stream
126: */
127:
128: protected void output(Object doc, OutputStream out) {
129: if (m_serializer == null) {
130: OutputFormat format = new OutputFormat((Document) doc);
131: m_serializer = new XMLSerializer(format);
132: }
133: try {
134: m_serializer.reset();
135: m_serializer.setOutputByteStream(out);
136: m_serializer.serialize(((Document) doc)
137: .getDocumentElement());
138: } catch (Exception ex) {
139: ex.printStackTrace(System.err);
140: System.exit(0);
141: }
142: }
143:
144: /**
145: * Reset test class instance. This discards the parser used
146: * within a test pass.
147: */
148:
149: protected void reset() {
150: m_parser = null;
151: m_serializer = null;
152: }
153: }
|