/** Delete files that aren't needed anymore */
 private void cleanup(Feed feed) {
   if (feed.getFile_url() != null) {
     if (new File(feed.getFile_url()).delete())
       if (BuildConfig.DEBUG) Log.d(TAG, "Successfully deleted cache file.");
       else Log.e(TAG, "Failed to delete cache file.");
     feed.setFile_url(null);
   } else if (BuildConfig.DEBUG) {
     Log.d(TAG, "Didn't delete cache file: File url is not set.");
   }
 }
 /**
  * Checks if the FeedItems of this feed have images that point to the same URL. If two FeedItems
  * have an image that points to the same URL, the reference of the second item is removed, so
  * that every image reference is unique.
  */
 private void removeDuplicateImages(Feed feed) {
   for (int x = 0; x < feed.getItems().size(); x++) {
     for (int y = x + 1; y < feed.getItems().size(); y++) {
       FeedItem item1 = feed.getItems().get(x);
       FeedItem item2 = feed.getItems().get(y);
       if (item1.hasItemImage() && item2.hasItemImage()) {
         if (StringUtils.equals(
             item1.getImage().getDownload_url(), item2.getImage().getDownload_url())) {
           item2.setImage(null);
         }
       }
     }
   }
 }
 /** Checks if the feed was parsed correctly. */
 private boolean checkFeedData(Feed feed) {
   if (feed.getTitle() == null) {
     Log.e(TAG, "Feed has no title.");
     return false;
   }
   if (!hasValidFeedItems(feed)) {
     Log.e(TAG, "Feed has invalid items");
     return false;
   }
   return true;
 }
 private boolean hasValidFeedItems(Feed feed) {
   for (FeedItem item : feed.getItems()) {
     if (item.getTitle() == null) {
       Log.e(TAG, "Item has no title");
       return false;
     }
     if (item.getPubDate() == null) {
       Log.e(TAG, "Item has no pubDate. Using current time as pubDate");
       if (item.getTitle() != null) {
         Log.e(TAG, "Title of invalid item: " + item.getTitle());
       }
       item.setPubDate(new Date());
     }
   }
   return true;
 }
    private Feed parseFeed(DownloadRequest request) {
      Feed savedFeed = null;

      Feed feed = new Feed(request.getSource(), new Date());
      feed.setFile_url(request.getDestination());
      feed.setId(request.getFeedfileId());
      feed.setDownloaded(true);
      feed.setPreferences(
          new FeedPreferences(0, true, request.getUsername(), request.getPassword()));

      DownloadError reason = null;
      String reasonDetailed = null;
      boolean successful = true;
      FeedHandler feedHandler = new FeedHandler();

      try {
        feed = feedHandler.parseFeed(feed).feed;
        if (BuildConfig.DEBUG) Log.d(TAG, feed.getTitle() + " parsed");
        if (checkFeedData(feed) == false) {
          throw new InvalidFeedException();
        }

      } catch (SAXException e) {
        successful = false;
        e.printStackTrace();
        reason = DownloadError.ERROR_PARSER_EXCEPTION;
        reasonDetailed = e.getMessage();
      } catch (IOException e) {
        successful = false;
        e.printStackTrace();
        reason = DownloadError.ERROR_PARSER_EXCEPTION;
        reasonDetailed = e.getMessage();
      } catch (ParserConfigurationException e) {
        successful = false;
        e.printStackTrace();
        reason = DownloadError.ERROR_PARSER_EXCEPTION;
        reasonDetailed = e.getMessage();
      } catch (UnsupportedFeedtypeException e) {
        e.printStackTrace();
        successful = false;
        reason = DownloadError.ERROR_UNSUPPORTED_TYPE;
        reasonDetailed = e.getMessage();
      } catch (InvalidFeedException e) {
        e.printStackTrace();
        successful = false;
        reason = DownloadError.ERROR_PARSER_EXCEPTION;
        reasonDetailed = e.getMessage();
      }

      // cleanup();
      if (savedFeed == null) {
        savedFeed = feed;
      }

      if (successful) {
        return savedFeed;
      } else {
        saveDownloadStatus(
            new DownloadStatus(
                savedFeed,
                savedFeed.getHumanReadableIdentifier(),
                reason,
                successful,
                reasonDetailed));
        return null;
      }
    }