1 16 17 package org.apache.taglibs.xtags.xslt; 18 19 import java.io.File ; 20 import java.io.FileReader ; 21 import java.io.FileWriter ; 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.io.Reader ; 26 import java.io.StringReader ; 27 import java.io.StringWriter ; 28 import java.io.Writer ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 35 import javax.servlet.ServletContext ; 36 import javax.servlet.jsp.JspException ; 37 import javax.servlet.jsp.tagext.BodyTagSupport ; 38 39 import javax.xml.transform.Result ; 40 import javax.xml.transform.Source ; 41 import javax.xml.transform.OutputKeys ; 42 import javax.xml.transform.Transformer ; 43 import javax.xml.transform.TransformerException ; 44 import javax.xml.transform.TransformerFactory ; 45 import javax.xml.transform.URIResolver ; 46 import javax.xml.transform.dom.DOMResult ; 47 import javax.xml.transform.dom.DOMSource ; 48 import javax.xml.transform.sax.SAXResult ; 49 import javax.xml.transform.sax.SAXSource ; 50 import javax.xml.transform.stream.StreamResult ; 51 import javax.xml.transform.stream.StreamSource ; 52 53 import org.dom4j.Node; 54 import org.dom4j.Document; 55 import org.dom4j.io.DocumentSource; 56 57 import org.apache.taglibs.xtags.util.JspNestedException; 58 import org.apache.taglibs.xtags.util.URLHelper; 59 60 import org.xml.sax.ContentHandler ; 61 import org.xml.sax.InputSource ; 62 import org.xml.sax.SAXException ; 63 64 68 public class StyleTag extends BodyTagSupport implements ParameterAcceptingTag { 69 70 71 private Object xml; 72 73 74 private Object xsl; 75 76 77 private Result result; 78 79 80 private String outputMethod; 81 82 83 private Map parameters; 84 85 86 private StringWriter stringWriter; 87 88 89 public void setParameter(String name, Object value) { 92 if ( parameters == null ) { 93 parameters = new HashMap (); 94 } 95 parameters.put( name, value ); 96 } 97 98 99 102 107 public int doStartTag() throws JspException { 108 parameters = null; 109 return EVAL_BODY_TAG; 110 } 111 112 113 117 public int doAfterBody() throws JspException { 118 if ( bodyContent != null ) { 119 String text = bodyContent.getString().trim(); 120 if ( text.length() > 0 ) { 121 if ( xsl == null ) { 122 xsl = new StringReader ( text ); 123 } 124 else if ( xml == null ) { 125 xml = new StringReader ( text ); 126 } 127 } 128 } 129 return SKIP_BODY; 130 } 131 132 133 137 public int doEndTag() throws JspException { 138 if ( xml == null || xsl == null ) { 139 throw new JspException ( "Must specify both XML and an XSLT to style" ); 140 } 141 142 Source data = getSource(xml); 144 145 Source style = getSource(xsl); 147 148 Result result = getResult(); 150 151 try { 153 TransformerFactory factory = TransformerFactory.newInstance(); 154 factory.setURIResolver( createURIResolver() ); 155 Transformer transformer = factory.newTransformer(style); 156 configure(transformer); 157 transformer.transform( data, result ); 158 if ( stringWriter != null ) { 159 pageContext.getOut().write( stringWriter.toString() ); 160 } 161 } 162 catch (TransformerException e) { 163 handleException(e); 164 } 165 catch (IOException e) { 166 handleException(e); 167 } 168 finally { 169 stringWriter = null; 170 } 171 return EVAL_PAGE; 172 } 173 174 175 178 public void release() { 179 xml = null; 180 xsl = null; 181 result = null; 182 parameters = null; 183 } 184 185 186 187 190 public void setOutputMethod(String outputMethod) { 191 this.outputMethod = outputMethod; 192 } 193 194 195 public void setDocument(Document document) { 196 this.xml = document; 197 } 198 199 200 public void setTransformer(Transformer transformer) { 201 this.xsl = transformer; 202 } 203 204 206 public void setResult(Result result) { 207 this.result = result; 208 } 209 210 212 public void setWriter(Writer writer) { 213 setResult( new StreamResult ( writer ) ); 214 } 215 216 217 public void setResultHandler(ContentHandler handler) { 218 setResult( new SAXResult ( handler ) ); 219 } 220 221 222 public void setXml(String xml) { 223 this.xml = xml; 224 } 225 226 227 public void setXmlSource(Source source) { 228 this.xml = source; 229 } 230 231 public void setXmlReader(Reader reader) { 232 this.xml = reader; 233 } 234 235 236 public void setXsl(String xsl) { 237 this.xsl = xsl; 238 } 239 240 public void setXslReader(Reader reader) { 241 this.xsl = reader; 242 } 243 244 245 public void setXslSource(Source source) { 246 this.xsl = source; 247 } 248 249 250 251 252 255 257 protected void configure(Transformer transformer) { 258 if ( outputMethod != null ) { 259 transformer.setOutputProperty( OutputKeys.METHOD, outputMethod ); 260 } 261 if ( parameters != null ) { 262 for ( Iterator iter = parameters.entrySet().iterator(); iter.hasNext(); ) { 263 Map.Entry entry = (Map.Entry ) iter.next(); 264 String name = entry.getKey().toString(); 265 Object value = entry.getValue(); 266 transformer.setParameter( name, value ); 267 } 268 } 269 } 270 271 276 protected Source getSource(Object source) throws JspException { 277 if (source instanceof Source ) { 278 return (Source ) source; 279 } 280 else if (source instanceof Reader ) { 281 return new StreamSource ((Reader ) source); 282 } 283 else if (source instanceof String ) { 284 try { 285 URL url = URLHelper.createURL( (String ) source, pageContext ); 286 if ( url == null ) { 287 throw new JspException ("Bad URL: " + source + "."); 288 } 289 return new StreamSource (url.openStream(), url.toExternalForm()); 290 } 291 catch (IOException e) { 292 throw new JspException ( 293 "Bad URL: " + source + ". Exception: " + e.getMessage() 294 ); 295 } 296 } 297 else if (source instanceof Node) { 298 return new DocumentSource((Node) source); 299 } 300 else if (source instanceof org.w3c.dom.Node ) { 301 return new DOMSource ((org.w3c.dom.Node ) source); 302 } 303 else if (source instanceof InputSource ) { 304 return new SAXSource ((InputSource ) source); 305 } 306 else if ( source == null ) { 307 throw new JspException ( "No XSLT source specified" ); 308 } 309 else { 310 throw new JspException ( 311 "Invalid input source type '" + source.getClass().getName() + "'" 312 ); 313 } 314 } 315 316 320 protected Result getResult() throws JspException { 321 if ( result == null ) { 322 stringWriter = new StringWriter (); 324 return new StreamResult ( stringWriter ); 325 } 326 else { 327 stringWriter = null; 328 } 329 return result; 330 } 331 332 333 protected URIResolver createURIResolver() { 334 return new URIResolver () { 335 public Source resolve(String href, String base) 336 throws TransformerException { 337 try { 338 return getSource(href); 339 } 340 catch(javax.servlet.jsp.JspException e) { 341 return null; 342 } 343 } 344 345 }; 346 } 347 348 350 protected void handleException( Exception e ) throws JspException { 351 if ( e instanceof JspException ) { 352 throw (JspException ) e; 353 } 354 else { 355 pageContext.getServletContext().log( e.getMessage(), e ); 356 e.printStackTrace(); 357 throw new JspNestedException( e ); 358 } 359 } 360 } 361 | Popular Tags |