Go to most recent revision |
View as "text/plain" |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
1
/**
* Defines a class to update table rates in the background
*/
package de.pointedears.converter.helpers;
import java.io.IOException;
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;
/**
* @author pelinux
*
*/
public class CurrenciesUpdateThread
extends Thread
{
private static final String URL_ECB =
"http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"; //$NON-NLS-1$
/**
* Default constructor which sets the runnable
*
* @param runnable
*/
private CurrenciesUpdateThread
(Runnable runnable
)
{
super(runnable
);
}
/**
* Default constructor
*/
public CurrenciesUpdateThread
()
{
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();
}
}
});
}
}