1 /*
2 * ========================================================================
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * ========================================================================
20 */
21 package org.apache.cactus.internal.server.runner;
22
23 import java.io.InputStream;
24 import java.io.Reader;
25 import java.io.Writer;
26
27 import javax.xml.transform.Source;
28 import javax.xml.transform.Templates;
29 import javax.xml.transform.Transformer;
30 import javax.xml.transform.TransformerConfigurationException;
31 import javax.xml.transform.TransformerException;
32 import javax.xml.transform.TransformerFactory;
33 import javax.xml.transform.stream.StreamResult;
34 import javax.xml.transform.stream.StreamSource;
35
36 /**
37 * Helper class that handles the transformation of the XML test results to
38 * some output format determined by a stylesheet.
39 *
40 * @since Cactus 1.5
41 *
42 * @version $Id: XMLTransformer.java 238991 2004-05-22 11:34:50Z vmassol $
43 */
44 public class XMLTransformer
45 {
46 // Constants ---------------------------------------------------------------
47
48 /**
49 * Mime type of HTML content.
50 */
51 private static final String HTML_MIME_TYPE = "text/html";
52
53 /**
54 * XSLT output method for HTML.
55 */
56 private static final String HTML_OUTPUT_METHOD = "html";
57
58 /**
59 * Mime type of plain text content.
60 */
61 private static final String TEXT_MIME_TYPE = "text/plain";
62
63 /**
64 * XSLT output method for plain text.
65 */
66 private static final String TEXT_OUTPUT_METHOD = "text";
67
68 /**
69 * Mime type of XML content.
70 */
71 private static final String XML_MIME_TYPE = "text/xml";
72
73 /**
74 * Name of the XSLT output method property.
75 */
76 private static final String XSL_OUTPUT_PROPERTY_METHOD = "method";
77
78 // Instance Variables ------------------------------------------------------
79
80 /**
81 * The XSLT templates to use for transforming the XML report into HTML.
82 */
83 private Templates templates = null;
84
85 /**
86 * The MIME type of the content we'll be sending to the client. This
87 * defaults to "text/xml", but depends on the provided XSLT stylesheet.
88 */
89 private String contentType = XML_MIME_TYPE;
90
91 // Constructors ------------------------------------------------------------
92
93 /**
94 * Constructor.
95 *
96 * @param theStylesheet The input stream for the stylesheet to use for the
97 * transformations
98 * @exception TransformerConfigurationException if an error occurs when
99 * creating the XSL templates
100 */
101 public XMLTransformer(InputStream theStylesheet)
102 throws TransformerConfigurationException
103 {
104 // Setup the transformation templates
105 // NOTE: Because this is done at initialization time for
106 // better performance and simplicity, changes to the
107 // stylesheet will only go live after the web-app is
108 // restarted
109 TransformerFactory transformerFactory =
110 TransformerFactory.newInstance();
111 Source source = new StreamSource(theStylesheet);
112 this.templates = transformerFactory.newTemplates(source);
113
114 // Find out which content type is produced by the
115 // stylesheet (valid values per XSLT 1.0 are 'xml', 'html'
116 // and 'text')
117 String outputMethod = this.templates.getOutputProperties().getProperty(
118 XSL_OUTPUT_PROPERTY_METHOD);
119
120 this.contentType = getContentType(outputMethod);
121 }
122
123 // Public Methods ----------------------------------------------------------
124
125 /**
126 * Returns the content type that will be produced by the XSLT stylesheet
127 * after transformation.
128 *
129 * @return The content type
130 */
131 public String getContentType()
132 {
133 return this.contentType;
134 }
135
136 /**
137 * Performs the actual transformation.
138 *
139 * @param theXml The XML source to transform
140 * @param theWriter The writer to which the transformation result should be
141 * written.
142 * @exception TransformerException if an error occurs when applying the
143 * XSL template to the XML source
144 */
145 public void transform(Reader theXml, Writer theWriter)
146 throws TransformerException
147 {
148 Transformer transformer = this.templates.newTransformer();
149 transformer.transform(new StreamSource(theXml),
150 new StreamResult(theWriter));
151 }
152
153 // Private Methods --------------------------------------------------------
154
155 /**
156 * @param theOutputMethod the output method type (Ex: "xml", "html",
157 * "text", etc)
158 * @return the MIME type of the content we'll be sending to the client
159 */
160 private String getContentType(String theOutputMethod)
161 {
162 String contentType;
163
164 if (HTML_OUTPUT_METHOD.equals(theOutputMethod))
165 {
166 contentType = HTML_MIME_TYPE;
167 }
168 else if (TEXT_OUTPUT_METHOD.equals(theOutputMethod))
169 {
170 contentType = TEXT_MIME_TYPE;
171 }
172 else
173 {
174 contentType = XML_MIME_TYPE;
175 }
176 return contentType;
177 }
178
179 }