Package com.sun.syndication.io

Source Code of com.sun.syndication.io.FeedInput

/*
* Copyright 2004 Sun Microsystems, Inc.
*
* 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.sun.syndication.io;

import com.sun.syndication.feed.AbstractFeed;
import com.sun.syndication.io.impl.FeedParsers;
import org.jdom.Document;
import org.jdom.input.DOMBuilder;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.IOException;
import java.util.List;

/**
* Parses an XML document (File, InputStream, Reader, W3C SAX InputSource, W3C DOM Document or JDom DOcument)
* into an AbstractFeed (RSS/Atom).
* <p>
* It accepts all flavors of RSS (0.90, 0.91, 0.92, 0.93, 0.94, 1.0 and 2.0) and
* Atom 0.3 feeds. Parsers are plugable (they must implement the FeedParser interface).
* <p>
* The FeedInput useds liberal parsers.
* <p>
* @author Alejandro Abdelnur
*
*/
public class FeedInput {
    private static FeedParsers FEED_PARSERS = new FeedParsers();

    private boolean _validate;

    /**
     * Returns the list of supported input feed types.
     * <p>
     * @see AbstractFeed for details on the format of these strings.
     * <p>
     * @return a list of String elements with the supported input feed types.
     *
     */
    public static List getSupportedFeedTypes() {
        return FEED_PARSERS.getSupportedFeedTypes();
    }

    /**
     * Creates a FeedInput instance.
     * <p>
     * @param validate indicates if the input should be validated. NOT IMPLEMENTED YET (validation does not happen)
     *
     */
    public FeedInput(boolean validate) {
        _validate = false; // TODO FIX THIS THINGY
    }

    /**
     * Builds an AbstractFeed (RSS or Atom) from a file.
     * <p>
     * NOTE: This method delages to the 'AsbtractFeed FeedInput#build(org.jdom.Document)'.
     * <p>
     * @param file file to read to create the AbstractFeed.
     * @return the AbstractFeed read from the file.
     * @throws FileNotFoundException thrown if the file could not be found.
     * @throws IOException thrown if there is problem reading the file.
     * @throws IllegalArgumentException thrown if feed type could not be understood by any of the underlying parsers.
     * @throws FeedException if the feed could not be parsed
     *
     */
    public AbstractFeed build(File file) throws FileNotFoundException,IOException,IllegalArgumentException,FeedException {
        AbstractFeed feed;
        Reader reader = new FileReader(file);
        feed = build(reader);
        reader.close();
        return feed;
    }

    /**
     * Builds an AbstractFeed (RSS or Atom) from an InputStream.
     * <p>
     * NOTE: This method delages to the 'AsbtractFeed FeedInput#build(org.jdom.Document)'.
     * <p>
     * @param is InputStream to read to create the AbstractFeed.
     * @return the AbstractFeed read from the InputStream.
     * @throws IllegalArgumentException thrown if feed type could not be understood by any of the underlying parsers.
     * @throws FeedException if the feed could not be parsed
     *
     */
    public AbstractFeed build(InputStream is) throws IllegalArgumentException,FeedException {
        return build(new InputStreamReader(is));
    }

    /**
     * Builds an AbstractFeed (RSS or Atom) from an Reader.
     * <p>
     * NOTE: This method delages to the 'AsbtractFeed FeedInput#build(org.jdom.Document)'.
     * <p>
     * @param reader Reader to read to create the AbstractFeed.
     * @return the AbstractFeed read from the Reader.
     * @throws IllegalArgumentException thrown if feed type could not be understood by any of the underlying parsers.
     * @throws FeedException if the feed could not be parsed
     *
     */
    public AbstractFeed build(Reader reader) throws IllegalArgumentException,FeedException {
        SAXBuilder saxBuilder = new SAXBuilder(_validate);
        try {
            Document document = saxBuilder.build(reader);
            return build(document);
        }
        catch (Exception ex) {
            throw new FeedException("Invalid XML",ex);
        }
    }

    /**
     * Builds an AbstractFeed (RSS or Atom) from an W3C SAX InputSource.
     * <p>
     * NOTE: This method delages to the 'AsbtractFeed FeedInput#build(org.jdom.Document)'.
     * <p>
     * @param is W3C SAX InputSource to read to create the AbstractFeed.
     * @return the AbstractFeed read from the W3C SAX InputSource.
     * @throws IllegalArgumentException thrown if feed type could not be understood by any of the underlying parsers.
     * @throws FeedException if the feed could not be parsed
     *
     */
    public AbstractFeed build(InputSource is) throws IllegalArgumentException,FeedException {
        SAXBuilder saxBuilder = new SAXBuilder(_validate);
        try {
            Document document = saxBuilder.build(is);
            return build(document);
        }
        catch (Exception ex) {
            throw new FeedException("Invalid XML",ex);
        }
    }

    /**
     * Builds an AbstractFeed (RSS or Atom) from an W3C DOM document.
     * <p>
     * NOTE: This method delages to the 'AsbtractFeed FeedInput#build(org.jdom.Document)'.
     * <p>
     * @param document W3C DOM document to read to create the AbstractFeed.
     * @return the AbstractFeed read from the W3C DOM document.
     * @throws IllegalArgumentException thrown if feed type could not be understood by any of the underlying parsers.
     * @throws FeedException if the feed could not be parsed
     *
     */
    public AbstractFeed build(org.w3c.dom.Document document) throws IllegalArgumentException,FeedException {
        DOMBuilder domBuilder = new DOMBuilder();
        try {
            Document jdomDoc = domBuilder.build(document);
            return build(jdomDoc);
        }
        catch (Exception ex) {
            throw new FeedException("Invalid XML",ex);
        }
    }

    /**
     * Builds an AbstractFeed (RSS or Atom) from an JDOM document.
     * <p>
     * NOTE: All other build methods delegate to this method.
     * <p>
     * @param document JDOM document to read to create the AbstractFeed.
     * @return the AbstractFeed read from the JDOM document.
     * @throws IllegalArgumentException thrown if feed type could not be understood by any of the underlying parsers.
     * @throws FeedException if the feed could not be parsed
     *
     */
    public AbstractFeed build(Document document) throws IllegalArgumentException,FeedException {
        FeedParser parser = FEED_PARSERS.getParserFor(document);
        if (parser==null) {
            throw new IllegalArgumentException("Invalid document");
        }
        return parser.parse(document, _validate);
    }

}
TOP

Related Classes of com.sun.syndication.io.FeedInput

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.