Merge pull request #2147 from Cj-Malone/atom-clean-up

Parsing clean up
This commit is contained in:
Martin Fietz 2016-11-01 10:42:59 +01:00 committed by GitHub
commit cfef273eba
4 changed files with 92 additions and 30 deletions

View File

@ -26,6 +26,11 @@ public class NSMedia extends Namespace {
private static final String MIME_TYPE = "type";
private static final String DURATION = "duration";
private static final String DEFAULT = "isDefault";
private static final String MEDIUM = "medium";
private static final String MEDIUM_IMAGE = "image";
private static final String MEDIUM_AUDIO = "audio";
private static final String MEDIUM_VIDEO = "video";
private static final String IMAGE = "thumbnail";
private static final String IMAGE_URL = "url";
@ -40,20 +45,31 @@ public class NSMedia extends Namespace {
String url = attributes.getValue(DOWNLOAD_URL);
String type = attributes.getValue(MIME_TYPE);
String defaultStr = attributes.getValue(DEFAULT);
boolean validType;
String medium = attributes.getValue(MEDIUM);
boolean validTypeMedia = false;
boolean validTypeImage = false;
boolean isDefault = "true".equals(defaultStr);
if (SyndTypeUtils.enclosureTypeValid(type)) {
validType = true;
if (MEDIUM_AUDIO.equals(medium) || MEDIUM_VIDEO.equals(medium)) {
validTypeMedia = true;
} else if (MEDIUM_IMAGE.equals(medium)) {
validTypeImage = true;
} else {
type = SyndTypeUtils.getValidMimeTypeFromUrl(url);
validType = type != null;
if (type == null) {
type = SyndTypeUtils.getMimeTypeFromUrl(url);
}
if (SyndTypeUtils.enclosureTypeValid(type)) {
validTypeMedia = true;
} else if (SyndTypeUtils.imageTypeValid(type)) {
validTypeImage = true;
}
}
if (state.getCurrentItem() != null &&
(state.getCurrentItem().getMedia() == null || isDefault) &&
url != null && validType) {
url != null && validTypeMedia) {
long size = 0;
String sizeStr = attributes.getValue(SIZE);
try {
@ -77,6 +93,12 @@ public class NSMedia extends Namespace {
media.setDuration(durationMs);
}
state.getCurrentItem().setMedia(media);
} else if (state.getCurrentItem() != null && url != null && validTypeImage) {
FeedImage image = new FeedImage();
image.setDownload_url(url);
image.setOwner(state.getCurrentItem());
state.getCurrentItem().setImage(image);
}
} else if (IMAGE.equals(localName)) {
String url = attributes.getValue(IMAGE_URL);

View File

@ -53,14 +53,17 @@ public class NSRSS20 extends Namespace {
} else if (ENCLOSURE.equals(localName)) {
String type = attributes.getValue(ENC_TYPE);
String url = attributes.getValue(ENC_URL);
boolean validType;
boolean validType = false;
boolean validUrl = !TextUtils.isEmpty(url);
if (type == null) {
type = SyndTypeUtils.getMimeTypeFromUrl(url);
}
if(SyndTypeUtils.enclosureTypeValid(type)) {
validType = true;
} else {
type = SyndTypeUtils.getValidMimeTypeFromUrl(url);
validType = type != null;
}
boolean validUrl = !TextUtils.isEmpty(url);
if (state.getCurrentItem() != null && state.getCurrentItem().getMedia() == null &&
validType && validUrl) {
long size = 0;

View File

@ -45,6 +45,7 @@ public class NSAtom extends Namespace {
private static final String LINK_LENGTH = "length";
// rel-values
private static final String LINK_REL_ALTERNATE = "alternate";
private static final String LINK_REL_ARCHIVES = "archives";
private static final String LINK_REL_ENCLOSURE = "enclosure";
private static final String LINK_REL_PAYMENT = "payment";
private static final String LINK_REL_RELATED = "related";
@ -94,14 +95,12 @@ public class NSAtom extends Namespace {
Log.d(TAG, "Length attribute could not be parsed.");
}
String type = attributes.getValue(LINK_TYPE);
boolean validType;
if(SyndTypeUtils.enclosureTypeValid(type)) {
validType = true;
} else {
type = SyndTypeUtils.getValidMimeTypeFromUrl(href);
validType = type != null;
if (type == null) {
type = SyndTypeUtils.getMimeTypeFromUrl(href);
}
if (validType) {
if(SyndTypeUtils.enclosureTypeValid(type)) {
FeedItem currItem = state.getCurrentItem();
if(currItem != null && !currItem.hasMedia()) {
currItem.setMedia(new FeedMedia(currItem, href, size, type));
@ -130,6 +129,17 @@ public class NSAtom extends Namespace {
}
state.addAlternateFeedUrl(title, href);
}
} else if (LINK_REL_ARCHIVES.equals(rel) && state.getFeed() != null) {
String type = attributes.getValue(LINK_TYPE);
if (LINK_TYPE_ATOM.equals(type) || LINK_TYPE_RSS.equals(type)) {
String title = attributes.getValue(LINK_TITLE);
if (TextUtils.isEmpty(title)) {
title = href;
}
state.addAlternateFeedUrl(title, href);
} else if (LINK_TYPE_HTML.equals(type) || LINK_TYPE_XHTML.equals(type)) {
//A Link such as to a directory such as iTunes
}
} else if (LINK_REL_PAYMENT.equals(rel) && state.getFeed() != null) {
state.getFeed().setPaymentLink(href);
} else if (LINK_REL_NEXT.equals(rel) && state.getFeed() != null) {
@ -206,7 +216,12 @@ public class NSAtom extends Namespace {
state.getFeed().setImage(new FeedImage(state.getFeed(), content, null));
} else if (AUTHOR.equals(second) && state.getFeed() != null) {
if (AUTHOR_NAME.equals(top)) {
state.getFeed().setAuthor(content);
String currentName = state.getFeed().getAuthor();
if (currentName == null) {
state.getFeed().setAuthor(content);
} else {
state.getFeed().setAuthor(currentName + ", " + content);
}
}
}
}

View File

@ -6,8 +6,10 @@ import org.apache.commons.io.FilenameUtils;
/** Utility class for handling MIME-Types of enclosures */
public class SyndTypeUtils {
private static final String VALID_MIMETYPE = "audio/.*" + "|" + "video/.*"
private static final String VALID_MEDIA_MIMETYPE = "audio/.*" + "|" + "video/.*"
+ "|" + "application/ogg";
private static final String VALID_IMAGE_MIMETYPE = "image/.*";
private SyndTypeUtils() {
@ -17,9 +19,17 @@ public class SyndTypeUtils {
if (type == null) {
return false;
} else {
return type.matches(VALID_MIMETYPE);
return type.matches(VALID_MEDIA_MIMETYPE);
}
}
public static boolean imageTypeValid(String type) {
if (type == null) {
return false;
} else {
return type.matches(VALID_IMAGE_MIMETYPE);
}
}
/**
* Should be used if mime-type of enclosure tag is not supported. This
@ -27,15 +37,27 @@ public class SyndTypeUtils {
* the type is not supported, this method will return null.
*/
public static String getValidMimeTypeFromUrl(String url) {
if (url != null) {
String extension = FilenameUtils.getExtension(url);
if (extension != null) {
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
if (type != null && enclosureTypeValid(type)) {
return type;
}
}
String type = getMimeTypeFromUrl(url);
if (enclosureTypeValid(type)) {
return type;
} else {
return null;
}
return null;
}
/**
* Should be used if mime-type of enclosure tag is not supported. This
* method will return the mime-type of the file extension.
*/
public static String getMimeTypeFromUrl(String url) {
if (url == null) {
return null;
}
String extension = FilenameUtils.getExtension(url);
if (extension == null) {
return null;
}
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
}