1 16 17 package org.apache.taglibs.xsl; 18 19 20 import java.io.InputStream ; 21 import java.io.Reader ; 22 import java.io.StringReader ; 23 import java.lang.reflect.Method ; 24 import javax.servlet.ServletContext ; 25 import javax.servlet.jsp.JspException ; 26 import javax.servlet.jsp.tagext.BodyTagSupport ; 27 import org.apache.xalan.xslt.XSLTInputSource; 28 import org.apache.xalan.xslt.XSLTProcessor; 29 import org.apache.xalan.xslt.XSLTProcessorFactory; 30 import org.apache.xalan.xslt.XSLTResultTarget; 31 import org.w3c.dom.Node ; 32 import org.xml.sax.InputSource ; 33 import org.xml.sax.SAXException ; 34 35 36 44 45 public class ApplyTag extends BodyTagSupport { 46 47 48 50 51 54 private String body = null; 55 56 57 60 private String nameXml = null; 61 62 public String getNameXml() { 63 return (this.nameXml); 64 } 65 66 public void setNameXml(String nameXml) { 67 this.nameXml = nameXml; 68 } 69 70 71 74 private String nameXsl = null; 75 76 public String getNameXsl() { 77 return (this.nameXsl); 78 } 79 80 public void setNameXsl(String nameXsl) { 81 this.nameXsl = nameXsl; 82 } 83 84 85 88 private String propertyXml = null; 89 90 public String getPropertyXml() { 91 return (this.propertyXml); 92 } 93 94 public void setPropertyXml(String propertyXml) { 95 this.propertyXml = propertyXml; 96 } 97 98 99 102 private String propertyXsl = null; 103 104 public String getPropertyXsl() { 105 return (this.propertyXsl); 106 } 107 108 public void setPropertyXsl(String propertyXsl) { 109 this.propertyXsl = propertyXsl; 110 } 111 112 113 116 private String xml = null; 117 118 public String getXml() { 119 return (this.xml); 120 } 121 122 public void setXml(String xml) { 123 this.xml = xml; 124 } 125 126 127 130 private String xsl = null; 131 132 public String getXsl() { 133 return (this.xsl); 134 } 135 136 public void setXsl(String xsl) { 137 this.xsl = xsl; 138 } 139 140 141 143 144 151 public int doStartTag() throws JspException { 152 153 if (nameXml != null) { 155 if (xml != null) 156 throw new JspException 157 ("Cannot specify both 'nameXml' and 'xml'"); 158 } else if (propertyXml != null) { 159 throw new JspException 160 ("Cannot specify 'propertyXml' without 'nameXml'"); 161 } 162 163 if (nameXsl != null) { 165 if (xsl != null) 166 throw new JspException 167 ("Cannot specify both 'nameXsl' and 'xsl'"); 168 } else if (propertyXsl != null) { 169 throw new JspException 170 ("Cannot specify 'propertyXsl' without 'nameXsl'"); 171 } 172 if ((nameXsl == null) && (xsl == null)) { 173 throw new JspException 174 ("Must specify either 'nameXsl' or 'xsl'"); 175 } 176 177 if ((nameXml == null) && (xml == null)) 179 return (EVAL_BODY_TAG); 180 else 181 return (SKIP_BODY); 182 183 } 184 185 186 191 public int doAfterBody() throws JspException { 192 193 if (bodyContent == null) 194 body = ""; 195 else 196 body = bodyContent.getString().trim(); 197 return (SKIP_BODY); 198 199 } 200 201 202 207 public int doEndTag() throws JspException { 208 209 XSLTInputSource data = null; 211 if (body != null) 212 data = new XSLTInputSource(new StringReader (body)); 213 else 214 data = getInputSource(nameXml, propertyXml, xml); 215 216 XSLTInputSource style = 218 getInputSource(nameXsl, propertyXsl, xsl); 219 220 XSLTResultTarget result = 222 new XSLTResultTarget(pageContext.getOut()); 223 224 XSLTProcessor processor = null; 226 try { 227 processor = XSLTProcessorFactory.getProcessor(); 228 processor.process(data, style, result); 229 } catch (SAXException e) { 230 throw new JspException (e.toString()); 231 } 232 return (EVAL_PAGE); 233 234 } 235 236 237 240 public void release() { 241 242 this.body = null; 243 244 } 245 246 247 249 250 263 private XSLTInputSource getInputSource(String name, String property, 264 String resource) 265 throws JspException { 266 267 268 if (resource != null) { 270 ServletContext context = pageContext.getServletContext(); 271 if (context == null) 272 throw new JspException ("Cannot find servlet context"); 273 InputStream stream = 274 context.getResourceAsStream(resource); 275 if (stream == null) 276 throw new JspException ("Missing resource '" + resource + "'"); 277 return new XSLTInputSource(stream); 278 } 279 280 Object source = null; 282 Object bean = pageContext.findAttribute(name); 283 if (bean == null) 284 throw new JspException ("Missing bean '" + name + "'"); 285 if (property == null) 286 source = bean; 287 else { 288 try { 289 char first = Character.toUpperCase(property.charAt(0)); 290 String methodName = "get" + first + property.substring(1); 291 Class paramTypes[] = new Class [0]; 292 Method method = 293 bean.getClass().getMethod(methodName, paramTypes); 294 source = method.invoke(bean, new Object [0]); 295 } catch (Exception e) { 296 throw new JspException (e.toString()); 297 } 298 } 299 300 301 if (source instanceof XSLTInputSource) 303 return ((XSLTInputSource) source); 304 else if (source instanceof String ) 305 return (new XSLTInputSource(new StringReader ((String ) source))); 306 else if (source instanceof InputSource) 307 return (new XSLTInputSource((InputSource) source)); 308 else if (source instanceof InputStream ) 309 return (new XSLTInputSource((InputStream ) source)); 310 else if (source instanceof Node ) 311 return (new XSLTInputSource((Node ) source)); 312 else if (source instanceof Reader ) 313 return (new XSLTInputSource((Reader ) source)); 314 else 315 throw new JspException ("Invalid input source type '" + 316 source.getClass().getName() + "'"); 317 318 } 319 320 321 } 322 | Popular Tags |