fr.mael.microrss.xml.opml.OPMLReader.java Source code

Java tutorial

Introduction

Here is the source code for fr.mael.microrss.xml.opml.OPMLReader.java

Source

/*
   Copyright  2013 Mael Le Guvel
   This work is free. You can redistribute it and/or modify it under the
   terms of the Do What The Fuck You Want To Public License, Version 2,
   as published by Sam Hocevar. See the COPYING file for more details.
*/
package fr.mael.microrss.xml.opml;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import fr.mael.microrss.domain.Category;
import fr.mael.microrss.domain.UserFeed;
import fr.mael.microrss.service.CategoryService;
import fr.mael.microrss.service.FeedService;
import fr.mael.microrss.util.SecurityUtil;

@Component
public class OPMLReader {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private FeedService feedService;
    @Autowired
    private SecurityUtil securityUtil;

    public List<Category> parse(URL url) throws DocumentException, IOException {
        return parse(url.openStream());
    }

    public List<Category> parse(InputStream stream) throws DocumentException {
        List<Category> categories = new ArrayList<Category>();
        SAXReader reader = new SAXReader();
        Document document = reader.read(stream);
        Element root = document.getRootElement();
        Element body = (Element) root.elementIterator("body").next();
        for (Iterator i = body.elementIterator("outline"); i.hasNext();) {
            Element el = (Element) i.next();
            Category cat = getCategory(el, null);
            categories.add(cat);
            parseCategories(el, cat);
        }
        return categories;
    }

    private void parseCategories(Element element, Category parent) {
        for (Iterator i = element.elementIterator("outline"); i.hasNext();) {
            Element el = (Element) i.next();
            if (isFeed(el)) {
                parent.getUserFeeds().add(getFeed(el, parent));
            } else {
                Category cat = getCategory(el, parent);
                parseCategories(el, cat);
            }
        }
    }

    private boolean isFeed(Element element) {
        return getAttribute("xmlUrl", element) != null;
    }

    private Category getCategory(Element element, Category parent) {
        Category category = new Category();
        category.setTitle(getAttribute("text", element));
        category.setUser(securityUtil.getCurrentUser());
        category.setCategoryId(parent == null ? null : parent.getId());
        return categoryService.save(category);
    }

    private UserFeed getFeed(Element element, Category parent) {
        return feedService.getOrCreateFeed(getAttribute("xmlUrl", element), getAttribute("text", element), parent);
    }

    private String getAttribute(String attributeName, Element element) {
        for (Iterator i = element.attributeIterator(); i.hasNext();) {
            Attribute attribute = (Attribute) i.next();
            if (attributeName.equals(attribute.getName())) {
                return attribute.getValue();
            }
        }
        return null;
    }

}