KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > number > NumberFormatter


1 package com.icl.saxon.number;
2 import java.util.Vector JavaDoc;
3
4 /**
5   * Class NumberFormatter defines a method to format a Vector of integers as a character
6   * string according to a supplied format specification.
7   * @author <A HREF="mailto:mhkay@iclway.co.uk>Michael H. Kay</A>
8   * @version 17 November 1999
9   */

10
11 public class NumberFormatter {
12
13     private Numberer numberer;
14     private Vector JavaDoc formatTokens;
15     private Vector JavaDoc separators;
16     private boolean startsWithSeparator;
17
18     /**
19     * Allocate a numberer appropriate to the selected language
20     */

21
22     public void setNumberer(Numberer numberer) {
23         this.numberer = numberer;
24     }
25
26     /**
27     * Tokenize the format pattern.
28     * @param numbers the number to be formatted (a sequence of integer value)
29     * @param form the format specification. Contains one of the following values:<ul>
30     * <li>"1": conventional decimal numbering</li>
31     * <li>"a": sequence a, b, c, ... aa, ab, ac, ...</li>
32     * <li>"A": sequence A, B, C, ... AA, AB, AC, ...</li>
33     * <li>"i": sequence i, ii, iii, iv, v ...</li>
34     * <li>"I": sequence I, II, III, IV, V, ...</li>
35     * </ul>
36     * This symbol may be preceded and followed by punctuation (any other characters) which is
37     * copied to the output string.
38     * @return the formatted output string. Note that the fallback representation (e.g. for negative
39     * numbers in roman notation) is decimal.
40     */

41     
42     public void prepare(String JavaDoc format) {
43
44         // Tokenize the format string into alternating alphanumeric and non-alphanumeric tokens
45

46         if (format.length()==0) format="1";
47         
48         formatTokens = new Vector JavaDoc();
49         separators = new Vector JavaDoc();
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 JavaDoc 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 JavaDoc sep = format.substring(t, i);
85                 separators.addElement(sep);
86             }
87         }
88         
89     }
90
91     /**
92     * Format a vector of numbers.
93     * @param numbers the numbers to be formatted (a sequence of integer values)
94     * @return the formatted output string.
95     */

96     
97     public String JavaDoc format(Vector JavaDoc numbers, int groupSize, String JavaDoc groupSeparator,
98                         String JavaDoc letterValue, Numberer numberer) {
99        
100         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
101         int num = 0;
102         int tok = 0;
103         // output first punctuation token
104
if (startsWithSeparator) {
105             sb.append((String JavaDoc)separators.elementAt(tok));
106         }
107         // output the list of numbers
108
while (num<numbers.size()) {
109             if (num>0) {
110                 sb.append((String JavaDoc)separators.elementAt(tok));
111             }
112             int nr = ((Integer JavaDoc)numbers.elementAt(num++)).intValue();
113             String JavaDoc s = numberer.format(nr, (String JavaDoc)formatTokens.elementAt(tok),
114                          groupSize, groupSeparator, letterValue);
115             sb.append(s);
116             tok++;
117             if (tok==formatTokens.size()) tok--;
118         }
119         // output the final punctuation token
120
if (separators.size()>formatTokens.size()) {
121             sb.append((String JavaDoc)separators.elementAt(separators.size()-1));
122         }
123         return sb.toString();
124     }
125
126     /**
127     * Format a single number. The format/template has the same syntax as for a Vector
128     * of numbers.
129     */

130
131     public String JavaDoc format(int number, int groupSize,
132              String JavaDoc groupSeparator, String JavaDoc letterValue, Numberer numberer) {
133         Vector JavaDoc v = new Vector JavaDoc();
134         v.addElement(new Integer JavaDoc(number));
135         return format(v, groupSize, groupSeparator, letterValue, numberer);
136     }
137
138 }
139
140 //
141
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
142
// you may not use this file except in compliance with the License. You may obtain a copy of the
143
// License at http://www.mozilla.org/MPL/
144
//
145
// Software distributed under the License is distributed on an "AS IS" basis,
146
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
147
// See the License for the specific language governing rights and limitations under the License.
148
//
149
// The Original Code is: all this file.
150
//
151
// The Initial Developer of the Original Code is
152
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
153
//
154
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
155
//
156
// Contributor(s): none.
157
//
158
Popular Tags