| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.number; |
| 9 |
|
| 10 |
import net.sf.saxon.lib.Numberer; |
| 11 |
import net.sf.saxon.regex.EmptyString; |
| 12 |
import net.sf.saxon.regex.UnicodeString; |
| 13 |
import net.sf.saxon.regex.charclass.Categories; |
| 14 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 15 |
|
| 16 |
import java.math.BigInteger; |
| 17 |
import java.util.ArrayList; |
| 18 |
import java.util.List; |
| 19 |
import java.util.function.IntPredicate; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class NumberFormatter { |
| 29 |
|
| 30 |
private ArrayList<UnicodeString> formatTokens; |
| 31 |
private ArrayList<UnicodeString> punctuationTokens; |
| 32 |
private boolean startsWithPunctuation; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public void prepare(String format) { |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
if (format.isEmpty()) { |
| 53 |
format = "1"; |
| 54 |
} |
| 55 |
|
| 56 |
formatTokens = new ArrayList<UnicodeString>(10); |
| 57 |
punctuationTokens = new ArrayList<UnicodeString>(10); |
| 58 |
|
| 59 |
UnicodeString uFormat = UnicodeString.makeUnicodeString(format); |
| 60 |
int len = uFormat.uLength(); |
| 61 |
int i = 0; |
| 62 |
int t; |
| 63 |
boolean first = true; |
| 64 |
startsWithPunctuation = true; |
| 65 |
|
| 66 |
while (i < len) { |
| 67 |
int c = uFormat.uCharAt(i); |
| 68 |
t = i; |
| 69 |
while (isLetterOrDigit(c)) { |
| 70 |
i++; |
| 71 |
if (i == len) break; |
| 72 |
c = uFormat.uCharAt(i); |
| 73 |
} |
| 74 |
if (i > t) { |
| 75 |
UnicodeString tok = uFormat.uSubstring(t, i); |
| 76 |
formatTokens.add(tok); |
| 77 |
if (first) { |
| 78 |
punctuationTokens.add(UnicodeString.makeUnicodeString(".")); |
| 79 |
startsWithPunctuation = false; |
| 80 |
first = false; |
| 81 |
} |
| 82 |
} |
| 83 |
if (i == len) break; |
| 84 |
t = i; |
| 85 |
c = uFormat.uCharAt(i); |
| 86 |
while (!isLetterOrDigit(c)) { |
| 87 |
first = false; |
| 88 |
i++; |
| 89 |
if (i == len) break; |
| 90 |
c = uFormat.uCharAt(i); |
| 91 |
} |
| 92 |
if (i > t) { |
| 93 |
UnicodeString sep = uFormat.uSubstring(t, i); |
| 94 |
punctuationTokens.add(sep); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if (formatTokens.isEmpty()) { |
| 99 |
formatTokens.add(UnicodeString.makeUnicodeString("1")); |
| 100 |
if (punctuationTokens.size() == 1) { |
| 101 |
punctuationTokens.add(punctuationTokens.get(0)); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public static boolean isLetterOrDigit(int c) { |
| 115 |
if (c <= 0x7F) { |
| 116 |
|
| 117 |
return (c >= 0x30 && c <= 0x39) || (c >= 0x41 && c <= 0x5A) || (c >= 0x61 && c <= 0x7A); |
| 118 |
} else { |
| 119 |
return alphanumeric.test(c); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
private static IntPredicate alphanumeric = |
| 124 |
Categories.getCategory("N").or(Categories.getCategory("L")); |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public CharSequence format(List numbers, int groupSize, String groupSeparator, |
| 135 |
String letterValue, String ordinal, Numberer numberer) { |
| 136 |
|
| 137 |
FastStringBuffer sb = new FastStringBuffer(FastStringBuffer.C16); |
| 138 |
int num = 0; |
| 139 |
int tok = 0; |
| 140 |
|
| 141 |
if (startsWithPunctuation) { |
| 142 |
sb.append(punctuationTokens.get(tok)); |
| 143 |
} |
| 144 |
|
| 145 |
while (num < numbers.size()) { |
| 146 |
if (num > 0) { |
| 147 |
if (tok == 0 && startsWithPunctuation) { |
| 148 |
|
| 149 |
|
| 150 |
sb.append("."); |
| 151 |
} else { |
| 152 |
sb.append(punctuationTokens.get(tok)); |
| 153 |
} |
| 154 |
} |
| 155 |
Object o = numbers.get(num++); |
| 156 |
String s; |
| 157 |
if (o instanceof Long) { |
| 158 |
long nr = (Long) o; |
| 159 |
RegularGroupFormatter rgf = new RegularGroupFormatter(groupSize, groupSeparator, EmptyString.THE_INSTANCE); |
| 160 |
s = numberer.format(nr, formatTokens.get(tok), rgf, letterValue, ordinal); |
| 161 |
} else if (o instanceof BigInteger) { |
| 162 |
|
| 163 |
FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C64); |
| 164 |
fsb.append(o.toString()); |
| 165 |
RegularGroupFormatter rgf = new RegularGroupFormatter(groupSize, groupSeparator, EmptyString.THE_INSTANCE); |
| 166 |
s = rgf.format(fsb); |
| 167 |
s = translateDigits(s, formatTokens.get(tok)); |
| 168 |
} else { |
| 169 |
|
| 170 |
s = o.toString(); |
| 171 |
} |
| 172 |
sb.append(s); |
| 173 |
tok++; |
| 174 |
if (tok == formatTokens.size()) { |
| 175 |
tok--; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
if (punctuationTokens.size() > formatTokens.size()) { |
| 180 |
sb.append(punctuationTokens.get(punctuationTokens.size() - 1)); |
| 181 |
} |
| 182 |
return sb.condense(); |
| 183 |
} |
| 184 |
|
| 185 |
private String translateDigits(String in, UnicodeString picture) { |
| 186 |
if (picture.length() == 0) { |
| 187 |
return in; |
| 188 |
} |
| 189 |
int formchar = picture.uCharAt(0); |
| 190 |
int digitValue = Alphanumeric.getDigitValue(formchar); |
| 191 |
if (digitValue >= 0) { |
| 192 |
int zero = formchar - digitValue; |
| 193 |
if (zero == (int) '0') { |
| 194 |
return in; |
| 195 |
} |
| 196 |
int[] digits = new int[10]; |
| 197 |
for (int z = 0; z <= 9; z++) { |
| 198 |
digits[z] = zero + z; |
| 199 |
} |
| 200 |
FastStringBuffer sb = new FastStringBuffer(128); |
| 201 |
for (int i = 0; i < in.length(); i++) { |
| 202 |
char c = in.charAt(i); |
| 203 |
if (c >= '0' && c <= '9') { |
| 204 |
sb.appendWideChar(digits[c - '0']); |
| 205 |
} else { |
| 206 |
sb.append(c); |
| 207 |
} |
| 208 |
} |
| 209 |
return sb.toString(); |
| 210 |
} else { |
| 211 |
return in; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
} |
| 217 |
|