com.seajas.search.contender.service.builder.RSSDirectoryBuilder.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.contender.service.builder.RSSDirectoryBuilder.java

Source

/**
 * Copyright (C) 2013 Seajas, the Netherlands.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.seajas.search.contender.service.builder;

import com.seajas.search.utilities.web.WebFeeds;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedOutput;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Converts a simple directory into an RSS feed.
 *
 * @author Jasper van Veghel <jasper@seajas.com>
 */
public class RSSDirectoryBuilder {
    /**
     * The logger.
     */
    private static final Logger logger = LoggerFactory.getLogger(RSSDirectoryBuilder.class);

    /**
     * Create a UTF-8 stream containing the contents of the directory as a stream.
     *
     * @param directory
     * @return InputStream
     * @throws IOException
     */
    public static InputStream build(final File directory) throws IOException {
        if (!directory.isDirectory())
            throw new IllegalStateException("The given handle is not a directory");

        // Create a simple feed

        SyndFeedImpl feed = new SyndFeedImpl();

        feed.setEntries(new ArrayList<SyndEntry>());

        // Then add the entries to it

        Map<String, Long> files = travelDirectory(directory, new HashMap<String, Long>());

        for (Map.Entry<String, Long> file : files.entrySet()) {
            SyndEntryImpl entry = new SyndEntryImpl();

            entry.setUri("file://" + file.getKey());
            entry.setTitle(file.getKey());
            entry.setPublishedDate(new Date(file.getValue()));

            feed.getEntries().add(entry);
        }

        // And serialize it to an InputStream

        SyndFeedOutput serializer = new SyndFeedOutput();

        try {
            WebFeeds.validate(feed, URI.create("file://" + directory.getAbsolutePath()));

            return IOUtils.toInputStream(serializer.outputString(feed, true));
        } catch (FeedException e) {
            throw new IOException("Unable to serialize stream", e);
        }
    }

    /**
     * Recursively travel a directory and add all files to the result
     *
     * @param directory
     * @param result
     * @return Map<String, Long>
     */
    private static Map<String, Long> travelDirectory(final File directory, final Map<String, Long> result) {
        // Skip files that start with a '.'

        if (!directory.getName().startsWith(".")) {
            if (directory.isDirectory())
                for (File child : directory.listFiles())
                    travelDirectory(child, result);
            else if (directory.isFile())
                result.put(directory.getAbsolutePath(), directory.lastModified());
            else
                logger.warn("Unknown type for file " + directory);
        }

        return result;
    }
}