1 29 30 package com.caucho.jstl.el; 31 32 import com.caucho.el.Expr; 33 import com.caucho.jsp.PageContextImpl; 34 import com.caucho.jstl.NameValueTag; 35 import com.caucho.log.Log; 36 import com.caucho.util.L10N; 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.el.ELContext; 44 import javax.el.ELException; 45 import javax.servlet.http.HttpServletRequest ; 46 import javax.servlet.jsp.JspException ; 47 import javax.servlet.jsp.JspWriter ; 48 import javax.servlet.jsp.tagext.BodyContent ; 49 import javax.servlet.jsp.tagext.BodyTagSupport ; 50 import javax.xml.transform.Result ; 51 import javax.xml.transform.Source ; 52 import javax.xml.transform.Transformer ; 53 import javax.xml.transform.TransformerFactory ; 54 import javax.xml.transform.dom.DOMResult ; 55 import javax.xml.transform.dom.DOMSource ; 56 import javax.xml.transform.stream.StreamResult ; 57 import javax.xml.transform.stream.StreamSource ; 58 import java.io.InputStream ; 59 import java.io.Reader ; 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 Expr _xml; 68 private Expr _xslt; 69 70 private Expr _xmlSystemId; 71 private Expr _xsltSystemId; 72 73 private String _var; 74 private String _scope; 75 76 private Expr _result; 77 78 private ArrayList <String > _paramNames = new ArrayList <String >(); 79 private ArrayList <String > _paramValues = new ArrayList <String >(); 80 81 84 public void setXml(Expr xml) 85 { 86 setDoc(xml); 87 } 88 89 92 public void setDoc(Expr xml) 93 { 94 _xml = xml; 95 } 96 97 100 public void setXslt(Expr xslt) 101 { 102 _xslt = xslt; 103 } 104 105 108 public void setXmlSystemId(Expr xmlSystemId) 109 { 110 _xmlSystemId = xmlSystemId; 111 } 112 113 116 public void setDocSystemId(Expr xmlSystemId) 117 { 118 _xmlSystemId = xmlSystemId; 119 } 120 121 124 public void setXsltSystemId(Expr 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(Expr 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 EVAL_BODY_BUFFERED; 172 } 173 174 177 public int doEndTag() 178 throws JspException 179 { 180 try { 181 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 182 ELContext env = pageContext.getELContext(); 183 184 JspWriter out = pageContext.getOut(); 185 186 TransformerFactory factory = TransformerFactory.newInstance(); 187 188 Source source = getSource(_xslt, _xsltSystemId); 189 190 Transformer transformer = factory.newTransformer(source); 191 192 for (int i = 0; i < _paramNames.size(); i++) { 193 String name = _paramNames.get(i); 194 String value = _paramValues.get(i); 195 196 transformer.setParameter(name, value); 197 } 198 199 if (_xml != null) 200 source = getSource(_xml, _xmlSystemId); 201 else { 202 BodyContent bodyContent = getBodyContent(); 203 204 source = new StreamSource (bodyContent.getReader()); 205 source.setSystemId(((HttpServletRequest ) pageContext.getRequest()).getRequestURI()); 206 } 207 208 Result result; 209 Node top = null; 210 211 if (_result != null) { 212 result = (Result ) _result.evalObject(env); 213 } 214 else if (_var != null) { 215 top = new com.caucho.xml.QDocument(); 216 217 result = new DOMResult (top); 218 } 219 else 220 result = new StreamResult (out); 221 222 transformer.transform(source, result); 223 224 if (_var != null) 225 CoreSetTag.setValue(pageContext, _var, _scope, top); 226 } catch (Exception e) { 227 throw new JspException (e); 228 } 229 230 return SKIP_BODY; 231 } 232 233 private Source getSource(Expr xmlExpr, Expr systemIdExpr) 234 throws JspException 235 { 236 try { 237 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 238 239 Object xmlObj = xmlExpr.evalObject(pageContext.getELContext()); 240 String systemId = null; 241 Source source = null; 242 243 if (systemIdExpr != null) 244 systemId = systemIdExpr.evalString(pageContext.getELContext()); 245 246 source = convertToSource(xmlObj, systemId); 247 248 return source; 249 } catch (ELException e) { 250 throw new JspException (e); 251 } 252 } 253 254 public static Source convertToSource(Object xmlObj, String systemId) 255 throws JspException 256 { 257 if (xmlObj instanceof String ) { 258 ReadStream is = Vfs.openString((String ) xmlObj); 259 260 return new StreamSource (is, systemId); 261 } 262 else if (xmlObj instanceof InputStream ) { 263 return new StreamSource ((InputStream ) xmlObj, systemId); 264 } 265 else if (xmlObj instanceof Reader ) { 266 return new StreamSource ((Reader ) xmlObj, systemId); 267 } 268 else if (xmlObj instanceof Node ) { 269 return new DOMSource ((Node ) xmlObj, systemId); 270 } 271 else if (xmlObj instanceof NodeList ) { 272 return new DOMSource (((NodeList ) xmlObj).item(0), systemId); 273 } 274 else if (xmlObj instanceof Source ) 275 return (Source ) xmlObj; 276 else if (xmlObj instanceof ArrayList ) { 277 ArrayList list = (ArrayList ) xmlObj; 278 279 if (list.size() > 0) 280 return convertToSource(list.get(0), systemId); 281 } 282 283 throw new JspException (L.l("unknown xml object type '{0}' '{1}'", 284 xmlObj, xmlObj.getClass())); 285 } 286 } 287 | Popular Tags |