1 16 package org.apache.taglibs.dbtags.resultset; 17 18 import java.io.IOException ; 19 import java.sql.ResultSet ; 20 import java.sql.SQLException ; 21 import java.sql.Statement ; 22 23 import javax.servlet.jsp.JspTagException ; 24 import javax.servlet.jsp.PageContext ; 25 import javax.servlet.jsp.tagext.BodyTagSupport ; 26 27 import org.apache.taglibs.dbtags.statement.StatementTag; 28 29 30 81 public class ResultSetTag extends BodyTagSupport { 82 83 private Statement _statement = null; 84 private ResultSet _rset = null; 85 private boolean _shouldLoop = true; 86 private String _name = null; 87 private String _scope = null; 88 private int _rowCount = 0; 89 90 104 protected Object lookup(PageContext pageContext, 105 String name, String scope) throws JspTagException { 106 107 Object bean = null; 108 if (scope == null) 109 bean = pageContext.findAttribute(name); 110 else if (scope.equalsIgnoreCase("page")) 111 bean = pageContext.getAttribute(name, PageContext.PAGE_SCOPE); 112 else if (scope.equalsIgnoreCase("request")) 113 bean = pageContext.getAttribute(name, PageContext.REQUEST_SCOPE); 114 else if (scope.equalsIgnoreCase("session")) 115 bean = pageContext.getAttribute(name, PageContext.SESSION_SCOPE); 116 else if (scope.equalsIgnoreCase("application")) 117 bean = 118 pageContext.getAttribute(name, PageContext.APPLICATION_SCOPE); 119 else { 120 JspTagException e = new JspTagException ("Invalid scope " + scope); 121 throw e; 122 } 123 return (bean); 124 125 } 126 127 130 131 132 public void setName(String name) { 133 _name = name; 134 } 135 136 public void setScope(String scope) { 137 _scope = scope; 138 } 139 140 public void setLoop(boolean shouldLoop) { 141 _shouldLoop = shouldLoop; 142 } 143 144 public ResultSet getResultSet() { 145 return _rset; 146 } 147 148 public int doStartTag() throws JspTagException { 149 150 super.bodyContent = null; 153 154 try { 155 if (_name!=null) { 158 _rset = (ResultSet ) lookup(pageContext, _name, _scope); 159 } 160 else { 161 StatementTag _stmtTag = 162 (StatementTag) findAncestorWithClass(this, 163 Class.forName("org.apache.taglibs.dbtags.statement.StatementTag")); 164 _rset = _stmtTag.executeQuery(); 165 } 166 167 pageContext.setAttribute(getId(), _rset); 168 169 if (_shouldLoop == false) { 170 return EVAL_BODY_TAG; 171 } 172 173 if (_rset.next() == false) { 174 setTotalRowCount(_rowCount); 175 return SKIP_BODY; 176 } 177 178 ++_rowCount; 179 setTotalRowCount(_rowCount); 180 181 } catch (ClassNotFoundException e) { 182 throw new JspTagException (e.toString()); 183 } catch (SQLException e) { 184 throw new JspTagException (e.toString()); 185 } 186 187 return EVAL_BODY_TAG; 188 } 189 190 public int doEndTag() throws JspTagException { 191 pageContext.removeAttribute(getId()); 192 try { 193 if (getBodyContent() != null && getPreviousOut() != null) { 194 getPreviousOut().write(getBodyContent().getString()); 195 } 196 } catch (IOException e) { 197 throw new JspTagException (e.toString()); 198 } finally { 199 try { 200 _rset.close(); 201 } 202 catch (SQLException e) { 203 e.printStackTrace(); 205 } 206 } 207 208 release(); 211 212 return EVAL_PAGE; 213 } 214 215 public int doAfterBody() throws JspTagException { 216 217 if (_shouldLoop == false) { 218 return EVAL_PAGE; 219 } 220 221 try { 222 if (_rset.next() == true) { 223 ++_rowCount; 224 setTotalRowCount(_rowCount); 225 return EVAL_BODY_TAG; 226 } 227 } catch (SQLException e) { 228 throw new JspTagException (e.toString()); 229 } 230 231 setTotalRowCount(_rowCount); 232 return EVAL_PAGE; 233 } 234 235 public void release() { 236 _statement = null; 237 _rset = null; 238 _shouldLoop = true; 239 _name = null; 240 _scope = null; 241 _rowCount = 0; 242 } 243 244 protected void setTotalRowCount(int rowCount) { 245 pageContext.setAttribute("org.apache.taglibs.dbtags.resultset.rowcount", 246 new Integer (rowCount)); 247 } 248 249 } 250 | Popular Tags |