Search in sources :

Example 1 with SyndElement

use of de.danoeh.antennapod.parser.feed.element.SyndElement 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 2 with SyndElement

use of de.danoeh.antennapod.parser.feed.element.SyndElement 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 3 with SyndElement

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

the class SyndHandler method startElement.

@Override
public void startElement(String uri, String localName, String qualifiedName, Attributes attributes) throws SAXException {
    state.contentBuf = new StringBuilder();
    Namespace handler = getHandlingNamespace(uri, qualifiedName);
    if (handler != null) {
        SyndElement element = handler.handleElementStart(localName, state, attributes);
        state.tagstack.push(element);
    }
}
Also used : SyndElement(de.danoeh.antennapod.parser.feed.element.SyndElement) Namespace(de.danoeh.antennapod.parser.feed.namespace.Namespace)

Example 4 with SyndElement

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

the class HandlerState method getSecondTag.

/**
 * Returns the SyndElement that comes after the top element of the tagstack.
 */
public SyndElement getSecondTag() {
    SyndElement top = tagstack.pop();
    SyndElement second = tagstack.peek();
    tagstack.push(top);
    return second;
}
Also used : SyndElement(de.danoeh.antennapod.parser.feed.element.SyndElement)

Example 5 with SyndElement

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

the class HandlerState method getThirdTag.

public SyndElement getThirdTag() {
    SyndElement top = tagstack.pop();
    SyndElement second = tagstack.pop();
    SyndElement third = tagstack.peek();
    tagstack.push(second);
    tagstack.push(top);
    return third;
}
Also used : SyndElement(de.danoeh.antennapod.parser.feed.element.SyndElement)

Aggregations

SyndElement (de.danoeh.antennapod.parser.feed.element.SyndElement)9 FeedItem (de.danoeh.antennapod.model.feed.FeedItem)4 FeedMedia (de.danoeh.antennapod.model.feed.FeedMedia)3 AtomText (de.danoeh.antennapod.parser.feed.element.AtomText)3 FeedFunding (de.danoeh.antennapod.model.feed.FeedFunding)2 Namespace (de.danoeh.antennapod.parser.feed.namespace.Namespace)1