KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > server > runner > XMLTransformer


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 JavaDoc;
23 import java.io.Reader JavaDoc;
24 import java.io.Writer JavaDoc;
25
26 import javax.xml.transform.Source JavaDoc;
27 import javax.xml.transform.Templates JavaDoc;
28 import javax.xml.transform.Transformer JavaDoc;
29 import javax.xml.transform.TransformerConfigurationException JavaDoc;
30 import javax.xml.transform.TransformerException JavaDoc;
31 import javax.xml.transform.TransformerFactory JavaDoc;
32 import javax.xml.transform.stream.StreamResult JavaDoc;
33 import javax.xml.transform.stream.StreamSource JavaDoc;
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,v 1.1 2004/05/22 11:34:46 vmassol Exp $
42  */

43 public class XMLTransformer
44 {
45     // Constants ---------------------------------------------------------------
46

47     /**
48      * Mime type of HTML content.
49      */

50     private static final String JavaDoc HTML_MIME_TYPE = "text/html";
51
52     /**
53      * XSLT output method for HTML.
54      */

55     private static final String JavaDoc HTML_OUTPUT_METHOD = "html";
56
57     /**
58      * Mime type of plain text content.
59      */

60     private static final String JavaDoc TEXT_MIME_TYPE = "text/plain";
61
62     /**
63      * XSLT output method for plain text.
64      */

65     private static final String JavaDoc TEXT_OUTPUT_METHOD = "text";
66
67     /**
68      * Mime type of XML content.
69      */

70     private static final String JavaDoc XML_MIME_TYPE = "text/xml";
71
72     /**
73      * Name of the XSLT output method property.
74      */

75     private static final String JavaDoc 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 JavaDoc 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 JavaDoc 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     public XMLTransformer(InputStream JavaDoc theStylesheet)
101         throws TransformerConfigurationException JavaDoc
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
TransformerFactory JavaDoc transformerFactory =
109             TransformerFactory.newInstance();
110         Source JavaDoc source = new StreamSource JavaDoc(theStylesheet);
111         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
String JavaDoc outputMethod = this.templates.getOutputProperties().getProperty(
117             XSL_OUTPUT_PROPERTY_METHOD);
118
119         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     public String JavaDoc getContentType()
131     {
132         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     public void transform(Reader JavaDoc theXml, Writer JavaDoc theWriter)
145         throws TransformerException JavaDoc
146     {
147         Transformer JavaDoc transformer = this.templates.newTransformer();
148         transformer.transform(new StreamSource JavaDoc(theXml),
149             new StreamResult JavaDoc(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     private String JavaDoc getContentType(String JavaDoc theOutputMethod)
160     {
161         String JavaDoc contentType;
162
163         if (HTML_OUTPUT_METHOD.equals(theOutputMethod))
164         {
165             contentType = HTML_MIME_TYPE;
166         }
167         else if (TEXT_OUTPUT_METHOD.equals(theOutputMethod))
168         {
169             contentType = TEXT_MIME_TYPE;
170         }
171         else
172         {
173             contentType = XML_MIME_TYPE;
174         }
175         return contentType;
176     }
177
178 }
179
Popular Tags