1 16 17 package org.apache.taglibs.standard.tag.common.xml; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.Reader ; 22 import java.io.StringReader ; 23 import java.io.Writer ; 24 import java.util.List ; 25 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.jsp.JspException ; 28 import javax.servlet.jsp.JspTagException ; 29 import javax.servlet.jsp.PageContext ; 30 import javax.servlet.jsp.tagext.BodyTagSupport ; 31 import javax.xml.parsers.DocumentBuilder ; 32 import javax.xml.parsers.DocumentBuilderFactory ; 33 import javax.xml.parsers.ParserConfigurationException ; 34 import javax.xml.transform.Result ; 35 import javax.xml.transform.Source ; 36 import javax.xml.transform.Transformer ; 37 import javax.xml.transform.TransformerConfigurationException ; 38 import javax.xml.transform.TransformerException ; 39 import javax.xml.transform.TransformerFactory ; 40 import javax.xml.transform.URIResolver ; 41 import javax.xml.transform.dom.DOMResult ; 42 import javax.xml.transform.dom.DOMSource ; 43 import javax.xml.transform.sax.SAXSource ; 44 import javax.xml.transform.stream.StreamResult ; 45 import javax.xml.transform.stream.StreamSource ; 46 47 import org.apache.taglibs.standard.resources.Resources; 48 import org.apache.taglibs.standard.tag.common.core.ImportSupport; 49 import org.apache.taglibs.standard.tag.common.core.Util; 50 import org.w3c.dom.Document ; 51 import org.w3c.dom.Node ; 52 import org.xml.sax.InputSource ; 53 import org.xml.sax.SAXException ; 54 import org.xml.sax.XMLReader ; 55 import org.xml.sax.helpers.XMLReaderFactory ; 56 57 63 public abstract class TransformSupport extends BodyTagSupport { 64 65 68 protected Object xml; protected String xmlSystemId; protected Object xslt; protected String xsltSystemId; protected Result result; 74 77 private String var; private int scope; private Transformer t; private TransformerFactory tf; private DocumentBuilder db; private DocumentBuilderFactory dbf; 84 85 88 public TransformSupport() { 89 super(); 90 init(); 91 } 92 93 private void init() { 94 xml = xslt = null; 95 xmlSystemId = xsltSystemId = null; 96 var = null; 97 result = null; 98 tf = null; 99 scope = PageContext.PAGE_SCOPE; 100 } 101 102 103 106 public int doStartTag() throws JspException { 107 112 try { 113 114 117 if (dbf == null) { 119 dbf = DocumentBuilderFactory.newInstance(); 120 dbf.setNamespaceAware(true); 121 dbf.setValidating(false); 122 } 123 if (db == null) 124 db = dbf.newDocumentBuilder(); 125 126 if (tf == null) 128 tf = TransformerFactory.newInstance(); 129 130 133 Source s; 134 if (xslt != null) { 135 if (!(xslt instanceof String ) && !(xslt instanceof Reader ) 136 && !(xslt instanceof javax.xml.transform.Source )) 137 throw new JspTagException ( 138 Resources.getMessage("TRANSFORM_XSLT_UNRECOGNIZED")); 139 s = getSource(xslt, xsltSystemId); 140 } else { 141 throw new JspTagException ( 142 Resources.getMessage("TRANSFORM_NO_TRANSFORMER")); 143 } 144 tf.setURIResolver(new JstlUriResolver(pageContext)); 145 t = tf.newTransformer(s); 146 147 return EVAL_BODY_BUFFERED; 148 149 } catch (SAXException ex) { 150 throw new JspException (ex); 151 } catch (ParserConfigurationException ex) { 152 throw new JspException (ex); 153 } catch (IOException ex) { 154 throw new JspException (ex); 155 } catch (TransformerConfigurationException ex) { 156 throw new JspException (ex); 157 } 158 } 159 160 public int doEndTag() throws JspException { 163 try { 164 165 168 Object xml = this.xml; 170 if (xml == null) if (bodyContent != null && bodyContent.getString() != null) 172 xml = bodyContent.getString().trim(); 173 else 174 xml = ""; 175 176 Source source = getSource(xml, xmlSystemId); 178 179 182 if (result != null) 184 t.transform(source, result); 186 else if (var != null) { 187 Document d = db.newDocument(); 189 Result doc = new DOMResult (d); 190 t.transform(source, doc); 191 pageContext.setAttribute(var, d, scope); 192 } else { 193 Result page = 194 new StreamResult (new SafeWriter(pageContext.getOut())); 195 t.transform(source, page); 196 } 197 198 return EVAL_PAGE; 199 } catch (SAXException ex) { 200 throw new JspException (ex); 201 } catch (ParserConfigurationException ex) { 202 throw new JspException (ex); 203 } catch (IOException ex) { 204 throw new JspException (ex); 205 } catch (TransformerException ex) { 206 throw new JspException (ex); 207 } 208 } 209 210 public void release() { 212 init(); 213 } 214 215 216 219 220 public void addParameter(String name, Object value) { 221 t.setParameter(name, value); 222 } 223 224 225 228 233 private static String wrapSystemId(String systemId) { 234 if (systemId == null) 235 return "jstl:"; 236 else if (ImportSupport.isAbsoluteUrl(systemId)) 237 return systemId; 238 else 239 return ("jstl:" + systemId); 240 } 241 242 248 private Source getSource(Object o, String systemId) 249 throws SAXException , ParserConfigurationException , IOException { 250 if (o == null) 251 return null; 252 else if (o instanceof Source ) { 253 return (Source ) o; 254 } else if (o instanceof String ) { 255 return getSource(new StringReader ((String ) o), systemId); 257 } else if (o instanceof Reader ) { 258 XMLReader xr = XMLReaderFactory.createXMLReader(); 261 xr.setEntityResolver( 262 new ParseSupport.JstlEntityResolver(pageContext)); 263 InputSource s = new InputSource ((Reader ) o); 264 s.setSystemId(wrapSystemId(systemId)); 265 Source result = new SAXSource (xr, s); 266 result.setSystemId(wrapSystemId(systemId)); 267 return result; 268 } else if (o instanceof Node ) { 269 return new DOMSource ((Node ) o); 270 } else if (o instanceof List ) { 271 List l = (List ) o; 273 if (l.size() == 1) { 274 return getSource(l.get(0), systemId); } else { 276 throw new IllegalArgumentException ( 277 Resources.getMessage("TRANSFORM_SOURCE_INVALID_LIST")); 278 } 279 } else { 280 throw new IllegalArgumentException ( 281 Resources.getMessage("TRANSFORM_SOURCE_UNRECOGNIZED") 282 + o.getClass()); 283 } 284 } 285 286 287 290 public void setVar(String var) { 291 this.var = var; 292 } 293 294 public void setScope(String scope) { 295 this.scope = Util.getScope(scope); 296 } 297 298 299 302 307 private static class SafeWriter extends Writer { 308 private Writer w; 309 public SafeWriter(Writer w) { this.w = w; } 310 public void close() { } 311 public void flush() { } 312 public void write(char[] cbuf, int off, int len) throws IOException { 313 w.write(cbuf, off, len); 314 } 315 } 316 317 320 321 private static class JstlUriResolver implements URIResolver { 322 private final PageContext ctx; 323 public JstlUriResolver(PageContext ctx) { 324 this.ctx = ctx; 325 } 326 public Source resolve(String href, String base) 327 throws TransformerException { 328 329 if (href == null) 331 return null; 332 333 int index; 337 if (base != null && (index = base.indexOf("jstl:")) != -1) { 338 base = base.substring(index + 5); 339 } 340 341 if (ImportSupport.isAbsoluteUrl(href) 343 || (base != null && ImportSupport.isAbsoluteUrl(base))) 344 return null; 345 346 if (base == null || base.lastIndexOf("/") == -1) 348 base = ""; 349 else 350 base = base.substring(0, base.lastIndexOf("/") + 1); 351 352 String target = base + href; 354 355 InputStream s; 359 if (target.startsWith("/")) { 360 s = ctx.getServletContext().getResourceAsStream(target); 361 if (s == null) 362 throw new TransformerException ( 363 Resources.getMessage("UNABLE_TO_RESOLVE_ENTITY", 364 href)); 365 } else { 366 String pagePath = 367 ((HttpServletRequest ) ctx.getRequest()).getServletPath(); 368 String basePath = 369 pagePath.substring(0, pagePath.lastIndexOf("/")); 370 s = ctx.getServletContext().getResourceAsStream( 371 basePath + "/" + target); 372 if (s == null) 373 throw new TransformerException ( 374 Resources.getMessage("UNABLE_TO_RESOLVE_ENTITY", 375 href)); 376 } 377 return new StreamSource (s); 378 } 379 } 380 381 } 382 | Popular Tags |