package xmlpodCast.castrohs.com;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
import anywheresoftware.b4a.BA.ShortName;
@ShortName("XMLLeitorFeed")
public class XMLLeitorFeed {
private String TITLE = "title";
private String DESCRIPTION = "description";
// private String CHANNEL = "channel";
private String LANGUAGE = "language";
private String COPYRIGHT = "copyright";
private String LINK = "origLink";
private String AUTHOR = "author";
private String ITEM = "item";
private String PUB_DATE = "pubDate";
private String GUID = "guid";
private String DURATION = "duration";
private String ENCLOSURE = "origEnclosureLink";
private URL url;
private XMLCanal feed= null;
public XMLLeitorFeed(){
}
/**
*
* Create a feed from url
* and run to canal
*
*/
public void GeraRSSFeedParser(String feedUrl) {
try {
this.url = new URL(feedUrl);
if (url!=null)
readFeed();
} catch (MalformedURLException e) {
}
}
public URL getUrl() {
return url;
}
public void setUrl(URL url) {
this.url = url;
}
/*
private InputStream read() {
try {
return url.openStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
*/
public XMLCanal readFeed() {
//XMLCanal feed = null;
try {
boolean isFeedHeader = true;
// Set header values inicia to the empty string
String description = "";
String title = "";
String link = "";
String language = "";
String copyright = "";
String author = "";
String pubdate = "";
String guid = "";
String duration = "0.0";
String enclosure = "";
// First create a new XMLInputFactory
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
// Setup a new eventReader
InputStream in = null;
try {
in = url.openStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (in!=null){
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
// Read the XML document
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
if (event.asStartElement().getName().getLocalPart() == (ITEM)) {
if (isFeedHeader) {
isFeedHeader = false;
feed = new XMLCanal(title, link, description, language,
copyright, pubdate);
}
event = eventReader.nextEvent();
continue;
}
if (event.asStartElement().getName().getLocalPart() == (TITLE)) {
event = eventReader.nextEvent();
title = event.asCharacters().getData();
continue;
}
if (event.asStartElement().getName().getLocalPart() == (DESCRIPTION)) {
event = eventReader.nextEvent();
description = event.asCharacters().getData();
continue;
}
/*
if (event.asStartElement().getName().getLocalPart() == (LINK)) {
event = eventReader.nextEvent();
link = event.asCharacters().getData();
continue;
}*/
if (event.asStartElement().getName().getLocalPart() == (GUID)) {
event = eventReader.nextEvent();
guid = event.asCharacters().getData();
continue;
}
if (event.asStartElement().getName().getLocalPart() == (LANGUAGE)) {
event = eventReader.nextEvent();
language = event.asCharacters().getData();
continue;
}
if (event.asStartElement().getName().getLocalPart() == (AUTHOR)) {
event = eventReader.nextEvent();
author = event.asCharacters().getData();
continue;
}
if (event.asStartElement().getName().getLocalPart() == (PUB_DATE)) {
event = eventReader.nextEvent();
pubdate = event.asCharacters().getData();
continue;
}
if (event.asStartElement().getName().getLocalPart() == (COPYRIGHT)) {
event = eventReader.nextEvent();
copyright = event.asCharacters().getData();
continue;
}
if (event.asStartElement().getName().getLocalPart() == (ENCLOSURE)) {
event = eventReader.nextEvent();
enclosure = event.asCharacters().getData();
continue;
}
if (event.asStartElement().getName().getLocalPart() == (DURATION)) {
event = eventReader.nextEvent();
duration = event.asCharacters().getData();
continue;
}
if (event.asStartElement().getName().getLocalPart() == (LINK)) {
event = eventReader.nextEvent();
link = event.asCharacters().getData();
continue;
}
} else if (event.isEndElement()) {
if (event.asEndElement().getName().getLocalPart() == (ITEM)) {
XMLPodCast message = new XMLPodCast();
message.setAuthor(author);
message.setDescription(description);
message.setGuid(guid);
message.setLink(link);
message.setTitle(title);
message.setDuracao(duration);
message.setEnclosure(enclosure);
feed.addMessages(message);
event = eventReader.nextEvent();
continue;
}
}
}
}
} catch (XMLStreamException e) {
throw new RuntimeException(e);
}
return feed;
}
public XMLCanal getCanal() {
return feed;
}
public void setCanal(XMLCanal feed) {
this.feed = feed;
}
public String getTITLE() {
return TITLE;
}
public void setTITLE(String tITLE) {
TITLE = tITLE;
}
public String getDESCRIPTION() {
return DESCRIPTION;
}
public void setDESCRIPTION(String dESCRIPTION) {
DESCRIPTION = dESCRIPTION;
}
public String getLANGUAGE() {
return LANGUAGE;
}
public void setLANGUAGE(String lANGUAGE) {
LANGUAGE = lANGUAGE;
}
public String getCOPYRIGHT() {
return COPYRIGHT;
}
public void setCOPYRIGHT(String cOPYRIGHT) {
COPYRIGHT = cOPYRIGHT;
}
public String getLINK() {
return LINK;
}
public void setLINK(String lINK) {
LINK = lINK;
}
public String getAUTHOR() {
return AUTHOR;
}
public void setAUTHOR(String aUTHOR) {
AUTHOR = aUTHOR;
}
public String getITEM() {
return ITEM;
}
public void setITEM(String iTEM) {
ITEM = iTEM;
}
public String getPUB_DATE() {
return PUB_DATE;
}
public void setPUB_DATE(String pUB_DATE) {
PUB_DATE = pUB_DATE;
}
public String getGUID() {
return GUID;
}
public void setGUID(String gUID) {
GUID = gUID;
}
public String getDURATION() {
return DURATION;
}
public void setDURATION(String dURATION) {
DURATION = dURATION;
}
public String getENCLOSURE() {
return ENCLOSURE;
}
public void setENCLOSURE(String eNCLOSURE) {
ENCLOSURE = eNCLOSURE;
}
}