1,105 → 1,30 |
/** |
* Defines a class to update table rates in the background |
*/ |
package de.pointedears.converter.helpers; |
|
import java.io.IOException; |
import android.os.Handler; |
|
import javax.xml.parsers.DocumentBuilder; |
import javax.xml.parsers.DocumentBuilderFactory; |
import javax.xml.parsers.ParserConfigurationException; |
import javax.xml.xpath.XPath; |
import javax.xml.xpath.XPathConstants; |
import javax.xml.xpath.XPathExpression; |
import javax.xml.xpath.XPathExpressionException; |
import javax.xml.xpath.XPathFactory; |
|
import org.w3c.dom.Document; |
import org.w3c.dom.NamedNodeMap; |
import org.w3c.dom.Node; |
import org.w3c.dom.NodeList; |
import org.xml.sax.SAXException; |
|
/** |
* General thread to perform background tasks |
* |
* @author pelinux |
* |
*/ |
public class CurrenciesUpdateThread extends Thread |
public class ConverterThread extends Thread |
{ |
private static final String URL_ECB = |
"http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"; //$NON-NLS-1$ |
private Handler handler = null; |
private Runnable runnable = null; |
|
/** |
* Default constructor which sets the runnable |
* |
* @param runnable |
* @param handler |
*/ |
private CurrenciesUpdateThread(Runnable runnable) |
public ConverterThread(Runnable runnable, Handler handler) |
{ |
super(runnable); |
this.handler = handler; |
this.runnable = runnable; |
} |
|
/** |
* Default constructor |
*/ |
public CurrenciesUpdateThread() |
@Override |
public void run() |
{ |
this(new Runnable() { |
@Override |
public void run() |
{ |
DocumentBuilderFactory documentBuilderFactory = |
DocumentBuilderFactory.newInstance(); |
documentBuilderFactory.setNamespaceAware(true); |
try |
{ |
DocumentBuilder builder = |
documentBuilderFactory.newDocumentBuilder(); |
try |
{ |
Document doc = builder.parse(CurrenciesUpdateThread.URL_ECB); |
XPathFactory xpathFactory = XPathFactory.newInstance(); |
XPath xpath = xpathFactory.newXPath(); |
try |
{ |
XPathExpression expr = xpath.compile("/Cube/Cube//Cube"); //$NON-NLS-1$ |
Object result = expr.evaluate(doc, XPathConstants.NODESET); |
NodeList nodes = (NodeList) result; |
for (int i = 0, len = nodes.getLength(); i < len; i++) |
{ |
Node item = nodes.item(i); |
NamedNodeMap attributes = item.getAttributes(); |
String currency = |
attributes.getNamedItem("currency").getNodeValue(); //$NON-NLS-1$ |
String rate = attributes.getNamedItem("rate").toString(); //$NON-NLS-1$ |
|
/* TODO: Update UI */ |
System.out.println(currency + ": " + rate); //$NON-NLS-1$ |
} |
} |
catch (XPathExpressionException e) |
{ |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
} |
catch (SAXException e) |
{ |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
catch (IOException e) |
{ |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
} |
catch (ParserConfigurationException e) |
{ |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
} |
}); |
this.handler.post(this.runnable); |
} |
} |