1 29 30 package com.caucho.jstl.el; 31 32 import com.caucho.el.Expr; 33 import com.caucho.jsp.BodyContentImpl; 34 import com.caucho.jsp.PageContextImpl; 35 import com.caucho.util.L10N; 36 import com.caucho.vfs.Vfs; 37 import com.caucho.xml.Xml; 38 import com.caucho.xml.XmlParser; 39 40 import org.w3c.dom.Document ; 41 import org.xml.sax.InputSource ; 42 43 import javax.servlet.jsp.JspException ; 44 import javax.servlet.jsp.tagext.BodyTagSupport ; 45 import java.io.Reader ; 46 47 public class XmlParseTag extends BodyTagSupport { 48 private static L10N L = new L10N(XmlParseTag.class); 49 50 private Expr _xml; 51 private Expr _systemId; 52 private Expr _filter; 53 54 private String _var; 55 private String _scope; 56 57 private String _varDom; 58 private String _scopeDom; 59 60 63 public void setXml(Expr xml) 64 { 65 setDoc(xml); 66 } 67 68 71 public void setDoc(Expr xml) 72 { 73 _xml = xml; 74 } 75 76 79 public void setSystemId(Expr systemId) 80 { 81 _systemId = systemId; 82 } 83 84 87 public void setFilter(Expr filter) 88 { 89 _filter = filter; 90 } 91 92 95 public void setVar(String var) 96 { 97 _var = var; 98 } 99 100 103 public void setScope(String scope) 104 { 105 _scope = scope; 106 } 107 108 111 public void setVarDom(String var) 112 { 113 _varDom = var; 114 } 115 116 119 public void setScopeDom(String scope) 120 { 121 _scopeDom = scope; 122 } 123 124 public int doEndTag() throws JspException 125 { 126 try { 127 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 128 BodyContentImpl body = (BodyContentImpl) getBodyContent(); 129 130 Reader reader; 131 132 if (_xml != null) { 133 Object obj = _xml.evalObject(pageContext.getELContext()); 134 135 if (obj instanceof Reader ) 136 reader = (Reader ) obj; 137 else if (obj instanceof String ) 138 reader = Vfs.openString((String ) obj).getReader(); 139 else 140 throw new JspException (L.l("'doc' attribute must be a Reader or String at `{0}'", 141 obj)); 142 } 143 else if (body != null) 144 reader = body.getReader(); 145 else 146 throw new JspException (L.l("x:parse requires a body")); 147 148 InputSource is = new InputSource (reader); 149 150 XmlParser parser = new Xml(); 151 152 Document doc = parser.parseDocument(is); 153 154 reader.close(); 155 156 if (_var != null) 157 CoreSetTag.setValue(pageContext, _var, _scope, doc); 158 else if (_varDom != null) 159 CoreSetTag.setValue(pageContext, _varDom, _scopeDom, doc); 160 else 161 throw new JspException (L.l("x:parse needs either var or varDom")); 162 } catch (JspException e) { 163 throw e; 164 } catch (Exception e) { 165 throw new JspException (e); 166 } 167 168 return EVAL_PAGE; 169 } 170 } 171 | Popular Tags |