All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.mobile.rss.RSSFeedReader Maven / Gradle / Ivy

There is a newer version: 1.0.15
Show newest version
/***********************************************************************************************************************
 *
 * blueBill Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * $Id: RSSFeedReader.java,v 2210e84c4896 2010/07/04 20:41:02 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.mobile.rss;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.io.InputStream;
import org.jaxen.XPath;
import it.tidalwave.util.Id;
import it.tidalwave.mobile.xml.XPathProvider;
import it.tidalwave.mobile.xml.XmlParser;
import it.tidalwave.mobile.xml.dom4j.Dom4jXmlParser;
import it.tidalwave.mobile.xml.jaxen.JaxenXPathProvider;
import static it.tidalwave.mobile.rss.Vocabulary.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public class RSSFeedReader
  {
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public RSSFeed readMessages (final @Nonnull InputStream is)
      throws Exception
      {
        final XmlParser xmlParser = new Dom4jXmlParser();
        final Object document = xmlParser.parse(is);
        final XPathProvider xPathProvider = new JaxenXPathProvider();

        final XPath channelExpr = xPathProvider.createXPath(document, "/rss/channel");
        final XPath titleExpr = xPathProvider.createXPath(document, "title");
        final XPath linkExpr = xPathProvider.createXPath(document, "link");
        final XPath descriptionExpr = xPathProvider.createXPath(document, "description");
        final XPath languageExpr = xPathProvider.createXPath(document, "language");
        final XPath copyrightExpr = xPathProvider.createXPath(document, "copyright");
        final XPath lastBuildDate = xPathProvider.createXPath(document, "lastBuildDate");
        final XPath generatorExpr = xPathProvider.createXPath(document, "generator");
        final XPath itemExpr = xPathProvider.createXPath(document, "item");

        final XPath pubDateExpr = xPathProvider.createXPath(document, "pubDate");
        final XPath dcCreatorExpr = xPathProvider.createXPath(document, "dc:creator");
        dcCreatorExpr.addNamespace("dc", "http://purl.org/dc/elements/1.1/");
        final XPath guidExpr = xPathProvider.createXPath(document, "guid");
        final XPath contentExpr = xPathProvider.createXPath(document, "content:encoded");
        contentExpr.addNamespace("content", "http://purl.org/rss/1.0/modules/content/");

        final Object channel = channelExpr.selectSingleNode(document);
        final List itemNodes = itemExpr.selectNodes(channel);

        final List messages = new ArrayList();

        // Tue, 29 Jun 2010 09:44:00 -0500
        final DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", new Locale("en"));

        for (final Object item : itemNodes)
          {
            final String link = linkExpr.stringValueOf(item);
            messages.add(new Message(new Id(link)).with(TITLE, titleExpr.stringValueOf(item))
                                                  .with(PUB_DATE, df.parse(pubDateExpr.stringValueOf(item)))
                                                  .with(DC_CREATOR, dcCreatorExpr.stringValueOf(item))
                                                  .with(LINK, link)
                                                  .with(GUID, guidExpr.stringValueOf(item))
                                                  .with(DESCRIPTION, descriptionExpr.stringValueOf(item))
                                                  .with(CONTENT, contentExpr.stringValueOf(item)));
          }

        final String link = linkExpr.stringValueOf(channel);
        final RSSFeed rssFeed = new RSSFeed(new Id(link))
                                    .with(TITLE, titleExpr.stringValueOf(channel))
                                    .with(LINK, link)
                                    .with(DESCRIPTION, descriptionExpr.stringValueOf(channel))
                                    .with(LANGUAGE, languageExpr.stringValueOf(channel))
                                    .with(COPYRIGHT, copyrightExpr.stringValueOf(channel))
                                    .with(LAST_BUILD_DATE, df.parse(lastBuildDate.stringValueOf(channel)))
                                    .with(GENERATOR, generatorExpr.stringValueOf(channel))
                                    .with(MESSAGES, messages);

        return rssFeed;
      }
  }