Android Open Source - feedhive R S S Parser






From Project

Back to project page feedhive.

License

The source code is released under:

SOFTWARE LICENSE ---------------- Copyright (C) 2012, 2013, 2014 Younghyung Cho. <yhcting77@gmail.com> All rights reserved. This file is part of FeedHive This program is licensed under the FreeBSD l...

If you think the Android project feedhive listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/******************************************************************************
 * Copyright (C) 2012, 2013, 2014//from ww  w.j  a v  a 2 s . c om
 * Younghyung Cho. <yhcting77@gmail.com>
 * All rights reserved.
 *
 * This file is part of FeedHive
 *
 * This program is licensed under the FreeBSD license
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation
 * are those of the authors and should not be interpreted as representing
 * official policies, either expressed or implied, of the FreeBSD Project.
 *****************************************************************************/

package free.yhc.feeder.model;

import java.util.LinkedList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class RSSParser extends FeedParser implements
UnexpectedExceptionHandler.TrackedModule {
    private static final boolean DBG = false;
    private static final Utils.Logger P = new Utils.Logger(RSSParser.class);

    // parsing priority of namespace supported (larger number has priority)
    private static final short PRI_ITUNES     = 2;
    private static final short PRI_DEFAULT    = 1; // RSS default
    private static final short PRI_DC         = 0;

    // ========================================
    //        To Support 'itunes' Namespace
    // ========================================
    private class NSItunesParser extends NSParser {
        NSItunesParser() {
            super(PRI_ITUNES);
        }

        @Override
        boolean
        parseChannel(ChannelValues cv, Node n)
                throws FeederException {
            boolean ret = true;

            if (n.getNodeName().equalsIgnoreCase("itunes:summary"))
                setValue(cv.description, getTextValue(n));
            else if (n.getNodeName().equalsIgnoreCase("itunes:image")) {
                NamedNodeMap nnm = n.getAttributes();
                Node img = nnm.getNamedItem("href");
                if (null != img)
                    setValue(cv.imageref, img.getNodeValue());
            } else
                ret = false;

            return ret;
        }

        @Override
        boolean
        parseItem(ItemValues iv, Node n)
                throws FeederException {
            boolean ret = true;

            if (n.getNodeName().equalsIgnoreCase("itunes:summary"))
                setValue(iv.description, getTextValue(n));
            else if (n.getNodeName().equalsIgnoreCase("itunes:duration"))
                setValue(iv.enclosure_length, getTextValue(n));
            else
                ret = false;

            return ret;
        }

    }

    // ========================================
    //        To Support 'dublincore(dc)' Namespace
    // ========================================
    private class NSDcParser extends NSParser {
        NSDcParser() {
            super(PRI_DC);
        }

        @Override
        boolean
        parseItem(ItemValues iv, Node n)
                throws FeederException {
            boolean ret = true;

            if (n.getNodeName().equalsIgnoreCase("dc:date"))
                setValue(iv.pubDate, getTextValue(n));
            else
                ret = false;

            return ret;
        }

        @Override
        boolean
        parseChannel(ChannelValues cv, Node n) {
            return false;
        }
    }

    // ========================================
    //        Default RSS Parser
    // ========================================
    private class NSDefaultParser extends NSParser {
        NSDefaultParser() {
            super(PRI_DEFAULT);
        }

        private void
        nodeImage(ChannelValues cv, Node n)
                throws FeederException {
            n = n.getFirstChild();
            while (null != n) {
                if (n.getNodeName().equalsIgnoreCase("url")) {
                    setValue(cv.imageref, getTextValue(n));
                    return;
                }
                n = n.getNextSibling();
            }
        }

        private void
        nodeEnclosure(ItemValues iv, Node n) {
            NamedNodeMap nnm = n.getAttributes();
            n = nnm.getNamedItem("url");
            if (null != n)
                setValue(iv.enclosure_url, n.getNodeValue());

            n = nnm.getNamedItem("length");
            if (null != n)
                setValue(iv.enclosure_length, n.getNodeValue());

            n = nnm.getNamedItem("type");
            if (null != n)
                setValue(iv.enclosure_type, n.getNodeValue());
        }

        @Override
        boolean
        parseChannel(ChannelValues cv, Node n)
                throws FeederException {
            boolean ret = true;

            if (n.getNodeName().equalsIgnoreCase("title"))
                setValue(cv.title, getTextValue(n));
            else if (n.getNodeName().equalsIgnoreCase("description"))
                setValue(cv.description, getTextValue(n));
            else if (n.getNodeName().equalsIgnoreCase("image"))
                nodeImage(cv, n);
            else
                ret = false;

            return ret;
        }

        @Override
        boolean
        parseItem(ItemValues iv, Node n)
                throws FeederException {
            boolean ret = true;

            if (n.getNodeName().equalsIgnoreCase("title"))
                setValue(iv.title, getTextValue(n));
            else if (n.getNodeName().equalsIgnoreCase("link"))
                setValue(iv.link, getTextValue(n));
            else if (n.getNodeName().equalsIgnoreCase("description"))
                setValue(iv.description, getTextValue(n));
            else if (n.getNodeName().equalsIgnoreCase("enclosure"))
                nodeEnclosure(iv, n);
            else if (n.getNodeName().equalsIgnoreCase("pubDate"))
                setValue(iv.pubDate, getTextValue(n));
            else if (n.getNodeName().equalsIgnoreCase("guid"))
                setValue(iv.guid, getTextValue(n));
            else
                ret = false;

            return ret;
        }
    }

    // ===========================================================
    //
    // ===========================================================

    private void
    constructNSParser(LinkedList<NSParser> pl, Node n)
            throws FeederException {
        NamedNodeMap nnm = n.getAttributes();
        Node nVer = nnm.getNamedItem("version");

        /*
        if (!"2.0".equals(nVer.getNodeValue()))
            throw new FeederException(Err.ParserUnsupportedVersion);
         */

        // add default namespace parser
        pl.add(new NSDefaultParser());

        // Some element from 'itunes' and 'dc' is supported.
        // So, check it!
        if (null != nnm.getNamedItem("xmlns:itunes"))
            pl.add(new NSItunesParser());

        if (null != nnm.getNamedItem("xmlns:dc"))
            pl.add(new NSDcParser());

    }

    private void
    nodeChannel(Result res, NSParser[] parser, Node chn)
            throws FeederException {
        ChannelValues cv = new ChannelValues();
        ItemValues iv = new ItemValues();
        // count number of items in this channel

        LinkedList<Feed.Item.ParD> iteml = new LinkedList<Feed.Item.ParD>();
        cv.init();
        Node n = chn.getFirstChild();
        while (null != n) {
            if (n.getNodeName().equalsIgnoreCase("item")) {
                // Parsing elements of 'item'
                iv.init(); // to reuse
                Node in = n.getFirstChild();
                while (null != in) {
                    for (NSParser p : parser) {
                        if (p.parseItem(iv, in))
                            break; // handled
                    }
                    in = in.getNextSibling();
                }
                Feed.Item.ParD item = new Feed.Item.ParD();
                iv.set(item);
                if (isValidItem(item))
                    iteml.addLast(item);
            } else {
                for (NSParser p : parser) {
                    if (p.parseChannel(cv, n))
                        break; // handled
                }
            }
            n = n.getNextSibling();
        }

        cv.set(res.channel);
        res.items = iteml.toArray(new Feed.Item.ParD[0]);
    }

    // false (fail)
    private boolean
    verifyNotNullPolicy(Result res) {
        if (null == res.channel.title)
            return false;

        for (Feed.Item.ParD item : res.items)
            if (null == item.title)
                return false;

        return true;
    }

    @Override
    public String
    dump(UnexpectedExceptionHandler.DumpLevel lv) {
        return "[ RSSParser ]";
    }

    @Override
    protected Result
    parse(Document dom)
            throws FeederException {
        Result res = null;
        UnexpectedExceptionHandler.get().registerModule(this);
        try {
            Element root = dom.getDocumentElement();
            verifyFormat(root.getNodeName().equalsIgnoreCase("rss"));

            LinkedList<NSParser> pl = new LinkedList<NSParser>();
            constructNSParser(pl, root);

            res = new Result();

            // Some channels use itunes namespace even if they don't have any enclosure media.
            // So, below check doesn't have any meaning.
            /*
            // Channel type which uses itunes namespace is needed to be set as 'Media type'.
            for (NSParser p : pl.toArray(new NSParser[0]))
                if (p instanceof NSItunesParser)
                    res.channel.type = Feed.Channel.CHANN_TYPE_MEDIA;
             */

            // For Channel node
            Node n = findNodeByNameFromSiblings(root.getFirstChild(), "channel");

            nodeChannel(res, pl.toArray(new NSParser[0]), n);

            if (!verifyNotNullPolicy(res))
                throw new FeederException(Err.PARSER_UNSUPPORTED_FORMAT);
            //logI(feed.channel.dump());
        } finally {
            UnexpectedExceptionHandler.get().unregisterModule(this);
        }
        return res;
    }
}




Java Source Code List

free.yhc.feeder.AppWidgetCategoryChooserActivity.java
free.yhc.feeder.AppWidgetMenuActivity.java
free.yhc.feeder.AppWidgetUpdateCategoryActivity.java
free.yhc.feeder.AsyncAdapter.java
free.yhc.feeder.AsyncCursorAdapter.java
free.yhc.feeder.AsyncCursorListAdapter.java
free.yhc.feeder.ChannelListActivity.java
free.yhc.feeder.ChannelListAdapter.java
free.yhc.feeder.ChannelListFragment.java
free.yhc.feeder.ChannelListPagerAdapter.java
free.yhc.feeder.ChannelSettingActivity.java
free.yhc.feeder.DBManagerActivity.java
free.yhc.feeder.DiagAsyncTask.java
free.yhc.feeder.FeederActivity.java
free.yhc.feeder.FeederApp.java
free.yhc.feeder.FeederPreferenceActivity.java
free.yhc.feeder.FragmentPagerAdapterEx.java
free.yhc.feeder.ItemListActivity.java
free.yhc.feeder.ItemListAdapter.java
free.yhc.feeder.ItemViewActivity.java
free.yhc.feeder.LifeSupportService.java
free.yhc.feeder.NotiManager.java
free.yhc.feeder.PredefinedChannelActivity.java
free.yhc.feeder.ScheduledUpdateService.java
free.yhc.feeder.UiHelper.java
free.yhc.feeder.appwidget.AppWidgetUtils.java
free.yhc.feeder.appwidget.Provider.java
free.yhc.feeder.appwidget.UpdateService.java
free.yhc.feeder.appwidget.ViewsFactory.java
free.yhc.feeder.appwidget.ViewsService.java
free.yhc.feeder.db.ColumnCategory.java
free.yhc.feeder.db.ColumnChannel.java
free.yhc.feeder.db.ColumnItem.java
free.yhc.feeder.db.DBPolicy.java
free.yhc.feeder.db.DB.java
free.yhc.feeder.model.AssetSQLiteHelper.java
free.yhc.feeder.model.AtomParser.java
free.yhc.feeder.model.BGTaskDownloadToFile.java
free.yhc.feeder.model.BGTaskDownloadToItemContent.java
free.yhc.feeder.model.BGTaskManager.java
free.yhc.feeder.model.BGTaskUpdateChannel.java
free.yhc.feeder.model.BGTask.java
free.yhc.feeder.model.BaseBGTask.java
free.yhc.feeder.model.ContentsManager.java
free.yhc.feeder.model.DelayedAction.java
free.yhc.feeder.model.Environ.java
free.yhc.feeder.model.Err.java
free.yhc.feeder.model.FeedParser.java
free.yhc.feeder.model.FeedPolicy.java
free.yhc.feeder.model.Feed.java
free.yhc.feeder.model.FeederException.java
free.yhc.feeder.model.HtmlParser.java
free.yhc.feeder.model.ItemActionHandler.java
free.yhc.feeder.model.KeyBasedLinkedList.java
free.yhc.feeder.model.ListenerManager.java
free.yhc.feeder.model.NetLoader.java
free.yhc.feeder.model.RSSParser.java
free.yhc.feeder.model.RTTask.java
free.yhc.feeder.model.ThreadEx.java
free.yhc.feeder.model.UnexpectedExceptionHandler.java
free.yhc.feeder.model.UsageReport.java
free.yhc.feeder.model.Utils.java