// Path: core/main/src/com/complexible/common/openrdf/util/ModelBuildingRDFHandler.java
// public final class ModelBuildingRDFHandler extends AbstractRDFHandler {
// 
// 	/**
// 	 * The graph to collect statements in
// 	 */
// 	private final Model mGraph;
// 
// 	/**
// 	 * Create a new GraphBuildingRDFHandler
// 	 */
// 	public ModelBuildingRDFHandler() {
// 		this(Models2.newModel());
// 	}
// 
// 	/**
// 	 * Create a new GraphBuildingRDFHandler that will insert statements into the supplied Graph
// 	 * @param theGraph the graph to insert into
// 	 */
// 	public ModelBuildingRDFHandler(final Model theGraph) {
// 		mGraph = theGraph;
// 	}
// 
// 	/**
// 	 * {@inheritDoc}
// 	 */
// 	@Override
// 	public void handleStatement(final Statement theStatement) throws RDFHandlerException {
// 		mGraph.add(theStatement);
// 	}
// 
// 	/**
// 	 * Return the graph built from events fired to this handler
// 	 * @return the graph
// 	 */
// 	public Model getModel() {
// 		return mGraph;
// 	}
// 
// 	/**
// 	 * Clear the underlying graph of all collected statements
// 	 */
// 	public void clear() {
// 		mGraph.clear();
// 	}
// }
// 
// Path: core/main/src/com/complexible/common/openrdf/util/RDFByteSource.java
// public abstract class RDFByteSource extends ByteSource {
// 
// 	/**
// 	 * Return the RDF format used by the source
// 	 * @return  the format
// 	 */
// 	public abstract RDFFormat getFormat();
// 
// 	/**
// 	 * Return the base uri that should be used when parsing this source
// 	 * @return the base
// 	 */
// 	public String getBaseURI() {
// 		return "http://openrdf.clarkparsia.com";
// 	}
// 
// 	public static RDFByteSource create(final ByteSource theSource, final RDFFormat theFormat) {
// 		return new RDFByteSource() {
// 			@Override
// 			public RDFFormat getFormat() {
// 				return theFormat;
// 			}
// 
// 			@Override
// 			public InputStream openStream() throws IOException {
// 				return theSource.openStream();
// 			}
// 		};
// 	}
// }


import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import com.complexible.common.openrdf.util.ModelBuildingRDFHandler;
import com.complexible.common.openrdf.util.RDFByteSource;
import com.google.common.base.Charsets;
import org.openrdf.model.Model;
import org.openrdf.model.Statement;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandler;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFParseException;
import org.openrdf.rio.RDFParser;
import org.openrdf.rio.RDFWriter;
import org.openrdf.rio.Rio;
import org.openrdf.rio.helpers.BasicParserSettings;
/*
 * Copyright (c) 2009-2015 Clark & Parsia, LLC. <http://www.clarkparsia.com>
 *
 * 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.complexible.common.openrdf.model;



/**
 * <p>Support for IO for {@link Model models}/</p>
 *
 * @author  Michael Grove
 * @since   4.0
 * @version 4.0
 */
public final class ModelIO {
	public static final String DEFAULT_BASE_URI = "http://openrdf.clarkparsia.com/";

	private ModelIO() {
		throw new AssertionError();
	}

	/**
	 * Read an RDF graph from the specified file
	 * @param theFile	the file to read from
	 * @return			the RDF graph contained in the file
	 *
	 * @throws IOException			if there was an error reading from the file
	 * @throws RDFParseException	if the RDF could not be parsed
	 */
	public static Model read(final Path theFile) throws IOException, RDFParseException {
		return read(theFile, Rio.getParserFormatForFileName(theFile.getFileName().toString()).orElse(RDFFormat.TURTLE));
	}

	public static Model read(final Path theFile, final RDFFormat theFormat) throws IOException, RDFParseException {
		return read(new InputStreamReader(Files.newInputStream(theFile), getCharset(theFormat).orElse(Charsets.UTF_8)),
		            theFormat,
		            DEFAULT_BASE_URI);
	}

	private static Optional<Charset> getCharset(final RDFFormat theFormat) {
		return theFormat.hasCharset() ? Optional.of(theFormat.getCharset()) : Optional.empty();
	}

// Path: core/main/src/com/complexible/common/openrdf/util/ModelBuildingRDFHandler.java
// public final class ModelBuildingRDFHandler extends AbstractRDFHandler {
// 
// 	/**
// 	 * The graph to collect statements in
// 	 */
// 	private final Model mGraph;
// 
// 	/**
// 	 * Create a new GraphBuildingRDFHandler
// 	 */
// 	public ModelBuildingRDFHandler() {
// 		this(Models2.newModel());
// 	}
// 
// 	/**
// 	 * Create a new GraphBuildingRDFHandler that will insert statements into the supplied Graph
// 	 * @param theGraph the graph to insert into
// 	 */
// 	public ModelBuildingRDFHandler(final Model theGraph) {
// 		mGraph = theGraph;
// 	}
// 
// 	/**
// 	 * {@inheritDoc}
// 	 */
// 	@Override
// 	public void handleStatement(final Statement theStatement) throws RDFHandlerException {
// 		mGraph.add(theStatement);
// 	}
// 
// 	/**
// 	 * Return the graph built from events fired to this handler
// 	 * @return the graph
// 	 */
// 	public Model getModel() {
// 		return mGraph;
// 	}
// 
// 	/**
// 	 * Clear the underlying graph of all collected statements
// 	 */
// 	public void clear() {
// 		mGraph.clear();
// 	}
// }
// 
// Path: core/main/src/com/complexible/common/openrdf/util/RDFByteSource.java
// public abstract class RDFByteSource extends ByteSource {
// 
// 	/**
// 	 * Return the RDF format used by the source
// 	 * @return  the format
// 	 */
// 	public abstract RDFFormat getFormat();
// 
// 	/**
// 	 * Return the base uri that should be used when parsing this source
// 	 * @return the base
// 	 */
// 	public String getBaseURI() {
// 		return "http://openrdf.clarkparsia.com";
// 	}
// 
// 	public static RDFByteSource create(final ByteSource theSource, final RDFFormat theFormat) {
// 		return new RDFByteSource() {
// 			@Override
// 			public RDFFormat getFormat() {
// 				return theFormat;
// 			}
// 
// 			@Override
// 			public InputStream openStream() throws IOException {
// 				return theSource.openStream();
// 			}
// 		};
// 	}
// }


// Path: core/main/src/com/complexible/common/openrdf/model/ModelIO.java
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import com.complexible.common.openrdf.util.ModelBuildingRDFHandler;
import com.complexible.common.openrdf.util.RDFByteSource;
import com.google.common.base.Charsets;
import org.openrdf.model.Model;
import org.openrdf.model.Statement;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandler;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFParseException;
import org.openrdf.rio.RDFParser;
import org.openrdf.rio.RDFWriter;
import org.openrdf.rio.Rio;
import org.openrdf.rio.helpers.BasicParserSettings;

/*
 * Copyright (c) 2009-2015 Clark & Parsia, LLC. <http://www.clarkparsia.com>
 *
 * 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.complexible.common.openrdf.model;



/**
 * <p>Support for IO for {@link Model models}/</p>
 *
 * @author  Michael Grove
 * @since   4.0
 * @version 4.0
 */
public final class ModelIO {
	public static final String DEFAULT_BASE_URI = "http://openrdf.clarkparsia.com/";

	private ModelIO() {
		throw new AssertionError();
	}

	/**
	 * Read an RDF graph from the specified file
	 * @param theFile	the file to read from
	 * @return			the RDF graph contained in the file
	 *
	 * @throws IOException			if there was an error reading from the file
	 * @throws RDFParseException	if the RDF could not be parsed
	 */
	public static Model read(final Path theFile) throws IOException, RDFParseException {
		return read(theFile, Rio.getParserFormatForFileName(theFile.getFileName().toString()).orElse(RDFFormat.TURTLE));
	}

	public static Model read(final Path theFile, final RDFFormat theFormat) throws IOException, RDFParseException {
		return read(new InputStreamReader(Files.newInputStream(theFile), getCharset(theFormat).orElse(Charsets.UTF_8)),
		            theFormat,
		            DEFAULT_BASE_URI);
	}

	private static Optional<Charset> getCharset(final RDFFormat theFormat) {
		return theFormat.hasCharset() ? Optional.of(theFormat.getCharset()) : Optional.empty();
	}

	public static Model read(final RDFByteSource theSource) throws IOException, RDFParseException {