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.crimson.jaxp.*;
031: import org.apache.crimson.tree.*;
032:
033: /**
034: * Benchmark for measuring performance of the Apache Crimson DOM document
035: * representation. Since we may have several parsers and document models in
036: * the classpath, this creates the Crimson parser directly in order to avoid
037: * any confusion in going through JAXP.
038: *
039: * @author Dennis M. Sosnoski
040: * @version 1.2
041: */
042:
043: public class BenchCrimson extends BenchDOM {
044: /** Document builder used within a test run. */
045: private DocumentBuilder m_builder;
046:
047: /**
048: * Constructor.
049: */
050:
051: public BenchCrimson() {
052: super ("Crimson DOM");
053: }
054:
055: /**
056: * Build document representation by parsing XML. This implementation
057: * creates a document builder if one does not already exist, then reuses
058: * that builder for the duration of a test run..
059: *
060: * @param in XML document input stream
061: * @return document representation
062: */
063:
064: protected Object build(InputStream in) {
065: if (m_builder == null) {
066: DocumentBuilderFactory dbf = DocumentBuilderFactoryImpl
067: .newInstance();
068: try {
069: dbf.setValidating(false);
070: dbf.setNamespaceAware(true);
071: m_builder = dbf.newDocumentBuilder();
072: } catch (Exception ex) {
073: ex.printStackTrace(System.err);
074: System.exit(0);
075: }
076: }
077: Object doc = null;
078: try {
079: doc = m_builder.parse(in);
080: } catch (Exception ex) {
081: ex.printStackTrace(System.err);
082: System.exit(0);
083: }
084: return doc;
085: }
086:
087: /**
088: * Output a document as XML text. This method uses the method defined
089: * by the Crimson DOM to output a text representation of the document.
090: *
091: * @param doc document representation to be output
092: * @param out XML document output stream
093: */
094:
095: protected void output(Object doc, OutputStream out) {
096: XmlDocument cdoc = (XmlDocument) doc;
097: try {
098: cdoc.write(out);
099: } catch (Exception ex) {
100: ex.printStackTrace(System.err);
101: System.exit(0);
102: }
103: }
104:
105: /**
106: * Reset test class instance. This discards the document builder used
107: * within a test pass.
108: */
109:
110: protected void reset() {
111: m_builder = null;
112: }
113: }
|