001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2004 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.internal.server.runner;
021:
022: import java.io.InputStream;
023: import java.io.Reader;
024: import java.io.Writer;
025:
026: import javax.xml.transform.Source;
027: import javax.xml.transform.Templates;
028: import javax.xml.transform.Transformer;
029: import javax.xml.transform.TransformerConfigurationException;
030: import javax.xml.transform.TransformerException;
031: import javax.xml.transform.TransformerFactory;
032: import javax.xml.transform.stream.StreamResult;
033: import javax.xml.transform.stream.StreamSource;
034:
035: /**
036: * Helper class that handles the transformation of the XML test results to
037: * some output format determined by a stylesheet.
038: *
039: * @since Cactus 1.5
040: *
041: * @version $Id: XMLTransformer.java 238991 2004-05-22 11:34:50Z vmassol $
042: */
043: public class XMLTransformer {
044: // Constants ---------------------------------------------------------------
045:
046: /**
047: * Mime type of HTML content.
048: */
049: private static final String HTML_MIME_TYPE = "text/html";
050:
051: /**
052: * XSLT output method for HTML.
053: */
054: private static final String HTML_OUTPUT_METHOD = "html";
055:
056: /**
057: * Mime type of plain text content.
058: */
059: private static final String TEXT_MIME_TYPE = "text/plain";
060:
061: /**
062: * XSLT output method for plain text.
063: */
064: private static final String TEXT_OUTPUT_METHOD = "text";
065:
066: /**
067: * Mime type of XML content.
068: */
069: private static final String XML_MIME_TYPE = "text/xml";
070:
071: /**
072: * Name of the XSLT output method property.
073: */
074: private static final String XSL_OUTPUT_PROPERTY_METHOD = "method";
075:
076: // Instance Variables ------------------------------------------------------
077:
078: /**
079: * The XSLT templates to use for transforming the XML report into HTML.
080: */
081: private Templates templates = null;
082:
083: /**
084: * The MIME type of the content we'll be sending to the client. This
085: * defaults to "text/xml", but depends on the provided XSLT stylesheet.
086: */
087: private String contentType = XML_MIME_TYPE;
088:
089: // Constructors ------------------------------------------------------------
090:
091: /**
092: * Constructor.
093: *
094: * @param theStylesheet The input stream for the stylesheet to use for the
095: * transformations
096: * @exception TransformerConfigurationException if an error occurs when
097: * creating the XSL templates
098: */
099: public XMLTransformer(InputStream theStylesheet)
100: throws TransformerConfigurationException {
101: // Setup the transformation templates
102: // NOTE: Because this is done at initialization time for
103: // better performance and simplicity, changes to the
104: // stylesheet will only go live after the web-app is
105: // restarted
106: TransformerFactory transformerFactory = TransformerFactory
107: .newInstance();
108: Source source = new StreamSource(theStylesheet);
109: this .templates = transformerFactory.newTemplates(source);
110:
111: // Find out which content type is produced by the
112: // stylesheet (valid values per XSLT 1.0 are 'xml', 'html'
113: // and 'text')
114: String outputMethod = this .templates.getOutputProperties()
115: .getProperty(XSL_OUTPUT_PROPERTY_METHOD);
116:
117: this .contentType = getContentType(outputMethod);
118: }
119:
120: // Public Methods ----------------------------------------------------------
121:
122: /**
123: * Returns the content type that will be produced by the XSLT stylesheet
124: * after transformation.
125: *
126: * @return The content type
127: */
128: public String getContentType() {
129: return this .contentType;
130: }
131:
132: /**
133: * Performs the actual transformation.
134: *
135: * @param theXml The XML source to transform
136: * @param theWriter The writer to which the transformation result should be
137: * written.
138: * @exception TransformerException if an error occurs when applying the
139: * XSL template to the XML source
140: */
141: public void transform(Reader theXml, Writer theWriter)
142: throws TransformerException {
143: Transformer transformer = this .templates.newTransformer();
144: transformer.transform(new StreamSource(theXml),
145: new StreamResult(theWriter));
146: }
147:
148: // Private Methods --------------------------------------------------------
149:
150: /**
151: * @param theOutputMethod the output method type (Ex: "xml", "html",
152: * "text", etc)
153: * @return the MIME type of the content we'll be sending to the client
154: */
155: private String getContentType(String theOutputMethod) {
156: String contentType;
157:
158: if (HTML_OUTPUT_METHOD.equals(theOutputMethod)) {
159: contentType = HTML_MIME_TYPE;
160: } else if (TEXT_OUTPUT_METHOD.equals(theOutputMethod)) {
161: contentType = TEXT_MIME_TYPE;
162: } else {
163: contentType = XML_MIME_TYPE;
164: }
165: return contentType;
166: }
167:
168: }
|