1 2 17 package org.apache.taglibs.dbtags.resultset; 18 19 import java.sql.ResultSet ; 20 import java.sql.ResultSetMetaData ; 21 import java.sql.SQLException ; 22 import java.util.Locale ; 23 24 import javax.servlet.jsp.JspTagException ; 25 import javax.servlet.jsp.PageContext ; 26 import javax.servlet.jsp.tagext.TagSupport ; 27 28 34 public class BaseGetterTag extends TagSupport { 35 36 private int _position = -1; 39 private String _name = null; 40 41 protected String _attributeName = null; 42 protected String _scope = "page"; 43 protected ResultSetTag _tag = null; 44 protected ResultSetMetaData _metaData = null; 45 protected String _locale = null; 46 47 public void setLocale (String strLocale) 48 { 49 _locale = strLocale; 50 } 51 52 57 public void setPosition(int position) { 58 _position = position; 59 } 60 61 66 public int getPosition() throws JspTagException { 67 if (_position == -1) { 70 _position = getColumnNumber(_name); 71 } 72 return _position; 73 } 74 75 80 public void setPosition(String strPosition) throws JspTagException 81 { 82 try { 83 _position = Integer.parseInt (strPosition); 84 } catch (NumberFormatException ex) { 85 throw new JspTagException ("The 'position' attribute must be an int: " + ex.getMessage()); 86 } 87 } 88 89 94 public void setColName (String strName) 95 { 96 _name = strName; 97 } 98 99 105 public void setTo(String attributeName) { 106 _attributeName = attributeName; 107 } 108 109 114 public void setScope(String scope) { 115 _scope = scope; 116 } 117 118 125 protected void setAttribute(String name, Object object, String scope) { 126 if (scope.equals("request")) { 127 pageContext.setAttribute(name, object, PageContext.REQUEST_SCOPE); 128 } else if (scope.equals("application")) { 129 pageContext.setAttribute(name, object, PageContext.APPLICATION_SCOPE); 130 } else if (scope.equals("session")) { 131 pageContext.setAttribute(name, object, PageContext.SESSION_SCOPE); 132 } else { 133 pageContext.setAttribute(name, object); 134 } 135 } 136 137 142 protected ResultSet getResultSet() throws JspTagException { 143 if (_tag == null) { 144 _tag = getResultSetTag(); 145 } 146 return _tag.getResultSet(); 147 } 148 149 155 protected ResultSetMetaData getMetaData() throws JspTagException { 156 if (_metaData == null) { 157 158 if (_tag == null) { 159 _tag = getResultSetTag(); 160 } 161 162 try { 163 _metaData = _tag.getResultSet().getMetaData(); 164 } catch (SQLException e) { 165 throw new JspTagException (e.toString()); 166 } 167 } 168 return _metaData; 169 } 170 171 177 protected ResultSetTag getResultSetTag() throws JspTagException { 178 try { 179 ResultSetTag rsetTag = 180 (ResultSetTag) findAncestorWithClass(this, Class.forName("org.apache.taglibs.dbtags.resultset.ResultSetTag")); 181 return rsetTag; 182 } catch (ClassNotFoundException e) { 183 throw new JspTagException (e.toString()); 184 } 185 } 186 187 private int getColumnNumber (String strName) 190 throws JspTagException { 191 192 ResultSetMetaData meta = getMetaData(); 193 194 try { 195 int cntColumn = meta.getColumnCount (); 196 197 for (int i = 1; i <= cntColumn; i++) { 198 if (strName.equalsIgnoreCase (meta.getColumnName (i))) { 199 return i; 200 } 201 } 202 } catch (SQLException e) { 203 throw new JspTagException (e.toString()); 204 } 205 206 throw new JspTagException ("Could not find column named " + strName); 207 208 } 209 210 211 218 protected Locale getLocale (String strLocale) 219 { 220 if (strLocale == null) 221 return Locale.getDefault (); 222 223 int pos1 = strLocale.indexOf ("_"); 224 225 if (pos1 == -1) 226 return new Locale (strLocale, ""); 227 228 String strLanguage = strLocale.substring (0, pos1); 229 int pos2 = strLocale.indexOf ("_", pos1 + 1); 230 231 if (pos2 == -1) 232 return new Locale (strLanguage, strLocale.substring (pos1 + 1)); 233 234 return new Locale (strLanguage, strLocale.substring (pos1 + 1, pos2), strLocale.substring (pos2 + 1)); 235 } 236 237 238 245 public int doEndTag() { 246 release(); 247 return EVAL_PAGE; 248 } 249 250 public void release() { 251 _position = -1; 252 _attributeName = null; 253 _name = null; 254 _scope = "page"; 255 _tag = null; 256 _metaData = null; 257 _locale = null; 258 } 259 } 260 | Popular Tags |