1 package com.icl.saxon.number; 2 import java.util.Vector ; 3 4 10 11 public class NumberFormatter { 12 13 private Numberer numberer; 14 private Vector formatTokens; 15 private Vector separators; 16 private boolean startsWithSeparator; 17 18 21 22 public void setNumberer(Numberer numberer) { 23 this.numberer = numberer; 24 } 25 26 41 42 public void prepare(String format) { 43 44 46 if (format.length()==0) format="1"; 47 48 formatTokens = new Vector (); 49 separators = new Vector (); 50 51 int len = format.length(); 52 int i=0; 53 int t=0; 54 boolean first = true; 55 startsWithSeparator = true; 56 57 while (i<len) { 58 char c = format.charAt(i); 59 t=i; 60 while (Character.isLetterOrDigit(c)) { 61 i++; 62 if (i==len) break; 63 c = format.charAt(i); 64 } 65 if (i>t) { 66 String tok = format.substring(t, i); 67 formatTokens.addElement(tok); 68 if (first) { 69 separators.addElement("."); 70 startsWithSeparator = false; 71 first = false; 72 } 73 } 74 if (i==len) break; 75 t=i; 76 c = format.charAt(i); 77 while (!Character.isLetterOrDigit(c)) { 78 first = false; 79 i++; 80 if (i==len) break; 81 c = format.charAt(i); 82 } 83 if (i>t) { 84 String sep = format.substring(t, i); 85 separators.addElement(sep); 86 } 87 } 88 89 } 90 91 96 97 public String format(Vector numbers, int groupSize, String groupSeparator, 98 String letterValue, Numberer numberer) { 99 100 StringBuffer sb = new StringBuffer (); 101 int num = 0; 102 int tok = 0; 103 if (startsWithSeparator) { 105 sb.append((String )separators.elementAt(tok)); 106 } 107 while (num<numbers.size()) { 109 if (num>0) { 110 sb.append((String )separators.elementAt(tok)); 111 } 112 int nr = ((Integer )numbers.elementAt(num++)).intValue(); 113 String s = numberer.format(nr, (String )formatTokens.elementAt(tok), 114 groupSize, groupSeparator, letterValue); 115 sb.append(s); 116 tok++; 117 if (tok==formatTokens.size()) tok--; 118 } 119 if (separators.size()>formatTokens.size()) { 121 sb.append((String )separators.elementAt(separators.size()-1)); 122 } 123 return sb.toString(); 124 } 125 126 130 131 public String format(int number, int groupSize, 132 String groupSeparator, String letterValue, Numberer numberer) { 133 Vector v = new Vector (); 134 v.addElement(new Integer (number)); 135 return format(v, groupSize, groupSeparator, letterValue, numberer); 136 } 137 138 } 139 140 | Popular Tags |