KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infohazard > maverick > transform > XSLTransformFactory


1 /*
2  * $Id: XSLTransformFactory.java,v 1.12 2004/06/27 17:41:31 eelco12 Exp $
3  * $Source: /cvsroot/mav/maverick/src/java/org/infohazard/maverick/transform/XSLTransformFactory.java,v $
4  */

5 package org.infohazard.maverick.transform;
6
7 import javax.servlet.*;
8 import javax.xml.transform.URIResolver JavaDoc;
9
10 import org.infohazard.maverick.flow.*;
11 import org.infohazard.maverick.util.XML;
12 import org.jdom.Element;
13
14 /**
15  * @author Jeff Schnitzer
16  * @author Scott Hernandez
17  * @version $Revision: 1.12 $ $Date: 2004/06/27 17:41:31 $
18  *
19  * <p>Factory for creating transformation pipelines using XSLT.</p>
20  *
21  * <p>This factory has certain options which can be defined when
22  * declaring the factory. The defaults are shown here:</p>
23  *
24  * <pre>
25  * &lt;transform-factory type="xslt" provider="org.infohazard.maverick.transform.XSLTransformFactory"&gt;
26  * &lt;default-output-type value="text/html"/&gt;
27  * &lt;template-caching value="preload"/&gt;
28  * &lt;uri-resolver value=""/&gt;
29  * &lt;/transform-factory&gt;
30  * </pre>
31  *
32  * <ul>
33  * <li>
34  * <b>default-output-type</b> - Unless an individual transform
35  * explicitly specifies an output-type, this will be the http
36  * content-type for a successfully completing transformation.
37  * </li>
38  * <li>
39  * <b>template-caching</b> - Must be one of three values: <b>preload</b>,
40  * which compiles and caches all XSLT templates when the web application
41  * starts, <b>lazy</b>, which caches compiled XSLT templates but does
42  * not load them until they are requested, and <b>disabled</b>, which
43  * causes templates to be (re)loaded for every request. The default is
44  * preload.
45  * </li>
46  * <li>
47  * <b>uri-resolver</b> - The classname of a custom URI Resolver to set on
48  * all transformations.
49  * </li>
50  * </ul>
51  *
52  * <p>The options for an individual transform are like this:</p>
53  *
54  * <pre>
55  * &lt;transform type="xslt" path="blah.xsl" output-type="text/plain"/&gt;
56  * </pre>
57  */

58 public class XSLTransformFactory implements TransformFactory
59 {
60     /** For the factory configuration */
61     protected final static String JavaDoc ATTR_DEFAULT_FINAL_CONTENT_TYPE = "default-output-type";
62     /** */
63     protected final static String JavaDoc ATTR_TEMPLATE_CACHING = "template-caching";
64     /** */
65     protected final static String JavaDoc VAL_TEMPLATE_CACHING_LAZY = "lazy";
66     /** */
67     protected final static String JavaDoc VAL_TEMPLATE_CACHING_PRELOAD = "preload";
68     /** */
69     protected final static String JavaDoc VAL_TEMPLATE_CACHING_DISABLED = "disabled";
70     /** */
71     protected final static String JavaDoc ATTR_URI_RESOLVER = "uri-resolver";
72
73     /** For individual transform nodes */
74     protected final static String JavaDoc ATTR_PATH = "path";
75     /** */
76     protected final static String JavaDoc ATTR_MONITOR = "monitor";
77     /** */
78     protected final static String JavaDoc ATTR_FINAL_CONTENT_TYPE = "output-type";
79
80     /**
81      * If the init param for default content type is not specified, this
82      * becomes the actual default final content type. The default default :-)
83      */

84     protected final static String JavaDoc DEFAULT_DEFAULT_FINAL_CONTENT_TYPE = "text/html";
85
86     /**
87      * Unless specified in individual transform nodes, this is the content type
88      * for a successfully completed transformation.
89      */

90     protected String JavaDoc defaultFinalContentType = DEFAULT_DEFAULT_FINAL_CONTENT_TYPE;
91
92     /** Should templates be preloaded and cached */
93     protected int templateCachingStyle = XSLTransform.CACHE_PRELOAD;
94
95     /** Allow the user to override the URI resolver. */
96     protected URIResolver JavaDoc uriResolver = null;
97
98     /** We need this to be able to look up real pathnames. */
99     protected ServletContext servletCtx;
100
101     /**
102      * @param factoryNode The XML from which to draw configuration information.
103      * @param servletCfg Information about the container.
104      * @exception ConfigException Thrown if configuration is bad.
105      */

106     public void init(Element factoryNode, ServletConfig servletCfg) throws ConfigException
107     {
108         this.servletCtx = servletCfg.getServletContext();
109
110         if (factoryNode != null)
111         {
112             // Figure out the default content type for successful transforms
113
String JavaDoc outputTypeStr = XML.getValue(factoryNode, ATTR_DEFAULT_FINAL_CONTENT_TYPE);
114             if (outputTypeStr != null)
115                 this.defaultFinalContentType = outputTypeStr;
116
117             // Should we lazy-load the templates
118
String JavaDoc templateLoadingStr = XML.getValue(factoryNode, ATTR_TEMPLATE_CACHING);
119             if (templateLoadingStr != null)
120             {
121                 templateLoadingStr = templateLoadingStr.toLowerCase();
122
123                 if (VAL_TEMPLATE_CACHING_PRELOAD.equals(templateLoadingStr))
124                 {
125                     this.templateCachingStyle = XSLTransform.CACHE_PRELOAD;
126                 }
127                 else if (VAL_TEMPLATE_CACHING_LAZY.equals(templateLoadingStr))
128                 {
129                     this.templateCachingStyle = XSLTransform.CACHE_LAZY;
130                 }
131                 else if (VAL_TEMPLATE_CACHING_DISABLED.equals(templateLoadingStr))
132                 {
133                     this.templateCachingStyle = XSLTransform.CACHE_DISABLED;
134                 }
135             }
136
137             // Override the uri resolver?
138
String JavaDoc uriResolverStr = XML.getValue(factoryNode, ATTR_URI_RESOLVER);
139             if (uriResolverStr != null)
140             {
141                 try
142                 {
143                     ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
144                     if (classLoader == null)
145                     {
146                         classLoader = DefaultControllerFactory.class.getClassLoader();
147                     }
148                     Class JavaDoc resolverClass = classLoader.loadClass(uriResolverStr);
149                     this.uriResolver = (URIResolver JavaDoc)resolverClass.newInstance();
150                 }
151                 catch (ClassNotFoundException JavaDoc ex)
152                 {
153                     throw new ConfigException(ex);
154                 }
155                 catch (InstantiationException JavaDoc ex)
156                 {
157                     throw new ConfigException(ex);
158                 }
159                 catch (IllegalAccessException JavaDoc ex)
160                 {
161                     throw new ConfigException(ex);
162                 }
163             }
164         }
165     }
166
167     /**
168      * @param transformNode
169      * @return
170      * @exception ConfigException
171      */

172     public Transform createTransform(Element transformNode) throws ConfigException
173     {
174         String JavaDoc outputType = XML.getValue(transformNode, ATTR_FINAL_CONTENT_TYPE);
175         if (outputType == null)
176             outputType = this.defaultFinalContentType;
177
178         boolean isMonitored = false;
179         String JavaDoc monitor = XML.getValue(transformNode, ATTR_MONITOR);
180         if (monitor != null && "true".equals(monitor.toLowerCase()))
181             isMonitored = true;
182
183         String JavaDoc path = XML.getValue(transformNode, ATTR_PATH);
184         if (path == null)
185             throw new ConfigException("XSLT transform node must have a \""
186                                         + ATTR_PATH + "\" attribute: "
187                                         + XML.toString(transformNode));
188
189         return new XSLTransform(
190                         path,
191                         isMonitored,
192                         this.templateCachingStyle,
193                         this.servletCtx,
194                         outputType,
195                         this.uriResolver);
196     }
197 }
198
199
Popular Tags