All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.mrapp.parser.feed.common.source.InputStreamSource Maven / Gradle / Ivy

/*
 * JavaFeedParserCommon Copyright 2013-2014 Michael Rapp
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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 . 
 */
package de.mrapp.parser.feed.common.source;

import static de.mrapp.parser.feed.common.util.Condition.ensureNotNull;

import java.io.IOException;
import java.io.InputStream;

import org.jdom2.Document;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import de.mrapp.parser.feed.common.builder.InvalidSourceException;

/**
 * A source, which allows to read the XML structure of a feed from an
 * {@link InputStream}.
 * 
 * @author Michael Rapp
 * 
 * @since 1.0.0
 */
public class InputStreamSource implements Source {

	/**
	 * The input stream, which allows to read the feed's content.
	 */
	private InputStream inputStream;

	/**
	 * Creates a new source, which allows to read the XML structure of a feed
	 * from an {@link InputStream}.
	 * 
	 * @param inputStream
	 *            The input stream, which allows to read the feed's content, as
	 *            an instance of the class {@link InputStream}. The input stream
	 *            may not be null
	 */
	public InputStreamSource(final InputStream inputStream) {
		setInputStream(inputStream);
	}

	/**
	 * Returns the {@link InputStream}, which allows to read the feed's content.
	 * 
	 * @return The input stream, which allows to read the feed's content, as an
	 *         instance of the class {@link InputStream}. The input stream may
	 *         not be null
	 */
	public final InputStream getInputStream() {
		return inputStream;
	}

	/**
	 * Sets the {@link InputStream}, which allows to read the feed's content.
	 * 
	 * @param inputStream
	 *            The input stream, which should be set, as an instance of the
	 *            class {@link InputStream}. The input stream may not be null
	 */
	public final void setInputStream(final InputStream inputStream) {
		ensureNotNull(inputStream, "The input stream may not be null");
		this.inputStream = inputStream;
	}

	@Override
	public final Document getDocument() throws InvalidSourceException {
		try {
			return new SAXBuilder().build(inputStream);
		} catch (JDOMException e) {
			throw new InvalidSourceException(e.getMessage(), e);
		} catch (IOException e) {
			throw new InvalidSourceException(e.getMessage(), e);
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy