You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-dev@jakarta.apache.org by mo...@apache.org on 2001/02/09 02:09:09 UTC

cvs commit: jakarta-taglibs/jdbc/src/org/apache/taglibs/jdbc/resultset BaseGetterTag.java

morgand     01/02/08 17:09:09

  Modified:    jdbc/src/org/apache/taglibs/jdbc/resultset
                        BaseGetterTag.java
  Log:
  added support for Locales and the colName attribute
  
  Revision  Changes    Path
  1.3       +109 -8    jakarta-taglibs/jdbc/src/org/apache/taglibs/jdbc/resultset/BaseGetterTag.java
  
  Index: BaseGetterTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-taglibs/jdbc/src/org/apache/taglibs/jdbc/resultset/BaseGetterTag.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- BaseGetterTag.java	2001/01/31 00:56:51	1.2
  +++ BaseGetterTag.java	2001/02/09 01:09:08	1.3
  @@ -61,6 +61,7 @@
   import java.sql.ResultSet;
   import java.sql.ResultSetMetaData;
   import java.sql.SQLException;
  +import java.util.Locale;
   
   import javax.servlet.jsp.JspTagException;
   import javax.servlet.jsp.tagext.TagSupport;
  @@ -69,15 +70,26 @@
    * Base class for all the getter tags in the resultset package.
    * 
    * @author Morgan Delagrange
  + * @author Marius Scurtescu
    */
   public class BaseGetterTag extends TagSupport {
  -  
  -  protected int _position = 1;
  +
  +  // subclasses should use getPosition to access
  +  // the column via getString(int) etc.
  +  private int _position = -1;
  +  private String _name = null;
  +
     protected String _attributeName = null;
     protected String _scope = "page";
     protected ResultSetTag _tag = null;
     protected ResultSetMetaData _metaData = null;
  -  
  +  protected String _locale = null;
  +
  +  public void setLocale (String strLocale)
  +  {
  +    _locale = strLocale;
  +  }
  +
     /**
      * Sets the column number of the result set.
      * 
  @@ -85,9 +97,47 @@
      */
     public void setPosition(int position) {
       _position = position;
  +  }
  +
  +  /**
  +   * Gets the column number for the result set.
  +   *
  +   * @return column position (1,2,3,etc.)
  +   */
  +  public int getPosition() throws JspTagException{
  +    // if the column was set according to its name,
  +    // figure out its position
  +    if (_position == -1) {
  +      _position = getColumnNumber(_name);
  +    }
  +    return _position;
     }
  -  
  +
     /**
  +   * Sets the column number of the result set.
  +   *
  +   * @param position column index
  +   */
  +  public void setPosition(String strPosition) throws JspTagException
  +  {
  +    try {
  +      _position = Integer.parseInt (strPosition);
  +    } catch (NumberFormatException ex) {
  +      throw new JspTagException ("The 'position' attribute must be an int: " + ex.getMessage());
  +    }
  +  }
  +
  +  /**
  +   * Sets the column name if the result set.
  +   *
  +   * @param strName The column name.
  +   */
  +  public void setColName (String strName)
  +  {
  +    _name = strName;
  +  }
  +
  +  /**
      * Name of the attribute.
      * 
      * @param attributeName
  @@ -96,7 +146,7 @@
     public void setTo(String attributeName) {
       _attributeName = attributeName;
     }
  -  
  +
     /**
      * Scope of the attribute.
      * 
  @@ -105,7 +155,7 @@
     public void setScope(String scope) {
       _scope = scope;
     }
  -    
  +
     /**
      * Set an attribute to the specified scope.
      * 
  @@ -156,7 +206,7 @@
       }
       return _metaData;
     }
  -  
  +
     /**
      * Get the parent result set tag
      * 
  @@ -166,16 +216,67 @@
     protected ResultSetTag getResultSetTag() throws JspTagException{
       try {
         ResultSetTag rsetTag = 
  -        (ResultSetTag) findAncestorWithClass(this, Class.forName("org.apache.taglibs.jdbc.resultset.ResultSetTag"));
  +      (ResultSetTag) findAncestorWithClass(this, Class.forName("org.apache.taglibs.jdbc.resultset.ResultSetTag"));
         return rsetTag;
       } catch (ClassNotFoundException e) {
         throw new JspTagException(e.toString());
       }
     }
   
  +  // get the position for a named column
  +  // needed for the metadata methods
  +  private int getColumnNumber (String strName)
  +  throws JspTagException {
  +
  +    ResultSetMetaData meta = getMetaData();
  +
  +    try {
  +      int cntColumn = meta.getColumnCount ();
  +
  +      for (int i = 1; i <= cntColumn; i++) {
  +        if (strName.equals (meta.getColumnName (i))) {
  +          return i;
  +        }
  +      }
  +    } catch (SQLException e) {
  +      throw new JspTagException(e.toString());
  +    }
  +
  +    throw new JspTagException("Could not find column named " + strName);
  +
  +  }
  +
  +
  +  /**
  +   * Create a Locale for the formatting of dates, times, 
  +   * and numbers.
  +   * 
  +   * @param strLocale
  +   * @return the indicated Locale
  +   */
  +  protected Locale getLocale (String strLocale)
  +  {
  +    if (strLocale == null)
  +      return Locale.getDefault ();
  +
  +    int pos1 = strLocale.indexOf ("_");
  +
  +    if (pos1 == -1)
  +      return new Locale (strLocale, "");
  +
  +    String strLanguage = strLocale.substring (0, pos1);
  +    int pos2 = strLocale.indexOf ("_", pos1 + 1);
  +
  +    if (pos2 == -1)
  +      return new Locale (strLanguage, strLocale.substring (pos1 + 1));
  +
  +    return new Locale (strLanguage, strLocale.substring (pos1 + 1, pos2), strLocale.substring (pos2 + 1));
  +  }
  +
     public void release() {
       _position = 1;
       _attributeName = null;
  +    _name = null;
       _scope = "page";
       _tag = null;
       _metaData = null;