Rev 15 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 15 | Rev 16 | ||
---|---|---|---|
Line 1... | Line -... | ||
1 | /**
|
- | |
2 | * Defines a class to update table rates in the background
|
- | |
3 | */
|
- | |
4 | package de.pointedears.converter.helpers; |
1 | package de.pointedears.converter.helpers; |
5 | 2 | ||
6 | import java.io.IOException; |
- | |
7 | - | ||
8 | import javax.xml.parsers.DocumentBuilder; |
- | |
9 | import javax.xml.parsers.DocumentBuilderFactory; |
- | |
10 | import javax.xml.parsers.ParserConfigurationException; |
- | |
11 | import javax.xml.xpath.XPath; |
- | |
12 | import javax.xml.xpath.XPathConstants; |
- | |
13 | import javax.xml.xpath.XPathExpression; |
- | |
14 | import javax.xml.xpath.XPathExpressionException; |
- | |
15 | import javax.xml.xpath.XPathFactory; |
- | |
16 | - | ||
17 | import org.w3c.dom.Document; |
- | |
18 | import org.w3c.dom.NamedNodeMap; |
- | |
19 | import org.w3c.dom.Node; |
3 | import android.os.Handler; |
20 | import org.w3c.dom.NodeList; |
- | |
21 | import org.xml.sax.SAXException; |
- | |
22 | 4 | ||
23 | /**
|
5 | /**
|
24 | * @author pelinux
|
6 | * General thread to perform background tasks
|
25 | *
|
7 | *
|
- | 8 | * @author pelinux
|
|
26 | */
|
9 | */
|
27 | public class CurrenciesUpdateThread extends Thread |
10 | public class ConverterThread extends Thread |
28 | {
|
11 | {
|
29 | private static final String URL_ECB = |
12 | private Handler handler = null; |
30 | "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"; //$NON-NLS-1$ |
13 | private Runnable runnable = null; |
31 | 14 | ||
32 | /**
|
15 | /**
|
33 | * Default constructor which sets the runnable
|
- | |
34 | *
|
- | |
35 | * @param runnable
|
16 | * @param runnable
|
- | 17 | * @param handler
|
|
36 | */
|
18 | */
|
37 | private CurrenciesUpdateThread(Runnable runnable) |
19 | public ConverterThread(Runnable runnable, Handler handler) |
38 | {
|
20 | {
|
- | 21 | this.handler = handler; |
|
39 | super(runnable); |
22 | this.runnable = runnable; |
40 | }
|
23 | }
|
41 | 24 | ||
42 | /**
|
- | |
43 | * Default constructor
|
- | |
44 | */
|
25 | @Override |
45 | public CurrenciesUpdateThread() |
26 | public void run() |
46 | {
|
27 | {
|
47 | this(new Runnable() { |
28 | this.handler.post(this.runnable); |
48 | @Override |
- | |
49 | public void run() |
- | |
50 | {
|
- | |
51 | DocumentBuilderFactory documentBuilderFactory = |
- | |
52 | DocumentBuilderFactory.newInstance(); |
- | |
53 | documentBuilderFactory.setNamespaceAware(true); |
- | |
54 | try
|
- | |
55 | {
|
- | |
56 | DocumentBuilder builder = |
- | |
57 | documentBuilderFactory.newDocumentBuilder(); |
- | |
58 | try
|
- | |
59 | {
|
- | |
60 | Document doc = builder.parse(CurrenciesUpdateThread.URL_ECB); |
- | |
61 | XPathFactory xpathFactory = XPathFactory.newInstance(); |
- | |
62 | XPath xpath = xpathFactory.newXPath(); |
- | |
63 | try
|
- | |
64 | {
|
- | |
65 | XPathExpression expr = xpath.compile("/Cube/Cube//Cube"); //$NON-NLS-1$ |
- | |
66 | Object result = expr.evaluate(doc, XPathConstants.NODESET); |
- | |
67 | NodeList nodes = (NodeList) result; |
- | |
68 | for (int i = 0, len = nodes.getLength(); i < len; i++) |
- | |
69 | {
|
- | |
70 | Node item = nodes.item(i); |
- | |
71 | NamedNodeMap attributes = item.getAttributes(); |
- | |
72 | String currency = |
- | |
73 | attributes.getNamedItem("currency").getNodeValue(); //$NON-NLS-1$ |
- | |
74 | String rate = attributes.getNamedItem("rate").toString(); //$NON-NLS-1$ |
- | |
75 | - | ||
76 | /* TODO: Update UI */
|
- | |
77 | System.out.println(currency + ": " + rate); //$NON-NLS-1$ |
- | |
78 | }
|
- | |
79 | }
|
- | |
80 | catch (XPathExpressionException e) |
- | |
81 | {
|
- | |
82 | // TODO Auto-generated catch block
|
- | |
83 | e.printStackTrace(); |
- | |
84 | }
|
- | |
85 | }
|
- | |
86 | catch (SAXException e) |
- | |
87 | {
|
- | |
88 | // TODO Auto-generated catch block
|
- | |
89 | e.printStackTrace(); |
- | |
90 | }
|
- | |
91 | catch (IOException e) |
- | |
92 | {
|
- | |
93 | // TODO Auto-generated catch block
|
- | |
94 | e.printStackTrace(); |
- | |
95 | }
|
- | |
96 | }
|
- | |
97 | catch (ParserConfigurationException e) |
- | |
98 | {
|
- | |
99 | // TODO Auto-generated catch block
|
- | |
100 | e.printStackTrace(); |
- | |
101 | }
|
- | |
102 | }
|
- | |
103 | }); |
- | |
104 | }
|
29 | }
|
105 | }
|
30 | }
|