1 5 package org.infohazard.maverick.transform; 6 7 import javax.servlet.*; 8 import javax.xml.transform.URIResolver ; 9 10 import org.infohazard.maverick.flow.*; 11 import org.infohazard.maverick.util.XML; 12 import org.jdom.Element; 13 14 58 public class XSLTransformFactory implements TransformFactory 59 { 60 61 protected final static String ATTR_DEFAULT_FINAL_CONTENT_TYPE = "default-output-type"; 62 63 protected final static String ATTR_TEMPLATE_CACHING = "template-caching"; 64 65 protected final static String VAL_TEMPLATE_CACHING_LAZY = "lazy"; 66 67 protected final static String VAL_TEMPLATE_CACHING_PRELOAD = "preload"; 68 69 protected final static String VAL_TEMPLATE_CACHING_DISABLED = "disabled"; 70 71 protected final static String ATTR_URI_RESOLVER = "uri-resolver"; 72 73 74 protected final static String ATTR_PATH = "path"; 75 76 protected final static String ATTR_MONITOR = "monitor"; 77 78 protected final static String ATTR_FINAL_CONTENT_TYPE = "output-type"; 79 80 84 protected final static String DEFAULT_DEFAULT_FINAL_CONTENT_TYPE = "text/html"; 85 86 90 protected String defaultFinalContentType = DEFAULT_DEFAULT_FINAL_CONTENT_TYPE; 91 92 93 protected int templateCachingStyle = XSLTransform.CACHE_PRELOAD; 94 95 96 protected URIResolver uriResolver = null; 97 98 99 protected ServletContext servletCtx; 100 101 106 public void init(Element factoryNode, ServletConfig servletCfg) throws ConfigException 107 { 108 this.servletCtx = servletCfg.getServletContext(); 109 110 if (factoryNode != null) 111 { 112 String outputTypeStr = XML.getValue(factoryNode, ATTR_DEFAULT_FINAL_CONTENT_TYPE); 114 if (outputTypeStr != null) 115 this.defaultFinalContentType = outputTypeStr; 116 117 String 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 String uriResolverStr = XML.getValue(factoryNode, ATTR_URI_RESOLVER); 139 if (uriResolverStr != null) 140 { 141 try 142 { 143 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 144 if (classLoader == null) 145 { 146 classLoader = DefaultControllerFactory.class.getClassLoader(); 147 } 148 Class resolverClass = classLoader.loadClass(uriResolverStr); 149 this.uriResolver = (URIResolver )resolverClass.newInstance(); 150 } 151 catch (ClassNotFoundException ex) 152 { 153 throw new ConfigException(ex); 154 } 155 catch (InstantiationException ex) 156 { 157 throw new ConfigException(ex); 158 } 159 catch (IllegalAccessException ex) 160 { 161 throw new ConfigException(ex); 162 } 163 } 164 } 165 } 166 167 172 public Transform createTransform(Element transformNode) throws ConfigException 173 { 174 String outputType = XML.getValue(transformNode, ATTR_FINAL_CONTENT_TYPE); 175 if (outputType == null) 176 outputType = this.defaultFinalContentType; 177 178 boolean isMonitored = false; 179 String monitor = XML.getValue(transformNode, ATTR_MONITOR); 180 if (monitor != null && "true".equals(monitor.toLowerCase())) 181 isMonitored = true; 182 183 String 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 |