1 25 26 29 package net.killingar.webwork.view.taglib; 30 31 import javax.servlet.jsp.JspException ; 32 import javax.servlet.jsp.JspTagException ; 33 34 public class SizeTag extends webwork.view.taglib.WebWorkTagSupport 35 { 36 protected String valueAttr; 38 39 public void setValue(String inName) 41 { 42 valueAttr = inName; 43 } 44 45 public int doStartTag() throws JspException 47 { 48 Object value = findValue(valueAttr); 49 50 try 51 { 52 if (value != null && value instanceof Number ) 53 { 54 double n = ((Number )value).longValue(); 55 56 String [] suffixes = {"B", "KB", "MB", "GB", "TB", "PB", "EB"}; 57 int suffix = 0; 58 59 for (int i = 0; i < suffixes.length; i++) 60 { 61 if (n >= 1024) 62 { 63 suffix++; 64 n /= 1024; 65 } 66 else 67 break; 68 } 69 String ns; 70 if (suffix == 0) 71 ns = Long.toString((long)n); 72 else 73 { 74 ns = Double.toString(n); 75 int foo = ns.indexOf("."); 76 if (foo < ns.length()-4) 77 ns = ns.substring(0, foo+3); 78 } 79 80 81 82 StringBuffer s = new StringBuffer (); 83 s.append(ns); 84 s.append(" "); 85 s.append(suffixes[suffix]); 86 87 pageContext.getOut().write(s.toString()); 88 } 89 else 90 pageContext.getOut().write(""); } 92 catch (Exception e) 93 { 94 e.printStackTrace(); 95 throw new JspTagException ("Could not show value " + valueAttr + ":" + e); 96 } 97 98 return SKIP_BODY; 99 } 100 } | Popular Tags |