Restructuring to comply InputStreams insted of Nodesomethong

This commit is contained in:
Emil Mogstad committed 2012-05-30 21:27:05 +02:00
1 parent a155e9acf8
commit a0f975d892
8 files changed
+100 -238

No files matched your search

@@ -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 <item> nodes.
return doc.getElementsByTagName("item");
}
private boolean doesFileNeedUpdate(Context c) throws IOException {
/** Maximum amount of time before the file needs updating, in ms: */
+3 -4
View File
@@ -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);
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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 <item> 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;
}
}
@@ -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 <item> 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;
}
}
@@ -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);
}
@@ -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;