Android Open Source - Hackful-Europe-Reader R S S Processor






From Project

Back to project page Hackful-Europe-Reader.

License

The source code is released under:

Apache License

If you think the Android project Hackful-Europe-Reader 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 2012 Damin Serrano Thode (@dsthode)
/*from   w  w w. j a va 2 s .co  m*/
   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.
*/

package com.nineducks.util.rss;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Xml;

public class RSSProcessor {

  static final String ITEM = "item";
  static final String TITLE = "title";
  static final String DESCRIPTION = "description";
  static final String PUBDATE = "pubDate";
  static final String LINK = "link";
  static final String GUID = "guid";
  static final String SUBMITTER = "submitter";
  static final String POINTS = "points";
  static final String COMMENTCOUNT = "commentcount";
  
  private URL feedURL;
  private List<HEMessage> messages;
  private String heNamespace;
  
  public RSSProcessor(String feedURL, String heNamespace) {
    messages = new ArrayList<HEMessage>();
    this.heNamespace = heNamespace;
    try {
      this.feedURL = new URL(feedURL);
    } catch (MalformedURLException murle) {
      this.feedURL = null;
      throw new RSSException(murle);
    }
  }
  
  public List<HEMessage> parse() {
    final HEMessage current = new HEMessage();
    RootElement root = new RootElement("rss");
    Element channel = root.getChild("channel");
    Element item = channel.getChild(ITEM);
    item.setStartElementListener(new StartElementListener() {
      @Override
      public void start(Attributes attributes) {
        current.clear();
      }
    });
    item.getChild(TITLE).setEndTextElementListener(new EndTextElementListener() {
      @Override
      public void end(String body) {
        current.setTitle(body);
      }
    });
    item.getChild(DESCRIPTION).setEndTextElementListener(new EndTextElementListener() {
      @Override
      public void end(String body) {
        current.setDescription(body);
      }
    });
    item.getChild(PUBDATE).setEndTextElementListener(new EndTextElementListener() {
      @Override
      public void end(String body) {
        current.setPubDate(body);
      }
    });
    item.getChild(LINK).setEndTextElementListener(new EndTextElementListener() {
      @Override
      public void end(String body) {
        current.setLink(body);
      }
    });
    item.getChild(GUID).setEndTextElementListener(new EndTextElementListener() {
      @Override
      public void end(String body) {
        current.setGuid(body);
      }
    });
    item.getChild(heNamespace, SUBMITTER).setEndTextElementListener(new EndTextElementListener() {
      @Override
      public void end(String body) {
        current.setSubmitter(body);
      }
    });
    item.getChild(heNamespace, POINTS).setEndTextElementListener(new EndTextElementListener() {
      @Override
      public void end(String body) {
        current.setPoints(body);
      }
    });
    item.getChild(heNamespace, COMMENTCOUNT).setEndTextElementListener(new EndTextElementListener() {
      @Override
      public void end(String body) {
        current.setCommentCount(body);
      }
    });
    item.setEndElementListener(new EndElementListener() {
      @Override
      public void end() {
        messages.add(current.copy());
      }
    });
    try {
      Xml.parse(feedURL.openStream(), Xml.Encoding.UTF_8, root.getContentHandler());
    } catch (Exception e) {
      throw new RSSException(e);
    }
    return messages;
  }
}




Java Source Code List

com.nineducks.hereader.AboutAction.java
com.nineducks.hereader.HEReaderState.java
com.nineducks.hereader.HackfulItemsController.java
com.nineducks.hereader.HackfulReaderActivity.java
com.nineducks.hereader.ItemsLoadedListener.java
com.nineducks.hereader.LoadAskItemsAction.java
com.nineducks.hereader.LoadFrontpageItemsAction.java
com.nineducks.hereader.LoadNewItemsAction.java
com.nineducks.hereader.NewsItemsLoaderTask.java
com.nineducks.hereader.ui.HEMessagesAdapter.java
com.nineducks.hereader.ui.ItemDetailsFragment.java
com.nineducks.hereader.ui.ListFragmentItem.java
com.nineducks.hereader.ui.NewsItemsFragment.java
com.nineducks.hereader.ui.ViewHolder.java
com.nineducks.util.rss.DateFormatter.java
com.nineducks.util.rss.HEMessage.java
com.nineducks.util.rss.RSSException.java
com.nineducks.util.rss.RSSProcessor.java