Rev 17 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 17 | Rev 18 | ||
---|---|---|---|
Line 38... | Line 38... | ||
38 | * @author pelinux
|
38 | * @author pelinux
|
39 | *
|
39 | *
|
40 | */
|
40 | */
|
41 | public class RatesUpdater implements Runnable |
41 | public class RatesUpdater implements Runnable |
42 | {
|
42 | {
|
- | 43 | /*
|
|
- | 44 | * XML markup attributes
|
|
- | 45 | */
|
|
- | 46 | private static final String ATTR_RATE = "rate"; //$NON-NLS-1$ |
|
- | 47 | private static final String ATTR_CURRENCY = "currency"; //$NON-NLS-1$ |
|
- | 48 | private static final String ATTR_TIME = "time"; //$NON-NLS-1$ |
|
- | 49 | ||
43 | private static final String URL_ECB = |
50 | private static final String URL_ECB = |
44 | "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"; //$NON-NLS-1$ |
51 | "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"; //$NON-NLS-1$ |
45 | 52 | ||
46 | private final CurrenciesActivity activityContext; |
53 | private final CurrenciesActivity activityContext; |
47 | private ConverterThread updateThread = null; |
54 | private ConverterThread updateThread = null; |
48 | 55 | ||
49 | private final UpdateService service; |
56 | private final UpdateService service; |
50 | 57 | ||
51 | /**
|
58 | /**
|
- | 59 | * @param activityContext
|
|
- | 60 | * The activityContext for this updater.
|
|
- | 61 | * FIXME: Required only for database access
|
|
52 | * @param updateService
|
62 | * @param updateService
|
53 | *
|
63 | * The service that started this updater
|
54 | */
|
64 | */
|
55 | public RatesUpdater(CurrenciesActivity activityContext, |
65 | public RatesUpdater(CurrenciesActivity activityContext, |
56 | UpdateService updateService)
|
66 | UpdateService updateService)
|
57 | {
|
67 | {
|
58 | this.activityContext = activityContext; |
68 | this.activityContext = activityContext; |
Line 67... | Line 77... | ||
67 | return this.updateThread; |
77 | return this.updateThread; |
68 | }
|
78 | }
|
69 | 79 | ||
70 | /**
|
80 | /**
|
71 | * @param updateThread
|
81 | * @param updateThread
|
- | 82 | * the thread that this updater is running in
|
|
72 | */
|
83 | */
|
73 | public void setUpdateThread(ConverterThread updateThread) |
84 | public void setUpdateThread(ConverterThread updateThread) |
74 | {
|
85 | {
|
75 | this.updateThread = updateThread; |
86 | this.updateThread = updateThread; |
76 | }
|
87 | }
|
Line 131... | Line 142... | ||
131 | return; |
142 | return; |
132 | }
|
143 | }
|
133 | 144 | ||
134 | try
|
145 | try
|
135 | {
|
146 | {
|
- | 147 | updated = |
|
136 | updated = df.parse(parentCube.getAttribute("time")); |
148 | df.parse(parentCube.getAttribute(RatesUpdater.ATTR_TIME)); |
137 | }
|
149 | }
|
138 | catch (ParseException e) |
150 | catch (ParseException e) |
139 | {
|
151 | {
|
140 | Log.e(this.getClass().toString(), |
152 | Log.e(this.getClass().toString(), |
141 | "Could not parse the `time' attribute into a Date", e); |
153 | "Could not parse the `time' attribute into a Date", e); //$NON-NLS-1$ |
142 | }
|
154 | }
|
143 | 155 | ||
144 | expr = |
156 | expr = |
145 | xpath |
157 | xpath |
146 | .compile("./*[local-name()='Cube' and (@currency='CHF' or @currency='USD')]"); //$NON-NLS-1$ |
158 | .compile("./*[local-name()='Cube' and (@currency='CHF' or @currency='USD')]"); //$NON-NLS-1$ |
Line 153... | Line 165... | ||
153 | HashMap<String, ConversionData> conversionRates = |
165 | HashMap<String, ConversionData> conversionRates = |
154 | this.activityContext.getConversionRates(); |
166 | this.activityContext.getConversionRates(); |
155 | for (int i = 0; i < len; ++i) |
167 | for (int i = 0; i < len; ++i) |
156 | {
|
168 | {
|
157 | Element item = (Element) childCubes.item(i); |
169 | Element item = (Element) childCubes.item(i); |
158 | String currency = item.getAttribute("currency"); |
170 | String currency = item.getAttribute(RatesUpdater.ATTR_CURRENCY); |
159 | 171 | ||
160 | try
|
172 | try
|
161 | {
|
173 | {
|
162 | Double rate = |
174 | Double rate = |
163 | Double.parseDouble(item.getAttribute("rate")); //$NON-NLS-1$ |
175 | Double.parseDouble(item.getAttribute(RatesUpdater.ATTR_RATE)); |
164 | conversionRates |
176 | conversionRates |
165 | .put(currency, new ConversionData(rate, updated)); |
177 | .put(currency, new ConversionData(rate, updated)); |
166 | }
|
178 | }
|
167 | catch (NumberFormatException e) |
179 | catch (NumberFormatException e) |
168 | {
|
180 | {
|
169 | 181 | ||
170 | }
|
182 | }
|
171 | }
|
183 | }
|
172 | 184 | ||
173 | this.activityContext.getDatabase().writeConversionsToDatabase(null); |
185 | this.activityContext.getDatabase().writeConversionsToDatabase(null); |
174 | this.activityContext.fillTableRates(); |
- | |
175 | }
|
186 | }
|
176 | catch (XPathExpressionException e) |
187 | catch (XPathExpressionException e) |
177 | {
|
188 | {
|
178 | Log.e(this.getClass().toString(), "Error in XPath expression", e); |
189 | Log.e(this.getClass().toString(), "Error in XPath expression", e); //$NON-NLS-1$ |
179 | }
|
190 | }
|
180 | }
|
191 | }
|
181 | catch (SAXException e) |
192 | catch (SAXException e) |
182 | {
|
193 | {
|
183 | Log.e(this.getClass().toString(), |
194 | Log.e(this.getClass().toString(), |
184 | "Exception while parsing external XML resource", e); |
195 | "Exception while parsing external XML resource", e); //$NON-NLS-1$ |
185 | }
|
196 | }
|
186 | catch (IOException e) |
197 | catch (IOException e) |
187 | {
|
198 | {
|
188 | Log.e(this.getClass().toString(), |
199 | Log.e(this.getClass().toString(), |
189 | "I/O exception while parsing external XML resource", e); |
200 | "I/O exception while parsing external XML resource", e); //$NON-NLS-1$ |
190 | }
|
201 | }
|
191 | }
|
202 | }
|
192 | catch (ParserConfigurationException e) |
203 | catch (ParserConfigurationException e) |
193 | {
|
204 | {
|
194 | Log.e(this.getClass().toString(), |
205 | Log.e(this.getClass().toString(), |
195 | "Document builder cannot be created", e); |
206 | "Document builder cannot be created", e); //$NON-NLS-1$ |
196 | }
|
207 | }
|
197 | 208 | ||
198 | if (len > 0) |
209 | if (len > 0) |
199 | {
|
210 | {
|
200 | /*
|
211 | /*
|