KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > number > NumberFormatter


1 package net.sf.saxon.number;
2 import net.sf.saxon.om.FastStringBuffer;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.List JavaDoc;
7
8 /**
9   * Class NumberFormatter defines a method to format a ArrayList of integers as a character
10   * string according to a supplied format specification.
11   * @author Michael H. Kay
12   */

13
14 public class NumberFormatter implements Serializable JavaDoc {
15
16     private ArrayList JavaDoc formatTokens;
17     private ArrayList JavaDoc punctuationTokens;
18     private boolean startsWithPunctuation;
19
20     /**
21     * Tokenize the format pattern.
22     * @param format the format specification. Contains one of the following values:<ul>
23     * <li>"1": conventional decimal numbering</li>
24     * <li>"a": sequence a, b, c, ... aa, ab, ac, ...</li>
25     * <li>"A": sequence A, B, C, ... AA, AB, AC, ...</li>
26     * <li>"i": sequence i, ii, iii, iv, v ...</li>
27     * <li>"I": sequence I, II, III, IV, V, ...</li>
28     * </ul>
29     * This symbol may be preceded and followed by punctuation (any other characters) which is
30     * copied to the output string.
31     */

32
33     public void prepare(String JavaDoc format) {
34
35         // Tokenize the format string into alternating alphanumeric and non-alphanumeric tokens
36

37         if (format.length()==0) format="1";
38
39         formatTokens = new ArrayList JavaDoc(10);
40         punctuationTokens = new ArrayList JavaDoc(10);
41
42         int len = format.length();
43         int i=0;
44         int t;
45         boolean first = true;
46         startsWithPunctuation = true;
47
48         while (i<len) {
49             char c = format.charAt(i);
50             t=i;
51             while (Character.isLetterOrDigit(c)) {
52                 i++;
53                 if (i==len) break;
54                 c = format.charAt(i);
55             }
56             if (i>t) {
57                 String JavaDoc tok = format.substring(t, i);
58                 formatTokens.add(tok);
59                 if (first) {
60                     punctuationTokens.add(".");
61                     startsWithPunctuation = false;
62                     first = false;
63                 }
64             }
65             if (i==len) break;
66             t=i;
67             c = format.charAt(i);
68             while (!Character.isLetterOrDigit(c)) {
69                 first = false;
70                 i++;
71                 if (i==len) break;
72                 c = format.charAt(i);
73             }
74             if (i>t) {
75                 String JavaDoc sep = format.substring(t, i);
76                 punctuationTokens.add(sep);
77             }
78         }
79
80         if (formatTokens.size() == 0) {
81             formatTokens.add("1");
82             if (punctuationTokens.size() == 1) {
83                 punctuationTokens.add(punctuationTokens.get(0));
84             }
85         }
86
87     }
88
89     /**
90     * Format a list of numbers.
91     * @param numbers the numbers to be formatted (a sequence of integer values; it may also contain
92      * preformatted strings as part of the error recovery fallback)
93     * @return the formatted output string.
94     */

95
96     public CharSequence JavaDoc format(List JavaDoc numbers, int groupSize, String JavaDoc groupSeparator,
97                         String JavaDoc letterValue, String JavaDoc ordinal, Numberer numberer) {
98
99         FastStringBuffer sb = new FastStringBuffer(20);
100         int num = 0;
101         int tok = 0;
102         // output first punctuation token
103
if (startsWithPunctuation) {
104             sb.append((String JavaDoc)punctuationTokens.get(tok));
105         }
106         // output the list of numbers
107
while (num<numbers.size()) {
108             if (num>0) {
109                 if (tok==0 && startsWithPunctuation) {
110                     // The first punctuation token isn't a separator if it appears before the first
111
// formatting token. Such a punctuation token is used only once, at the start.
112
sb.append(".");
113                 } else {
114                     sb.append((String JavaDoc)punctuationTokens.get(tok));
115                 }
116             }
117             Object JavaDoc o = numbers.get(num++);
118             String JavaDoc s;
119             if (o instanceof Long JavaDoc) {
120                 long nr = ((Long JavaDoc)o).longValue();
121
122                 s = numberer.format(nr, (String JavaDoc)formatTokens.get(tok),
123                              groupSize, groupSeparator, letterValue, ordinal);
124             } else {
125                 s = o.toString();
126             }
127             sb.append(s);
128             tok++;
129             if (tok==formatTokens.size()) tok--;
130         }
131         // output the final punctuation token
132
if (punctuationTokens.size()>formatTokens.size()) {
133             sb.append((String JavaDoc)punctuationTokens.get(punctuationTokens.size()-1));
134         }
135         return sb.condense();
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 Michael H. Kay.
152
//
153
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
154
//
155
// Contributor(s): none.
156
//
157
Popular Tags