Hi!

A request has been made to strengthen the format attribute to make it
similar to the Unix sprintf utility.  Any objections?

Basically, default formating would remain the same and it would allow more
robust formatting with just the added complexity of being more type specific
with the attribute.

For example, format="s--s" could be "%s is about %.2f" where your float (ie
%.2f) would be truncated to two decimals.

I'm not sure if anyone has any cases where a truncated value would get
stored in thier database but that would seem to be the only thing to look
out for.

For more info, please see the Unix man page or
http://developer.java.sun.com/developer/technicalArticles/Programming/sprint
f/

 Please ask me for the source code or do the following to test the code:

1) download the sprintf class from the link above
2) In EmbeddedData:

[add import statement for PrintfFormat]
 protected String formatted;//changed to String from ArrayList

  /*new format method added*/
        public String format(Object[] o){
            formatted =  (new PrintfFormat(format).sprintf(o));
            return formatted;
        }


 /*previous method gets removed
*/

3) In tableData and queryData's fetchData method:
.....
ps.close(); // #JP Jun 27, 2001

  Vector result = new Vector();
  //until here is the same, from the next if clause to the end is changed.

  if (format != null) {


                 for (int i = 0; i < rsv.size(); i++) {
                 String[] currentRow = (String[])rsv.elementAt(i);
                 String htKey = currentRow[0];
                 rsv.setPointer(i);
                 Object[] objs = rsv.getCurrentRowAsObjects();



                Object[] objs2 = new Object[objs.length -1];
                        for (int j = 0; j < objs2.length; j++) {


                            if ((objs[j] instanceof String) || (objs[j]
instanceof Byte)
                               || (objs[j] instanceof java.lang.Integer) ||
(objs[j] instanceof Short)
                              || (objs[j] instanceof Float) || (objs[j]
instanceof Long)
                              || (objs[j] instanceof
Double)){objs2[j]=objs[(j+1)];}
                            else {objs2[j]=objs[(j+1)].toString();}

                        }



                    String htValue=format((objs2)); //use PrintfFormat class
via format method in EmbeddedData


                    result.addElement(new KeyValuePair(htKey, htValue));
                    }
                }
                else {
                    for (int i = 0; i < rsv.size(); i++) {
   String[] currentRow = (String[]) rsv.elementAt(i);

   String htKey = currentRow[0];
   StringBuffer htValueBuf = new StringBuffer();
   for (int j = 1; j < currentRow.length; j++) {
    htValueBuf.append(currentRow[j]);
    /*
     *this whole else clause is for default formating
                                 and is the same as the original with code
for dealing with
                                 formatting from the format attribute taken
out
     **/

     if (j < currentRow.length - 1)
      htValueBuf.append(", ");

   }
   String htValue = htValueBuf.toString();//

   result.addElement(new KeyValuePair(htKey, htValue));
   // add current row, now well formatted, to result
  }

         }
  return result;

}
 }
//that's all -- Shawn



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
DbForms Mailing List

http://www.wap-force.net/dbforms

Reply via email to