|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
XMLTransformer.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
* ====================================================================
|
|
3 |
*
|
|
4 |
* The Apache Software License, Version 1.1
|
|
5 |
*
|
|
6 |
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
|
7 |
* reserved.
|
|
8 |
*
|
|
9 |
* Redistribution and use in source and binary forms, with or without
|
|
10 |
* modification, are permitted provided that the following conditions
|
|
11 |
* are met:
|
|
12 |
*
|
|
13 |
* 1. Redistributions of source code must retain the above copyright
|
|
14 |
* notice, this list of conditions and the following disclaimer.
|
|
15 |
*
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
17 |
* notice, this list of conditions and the following disclaimer in
|
|
18 |
* the documentation and/or other materials provided with the
|
|
19 |
* distribution.
|
|
20 |
*
|
|
21 |
* 3. The end-user documentation included with the redistribution, if
|
|
22 |
* any, must include the following acknowlegement:
|
|
23 |
* "This product includes software developed by the
|
|
24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
27 |
*
|
|
28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
29 |
* Foundation" must not be used to endorse or promote products
|
|
30 |
* derived from this software without prior written permission. For
|
|
31 |
* written permission, please contact apache@apache.org.
|
|
32 |
*
|
|
33 |
* 5. Products derived from this software may not be called "Apache"
|
|
34 |
* nor may "Apache" appear in their names without prior written
|
|
35 |
* permission of the Apache Group.
|
|
36 |
*
|
|
37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
48 |
* SUCH DAMAGE.
|
|
49 |
* ====================================================================
|
|
50 |
*
|
|
51 |
* This software consists of voluntary contributions made by many
|
|
52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
53 |
* information on the Apache Software Foundation, please see
|
|
54 |
* <http://www.apache.org/>.
|
|
55 |
*
|
|
56 |
*/
|
|
57 |
package org.apache.cactus.server.runner;
|
|
58 |
|
|
59 |
import java.io.InputStream;
|
|
60 |
import java.io.Reader;
|
|
61 |
import java.io.Writer;
|
|
62 |
|
|
63 |
import javax.xml.transform.Source;
|
|
64 |
import javax.xml.transform.Templates;
|
|
65 |
import javax.xml.transform.Transformer;
|
|
66 |
import javax.xml.transform.TransformerConfigurationException;
|
|
67 |
import javax.xml.transform.TransformerException;
|
|
68 |
import javax.xml.transform.TransformerFactory;
|
|
69 |
import javax.xml.transform.stream.StreamResult;
|
|
70 |
import javax.xml.transform.stream.StreamSource;
|
|
71 |
|
|
72 |
/**
|
|
73 |
* Helper class that handles the transformation of the XML test results to
|
|
74 |
* some output format determined by a stylesheet.
|
|
75 |
*
|
|
76 |
* @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
|
|
77 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
78 |
* @since Cactus 1.5
|
|
79 |
*
|
|
80 |
* @version $Id: XMLTransformer.java,v 1.5 2003/02/12 21:01:51 vmassol Exp $
|
|
81 |
*/
|
|
82 |
public class XMLTransformer |
|
83 |
{ |
|
84 |
// Constants ---------------------------------------------------------------
|
|
85 |
|
|
86 |
/**
|
|
87 |
* Mime type of HTML content.
|
|
88 |
*/
|
|
89 |
private static final String HTML_MIME_TYPE = "text/html"; |
|
90 |
|
|
91 |
/**
|
|
92 |
* XSLT output method for HTML.
|
|
93 |
*/
|
|
94 |
private static final String HTML_OUTPUT_METHOD = "html"; |
|
95 |
|
|
96 |
/**
|
|
97 |
* Mime type of plain text content.
|
|
98 |
*/
|
|
99 |
private static final String TEXT_MIME_TYPE = "text/plain"; |
|
100 |
|
|
101 |
/**
|
|
102 |
* XSLT output method for plain text.
|
|
103 |
*/
|
|
104 |
private static final String TEXT_OUTPUT_METHOD = "text"; |
|
105 |
|
|
106 |
/**
|
|
107 |
* Mime type of XML content.
|
|
108 |
*/
|
|
109 |
private static final String XML_MIME_TYPE = "text/xml"; |
|
110 |
|
|
111 |
/**
|
|
112 |
* Name of the XSLT output method property.
|
|
113 |
*/
|
|
114 |
private static final String XSL_OUTPUT_PROPERTY_METHOD = "method"; |
|
115 |
|
|
116 |
// Instance Variables ------------------------------------------------------
|
|
117 |
|
|
118 |
/**
|
|
119 |
* The XSLT templates to use for transforming the XML report into HTML.
|
|
120 |
*/
|
|
121 |
private Templates templates = null; |
|
122 |
|
|
123 |
/**
|
|
124 |
* The MIME type of the content we'll be sending to the client. This
|
|
125 |
* defaults to "text/xml", but depends on the provided XSLT stylesheet.
|
|
126 |
*/
|
|
127 |
private String contentType = XML_MIME_TYPE;
|
|
128 |
|
|
129 |
// Constructors ------------------------------------------------------------
|
|
130 |
|
|
131 |
/**
|
|
132 |
* Constructor.
|
|
133 |
*
|
|
134 |
* @param theStylesheet The input stream for the stylesheet to use for the
|
|
135 |
* transformations
|
|
136 |
* @exception TransformerConfigurationException if an error occurs when
|
|
137 |
* creating the XSL templates
|
|
138 |
*/
|
|
139 | 0 |
public XMLTransformer(InputStream theStylesheet)
|
140 |
throws TransformerConfigurationException
|
|
141 |
{ |
|
142 |
// Setup the transformation templates
|
|
143 |
// NOTE: Because this is done at initialization time for
|
|
144 |
// better performance and simplicity, changes to the
|
|
145 |
// stylesheet will only go live after the web-app is
|
|
146 |
// restarted
|
|
147 | 0 |
TransformerFactory transformerFactory = |
148 |
TransformerFactory.newInstance(); |
|
149 | 0 |
Source source = new StreamSource(theStylesheet);
|
150 | 0 |
this.templates = transformerFactory.newTemplates(source);
|
151 |
|
|
152 |
// Find out which content type is produced by the
|
|
153 |
// stylesheet (valid values per XSLT 1.0 are 'xml', 'html'
|
|
154 |
// and 'text')
|
|
155 | 0 |
String outputMethod = this.templates.getOutputProperties().getProperty(
|
156 |
XSL_OUTPUT_PROPERTY_METHOD); |
|
157 |
|
|
158 | 0 |
this.contentType = getContentType(outputMethod);
|
159 |
} |
|
160 |
|
|
161 |
// Public Methods ----------------------------------------------------------
|
|
162 |
|
|
163 |
/**
|
|
164 |
* Returns the content type that will be produced by the XSLT stylesheet
|
|
165 |
* after transformation.
|
|
166 |
*
|
|
167 |
* @return The content type
|
|
168 |
*/
|
|
169 | 0 |
public String getContentType()
|
170 |
{ |
|
171 | 0 |
return this.contentType; |
172 |
} |
|
173 |
|
|
174 |
/**
|
|
175 |
* Performs the actual transformation.
|
|
176 |
*
|
|
177 |
* @param theXml The XML source to transform
|
|
178 |
* @param theWriter The writer to which the transformation result should be
|
|
179 |
* written.
|
|
180 |
* @exception TransformerException if an error occurs when applying the
|
|
181 |
* XSL template to the XML source
|
|
182 |
*/
|
|
183 | 0 |
public void transform(Reader theXml, Writer theWriter) |
184 |
throws TransformerException
|
|
185 |
{ |
|
186 | 0 |
Transformer transformer = this.templates.newTransformer();
|
187 | 0 |
transformer.transform(new StreamSource(theXml),
|
188 |
new StreamResult(theWriter));
|
|
189 |
} |
|
190 |
|
|
191 |
// Private Methods --------------------------------------------------------
|
|
192 |
|
|
193 |
/**
|
|
194 |
* @param theOutputMethod the output method type (Ex: "xml", "html",
|
|
195 |
* "text", etc)
|
|
196 |
* @return the MIME type of the content we'll be sending to the client
|
|
197 |
*/
|
|
198 | 0 |
private String getContentType(String theOutputMethod)
|
199 |
{ |
|
200 | 0 |
String contentType; |
201 |
|
|
202 | 0 |
if (HTML_OUTPUT_METHOD.equals(theOutputMethod))
|
203 |
{ |
|
204 | 0 |
contentType = HTML_MIME_TYPE; |
205 |
} |
|
206 | 0 |
else if (TEXT_OUTPUT_METHOD.equals(theOutputMethod)) |
207 |
{ |
|
208 | 0 |
contentType = TEXT_MIME_TYPE; |
209 |
} |
|
210 |
else
|
|
211 |
{ |
|
212 | 0 |
contentType = XML_MIME_TYPE; |
213 |
} |
|
214 | 0 |
return contentType;
|
215 |
} |
|
216 |
|
|
217 |
} |
|
218 |
|
|