Search in sources :

Example 1 with AtomText

use of de.danoeh.antennapod.parser.feed.element.AtomText in project AntennaPod by AntennaPod.

the class AtomTextTest method testProcessingHtml.

@Test
public void testProcessingHtml() {
    for (String[] pair : TEST_DATA) {
        final AtomText atomText = new AtomText("", new Atom(), AtomText.TYPE_HTML);
        atomText.setContent(pair[0]);
        assertEquals(pair[1], atomText.getProcessedContent());
    }
}
Also used : AtomText(de.danoeh.antennapod.parser.feed.element.AtomText) Atom(de.danoeh.antennapod.parser.feed.namespace.Atom) Test(org.junit.Test)

Example 2 with AtomText

use of de.danoeh.antennapod.parser.feed.element.AtomText in project AntennaPod by AntennaPod.

the class Atom method handleElementEnd.

@Override
public void handleElementEnd(String localName, HandlerState state) {
    if (ENTRY.equals(localName)) {
        if (state.getCurrentItem() != null && state.getTempObjects().containsKey(Itunes.DURATION)) {
            FeedItem currentItem = state.getCurrentItem();
            if (currentItem.hasMedia()) {
                Integer duration = (Integer) state.getTempObjects().get(Itunes.DURATION);
                currentItem.getMedia().setDuration(duration);
            }
            state.getTempObjects().remove(Itunes.DURATION);
        }
        state.setCurrentItem(null);
    }
    if (state.getTagstack().size() >= 2) {
        AtomText textElement = null;
        String contentRaw;
        if (state.getContentBuf() != null) {
            contentRaw = state.getContentBuf().toString();
        } else {
            contentRaw = "";
        }
        String content = SyndStringUtils.trimAllWhitespace(contentRaw);
        SyndElement topElement = state.getTagstack().peek();
        String top = topElement.getName();
        SyndElement secondElement = state.getSecondTag();
        String second = secondElement.getName();
        if (top.matches(isText)) {
            textElement = (AtomText) topElement;
            textElement.setContent(content);
        }
        if (ID.equals(top)) {
            if (FEED.equals(second) && state.getFeed() != null) {
                state.getFeed().setFeedIdentifier(contentRaw);
            } else if (ENTRY.equals(second) && state.getCurrentItem() != null) {
                state.getCurrentItem().setItemIdentifier(contentRaw);
            }
        } else if (TITLE.equals(top) && textElement != null) {
            if (FEED.equals(second) && state.getFeed() != null) {
                state.getFeed().setTitle(textElement.getProcessedContent());
            } else if (ENTRY.equals(second) && state.getCurrentItem() != null) {
                state.getCurrentItem().setTitle(textElement.getProcessedContent());
            }
        } else if (SUBTITLE.equals(top) && FEED.equals(second) && textElement != null && state.getFeed() != null) {
            state.getFeed().setDescription(textElement.getProcessedContent());
        } else if (CONTENT.equals(top) && ENTRY.equals(second) && textElement != null && state.getCurrentItem() != null) {
            state.getCurrentItem().setDescriptionIfLonger(textElement.getProcessedContent());
        } else if (SUMMARY.equals(top) && ENTRY.equals(second) && textElement != null && state.getCurrentItem() != null) {
            state.getCurrentItem().setDescriptionIfLonger(textElement.getProcessedContent());
        } else if (UPDATED.equals(top) && ENTRY.equals(second) && state.getCurrentItem() != null && state.getCurrentItem().getPubDate() == null) {
            state.getCurrentItem().setPubDate(DateUtils.parseOrNullIfFuture(content));
        } else if (PUBLISHED.equals(top) && ENTRY.equals(second) && state.getCurrentItem() != null) {
            state.getCurrentItem().setPubDate(DateUtils.parseOrNullIfFuture(content));
        } else if (IMAGE_LOGO.equals(top) && state.getFeed() != null && state.getFeed().getImageUrl() == null) {
            state.getFeed().setImageUrl(content);
        } else if (IMAGE_ICON.equals(top) && state.getFeed() != null) {
            state.getFeed().setImageUrl(content);
        } else if (AUTHOR_NAME.equals(top) && AUTHOR.equals(second) && state.getFeed() != null && state.getCurrentItem() == null) {
            String currentName = state.getFeed().getAuthor();
            if (currentName == null) {
                state.getFeed().setAuthor(content);
            } else {
                state.getFeed().setAuthor(currentName + ", " + content);
            }
        }
    }
}
Also used : FeedItem(de.danoeh.antennapod.model.feed.FeedItem) SyndElement(de.danoeh.antennapod.parser.feed.element.SyndElement) AtomText(de.danoeh.antennapod.parser.feed.element.AtomText)

Example 3 with AtomText

use of de.danoeh.antennapod.parser.feed.element.AtomText in project AntennaPod by AntennaPod.

the class Media method handleElementStart.

@Override
public SyndElement handleElementStart(String localName, HandlerState state, Attributes attributes) {
    if (CONTENT.equals(localName)) {
        String url = attributes.getValue(DOWNLOAD_URL);
        String type = attributes.getValue(MIME_TYPE);
        String defaultStr = attributes.getValue(DEFAULT);
        String medium = attributes.getValue(MEDIUM);
        boolean validTypeMedia = false;
        boolean validTypeImage = false;
        boolean isDefault = "true".equals(defaultStr);
        String guessedType = SyndTypeUtils.getMimeTypeFromUrl(url);
        if (MEDIUM_AUDIO.equals(medium)) {
            validTypeMedia = true;
            type = "audio/*";
        } else if (MEDIUM_VIDEO.equals(medium)) {
            validTypeMedia = true;
            type = "video/*";
        } else if (MEDIUM_IMAGE.equals(medium) && (guessedType == null || (!guessedType.startsWith("audio/") && !guessedType.startsWith("video/")))) {
            // Apparently, some publishers explicitly specify the audio file as an image
            validTypeImage = true;
            type = "image/*";
        } else {
            if (type == null) {
                type = guessedType;
            }
            if (SyndTypeUtils.enclosureTypeValid(type)) {
                validTypeMedia = true;
            } else if (SyndTypeUtils.imageTypeValid(type)) {
                validTypeImage = true;
            }
        }
        if (state.getCurrentItem() != null && (state.getCurrentItem().getMedia() == null || isDefault) && url != null && validTypeMedia) {
            long size = 0;
            String sizeStr = attributes.getValue(SIZE);
            try {
                size = Long.parseLong(sizeStr);
            } catch (NumberFormatException e) {
                Log.e(TAG, "Size \"" + sizeStr + "\" could not be parsed.");
            }
            int durationMs = 0;
            String durationStr = attributes.getValue(DURATION);
            if (!TextUtils.isEmpty(durationStr)) {
                try {
                    long duration = Long.parseLong(durationStr);
                    durationMs = (int) TimeUnit.MILLISECONDS.convert(duration, TimeUnit.SECONDS);
                } catch (NumberFormatException e) {
                    Log.e(TAG, "Duration \"" + durationStr + "\" could not be parsed");
                }
            }
            FeedMedia media = new FeedMedia(state.getCurrentItem(), url, size, type);
            if (durationMs > 0) {
                media.setDuration(durationMs);
            }
            state.getCurrentItem().setMedia(media);
        } else if (state.getCurrentItem() != null && url != null && validTypeImage) {
            state.getCurrentItem().setImageUrl(url);
        }
    } else if (IMAGE.equals(localName)) {
        String url = attributes.getValue(IMAGE_URL);
        if (url != null) {
            if (state.getCurrentItem() != null) {
                state.getCurrentItem().setImageUrl(url);
            } else {
                if (state.getFeed().getImageUrl() == null) {
                    state.getFeed().setImageUrl(url);
                }
            }
        }
    } else if (DESCRIPTION.equals(localName)) {
        String type = attributes.getValue(DESCRIPTION_TYPE);
        return new AtomText(localName, this, type);
    }
    return new SyndElement(localName, this);
}
Also used : FeedMedia(de.danoeh.antennapod.model.feed.FeedMedia) SyndElement(de.danoeh.antennapod.parser.feed.element.SyndElement) AtomText(de.danoeh.antennapod.parser.feed.element.AtomText)

Example 4 with AtomText

use of de.danoeh.antennapod.parser.feed.element.AtomText in project AntennaPod by AntennaPod.

the class Atom method handleElementStart.

@Override
public SyndElement handleElementStart(String localName, HandlerState state, Attributes attributes) {
    if (ENTRY.equals(localName)) {
        state.setCurrentItem(new FeedItem());
        state.getItems().add(state.getCurrentItem());
        state.getCurrentItem().setFeed(state.getFeed());
    } else if (localName.matches(isText)) {
        String type = attributes.getValue(TEXT_TYPE);
        return new AtomText(localName, this, type);
    } else if (LINK.equals(localName)) {
        String href = attributes.getValue(LINK_HREF);
        String rel = attributes.getValue(LINK_REL);
        SyndElement parent = state.getTagstack().peek();
        if (parent.getName().matches(isFeedItem)) {
            if (rel == null || LINK_REL_ALTERNATE.equals(rel)) {
                state.getCurrentItem().setLink(href);
            } else if (LINK_REL_ENCLOSURE.equals(rel)) {
                String strSize = attributes.getValue(LINK_LENGTH);
                long size = 0;
                try {
                    if (strSize != null) {
                        size = Long.parseLong(strSize);
                    }
                } catch (NumberFormatException e) {
                    Log.d(TAG, "Length attribute could not be parsed.");
                }
                String type = attributes.getValue(LINK_TYPE);
                if (type == null) {
                    type = SyndTypeUtils.getMimeTypeFromUrl(href);
                }
                FeedItem currItem = state.getCurrentItem();
                if (SyndTypeUtils.enclosureTypeValid(type) && currItem != null && !currItem.hasMedia()) {
                    currItem.setMedia(new FeedMedia(currItem, href, size, type));
                }
            } else if (LINK_REL_PAYMENT.equals(rel)) {
                state.getCurrentItem().setPaymentLink(href);
            }
        } else if (parent.getName().matches(isFeed)) {
            if (rel == null || LINK_REL_ALTERNATE.equals(rel)) {
                String type = attributes.getValue(LINK_TYPE);
                /*
                     * Use as link if a) no type-attribute is given and
                     * feed-object has no link yet b) type of link is
                     * LINK_TYPE_HTML or LINK_TYPE_XHTML
                     */
                if (state.getFeed() != null && ((type == null && state.getFeed().getLink() == null) || (LINK_TYPE_HTML.equals(type) || LINK_TYPE_XHTML.equals(type)))) {
                    state.getFeed().setLink(href);
                } else if (LINK_TYPE_ATOM.equals(type) || LINK_TYPE_RSS.equals(type)) {
                    // treat as podlove alternate feed
                    String title = attributes.getValue(LINK_TITLE);
                    if (TextUtils.isEmpty(title)) {
                        title = href;
                    }
                    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().addPayment(new FeedFunding(href, ""));
            } else if (LINK_REL_NEXT.equals(rel) && state.getFeed() != null) {
                state.getFeed().setPaged(true);
                state.getFeed().setNextPageLink(href);
            }
        }
    }
    return new SyndElement(localName, this);
}
Also used : FeedItem(de.danoeh.antennapod.model.feed.FeedItem) SyndElement(de.danoeh.antennapod.parser.feed.element.SyndElement) FeedMedia(de.danoeh.antennapod.model.feed.FeedMedia) AtomText(de.danoeh.antennapod.parser.feed.element.AtomText) FeedFunding(de.danoeh.antennapod.model.feed.FeedFunding)

Aggregations

AtomText (de.danoeh.antennapod.parser.feed.element.AtomText)4 SyndElement (de.danoeh.antennapod.parser.feed.element.SyndElement)3 FeedItem (de.danoeh.antennapod.model.feed.FeedItem)2 FeedMedia (de.danoeh.antennapod.model.feed.FeedMedia)2 FeedFunding (de.danoeh.antennapod.model.feed.FeedFunding)1 Atom (de.danoeh.antennapod.parser.feed.namespace.Atom)1 Test (org.junit.Test)1