diff --git a/src/com/studentersamfundet/app/FeedFetcher.java b/src/com/studentersamfundet/app/FeedFetcher.java index ddf1cbf..aa4e59a 100644 --- a/src/com/studentersamfundet/app/FeedFetcher.java +++ b/src/com/studentersamfundet/app/FeedFetcher.java @@ -10,14 +10,6 @@ import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - -import org.w3c.dom.Document; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; - import android.content.Context; public class FeedFetcher implements Serializable { @@ -61,8 +53,7 @@ public class FeedFetcher implements Serializable { * @throws IOException Is thrown if the app cannot download the file from * the internet and there is no local copy. */ - public NodeList fetch(Context context, boolean forcedUpdate) throws IOException { - NodeList itemNodes = null; + public InputStream fetch(Context context, boolean forcedUpdate) throws IOException { InputStream in = null; /* Update the local file if possible. */ @@ -80,46 +71,16 @@ public class FeedFetcher implements Serializable { } /* ...load local file: */ - in = loadFile(context); // if we fail here, we fail ultimately. Throw the Exception! - itemNodes = parseInputStream(in); - in.close(); + in = loadFile(context); // if we fail here, we fail ultimately. Throw the Exception! - return itemNodes; + return in; } - public NodeList loadLocalData(Context c) throws IOException { - NodeList nodes; - + public InputStream loadLocalData(Context c) throws IOException { InputStream is = loadFile(c); - nodes = parseInputStream(is); - is.close(); - - return nodes; + return is; } - private static NodeList parseInputStream(InputStream iStream) throws IOException { - Document doc = null; - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - DocumentBuilder db; - - try { - db = dbf.newDocumentBuilder(); - doc = db.parse(iStream); - } catch (ParserConfigurationException e) { - /* This should never happen. */ - } catch (SAXException e) { - /* This shouldn't happen unless the xml returned by the server - * is somehow invalid. If it happens, the best course of action - * is to pretend we failed to download the event list (it's - * close enough and it simplifies exception-handling). */ - throw new IOException("Bad response from server."); - } - - doc.getDocumentElement().normalize(); - - // Retrieve all the nodes. - return doc.getElementsByTagName("item"); - } private boolean doesFileNeedUpdate(Context c) throws IOException { /** Maximum amount of time before the file needs updating, in ms: */ diff --git a/src/com/studentersamfundet/app/IParser.java b/src/com/studentersamfundet/app/IParser.java index 042baa9..afd0e7f 100644 --- a/src/com/studentersamfundet/app/IParser.java +++ b/src/com/studentersamfundet/app/IParser.java @@ -1,9 +1,8 @@ package com.studentersamfundet.app; +import java.io.InputStream; import java.io.Serializable; -import org.w3c.dom.NodeList; - public interface IParser extends Serializable { - public DataHandler parse(NodeList nodes); -} + public DataHandler parse(InputStream in); +} \ No newline at end of file diff --git a/src/com/studentersamfundet/app/JSONParserNews.java b/src/com/studentersamfundet/app/JSONParserNews.java new file mode 100644 index 0000000..06b7c6b --- /dev/null +++ b/src/com/studentersamfundet/app/JSONParserNews.java @@ -0,0 +1,33 @@ +package com.studentersamfundet.app; + +import java.io.InputStream; + +public class JSONParserNews implements IParser { + private static final long serialVersionUID = -2654591623794449527L; + private final DataHandler dh; + + public JSONParserNews() { + this(null); + } + + public JSONParserNews(DataHandler dh) { + if (dh == null) { + this.dh = new DataHandler(); + } else { + this.dh = dh; + } + } + + public DataHandler parse(InputStream in) { + String title = ""; + String description = ""; + String link = ""; + String content = ""; + String pubDate = ""; + + for (int i = 0;i < 0;i++) { + dh.insertNews(title, description, content, link, pubDate); + } + return dh; + } +} diff --git a/src/com/studentersamfundet/app/JSONParserProgram.java b/src/com/studentersamfundet/app/JSONParserProgram.java new file mode 100644 index 0000000..2ccbf20 --- /dev/null +++ b/src/com/studentersamfundet/app/JSONParserProgram.java @@ -0,0 +1,51 @@ +package com.studentersamfundet.app; + +import java.io.InputStream; + +public class JSONParserProgram implements IParser { + private static final long serialVersionUID = 3364287016121595334L; + private final DataHandler dh; + + public JSONParserProgram() { + this(null); + } + + public JSONParserProgram(DataHandler dh) { + if (dh == null) { + this.dh = new DataHandler(); + } else { + this.dh = dh; + } + } + + public DataHandler parse(InputStream in) { + for (int i = 0; i < 0; i++) { + String id = ""; + String title = ""; + String description = ""; + String date = ""; + String location = ""; + String text = ""; + String category = ""; + String image = ""; + String priceReg = ""; + String priceMem = ""; + String ticketUrl = ""; + + // This is as ugly as it gets. Refactor? + dh.insertEvent(id, + title, + description, + date, + location, + text, + category, + image, + priceReg, + priceMem, + ticketUrl); + + } + return dh; + } +} diff --git a/src/com/studentersamfundet/app/RSSParserNews.java b/src/com/studentersamfundet/app/RSSParserNews.java deleted file mode 100644 index b1900ca..0000000 --- a/src/com/studentersamfundet/app/RSSParserNews.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.studentersamfundet.app; - -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - -public class RSSParserNews implements IParser { - private static final long serialVersionUID = -2654591623794449527L; - private final DataHandler dh; - - public RSSParserNews() { - this(null); - } - - public RSSParserNews(DataHandler dh) { - if (dh == null) { - this.dh = new DataHandler(); - } else { - this.dh = dh; - } - } - - public DataHandler parse(NodeList itemNodes) { - String title = ""; - String description = ""; - String link = ""; - String content = ""; - String pubDate = ""; - - for (int i = 0; i < itemNodes.getLength(); i++) { - Node itemNode = itemNodes.item(i); - - if (itemNode.getNodeType() == Node.ELEMENT_NODE) { - // Convert the Node into an Element. - Element itemElement = (Element) itemNode; - - // Get all elements under the element. - NodeList titleNodes = (itemElement).getElementsByTagName("title"); - NodeList descriptionNodes = (itemElement).getElementsByTagName("description"); - NodeList linkNodes = (itemElement).getElementsByTagName("link"); - NodeList textNodes = (itemElement).getElementsByTagName("content:encoded"); - NodeList dateNodes = (itemElement).getElementsByTagName("pubDate"); - - // Convert a Node into an Element. - Node titleElement = titleNodes.item(0); - Node descriptionElement = descriptionNodes.item(0); - Node linkElement = linkNodes.item(0); - Node textElement = textNodes.item(0); - Node dateElement = dateNodes.item(0); - - // Get all the child nodes. - NodeList titleTextNodes = titleElement.getChildNodes(); - NodeList descriptionTextNodes = descriptionElement.getChildNodes(); - NodeList linkTextNodes = linkElement.getChildNodes(); - NodeList textTextNodes = textElement.getChildNodes(); - NodeList dateTextNodes = dateElement.getChildNodes(); - - // Retrieve the text. - title = titleTextNodes.item(0).getNodeValue(); // mandatory - if (descriptionTextNodes.getLength() > 0) description = descriptionTextNodes.item(0).getNodeValue(); - if (linkTextNodes.getLength() > 0) link = linkTextNodes.item(0).getNodeValue(); - if (textTextNodes.getLength() > 0) content = textTextNodes.item(0).getNodeValue(); - if (dateTextNodes.getLength() > 0) pubDate = dateTextNodes.item(0).getNodeValue(); - - dh.insertNews(title, description, content, link, pubDate); - } - } - return dh; - } -} diff --git a/src/com/studentersamfundet/app/RSSParserProgram.java b/src/com/studentersamfundet/app/RSSParserProgram.java deleted file mode 100644 index ce2fc57..0000000 --- a/src/com/studentersamfundet/app/RSSParserProgram.java +++ /dev/null @@ -1,111 +0,0 @@ -package com.studentersamfundet.app; - -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - -public class RSSParserProgram implements IParser { - private static final long serialVersionUID = 3364287016121595334L; - private final DataHandler dh; - - public RSSParserProgram() { - this(null); - } - - public RSSParserProgram(DataHandler dh) { - if (dh == null) { - this.dh = new DataHandler(); - } else { - this.dh = dh; - } - } - - public DataHandler parse(NodeList itemNodes) { - for (int i = 0; i < itemNodes.getLength(); i++) { - String id = ""; - String title = ""; - String description = ""; - String date = ""; - String location = ""; - String text = ""; - String category = ""; - String image = ""; - String priceReg = ""; - String priceMem = ""; - String ticketUrl = ""; - - Node itemNode = itemNodes.item(i); - - if (itemNode.getNodeType() == Node.ELEMENT_NODE) { - // Convert the Node into an Element. - Element itemElement = (Element) itemNode; - - // Get all elements under the element. - NodeList idNodes = (itemElement).getElementsByTagName("id"); - NodeList titleNodes = (itemElement).getElementsByTagName("title"); - NodeList descriptionNodes = (itemElement).getElementsByTagName("description"); - NodeList dateNodes = (itemElement).getElementsByTagName("date"); - NodeList locationNodes = (itemElement).getElementsByTagName("location"); - NodeList textNodes = (itemElement).getElementsByTagName("content"); - NodeList categoryNodes = (itemElement).getElementsByTagName("category"); - NodeList imageNodes = (itemElement).getElementsByTagName("image"); - NodeList priceRegNodes = (itemElement).getElementsByTagName("price-regular"); - NodeList priceMemNodes = (itemElement).getElementsByTagName("price-member"); - NodeList ticketUrlNodes = (itemElement).getElementsByTagName("ticket-url"); - - // Convert a Node into an Element. - Node idElement = idNodes.item(0); - Node titleElement = titleNodes.item(0); - Node descriptionElement = descriptionNodes.item(0); - Node dateElement = dateNodes.item(0); - Node locationElement = locationNodes.item(0); - Node textElement = textNodes.item(0); - Node categoryElement = categoryNodes.item(0); - Node imageElement = imageNodes.item(0); - Node priceRegElement = priceRegNodes.item(0); - Node priceMemElement = priceMemNodes.item(0); - Node ticketUrlElement = ticketUrlNodes.item(0); - - // Get all the child nodes. - NodeList idTextNodes = idElement.getChildNodes(); - NodeList titleTextNodes = titleElement.getChildNodes(); - NodeList descriptionTextNodes = descriptionElement.getChildNodes(); - NodeList dateTextNodes = dateElement.getChildNodes(); - NodeList locationTextNodes = locationElement.getChildNodes(); - NodeList textTextNodes = textElement.getChildNodes(); - NodeList categoryTextNodes = categoryElement.getChildNodes(); - NodeList imageTextNodes = imageElement.getChildNodes(); - NodeList priceRegTextNodes = priceRegElement.getChildNodes(); - NodeList priceMemTextNodes = priceMemElement.getChildNodes(); - NodeList ticketUrlTextNodes = ticketUrlElement.getChildNodes(); - - // Retrieve the text. - id = idTextNodes.item(0).getNodeValue(); // mandatory - title = titleTextNodes.item(0).getNodeValue(); // mandatory - if (descriptionTextNodes.getLength() > 0) description = descriptionTextNodes.item(0).getNodeValue(); - if (dateTextNodes.getLength() > 0) date = dateTextNodes.item(0).getNodeValue(); - if (locationTextNodes.getLength() > 0) location = locationTextNodes.item(0).getNodeValue(); - if (textTextNodes.getLength() > 0) text = textTextNodes.item(0).getNodeValue(); - if (categoryTextNodes.getLength() > 0) category = categoryTextNodes.item(0).getNodeValue(); - if (imageTextNodes.getLength() > 0) image = imageTextNodes.item(0).getNodeValue(); - if (priceRegTextNodes.getLength() > 0) priceReg = priceRegTextNodes.item(0).getNodeValue(); - if (priceMemTextNodes.getLength() > 0) priceMem = priceMemTextNodes.item(0).getNodeValue(); - if (ticketUrlTextNodes.getLength() > 0) ticketUrl = ticketUrlTextNodes.item(0).getNodeValue(); - - // This is as ugly as it gets. Refactor? - dh.insertEvent(id, - title, - description, - date, - location, - text, - category, - image, - priceReg, - priceMem, - ticketUrl); - } - } - return dh; - } -} diff --git a/src/com/studentersamfundet/app/ui/DnsActivity.java b/src/com/studentersamfundet/app/ui/DnsActivity.java index cd86274..eb62be2 100644 --- a/src/com/studentersamfundet/app/ui/DnsActivity.java +++ b/src/com/studentersamfundet/app/ui/DnsActivity.java @@ -12,9 +12,9 @@ import android.view.animation.Animation; import android.widget.TextView; import com.studentersamfundet.app.FeedFetcher; +import com.studentersamfundet.app.JSONParserNews; import com.studentersamfundet.app.R; -import com.studentersamfundet.app.RSSParserNews; -import com.studentersamfundet.app.RSSParserProgram; +import com.studentersamfundet.app.JSONParserProgram; import com.studentersamfundet.app.ui.lists.EventListActivity; import com.studentersamfundet.app.ui.lists.EventListTicketsActivity; import com.studentersamfundet.app.ui.lists.NewsListActivity; @@ -155,21 +155,21 @@ public class DnsActivity extends BaseDnsActivity { public void programButton(View v) { Intent intent = setupNewIntent(EventListActivity.class); intent.putExtra("feed", eventFeedFetcher); - intent.putExtra("parser", new RSSParserProgram()); + intent.putExtra("parser", new JSONParserProgram()); startActivity(intent); } public void ticketButton(View v) { Intent intent = setupNewIntent(EventListTicketsActivity.class); intent.putExtra("feed", eventFeedFetcher); - intent.putExtra("parser", new RSSParserProgram()); + intent.putExtra("parser", new JSONParserProgram()); startActivity(intent); } public void newsButton(View v) { Intent intent = setupNewIntent(NewsListActivity.class); intent.putExtra("feed", newsFeedFetcher); - intent.putExtra("parser", new RSSParserNews()); + intent.putExtra("parser", new JSONParserNews()); startActivity(intent); } diff --git a/src/com/studentersamfundet/app/ui/lists/BaseListActivity.java b/src/com/studentersamfundet/app/ui/lists/BaseListActivity.java index 25bdf90..4a2e937 100644 --- a/src/com/studentersamfundet/app/ui/lists/BaseListActivity.java +++ b/src/com/studentersamfundet/app/ui/lists/BaseListActivity.java @@ -1,8 +1,7 @@ package com.studentersamfundet.app.ui.lists; import java.io.IOException; - -import org.w3c.dom.NodeList; +import java.io.InputStream; import com.studentersamfundet.app.DataHandler; import com.studentersamfundet.app.Event; @@ -112,8 +111,8 @@ public abstract class BaseListActivity extends BaseDnsActivity { // NAY = Inform about no connection. try { if (datahandler == null) { - NodeList itemNodes = feed.fetch(BaseListActivity.this, this.forcedUpdate); - datahandler = parser.parse(itemNodes); + InputStream in = feed.fetch(BaseListActivity.this, this.forcedUpdate); + datahandler = parser.parse(in); } return true;