11 |
import java.io.BufferedWriter; |
import java.io.BufferedWriter; |
12 |
import java.io.IOException; |
import java.io.IOException; |
13 |
import java.io.FileInputStream; |
import java.io.FileInputStream; |
14 |
|
import java.util.HashMap; |
15 |
|
|
16 |
class SupplementalHandler extends DefaultHandler |
class SupplementalHandler extends DefaultHandler |
17 |
{ |
{ |
23 |
static final int STATE_CURRENCYDATA = 5; |
static final int STATE_CURRENCYDATA = 5; |
24 |
static final int STATE_ALTERNATE = 6; |
static final int STATE_ALTERNATE = 6; |
25 |
static final int STATE_SEENCURRENCY = 7; |
static final int STATE_SEENCURRENCY = 7; |
26 |
|
static final int STATE_FRACTIONS = 8; |
27 |
|
static final int STATE_INFO = 9; |
28 |
|
|
29 |
|
static class CurrencyInfo |
30 |
|
{ |
31 |
|
int digits; |
32 |
|
int rounding; |
33 |
|
|
34 |
|
CurrencyInfo(int digits, int rounding) |
35 |
|
{ |
36 |
|
this.digits = digits; |
37 |
|
this.rounding = rounding; |
38 |
|
} |
39 |
|
} |
40 |
|
|
41 |
int state; |
int state; |
42 |
int oldState; |
int oldState; |
43 |
int ignoreLevel; |
int ignoreLevel; |
44 |
|
|
45 |
|
HashMap currencyInfo = new HashMap(); |
46 |
|
String currentCurrency; |
47 |
|
String currentRegion; |
48 |
|
|
49 |
PrintWriter output; |
PrintWriter output; |
50 |
|
|
51 |
public SupplementalHandler(PrintWriter output) |
public SupplementalHandler(PrintWriter output) |
71 |
return; |
return; |
72 |
} |
} |
73 |
|
|
74 |
if (localName.equals("supplementalData")) { |
if (localName.equals("supplementalData")) |
75 |
checkState(STATE_ZERO, STATE_SUPPLEMENTAL); |
checkState(STATE_ZERO, STATE_SUPPLEMENTAL); |
76 |
} else if (localName.equals("currencyData")) { |
else if (localName.equals("currencyData")) |
77 |
checkState(STATE_SUPPLEMENTAL, STATE_CURRENCYDATA); |
checkState(STATE_SUPPLEMENTAL, STATE_CURRENCYDATA); |
78 |
} else if (localName.equals("region")) { |
else if (localName.equals("region")) |
79 |
checkState(STATE_CURRENCYDATA, STATE_REGION); |
checkState(STATE_CURRENCYDATA, STATE_REGION); |
80 |
} else if (localName.equals("currency")) { |
else if (localName.equals("currency")) |
81 |
checkMultiState(new int[] { STATE_SEENCURRENCY, STATE_REGION }, STATE_CURRENCY); |
checkMultiState(new int[] { STATE_SEENCURRENCY, STATE_REGION }, STATE_CURRENCY); |
82 |
} else if (localName.equals("alternate")) { |
else if (localName.equals("alternate")) |
83 |
checkState(STATE_CURRENCY, STATE_ALTERNATE); |
checkState(STATE_CURRENCY, STATE_ALTERNATE); |
84 |
} else { |
else if (localName.equals("fractions")) |
85 |
|
checkState(STATE_CURRENCYDATA, STATE_FRACTIONS); |
86 |
|
else if (localName.equals("info")) |
87 |
|
checkState(STATE_FRACTIONS, STATE_INFO); |
88 |
|
else { |
89 |
ignoreLevel++; |
ignoreLevel++; |
90 |
return; |
return; |
91 |
} |
} |
93 |
if (state == STATE_REGION) |
if (state == STATE_REGION) |
94 |
{ |
{ |
95 |
String tRegion = (String)atts.getValue("iso3166"); |
String tRegion = (String)atts.getValue("iso3166"); |
96 |
|
|
97 |
if (tRegion == null) |
if (tRegion == null) |
98 |
throw new SAXException("region must have a iso3166 attribute"); |
throw new SAXException("region must have a iso3166 attribute"); |
99 |
|
|
100 |
output.print(tRegion + '='); |
currentRegion = tRegion; |
101 |
|
|
102 |
|
output.print(tRegion + ".currency="); |
103 |
|
} |
104 |
|
|
105 |
|
if (state == STATE_INFO) |
106 |
|
{ |
107 |
|
String currencyCode = (String)atts.getValue("iso4217"); |
108 |
|
String digits = (String)atts.getValue("digits"); |
109 |
|
String rounding = (String)atts.getValue("rounding"); |
110 |
|
|
111 |
|
if (currencyCode == null || digits == null || rounding == null) |
112 |
|
throw new SAXException("currency info must have an iso4217, a digits and a rounding attribute (here we get " + |
113 |
|
currencyCode + "," + digits + "," + rounding + ")"); |
114 |
|
|
115 |
|
currencyInfo.put(currencyCode, new CurrencyInfo(Integer.parseInt(digits), Integer.parseInt(rounding))); |
116 |
} |
} |
117 |
|
|
118 |
if (state == STATE_CURRENCY || state == STATE_ALTERNATE) |
if (state == STATE_CURRENCY || state == STATE_ALTERNATE) |
122 |
if (tName == null) |
if (tName == null) |
123 |
throw new SAXException("currency must have a iso 4217 attribute"); |
throw new SAXException("currency must have a iso 4217 attribute"); |
124 |
|
|
125 |
|
if (state == STATE_CURRENCY) |
126 |
|
currentCurrency = tName; |
127 |
|
|
128 |
// We only treat current currencies. |
// We only treat current currencies. |
129 |
if (atts.getValue("before") == null) |
if (atts.getValue("before") == null) |
130 |
{ |
{ |
149 |
} |
} |
150 |
|
|
151 |
if (state == STATE_SEENCURRENCY || state == STATE_REGION) |
if (state == STATE_SEENCURRENCY || state == STATE_REGION) |
152 |
output.println(); |
{ |
153 |
|
output.println(); |
154 |
|
|
155 |
if (localName.equals("supplementalData")) { |
CurrencyInfo info = (CurrencyInfo)currencyInfo.get(currentCurrency); |
156 |
|
if (info == null) |
157 |
|
info = (CurrencyInfo)currencyInfo.get("DEFAULT"); |
158 |
|
|
159 |
|
if (info != null) |
160 |
|
{ |
161 |
|
output.println(currentRegion + ".fractionDigits=" + info.digits); |
162 |
|
} |
163 |
|
} |
164 |
|
|
165 |
|
if (localName.equals("supplementalData")) |
166 |
checkState(STATE_SUPPLEMENTAL, STATE_ZERO); |
checkState(STATE_SUPPLEMENTAL, STATE_ZERO); |
167 |
} else if (localName.equals("currencyData")) { |
else if (localName.equals("currencyData")) |
168 |
checkState(STATE_CURRENCYDATA, STATE_SUPPLEMENTAL); |
checkState(STATE_CURRENCYDATA, STATE_SUPPLEMENTAL); |
169 |
} else if (localName.equals("region")) { |
else if (localName.equals("region")) |
170 |
checkMultiState(new int[] { STATE_SEENCURRENCY, STATE_REGION }, STATE_CURRENCYDATA); |
checkMultiState(new int[] { STATE_SEENCURRENCY, STATE_REGION }, STATE_CURRENCYDATA); |
171 |
} else if (localName.equals("currency")) { |
else if (localName.equals("currency")) |
172 |
checkState(STATE_CURRENCY, STATE_SEENCURRENCY); |
checkState(STATE_CURRENCY, STATE_SEENCURRENCY); |
173 |
} else if (localName.equals("alternate")) { |
else if (localName.equals("alternate")) |
174 |
checkState(STATE_ALTERNATE, STATE_CURRENCY); |
checkState(STATE_ALTERNATE, STATE_CURRENCY); |
175 |
} |
else if (localName.equals("fractions")) |
176 |
|
checkState(STATE_FRACTIONS, STATE_CURRENCYDATA); |
177 |
|
else if (localName.equals("info")) |
178 |
|
checkState(STATE_INFO, STATE_FRACTIONS); |
179 |
} |
} |
180 |
|
|
181 |
void checkState(int currentState, int newState) |
void checkState(int currentState, int newState) |