Fixed crash when SimpleChapter 'start' tag was invalid

This commit is contained in:
daniel oeh 2014-10-05 18:35:56 +02:00
parent ae75f6f7f2
commit 19cb896800
2 changed files with 131 additions and 114 deletions

View File

@ -1,5 +1,8 @@
package de.danoeh.antennapod.syndication.namespace;
import android.util.Log;
import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.feed.Chapter;
import de.danoeh.antennapod.feed.SimpleChapter;
import de.danoeh.antennapod.syndication.handler.HandlerState;
@ -9,6 +12,8 @@ import org.xml.sax.Attributes;
import java.util.ArrayList;
public class NSSimpleChapters extends Namespace {
private static final String TAG = "NSSimpleChapters";
public static final String NSTAG = "psc|sc";
public static final String NSURI = "http://podlove.org/simple-chapters";
@ -24,12 +29,16 @@ public class NSSimpleChapters extends Namespace {
if (localName.equals(CHAPTERS)) {
state.getCurrentItem().setChapters(new ArrayList<Chapter>());
} else if (localName.equals(CHAPTER)) {
state.getCurrentItem()
.getChapters()
.add(new SimpleChapter(SyndDateUtils
.parseTimeString(attributes.getValue(START)),
attributes.getValue(TITLE), state.getCurrentItem(),
attributes.getValue(HREF)));
try {
state.getCurrentItem()
.getChapters()
.add(new SimpleChapter(SyndDateUtils
.parseTimeString(attributes.getValue(START)),
attributes.getValue(TITLE), state.getCurrentItem(),
attributes.getValue(HREF)));
} catch (NumberFormatException e) {
if (BuildConfig.DEBUG) Log.w(TAG, "Unable to read chapter", e);
}
}
return new SyndElement(localName, this);

View File

@ -7,130 +7,138 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/** Parses several date formats. */
/**
* Parses several date formats.
*/
public class SyndDateUtils {
private static final String TAG = "DateUtils";
private static final String TAG = "DateUtils";
private static final String[] RFC822DATES = { "dd MMM yy HH:mm:ss Z", };
private static final String[] RFC822DATES = {"dd MMM yy HH:mm:ss Z",};
/** RFC 3339 date format for UTC dates. */
public static final String RFC3339UTC = "yyyy-MM-dd'T'HH:mm:ss'Z'";
/**
* RFC 3339 date format for UTC dates.
*/
public static final String RFC3339UTC = "yyyy-MM-dd'T'HH:mm:ss'Z'";
/** RFC 3339 date format for localtime dates with offset. */
public static final String RFC3339LOCAL = "yyyy-MM-dd'T'HH:mm:ssZ";
/**
* RFC 3339 date format for localtime dates with offset.
*/
public static final String RFC3339LOCAL = "yyyy-MM-dd'T'HH:mm:ssZ";
private static ThreadLocal<SimpleDateFormat> RFC822Formatter = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(RFC822DATES[0], Locale.US);
}
private static ThreadLocal<SimpleDateFormat> RFC822Formatter = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(RFC822DATES[0], Locale.US);
}
};
};
private static ThreadLocal<SimpleDateFormat> RFC3339Formatter = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(RFC3339UTC, Locale.US);
}
private static ThreadLocal<SimpleDateFormat> RFC3339Formatter = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(RFC3339UTC, Locale.US);
}
};
};
public static Date parseRFC822Date(String date) {
Date result = null;
if (date.contains("PDT")) {
date = date.replace("PDT", "PST8PDT");
}
if (date.contains(",")) {
// Remove day of the week
date = date.substring(date.indexOf(",") + 1).trim();
}
SimpleDateFormat format = RFC822Formatter.get();
for (int i = 0; i < RFC822DATES.length; i++) {
try {
format.applyPattern(RFC822DATES[i]);
result = format.parse(date);
break;
} catch (ParseException e) {
e.printStackTrace();
}
}
if (result == null) {
Log.e(TAG, "Unable to parse feed date correctly");
}
public static Date parseRFC822Date(String date) {
Date result = null;
if (date.contains("PDT")) {
date = date.replace("PDT", "PST8PDT");
}
if (date.contains(",")) {
// Remove day of the week
date = date.substring(date.indexOf(",") + 1).trim();
}
SimpleDateFormat format = RFC822Formatter.get();
for (int i = 0; i < RFC822DATES.length; i++) {
try {
format.applyPattern(RFC822DATES[i]);
result = format.parse(date);
break;
} catch (ParseException e) {
e.printStackTrace();
}
}
if (result == null) {
Log.e(TAG, "Unable to parse feed date correctly");
}
return result;
}
return result;
}
public static Date parseRFC3339Date(String date) {
Date result = null;
SimpleDateFormat format = RFC3339Formatter.get();
boolean isLocal = date.endsWith("Z");
if (date.contains(".")) {
// remove secfrac
int fracIndex = date.indexOf(".");
String first = date.substring(0, fracIndex);
String second = null;
if (isLocal) {
second = date.substring(date.length() - 1);
} else {
if (date.contains("+")) {
second = date.substring(date.indexOf("+"));
} else {
second = date.substring(date.indexOf("-"));
}
}
public static Date parseRFC3339Date(String date) {
Date result = null;
SimpleDateFormat format = RFC3339Formatter.get();
boolean isLocal = date.endsWith("Z");
if (date.contains(".")) {
// remove secfrac
int fracIndex = date.indexOf(".");
String first = date.substring(0, fracIndex);
String second = null;
if (isLocal) {
second = date.substring(date.length() - 1);
} else {
if (date.contains("+")) {
second = date.substring(date.indexOf("+"));
} else {
second = date.substring(date.indexOf("-"));
}
}
date = first + second;
}
if (isLocal) {
try {
result = format.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
} else {
format.applyPattern(RFC3339LOCAL);
// remove last colon
StringBuffer buf = new StringBuffer(date.length() - 1);
int colonIdx = date.lastIndexOf(':');
for (int x = 0; x < date.length(); x++) {
if (x != colonIdx)
buf.append(date.charAt(x));
}
String bufStr = buf.toString();
try {
result = format.parse(bufStr);
} catch (ParseException e) {
e.printStackTrace();
Log.e(TAG, "Unable to parse date");
} finally {
format.applyPattern(RFC3339UTC);
}
date = first + second;
}
if (isLocal) {
try {
result = format.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
} else {
format.applyPattern(RFC3339LOCAL);
// remove last colon
StringBuffer buf = new StringBuffer(date.length() - 1);
int colonIdx = date.lastIndexOf(':');
for (int x = 0; x < date.length(); x++) {
if (x != colonIdx)
buf.append(date.charAt(x));
}
String bufStr = buf.toString();
try {
result = format.parse(bufStr);
} catch (ParseException e) {
e.printStackTrace();
Log.e(TAG, "Unable to parse date");
} finally {
format.applyPattern(RFC3339UTC);
}
}
}
return result;
return result;
}
}
/**
* Takes a string of the form [HH:]MM:SS[.mmm] and converts it to
* milliseconds.
*/
public static long parseTimeString(final String time) {
String[] parts = time.split(":");
long result = 0;
int idx = 0;
if (parts.length == 3) {
// string has hours
result += Integer.valueOf(parts[idx]) * 3600000L;
idx++;
}
result += Integer.valueOf(parts[idx]) * 60000L;
idx++;
result += (Float.valueOf(parts[idx])) * 1000L;
return result;
}
/**
* Takes a string of the form [HH:]MM:SS[.mmm] and converts it to
* milliseconds.
*
* @throws java.lang.NumberFormatException if the number segments contain invalid numbers.
*/
public static long parseTimeString(final String time) {
String[] parts = time.split(":");
long result = 0;
int idx = 0;
if (parts.length == 3) {
// string has hours
result += Integer.valueOf(parts[idx]) * 3600000L;
idx++;
}
result += Integer.valueOf(parts[idx]) * 60000L;
idx++;
result += (Float.valueOf(parts[idx])) * 1000L;
return result;
}
public static String formatRFC822Date(Date date) {
SimpleDateFormat format = RFC822Formatter.get();