1 16 17 package org.apache.taglibs.xtags.xpath; 18 19 import java.io.IOException ; 20 import java.io.Reader ; 21 import java.io.StringReader ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import javax.servlet.ServletContext ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.jsp.JspException ; 30 import javax.servlet.jsp.JspWriter ; 31 import javax.servlet.jsp.PageContext ; 32 import javax.servlet.jsp.tagext.BodyContent ; 33 34 import org.dom4j.Document; 35 import org.dom4j.io.SAXReader; 36 37 import org.apache.taglibs.xtags.util.URLHelper; 38 39 import org.xml.sax.SAXException ; 40 41 45 public class ParseTag extends AbstractBodyTag { 46 47 48 private static final boolean TRACE = false; 49 50 51 private String id; 52 53 54 private URL url; 55 56 57 private Reader reader; 58 59 private Document document; 60 61 62 private boolean validate; 63 64 public ParseTag() { 65 } 66 67 public int doStartTag() throws JspException { 70 document = null; 71 if ( url != null ) { 72 try { 73 document = getSAXReader().read( url ); 75 defineVariable( document ); 76 } 77 catch (Exception e) { 78 handleException(e); 79 } 80 return SKIP_BODY; 81 } 82 return EVAL_BODY_TAG; 83 } 84 85 public int doAfterBody() throws JspException { 86 if ( document == null ) { 87 try { 88 if ( reader != null ) { 89 document = getSAXReader().read( reader ); 90 reader = null; 91 } 92 else { 93 BodyContent body = getBodyContent(); 94 String text = body.getString().trim(); 95 body.clearBody(); 96 document = getSAXReader().read( new StringReader (text) ); 97 } 98 defineVariable( document ); 99 } 100 catch (Exception e) { 101 handleException(e); 102 } 103 } 104 return SKIP_BODY; 105 } 106 107 public void release() { 108 super.release(); 109 id = null; 110 url = null; 111 reader = null; 112 document = null; 113 validate = false; 114 } 115 116 119 122 public String getId() { 123 return id; 124 } 125 126 129 public void setId(String id) { 130 this.id = id; 131 } 132 133 public void setURL( URL url ) { 134 this.url = url; 135 136 if ( TRACE ) { 137 logInfo( "Set URL to: " + url ); 138 } 139 } 140 141 public void setUrl( String url ) throws IOException { 142 if ( TRACE ) { 143 logInfo( "Setting absolute URL to: " + url ); 144 } 145 146 setURL( new URL ( url ) ); 147 } 148 149 public void setUri( String uri ) throws IOException { 150 setURL( URLHelper.getResourceURL( uri, pageContext ) ); 151 } 152 153 public void setReader(Reader reader) { 154 this.reader = reader; 155 } 156 157 public boolean getValidate() { 158 return validate; 159 } 160 161 public void setValidate(boolean validate) { 162 this.validate = validate; 163 } 164 165 166 protected SAXReader getSAXReader() throws SAXException { 169 try { 179 System.setProperty( "org.xml.sax.driver", null ); 180 } 181 catch (Throwable t) { 182 } 184 return new SAXReader(validate); 185 } 186 187 protected void defineVariable( Document document ) { 188 TagHelper.defineVariable( pageContext, getId(), document ); 189 } 190 } 191 | Popular Tags |