diff options
author | Anderson Mesquita <andersonvom@gmail.com> | 2019-05-02 22:07:20 -0400 |
---|---|---|
committer | Anderson Mesquita <andersonvom@gmail.com> | 2019-05-04 13:59:05 -0400 |
commit | a4c852dac050e426587501b7b792cedb90571d4e (patch) | |
tree | 8aae3e8faf899c939dc383f72b1815d757741c76 /core | |
parent | 0107cf22cc8836c876696d56c98ae882d0c7dc78 (diff) | |
download | AntennaPod-a4c852dac050e426587501b7b792cedb90571d4e.zip |
Extract methods in NSITunes
References: #3024
Diffstat (limited to 'core')
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSITunes.java | 120 |
1 files changed, 70 insertions, 50 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSITunes.java b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSITunes.java index b3b8a40ce..62bc55e2e 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSITunes.java +++ b/core/src/main/java/de/danoeh/antennapod/core/syndication/namespace/NSITunes.java @@ -7,6 +7,7 @@ import org.xml.sax.Attributes; import java.util.concurrent.TimeUnit; +import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.syndication.handler.HandlerState; public class NSITunes extends Namespace { @@ -22,7 +23,6 @@ public class NSITunes extends Namespace { private static final String SUBTITLE = "subtitle"; private static final String SUMMARY = "summary"; - @Override public SyndElement handleElementStart(String localName, HandlerState state, Attributes attributes) { @@ -44,65 +44,85 @@ public class NSITunes extends Namespace { @Override public void handleElementEnd(String localName, HandlerState state) { - if(state.getContentBuf() == null) { + if (state.getContentBuf() == null) { return; } - SyndElement secondElement = state.getSecondTag(); - String second = secondElement.getName(); if (AUTHOR.equals(localName)) { - if (state.getFeed() != null) { - String author = state.getContentBuf().toString(); - state.getFeed().setAuthor(author); - } + parseAuthor(state); } else if (DURATION.equals(localName)) { - String durationStr = state.getContentBuf().toString(); - if(TextUtils.isEmpty(durationStr)) { - return; - } - String[] parts = durationStr.trim().split(":"); - try { - int durationMs = 0; - if (parts.length == 2) { - durationMs += TimeUnit.MINUTES.toMillis(Long.parseLong(parts[0])) + - TimeUnit.SECONDS.toMillis((long)Float.parseFloat(parts[1])); - } else if (parts.length >= 3) { - durationMs += TimeUnit.HOURS.toMillis(Long.parseLong(parts[0])) + - TimeUnit.MINUTES.toMillis(Long.parseLong(parts[1])) + - TimeUnit.SECONDS.toMillis((long)Float.parseFloat(parts[2])); - } else { - return; - } - state.getTempObjects().put(DURATION, durationMs); - } catch (NumberFormatException e) { - Log.e(NSTAG, "Duration \"" + durationStr + "\" could not be parsed"); - } + parseDuration(state); } else if (SUBTITLE.equals(localName)) { - String subtitle = state.getContentBuf().toString(); - if (TextUtils.isEmpty(subtitle)) { - return; - } - if (state.getCurrentItem() != null) { - if (TextUtils.isEmpty(state.getCurrentItem().getDescription())) { - state.getCurrentItem().setDescription(subtitle); - } - } else { - if (state.getFeed() != null && TextUtils.isEmpty(state.getFeed().getDescription())) { - state.getFeed().setDescription(subtitle); - } - } + parseSubtitle(state); } else if (SUMMARY.equals(localName)) { - String summary = state.getContentBuf().toString(); - if (TextUtils.isEmpty(summary)) { + SyndElement secondElement = state.getSecondTag(); + parseSummary(state, secondElement.getName()); + } + } + + private void parseAuthor(HandlerState state) { + if (state.getFeed() != null) { + String author = state.getContentBuf().toString(); + state.getFeed().setAuthor(author); + } + } + + private void parseDuration(HandlerState state) { + String durationStr = state.getContentBuf().toString(); + if (TextUtils.isEmpty(durationStr)) { + return; + } + String[] parts = durationStr.trim().split(":"); + try { + int durationMs = 0; + if (parts.length == 2) { + durationMs += TimeUnit.MINUTES.toMillis(Long.parseLong(parts[0])) + + TimeUnit.SECONDS.toMillis((long) Float.parseFloat(parts[1])); + } else if (parts.length >= 3) { + durationMs += TimeUnit.HOURS.toMillis(Long.parseLong(parts[0])) + + TimeUnit.MINUTES.toMillis(Long.parseLong(parts[1])) + + TimeUnit.SECONDS.toMillis((long) Float.parseFloat(parts[2])); + } else { return; } - if (state.getCurrentItem() != null && - (TextUtils.isEmpty(state.getCurrentItem().getDescription()) || - state.getCurrentItem().getDescription().length() * 1.25 < summary.length())) { - state.getCurrentItem().setDescription(summary); - } else if (NSRSS20.CHANNEL.equals(second) && state.getFeed() != null) { - state.getFeed().setDescription(summary); + state.getTempObjects().put(DURATION, durationMs); + } catch (NumberFormatException e) { + Log.e(NSTAG, "Duration \"" + durationStr + "\" could not be parsed"); + } + } + + private void parseSubtitle(HandlerState state) { + String subtitle = state.getContentBuf().toString(); + if (TextUtils.isEmpty(subtitle)) { + return; + } + if (state.getCurrentItem() != null) { + if (TextUtils.isEmpty(state.getCurrentItem().getDescription())) { + state.getCurrentItem().setDescription(subtitle); + } + } else { + if (state.getFeed() != null && TextUtils.isEmpty(state.getFeed().getDescription())) { + state.getFeed().setDescription(subtitle); } } } + + private void parseSummary(HandlerState state, String secondElementName) { + String summary = state.getContentBuf().toString(); + if (TextUtils.isEmpty(summary)) { + return; + } + + FeedItem currentItem = state.getCurrentItem(); + String description = getDescription(currentItem); + if (currentItem != null && description.length() * 1.25 < summary.length()) { + currentItem.setDescription(summary); + } else if (NSRSS20.CHANNEL.equals(secondElementName) && state.getFeed() != null) { + state.getFeed().setDescription(summary); + } + } + + private String getDescription(FeedItem item) { + return (item != null && item.getDescription() != null) ? item.getDescription() : ""; + } } |