1 28 29 package com.caucho.jstl.rt; 30 31 import com.caucho.jsp.PageContextImpl; 32 import com.caucho.jstl.NameValueTag; 33 import com.caucho.log.Log; 34 import com.caucho.server.webapp.WebApp; 35 import com.caucho.util.L10N; 36 import com.caucho.vfs.Path; 37 import com.caucho.vfs.ReadStream; 38 import com.caucho.vfs.Vfs; 39 40 import org.w3c.dom.Node ; 41 import org.w3c.dom.NodeList ; 42 43 import javax.servlet.http.HttpServletRequest ; 44 import javax.servlet.jsp.JspException ; 45 import javax.servlet.jsp.JspWriter ; 46 import javax.servlet.jsp.tagext.BodyContent ; 47 import javax.servlet.jsp.tagext.BodyTagSupport ; 48 import javax.xml.transform.Result ; 49 import javax.xml.transform.Source ; 50 import javax.xml.transform.Transformer ; 51 import javax.xml.transform.TransformerFactory ; 52 import javax.xml.transform.dom.DOMResult ; 53 import javax.xml.transform.dom.DOMSource ; 54 import javax.xml.transform.stream.StreamResult ; 55 import javax.xml.transform.stream.StreamSource ; 56 import java.io.IOException ; 57 import java.io.InputStream ; 58 import java.io.Reader ; 59 import java.io.Writer ; 60 import java.util.ArrayList ; 61 import java.util.logging.Logger ; 62 63 public class XmlTransformTag extends BodyTagSupport implements NameValueTag { 64 private static final Logger log = Log.open(XmlTransformTag.class); 65 private static final L10N L = new L10N(XmlTransformTag.class); 66 67 private Object _xml; 68 private Object _xslt; 69 70 private String _xmlSystemId; 71 private String _xsltSystemId; 72 73 private String _var; 74 private String _scope; 75 76 private Object _result; 77 78 private ArrayList <String > _paramNames = new ArrayList <String >(); 79 private ArrayList <String > _paramValues = new ArrayList <String >(); 80 81 84 public void setDoc(Object xml) 85 { 86 _xml = xml; 87 } 88 89 92 public void setXml(Object xml) 93 { 94 setDoc(xml); 95 } 96 97 100 public void setXslt(Object xslt) 101 { 102 _xslt = xslt; 103 } 104 105 108 public void setXmlSystemId(String xmlSystemId) 109 { 110 _xmlSystemId = xmlSystemId; 111 } 112 113 116 public void setDocSystemId(String docSystemId) 117 { 118 _xmlSystemId = docSystemId; 119 } 120 121 124 public void setXsltSystemId(String xsltSystemId) 125 { 126 _xsltSystemId = xsltSystemId; 127 } 128 129 132 public void setVar(String var) 133 { 134 _var = var; 135 } 136 137 140 public void setScope(String scope) 141 { 142 _scope = scope; 143 } 144 145 148 public void setResult(Object result) 149 { 150 _result = result; 151 } 152 153 156 public void addParam(String name, String value) 157 { 158 _paramNames.add(name); 159 _paramValues.add(value); 160 } 161 162 165 public int doStartTag() 166 throws JspException 167 { 168 _paramNames.clear(); 169 _paramValues.clear(); 170 171 return super.doStartTag(); 172 } 173 174 177 public int doEndTag() 178 throws JspException 179 { 180 try { 181 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 182 183 JspWriter out = pageContext.getOut(); 184 185 TransformerFactory factory = TransformerFactory.newInstance(); 186 187 String xsltSystemId = getCanonicalURL(pageContext, _xsltSystemId); 188 189 Source source = getSource(_xslt, xsltSystemId); 190 191 Transformer transformer = factory.newTransformer(source); 192 193 for (int i = 0; i < _paramNames.size(); i++) { 194 String name = _paramNames.get(i); 195 String value = _paramValues.get(i); 196 197 transformer.setParameter(name, value); 198 } 199 200 if (_xml != null) 201 source = getSource(_xml, getCanonicalURL(pageContext, _xmlSystemId)); 202 else { 203 BodyContent bodyContent = getBodyContent(); 204 205 source = new StreamSource (bodyContent.getReader()); 206 207 if (_xmlSystemId != null) 208 source.setSystemId(getCanonicalURL(pageContext, _xmlSystemId)); 209 else 210 source.setSystemId(((HttpServletRequest ) pageContext.getRequest()).getRequestURI()); 211 } 212 213 Result result; 214 Node top = null; 215 216 if (_result != null) { 217 result = (Result ) _result; 218 } 219 else if (_var != null) { 220 top = new com.caucho.xml.QDocument(); 221 222 result = new DOMResult (top); 223 } 224 else { 225 result = new StreamResult (new WriterWrapper(out)); 226 } 227 228 transformer.transform(source, result); 229 230 if (_var != null) 231 CoreSetTag.setValue(pageContext, _var, _scope, top); 232 } catch (Exception e) { 233 throw new JspException (e); 234 } 235 236 return SKIP_BODY; 237 } 238 239 242 private Source getSource(Object xml, String systemId) 243 throws JspException 244 { 245 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 246 247 Object xmlObj = xml; 248 Source source = null; 249 250 if (xmlObj instanceof String ) { 251 ReadStream is = Vfs.openString((String ) xmlObj); 252 253 source = new StreamSource (is, systemId); 254 } 255 else if (xmlObj instanceof InputStream ) { 256 source = new StreamSource ((InputStream ) xmlObj, systemId); 257 } 258 else if (xmlObj instanceof Reader ) { 259 source = new StreamSource ((Reader ) xmlObj, systemId); 260 } 261 else if (xmlObj instanceof Node ) { 262 source = new DOMSource ((Node ) xmlObj, systemId); 263 } 264 else if (xmlObj instanceof NodeList ) { 265 source = new DOMSource (((NodeList ) xmlObj).item(0), systemId); 266 } 267 else if (xmlObj instanceof Source ) 268 source = (Source ) xmlObj; 269 else 270 throw new JspException (L.l("unknown xml object type `{0}'", xmlObj)); 271 272 return source; 273 } 274 275 private String getCanonicalURL(PageContextImpl pageContext, String url) 276 { 277 WebApp app = pageContext.getApplication(); 278 Path appDir = pageContext.getApplication().getAppDir(); 279 280 if (url != null) { 281 if (url.startsWith("/")) 282 url = app.getRealPath(url); 283 else if (url.indexOf(':') > 0 && 284 url.indexOf(':') < url.indexOf('/')) { 285 } 286 else { 287 url = pageContext.getRequest().getRealPath(url); 288 } 289 290 url = appDir.lookup(url).getURL(); 291 } 292 293 return url; 294 } 295 296 static class WriterWrapper extends Writer { 297 private JspWriter _out; 298 299 WriterWrapper(JspWriter out) 300 { 301 _out = out; 302 } 303 304 public void write(char []buf, int off, int len) 305 throws IOException 306 { 307 _out.write(buf, off, len); 308 } 309 310 public void write(int ch) 311 throws IOException 312 { 313 _out.write(ch); 314 } 315 316 public void write(String s) 317 throws IOException 318 { 319 _out.write(s); 320 } 321 322 public void write(String s, int off, int len) 323 throws IOException 324 { 325 _out.write(s, off, len); 326 } 327 328 public void flush() 329 { 330 } 331 332 public void close() 333 { 334 } 335 } 336 } 337 | Popular Tags |