Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
15 | PointedEar | 1 | /** |
2 | * Defines a class to update table rates in the background |
||
3 | */ |
||
4 | package de.pointedears.converter.helpers; |
||
5 | |||
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; |
||
20 | import org.w3c.dom.NodeList; |
||
21 | import org.xml.sax.SAXException; |
||
22 | |||
23 | /** |
||
24 | * @author pelinux |
||
25 | * |
||
26 | */ |
||
27 | public class CurrenciesUpdateThread extends Thread |
||
28 | { |
||
29 | private static final String URL_ECB = |
||
30 | "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"; //$NON-NLS-1$ |
||
31 | |||
32 | /** |
||
33 | * Default constructor which sets the runnable |
||
34 | * |
||
35 | * @param runnable |
||
36 | */ |
||
37 | private CurrenciesUpdateThread(Runnable runnable) |
||
38 | { |
||
39 | super(runnable); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Default constructor |
||
44 | */ |
||
45 | public CurrenciesUpdateThread() |
||
46 | { |
||
47 | this(new 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 | } |
||
105 | } |