Recognize DublinCore date tags

This commit is contained in:
Martin Fietz 2015-02-27 19:59:46 +01:00
parent 30a202665a
commit 172d493d3f
3 changed files with 83 additions and 5 deletions

View File

@ -1,14 +1,23 @@
package de.danoeh.antennapod.core.syndication.handler;
import android.util.Log;
import de.danoeh.antennapod.core.BuildConfig;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.syndication.namespace.*;
import de.danoeh.antennapod.core.syndication.namespace.atom.NSAtom;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import de.danoeh.antennapod.core.BuildConfig;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.syndication.namespace.NSContent;
import de.danoeh.antennapod.core.syndication.namespace.NSDublinCore;
import de.danoeh.antennapod.core.syndication.namespace.NSITunes;
import de.danoeh.antennapod.core.syndication.namespace.NSMedia;
import de.danoeh.antennapod.core.syndication.namespace.NSRSS20;
import de.danoeh.antennapod.core.syndication.namespace.NSSimpleChapters;
import de.danoeh.antennapod.core.syndication.namespace.Namespace;
import de.danoeh.antennapod.core.syndication.namespace.SyndElement;
import de.danoeh.antennapod.core.syndication.namespace.atom.NSAtom;
/** Superclass for all SAX Handlers which process Syndication formats */
public class SyndHandler extends DefaultHandler {
private static final String TAG = "SyndHandler";
@ -100,7 +109,12 @@ public class SyndHandler extends DefaultHandler {
state.namespaces.put(uri, new NSMedia());
if (BuildConfig.DEBUG)
Log.d(TAG, "Recognized media namespace");
}
} else if (uri.equals(NSDublinCore.NSURI)
&& prefix.equals(NSDublinCore.NSTAG)) {
state.namespaces.put(uri, new NSDublinCore());
if (BuildConfig.DEBUG)
Log.d(TAG, "Recognized DublinCore namespace");
}
}
}

View File

@ -0,0 +1,37 @@
package de.danoeh.antennapod.core.syndication.namespace;
import org.xml.sax.Attributes;
import de.danoeh.antennapod.core.syndication.handler.HandlerState;
import de.danoeh.antennapod.core.syndication.util.SyndDateUtils;
public class NSDublinCore extends Namespace {
private static final String TAG = "NSDublinCore";
public static final String NSTAG = "dc";
public static final String NSURI = "http://purl.org/dc/elements/1.1/";
private static final String ITEM = "item";
private static final String DATE = "date";
@Override
public SyndElement handleElementStart(String localName, HandlerState state,
Attributes attributes) {
return new SyndElement(localName, this);
}
@Override
public void handleElementEnd(String localName, HandlerState state) {
if(state.getTagstack().size() >= 2
&& state.getContentBuf() != null) {
String content = state.getContentBuf().toString();
SyndElement topElement = state.getTagstack().peek();
String top = topElement.getName();
SyndElement secondElement = state.getSecondTag();
String second = secondElement.getName();
if (top.equals(DATE) && second.equals(ITEM)) {
state.getCurrentItem().setPubDate(
SyndDateUtils.parseISO8601Date(content));
}
}
}
}

View File

@ -28,6 +28,8 @@ public class SyndDateUtils {
*/
public static final String RFC3339LOCAL = "yyyy-MM-dd'T'HH:mm:ssZ";
public static final String ISO8601_SHORT = "yyyy-MM-dd";
private static ThreadLocal<SimpleDateFormat> RFC822Formatter = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
@ -44,6 +46,14 @@ public class SyndDateUtils {
};
private static ThreadLocal<SimpleDateFormat> ISO8601ShortFormatter = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(ISO8601_SHORT, Locale.US);
}
};
public static Date parseRFC822Date(String date) {
Date result = null;
if (date.contains("PDT")) {
@ -123,6 +133,23 @@ public class SyndDateUtils {
}
public static Date parseISO8601Date(String date) {
if(date.length() > ISO8601_SHORT.length()) {
return parseRFC3339Date(date);
}
Date result = null;
if(date.length() == "YYYYMMDD".length()) {
date = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6,8);
}
SimpleDateFormat format = ISO8601ShortFormatter.get();
try {
result = format.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}
/**
* Takes a string of the form [HH:]MM:SS[.mmm] and converts it to
* milliseconds.