1 29 30 package com.caucho.jstl.rt; 31 32 import com.caucho.jsp.BodyContentImpl; 33 import com.caucho.jsp.PageContextImpl; 34 import com.caucho.util.L10N; 35 import com.caucho.vfs.Vfs; 36 import com.caucho.xml.Xml; 37 import com.caucho.xml.XmlParser; 38 39 import org.w3c.dom.Document ; 40 import org.xml.sax.InputSource ; 41 42 import javax.servlet.jsp.JspException ; 43 import javax.servlet.jsp.tagext.BodyTagSupport ; 44 import java.io.Reader ; 45 46 public class XmlParseTag extends BodyTagSupport { 47 private static L10N L = new L10N(XmlParseTag.class); 48 49 private Object _xml; 50 private String _systemId; 51 private Object _filter; 52 53 private String _var; 54 private String _scope; 55 56 private String _varDom; 57 private String _scopeDom; 58 59 62 public void setXml(Object xml) 63 { 64 _xml = xml; 65 } 66 67 70 public void setDoc(Object xml) 71 { 72 setXml(xml); 73 } 74 75 78 public void setSystemId(String systemId) 79 { 80 _systemId = systemId; 81 } 82 83 86 public void setFilter(Object filter) 87 { 88 _filter = filter; 89 } 90 91 94 public void setVar(String var) 95 { 96 _var = var; 97 } 98 99 102 public void setScope(String scope) 103 { 104 _scope = scope; 105 } 106 107 110 public void setVarDom(String var) 111 { 112 _varDom = var; 113 } 114 115 118 public void setScopeDom(String scope) 119 { 120 _scopeDom = scope; 121 } 122 123 public int doEndTag() throws JspException 124 { 125 try { 126 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 127 BodyContentImpl body = (BodyContentImpl) getBodyContent(); 128 129 Reader reader; 130 131 if (_xml != null) { 132 Object obj = _xml; 133 134 if (obj instanceof Reader ) 135 reader = (Reader ) obj; 136 else if (obj instanceof String ) 137 reader = Vfs.openString((String ) obj).getReader(); 138 else 139 throw new JspException (L.l("xml must be Reader or String at `{0}'", 140 obj)); 141 } 142 else if (body != null) 143 reader = body.getReader(); 144 else 145 throw new JspException (L.l("doc attribute must be a Reader or String at `{0}'", 146 null)); 147 148 XmlParser parser = new Xml(); 149 150 InputSource is = new InputSource (reader); 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 |