Converted RSSHandler into RSS 2.0 Namespace
This commit is contained in:
parent
b19afaa82c
commit
a0237d6035
@ -17,7 +17,7 @@ public class FeedHandler {
|
|||||||
public Feed parseFeed(Feed feed) {
|
public Feed parseFeed(Feed feed) {
|
||||||
TypeGetter tg = new TypeGetter();
|
TypeGetter tg = new TypeGetter();
|
||||||
tg.getType(feed);
|
tg.getType(feed);
|
||||||
RSSHandler handler = new RSSHandler(feed);
|
SyndHandler handler = new SyndHandler(feed);
|
||||||
try {
|
try {
|
||||||
SAXParserFactory factory = SAXParserFactory.newInstance();
|
SAXParserFactory factory = SAXParserFactory.newInstance();
|
||||||
factory.setNamespaceAware(true);
|
factory.setNamespaceAware(true);
|
||||||
|
@ -36,4 +36,16 @@ public class HandlerState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setFeed(Feed feed) {
|
||||||
|
this.feed = feed;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setCurrentItem(FeedItem currentItem) {
|
||||||
|
this.currentItem = currentItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -10,49 +10,64 @@ import de.podfetcher.feed.Feed;
|
|||||||
import de.podfetcher.syndication.namespace.Namespace;
|
import de.podfetcher.syndication.namespace.Namespace;
|
||||||
import de.podfetcher.syndication.namespace.SyndElement;
|
import de.podfetcher.syndication.namespace.SyndElement;
|
||||||
import de.podfetcher.syndication.namespace.atom.NSAtom;
|
import de.podfetcher.syndication.namespace.atom.NSAtom;
|
||||||
|
import de.podfetcher.syndication.namespace.rss20.NSRSS20;
|
||||||
|
|
||||||
|
// TODO implement default namespace
|
||||||
/** Superclass for all SAX Handlers which process Syndication formats */
|
/** Superclass for all SAX Handlers which process Syndication formats */
|
||||||
public abstract class SyndHandler extends DefaultHandler{
|
public class SyndHandler extends DefaultHandler {
|
||||||
private static final String TAG = "SyndHandler";
|
private static final String TAG = "SyndHandler";
|
||||||
protected HandlerState state;
|
protected HandlerState state;
|
||||||
|
|
||||||
public SyndHandler(Feed feed) {
|
public SyndHandler(Feed feed) {
|
||||||
state = new HandlerState(feed);
|
state = new HandlerState(feed);
|
||||||
|
state.namespaces.put("", new NSRSS20()); // TODO remove later
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void startElement(String uri, String localName, String qName,
|
public void startElement(String uri, String localName, String qName,
|
||||||
Attributes attributes) throws SAXException {
|
Attributes attributes) throws SAXException {
|
||||||
state.tagstack.push(new SyndElement(qName));
|
|
||||||
|
|
||||||
|
|
||||||
Namespace handler = state.namespaces.get(uri);
|
Namespace handler = state.namespaces.get(uri);
|
||||||
if (handler != null) {
|
if (handler != null) {
|
||||||
handler.handleElement(localName, state, attributes);
|
handler.handleElementStart(localName, state, attributes);
|
||||||
|
state.tagstack.push(new SyndElement(localName, handler));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void characters(char[] ch, int start, int length)
|
||||||
|
throws SAXException {
|
||||||
|
|
||||||
|
SyndElement top = state.tagstack.peek();
|
||||||
|
if (top.getNamespace() != null) {
|
||||||
|
top.getNamespace().handleCharacters(state, ch, start, length);
|
||||||
|
}
|
||||||
|
// ignore element otherwise
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void endElement(String uri, String localName, String qName)
|
public void endElement(String uri, String localName, String qName)
|
||||||
throws SAXException {
|
throws SAXException {
|
||||||
state.tagstack.pop();
|
Namespace handler = state.namespaces.get(uri);
|
||||||
|
if (handler != null) {
|
||||||
|
handler.handleElementEnd(localName, state);
|
||||||
|
state.tagstack.pop();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void endPrefixMapping(String prefix) throws SAXException {
|
public void endPrefixMapping(String prefix) throws SAXException {
|
||||||
// TODO remove Namespace
|
// TODO remove Namespace
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void startPrefixMapping(String prefix, String uri)
|
public void startPrefixMapping(String prefix, String uri)
|
||||||
throws SAXException {
|
throws SAXException {
|
||||||
Log.d(TAG, "Found Prefix Mapping with prefix " + prefix + " and uri " + uri);
|
Log.d(TAG, "Found Prefix Mapping with prefix " + prefix + " and uri "
|
||||||
|
+ uri);
|
||||||
// Find the right namespace
|
// Find the right namespace
|
||||||
if (prefix.equals(NSAtom.NSTAG) || uri.equals(NSAtom.NSURI)) {
|
if (prefix.equals(NSAtom.NSTAG) || uri.equals(NSAtom.NSURI)) {
|
||||||
state.namespaces.put(uri, new NSAtom());
|
state.namespaces.put(uri, new NSAtom());
|
||||||
|
@ -10,10 +10,17 @@ public abstract class Namespace {
|
|||||||
public static final String NSTAG = null;
|
public static final String NSTAG = null;
|
||||||
public static final String NSURI = null;
|
public static final String NSURI = null;
|
||||||
|
|
||||||
/** Called by a Feedhandler when in startElement and it detects a namespace element */
|
/** Called by a Feedhandler when in startElement and it detects a namespace element
|
||||||
public abstract void handleElement(String localName, HandlerState state, Attributes attributes);
|
* @return true if namespace handled the element, false if it ignored it
|
||||||
|
* */
|
||||||
|
public abstract void handleElementStart(String localName, HandlerState state, Attributes attributes);
|
||||||
|
|
||||||
/** Called by a Feedhandler when in characters and it detects a namespace element */
|
/** Called by a Feedhandler when in characters and it detects a namespace element */
|
||||||
public abstract void handleCharacters(String localName, Feed feed, char ch[], int start, int length);
|
public abstract void handleCharacters(HandlerState state, char ch[], int start, int length);
|
||||||
|
|
||||||
|
/** Called by a Feedhandler when in endElement and it detects a namespace element
|
||||||
|
* @return true if namespace handled the element, false if it ignored it
|
||||||
|
* */
|
||||||
|
public abstract void handleElementEnd(String localName, HandlerState state);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,12 +6,12 @@ public class SyndElement {
|
|||||||
protected Namespace namespace;
|
protected Namespace namespace;
|
||||||
|
|
||||||
public SyndElement(String name, Namespace namespace) {
|
public SyndElement(String name, Namespace namespace) {
|
||||||
super();
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
this.namespace = namespace;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Namespace getNamespace() {
|
public Namespace getNamespace() {
|
||||||
return null;
|
return namespace;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
@ -16,17 +16,22 @@ public class NSAtom extends Namespace {
|
|||||||
private static final String AUTHOR = "author";
|
private static final String AUTHOR = "author";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleElement(String localName, HandlerState state, Attributes attributes) {
|
public void handleElementStart(String localName, HandlerState state,
|
||||||
|
Attributes attributes) {
|
||||||
if (localName.equals(TITLE)) {
|
if (localName.equals(TITLE)) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleCharacters(HandlerState state, char[] ch, int start,
|
||||||
|
int length) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleCharacters(String localName, Feed feed, char[] ch,
|
public void handleElementEnd(String localName, HandlerState state) {
|
||||||
int start, int length) {
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package de.podfetcher.syndication.handler;
|
package de.podfetcher.syndication.namespace.rss20;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
@ -6,6 +6,9 @@ import de.podfetcher.feed.Feed;
|
|||||||
import de.podfetcher.feed.FeedImage;
|
import de.podfetcher.feed.FeedImage;
|
||||||
import de.podfetcher.feed.FeedItem;
|
import de.podfetcher.feed.FeedItem;
|
||||||
import de.podfetcher.feed.FeedMedia;
|
import de.podfetcher.feed.FeedMedia;
|
||||||
|
import de.podfetcher.syndication.handler.HandlerState;
|
||||||
|
import de.podfetcher.syndication.handler.SyndHandler;
|
||||||
|
import de.podfetcher.syndication.namespace.Namespace;
|
||||||
import de.podfetcher.syndication.namespace.SyndElement;
|
import de.podfetcher.syndication.namespace.SyndElement;
|
||||||
|
|
||||||
import org.xml.sax.Attributes;
|
import org.xml.sax.Attributes;
|
||||||
@ -18,7 +21,10 @@ import org.xml.sax.helpers.DefaultHandler;
|
|||||||
* @author daniel
|
* @author daniel
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class RSSHandler extends SyndHandler {
|
public class NSRSS20 extends Namespace {
|
||||||
|
public static final String NSTAG = "rss";
|
||||||
|
public static final String NSURI = "";
|
||||||
|
|
||||||
public final static String CHANNEL = "channel";
|
public final static String CHANNEL = "channel";
|
||||||
public final static String ITEM = "item";
|
public final static String ITEM = "item";
|
||||||
public final static String TITLE = "title";
|
public final static String TITLE = "title";
|
||||||
@ -33,76 +39,67 @@ public class RSSHandler extends SyndHandler {
|
|||||||
public final static String ENC_LEN = "length";
|
public final static String ENC_LEN = "length";
|
||||||
public final static String ENC_TYPE = "type";
|
public final static String ENC_TYPE = "type";
|
||||||
|
|
||||||
|
|
||||||
public RSSHandler(Feed feed) {
|
|
||||||
super(feed);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void characters(char[] ch, int start, int length)
|
public void handleElementStart(String localName, HandlerState state,
|
||||||
throws SAXException {
|
Attributes attributes) {
|
||||||
if (state.tagstack.size() >= 2) {
|
|
||||||
String content = new String(ch, start, length);
|
|
||||||
SyndElement topElement = state.tagstack.pop();
|
|
||||||
String top = topElement.getName();
|
|
||||||
String second = state.tagstack.peek().getName();
|
|
||||||
state.tagstack.push(topElement);
|
|
||||||
if (top.equals(TITLE)) {
|
|
||||||
if (second.equals(ITEM)) {
|
|
||||||
state.currentItem.setTitle(content);
|
|
||||||
} else if (second.equals(CHANNEL)) {
|
|
||||||
state.feed.setTitle(content);
|
|
||||||
} else if (second.equals(IMAGE)) {
|
|
||||||
state.feed.getImage().setTitle(IMAGE);
|
|
||||||
}
|
|
||||||
} else if (top.equals(DESCR)) {
|
|
||||||
if (second.equals(CHANNEL)) {
|
|
||||||
state.feed.setDescription(content);
|
|
||||||
} else if (second.equals(ITEM)) {
|
|
||||||
state.feed.setDescription(content);
|
|
||||||
}
|
|
||||||
} else if (top.equals(LINK)) {
|
|
||||||
if (second.equals(CHANNEL)) {
|
|
||||||
state.feed.setLink(content);
|
|
||||||
} else if (second.equals(ITEM)) {
|
|
||||||
state.currentItem.setLink(content);
|
|
||||||
}
|
|
||||||
} else if (top.equals(PUBDATE) && second.equals(ITEM)) {
|
|
||||||
state.currentItem.setPubDate(content);
|
|
||||||
} else if (top.equals(URL) && second.equals(IMAGE)) {
|
|
||||||
state.feed.getImage().setDownload_url(content);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void endElement(String uri, String localName, String qName)
|
|
||||||
throws SAXException {
|
|
||||||
if (localName.equals(ITEM)) {
|
if (localName.equals(ITEM)) {
|
||||||
state.currentItem = null;
|
state.setCurrentItem(new FeedItem());
|
||||||
}
|
state.getFeed().getItems().add(state.getCurrentItem());
|
||||||
super.endElement(uri, localName, qName);
|
state.getCurrentItem().setFeed(state.getFeed());
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void startElement(String uri, String localName, String qName,
|
|
||||||
Attributes attributes) throws SAXException {
|
|
||||||
|
|
||||||
if (localName.equals(ITEM)) {
|
|
||||||
state.currentItem = new FeedItem();
|
|
||||||
state.feed.getItems().add(state.currentItem);
|
|
||||||
state.currentItem.setFeed(state.feed);
|
|
||||||
|
|
||||||
} else if (localName.equals(ENCLOSURE)) {
|
} else if (localName.equals(ENCLOSURE)) {
|
||||||
state.currentItem
|
state.getCurrentItem()
|
||||||
.setMedia(new FeedMedia(state.currentItem, attributes
|
.setMedia(new FeedMedia(state.getCurrentItem(), attributes
|
||||||
.getValue(ENC_URL), Long.parseLong(attributes
|
.getValue(ENC_URL), Long.parseLong(attributes
|
||||||
.getValue(ENC_LEN)), attributes.getValue(ENC_TYPE)));
|
.getValue(ENC_LEN)), attributes.getValue(ENC_TYPE)));
|
||||||
} else if (localName.equals(IMAGE)) {
|
} else if (localName.equals(IMAGE)) {
|
||||||
state.feed.setImage(new FeedImage());
|
state.getFeed().setImage(new FeedImage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleCharacters(HandlerState state, char[] ch, int start,
|
||||||
|
int length) {
|
||||||
|
if (state.getTagstack().size() >= 2) {
|
||||||
|
String content = new String(ch, start, length);
|
||||||
|
SyndElement topElement = state.getTagstack().pop();
|
||||||
|
String top = topElement.getName();
|
||||||
|
String second = state.getTagstack().peek().getName();
|
||||||
|
state.getTagstack().push(topElement);
|
||||||
|
if (top.equals(TITLE)) {
|
||||||
|
if (second.equals(ITEM)) {
|
||||||
|
state.getCurrentItem().setTitle(content);
|
||||||
|
} else if (second.equals(CHANNEL)) {
|
||||||
|
state.getFeed().setTitle(content);
|
||||||
|
} else if (second.equals(IMAGE)) {
|
||||||
|
state.getFeed().getImage().setTitle(IMAGE);
|
||||||
|
}
|
||||||
|
} else if (top.equals(DESCR)) {
|
||||||
|
if (second.equals(CHANNEL)) {
|
||||||
|
state.getFeed().setDescription(content);
|
||||||
|
} else if (second.equals(ITEM)) {
|
||||||
|
state.getFeed().setDescription(content);
|
||||||
|
}
|
||||||
|
} else if (top.equals(LINK)) {
|
||||||
|
if (second.equals(CHANNEL)) {
|
||||||
|
state.getFeed().setLink(content);
|
||||||
|
} else if (second.equals(ITEM)) {
|
||||||
|
state.getCurrentItem().setLink(content);
|
||||||
|
}
|
||||||
|
} else if (top.equals(PUBDATE) && second.equals(ITEM)) {
|
||||||
|
state.getCurrentItem().setPubDate(content);
|
||||||
|
} else if (top.equals(URL) && second.equals(IMAGE)) {
|
||||||
|
state.getFeed().getImage().setDownload_url(content);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
super.startElement(uri, localName, qName, attributes);
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleElementEnd(String localName, HandlerState state) {
|
||||||
|
if (localName.equals(ITEM)) {
|
||||||
|
state.setCurrentItem(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user