Subversion Repositories ES

Compare Revisions

Last modification

Ignore whitespace Rev 16 → Rev 15

/trunk/src/de/pointedears/converter/helpers/ConverterThread.java
File deleted
Property changes:
Deleted: svn:mime-type
## -1 +0,0 ##
-text/plain
\ No newline at end of property
Index: ConverterNamespaceContext.java
===================================================================
--- ConverterNamespaceContext.java (revision 16)
+++ ConverterNamespaceContext.java (nonexistent)
@@ -1,58 +0,0 @@
-package de.pointedears.converter.helpers;
-
-import java.util.HashMap;
-import java.util.Iterator;
-
-import javax.xml.XMLConstants;
-import javax.xml.namespace.NamespaceContext;
-
-/**
- * @author pelinux
- *
- */
-public final class ConverterNamespaceContext implements NamespaceContext
-{
- private final HashMap<String, String> namespaces =
- new HashMap<String, String>();
-
- public void add(String prefix, String uri)
- {
- this.namespaces.put(prefix, uri);
- }
-
- @Override
- public Iterator getPrefixes(String namespaceURI)
- {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public String getPrefix(String namespaceURI)
- {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public String getNamespaceURI(String prefix)
- {
- if (prefix == null)
- {
- throw new NullPointerException("Null prefix");
- }
- else
- {
- if ("xml".equals(prefix))
- {
- return XMLConstants.XML_NS_URI;
- }
-
- String storedPrefix = this.namespaces.get(prefix);
- if (storedPrefix != null)
- {
- return storedPrefix;
- }
-
- return XMLConstants.NULL_NS_URI;
- }
- }
-}
\ No newline at end of file
/ConverterNamespaceContext.java
Property changes:
Deleted: svn:mime-type
## -1 +0,0 ##
-text/plain
\ No newline at end of property
Index: CurrenciesUpdateThread.java
===================================================================
--- CurrenciesUpdateThread.java (nonexistent)
+++ CurrenciesUpdateThread.java (revision 15)
@@ -0,0 +1,105 @@
+/**
+ * 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();
+ }
+ }
+ });
+ }
+}
/CurrenciesUpdateThread.java
Property changes:
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property