Package com.danielvaughan.rssreader.server.services

Source Code of com.danielvaughan.rssreader.server.services.FeedServiceImpl

package com.danielvaughan.rssreader.server.services;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import com.danielvaughan.rssreader.client.services.FeedService;
import com.danielvaughan.rssreader.server.utils.FilePersistence;
import com.danielvaughan.rssreader.server.utils.Persistence;
import com.danielvaughan.rssreader.shared.Feed;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class FeedServiceImpl extends RemoteServiceServlet implements
    FeedService {
 
  private final static Logger LOGGER = Logger.getLogger(FeedServiceImpl.class.getName());
 
  private Map<String, Feed> feeds = new HashMap<String, Feed>();
  private final Persistence persistence = new FilePersistence();

  @Override
  public Feed createFeed() {
    UUID uuid = UUID.randomUUID();
    return new Feed(uuid.toString());
  }

  @Override
  public void saveFeed(Feed feed) {
    Element eleRoot = new Element("rss");
    eleRoot.setAttribute(new Attribute("version", "2.0"));
   
    //Create from feed
    Document document = new Document(eleRoot);
   
    Element eleChannel = new Element("channel");
    Element eleTitle = new Element("title");
    Element eleDescription = new Element("description");
    Element eleLink = new Element("link");
   
    eleTitle.setText(feed.getTitle());
    eleDescription.setText(feed.getDescription());
    eleLink.setText(feed.getLink());
   
    eleChannel.addContent(eleTitle);
    eleChannel.addContent(eleDescription);
    eleChannel.addContent(eleLink);
   
    eleRoot.addContent(eleChannel);
   
    try {
      XMLOutputter serializer = new XMLOutputter();
      Format prettyFormat = Format.getPrettyFormat();
      serializer.setFormat(prettyFormat);
      System.out.println("Here we would serialize the feed " +
            feed.getTitle() +
            " to a file. For now we are just going to write it to the console");
      serializer.output(document, System.out);
    } catch (IOException e){
      System.out.println("Error saving feed");
    }
  }
 
  private Feed loadFeed(String feedUrl){
    Feed feed = new Feed(feedUrl);
    try {
      SAXBuilder parser = new SAXBuilder();
      Document document = parser.build(new URL(feedUrl));
      Element eleRoot = document.getRootElement();
      Element eleChannel = eleRoot.getChild("channel");
      feed.setTitle(eleChannel.getChildText("title"));
      feed.setDescription(eleChannel.getChildText("description"));
      feed.setLink(eleChannel.getChildText("link"));
      return feed;
     
    } catch (IOException e){
      LOGGER.log(Level.SEVERE, "IO Error loading feed", e);
      return feed;
    } catch (JDOMException e) {
      LOGGER.log(Level.SEVERE, "Error parsing feed", e);
      return feed;
    }
  }

  @Override
  public void addExistingFeed(String feedUrl) {
    Feed loadResult = loadFeed(feedUrl);
    if (loadResult.getTitle() != null){
      feeds.put(feedUrl, loadFeed(feedUrl));
      persistence.saveFeedList(feeds.keySet());
    }
  }

}
TOP

Related Classes of com.danielvaughan.rssreader.server.services.FeedServiceImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.