1 16 17 package org.apache.taglibs.standard.tag.common.xml; 18 19 import java.io.FileNotFoundException ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.Reader ; 23 import java.io.StringReader ; 24 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.jsp.JspException ; 27 import javax.servlet.jsp.JspTagException ; 28 import javax.servlet.jsp.PageContext ; 29 import javax.servlet.jsp.tagext.BodyTagSupport ; 30 import javax.xml.parsers.DocumentBuilder ; 31 import javax.xml.parsers.DocumentBuilderFactory ; 32 import javax.xml.parsers.ParserConfigurationException ; 33 import javax.xml.transform.TransformerConfigurationException ; 34 import javax.xml.transform.TransformerFactory ; 35 import javax.xml.transform.dom.DOMResult ; 36 import javax.xml.transform.sax.SAXTransformerFactory ; 37 import javax.xml.transform.sax.TransformerHandler ; 38 39 import org.apache.taglibs.standard.resources.Resources; 40 import org.apache.taglibs.standard.tag.common.core.ImportSupport; 41 import org.apache.taglibs.standard.tag.common.core.Util; 42 import org.w3c.dom.Document ; 43 import org.xml.sax.EntityResolver ; 44 import org.xml.sax.InputSource ; 45 import org.xml.sax.SAXException ; 46 import org.xml.sax.XMLFilter ; 47 import org.xml.sax.XMLReader ; 48 import org.xml.sax.helpers.XMLReaderFactory ; 49 50 55 public abstract class ParseSupport extends BodyTagSupport { 56 57 60 protected Object xml; protected String systemId; protected XMLFilter filter; 64 67 private String var; private String varDom; private int scope; private int scopeDom; 72 private DocumentBuilderFactory dbf; 74 private DocumentBuilder db; 75 private TransformerFactory tf; 76 private TransformerHandler th; 77 78 79 82 public ParseSupport() { 83 super(); 84 init(); 85 } 86 87 private void init() { 88 var = varDom = null; 89 xml = null; 90 systemId = null; 91 filter = null; 92 dbf = null; 93 db = null; 94 tf = null; 95 th = null; 96 scope = PageContext.PAGE_SCOPE; 97 scopeDom = PageContext.PAGE_SCOPE; 98 } 99 100 101 104 public int doEndTag() throws JspException { 106 try { 107 108 if (dbf == null) { 110 dbf = DocumentBuilderFactory.newInstance(); 111 dbf.setNamespaceAware(true); 112 dbf.setValidating(false); 113 } 114 db = dbf.newDocumentBuilder(); 115 116 if (filter != null) { 118 if (tf == null) 119 tf = TransformerFactory.newInstance(); 120 if (!tf.getFeature(SAXTransformerFactory.FEATURE)) 121 throw new JspTagException ( 122 Resources.getMessage("PARSE_NO_SAXTRANSFORMER")); 123 SAXTransformerFactory stf = (SAXTransformerFactory ) tf; 124 th = stf.newTransformerHandler(); 125 } 126 127 Document d; 129 Object xmlText = this.xml; 130 if (xmlText == null) { 131 if (bodyContent != null && bodyContent.getString() != null) 133 xmlText = bodyContent.getString().trim(); 134 else 135 xmlText = ""; 136 } 137 if (xmlText instanceof String ) 138 d = parseStringWithFilter((String ) xmlText, filter); 139 else if (xmlText instanceof Reader ) 140 d = parseReaderWithFilter((Reader ) xmlText, filter); 141 else 142 throw new JspTagException ( 143 Resources.getMessage("PARSE_INVALID_SOURCE")); 144 145 if (var != null) 148 pageContext.setAttribute(var, d, scope); 149 if (varDom != null) 150 pageContext.setAttribute(varDom, d, scopeDom); 151 152 return EVAL_PAGE; 153 } catch (SAXException ex) { 154 throw new JspException (ex); 155 } catch (IOException ex) { 156 throw new JspException (ex); 157 } catch (ParserConfigurationException ex) { 158 throw new JspException (ex); 159 } catch (TransformerConfigurationException ex) { 160 throw new JspException (ex); 161 } 162 } 163 164 public void release() { 166 init(); 167 } 168 169 170 173 174 private Document parseInputSourceWithFilter(InputSource s, XMLFilter f) 175 throws SAXException , IOException { 176 if (f != null) { 177 Document o = db.newDocument(); 179 180 th.setResult(new DOMResult (o)); 182 XMLReader xr = XMLReaderFactory.createXMLReader(); 183 xr.setEntityResolver(new JstlEntityResolver(pageContext)); 184 f.setParent(xr); 189 f.setContentHandler(th); 190 f.parse(s); 191 return o; 192 } else 193 return parseInputSource(s); 194 } 195 196 197 private Document parseReaderWithFilter(Reader r, XMLFilter f) 198 throws SAXException , IOException { 199 return parseInputSourceWithFilter(new InputSource (r), f); 200 } 201 202 203 private Document parseStringWithFilter(String s, XMLFilter f) 204 throws SAXException , IOException { 205 StringReader r = new StringReader (s); 206 return parseReaderWithFilter(r, f); 207 } 208 209 210 private Document parseURLWithFilter(String url, XMLFilter f) 211 throws SAXException , IOException { 212 return parseInputSourceWithFilter(new InputSource (url), f); 213 } 214 215 216 private Document parseInputSource(InputSource s) 217 throws SAXException , IOException { 218 db.setEntityResolver(new JstlEntityResolver(pageContext)); 219 220 if (systemId == null) 222 s.setSystemId("jstl:"); 223 else if (ImportSupport.isAbsoluteUrl(systemId)) 224 s.setSystemId(systemId); 225 else 226 s.setSystemId("jstl:" + systemId); 227 return db.parse(s); 228 } 229 230 231 private Document parseReader(Reader r) throws SAXException , IOException { 232 return parseInputSource(new InputSource (r)); 233 } 234 235 236 private Document parseString(String s) throws SAXException , IOException { 237 StringReader r = new StringReader (s); 238 return parseReader(r); 239 } 240 241 242 private Document parseURL(String url) throws SAXException , IOException { 243 return parseInputSource(new InputSource (url)); 244 } 245 246 249 250 public static class JstlEntityResolver implements EntityResolver { 251 private final PageContext ctx; 252 public JstlEntityResolver(PageContext ctx) { 253 this.ctx = ctx; 254 } 255 public InputSource resolveEntity(String publicId, String systemId) 256 throws FileNotFoundException { 257 258 if (systemId == null) 260 return null; 261 262 if (systemId.startsWith("jstl:")) 264 systemId = systemId.substring(5); 265 266 if (ImportSupport.isAbsoluteUrl(systemId)) 268 return null; 269 270 InputStream s; 274 if (systemId.startsWith("/")) { 275 s = ctx.getServletContext().getResourceAsStream(systemId); 276 if (s == null) 277 throw new FileNotFoundException ( 278 Resources.getMessage("UNABLE_TO_RESOLVE_ENTITY", 279 systemId)); 280 } else { 281 String pagePath = 282 ((HttpServletRequest ) ctx.getRequest()).getServletPath(); 283 String basePath = 284 pagePath.substring(0, pagePath.lastIndexOf("/")); 285 s = ctx.getServletContext().getResourceAsStream( 286 basePath + "/" + systemId); 287 if (s == null) 288 throw new FileNotFoundException ( 289 Resources.getMessage("UNABLE_TO_RESOLVE_ENTITY", 290 systemId)); 291 } 292 return new InputSource (s); 293 } 294 } 295 296 299 public void setVar(String var) { 300 this.var = var; 301 } 302 303 public void setVarDom(String varDom) { 304 this.varDom = varDom; 305 } 306 307 public void setScope(String scope) { 308 this.scope = Util.getScope(scope); 309 } 310 311 public void setScopeDom(String scopeDom) { 312 this.scopeDom = Util.getScope(scopeDom); 313 } 314 } 315 | Popular Tags |