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