More work on Atom namespace

This commit is contained in:
daniel oeh 2012-06-10 12:15:22 +02:00
parent d397ef0df7
commit 19053b2b59
5 changed files with 135 additions and 3 deletions

View File

@ -12,6 +12,7 @@ import java.util.ArrayList;
*/ */
public class Feed extends FeedFile{ public class Feed extends FeedFile{
private String title; private String title;
/** Link to the website. */
private String link; private String link;
private String description; private String description;
private FeedImage image; private FeedImage image;

View File

@ -0,0 +1,15 @@
package de.podfetcher.syndication.namespace.atom;
import de.podfetcher.syndication.namespace.Namespace;
import de.podfetcher.syndication.namespace.SyndElement;
/** Represents a "link" - Element in an Atom Feed. */
public class AtomLink extends SyndElement {
private String href, rel, title, type, length;
public AtomLink(String name, Namespace namespace) {
super(name, namespace);
// TODO Auto-generated constructor stub
}
}

View File

@ -0,0 +1,46 @@
package de.podfetcher.syndication.namespace.atom;
import de.podfetcher.syndication.namespace.Namespace;
import de.podfetcher.syndication.namespace.SyndElement;
import de.podfetcher.syndication.util.HtmlUnescaper;
/** Represents Atom Element which contains text (content, title, summary). */
public class AtomText extends SyndElement {
public static final String TYPE_TEXT = "text";
public static final String TYPE_HTML = "html";
public static final String TYPE_XHTML = "xhtml";
private String type;
private String content;
public AtomText(String name, Namespace namespace, String type) {
super(name, namespace);
this.type = type;
}
/** Processes the content according to the type and returns it. */
public String getProcessedContent() {
if (type.equals(TYPE_HTML)) {
return HtmlUnescaper.unescape(content);
} else if (type.equals(TYPE_XHTML)) {
return content;
} else { // Handle as text by default
return content;
}
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getType() {
return type;
}
}

View File

@ -3,6 +3,8 @@ package de.podfetcher.syndication.namespace.atom;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
import de.podfetcher.feed.Feed; import de.podfetcher.feed.Feed;
import de.podfetcher.feed.FeedItem;
import de.podfetcher.feed.FeedMedia;
import de.podfetcher.syndication.handler.HandlerState; import de.podfetcher.syndication.handler.HandlerState;
import de.podfetcher.syndication.namespace.Namespace; import de.podfetcher.syndication.namespace.Namespace;
import de.podfetcher.syndication.namespace.SyndElement; import de.podfetcher.syndication.namespace.SyndElement;
@ -11,18 +13,61 @@ public class NSAtom extends Namespace {
public static final String NSTAG = "atom"; public static final String NSTAG = "atom";
public static final String NSURI = "http://www.w3.org/2005/Atom"; public static final String NSURI = "http://www.w3.org/2005/Atom";
private static final String FEED = "feed";
private static final String TITLE = "title"; private static final String TITLE = "title";
private static final String ENTRY = "entry";
private static final String LINK = "link"; private static final String LINK = "link";
private static final String UPDATED = "updated"; private static final String UPDATED = "updated";
private static final String AUTHOR = "author"; private static final String AUTHOR = "author";
private static final String CONTENT = "content";
private static final String TEXT_TYPE = "type";
// Link
private static final String LINK_HREF = "href";
private static final String LINK_REL = "rel";
private static final String LINK_TYPE = "type";
private static final String LINK_TITLE = "title";
private static final String LINK_LENGTH = "length";
// rel-values
private static final String LINK_REL_ALTERNATE = "alternate";
private static final String LINK_REL_ENCLOSURE = "enclosure";
private static final String LINK_REL_RELATED = "related";
private static final String LINK_REL_SELF = "self";
@Override @Override
public SyndElement handleElementStart(String localName, HandlerState state, public SyndElement handleElementStart(String localName, HandlerState state,
Attributes attributes) { Attributes attributes) {
if (localName.equals(TITLE)) { if (localName.equals(ENTRY)) {
state.setCurrentItem(new FeedItem());
state.getFeed().getItems().add(state.getCurrentItem());
state.getCurrentItem().setFeed(state.getFeed());
} else if (localName.equals(TITLE) || localName.equals(CONTENT)) {
String type = attributes.getValue(null, TEXT_TYPE);
return new AtomText(localName, this, type);
} else if (localName.equals(LINK)) {
String href = attributes.getValue(null, LINK_HREF);
String rel = attributes.getValue(null, LINK_REL);
SyndElement parent = state.getTagstack().peek();
if (parent.getName().equals(ENTRY)) {
if (rel == null || rel.equals(LINK_REL_ALTERNATE)) {
state.getCurrentItem().setLink(href);
} else if (rel.equals(LINK_REL_ENCLOSURE)) {
long size = Long.parseLong(attributes.getValue(null,
LINK_LENGTH));
String type = attributes.getValue(null, LINK_TYPE);
String download_url = attributes.getValue(null,
LINK_REL_ENCLOSURE);
state.getCurrentItem().setMedia(
new FeedMedia(state.getCurrentItem(), download_url,
size, type));
}
} else if (parent.getName().equals(FEED)) {
if (rel == null || rel.equals(LINK_REL_ALTERNATE)) {
state.getCurrentItem().setLink(href);
}
}
} }
return null; return new SyndElement(localName, this);
} }
@Override @Override

View File

@ -0,0 +1,25 @@
package de.podfetcher.syndication.util;
import java.util.HashMap;
/** Unescapes HTML */
public class HtmlUnescaper {
private static HashMap<String, String> symbols;
static {
symbols.put("&nbsp", " ");
symbols.put("&quot", "\"");
symbols.put("&amp", "&");
symbols.put("&lt", "<");
symbols.put("&gt", ">");
}
public static String unescape(final String source) {
String result = source;
for (String key : symbols.keySet()) {
result = result.replaceAll(key, symbols.get(key));
}
return result;
}
}