<commit_before>package org.sbolstandard.core2;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.net.URI;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import javax.json.Json;
import javax.json.JsonReader;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

import org.sbolstandard.core2.abstract_classes.Location;

import uk.ac.intbio.core.io.turtle.TurtleIo;
import uk.ac.ncl.intbio.core.datatree.DocumentRoot;
import uk.ac.ncl.intbio.core.datatree.Literal;
import uk.ac.ncl.intbio.core.datatree.NamedProperty;
import uk.ac.ncl.intbio.core.datatree.NamespaceBinding;
import uk.ac.ncl.intbio.core.datatree.NestedDocument;
import uk.ac.ncl.intbio.core.datatree.TopLevelDocument;
import uk.ac.ncl.intbio.core.io.IoReader;
import uk.ac.ncl.intbio.core.io.json.JsonIo;
import uk.ac.ncl.intbio.core.io.json.StringifyQName;
import uk.ac.ncl.intbio.core.io.rdf.RdfIo;

public class SBOLReader {

	//TODO: make sure to check each local variable if they are empty before setting them.

	public static SBOLDocument read(String fileName) throws Throwable
	{
		FileInputStream fis = new FileInputStream(fileName);
		String inputStreamString = new Scanner(fis,"UTF-8").useDelimiter("\\A").next();
		return readRdf(new File(fileName));
	}

	public static SBOLDocument readJson(String fileName) throws Throwable
	{
		return readJson(new File(fileName));
	}

	public static SBOLDocument readRdf(String fileName) throws Throwable
	{
		return readRdf(new File(fileName));
	}

	public static SBOLDocument readTurtle(String fileName) throws Throwable
	{
		return readTurtle(new File(fileName));
	}

	public static SBOLDocument readJson(File file) throws Throwable
	{
		FileInputStream stream = new FileInputStream(file);
		BufferedInputStream buffer = new BufferedInputStream(stream);
		String inputStreamString = new Scanner(stream,"UTF-8").useDelimiter("\\A").next();

		DocumentRoot<QName> document= readJson(new StringReader(inputStreamString));
		return readJson(buffer, document);
	}

	public static SBOLDocument read(File file) throws Throwable
	{
		FileInputStream stream = new FileInputStream(file);
		BufferedInputStream buffer = new BufferedInputStream(stream);
		String inputStreamString = new Scanner(stream,"UTF-8").useDelimiter("\\A").next();

		DocumentRoot<QName> document= readRdf(new StringReader(inputStreamString));
		return read(buffer, document);
	}

	/**
	 * Takes in the given .rdf file and converts the file to an SBOLDocument
	 */
	public static SBOLDocument readRdf(File file) throws Throwable
	{
		FileInputStream stream = new FileInputStream(file);
		BufferedInputStream buffer = new BufferedInputStream(stream);
		String inputStreamString = new Scanner(stream,"UTF-8").useDelimiter("\\A").next();

		DocumentRoot<QName> document= readRdf(new StringReader(inputStreamString));
		// TODO: search namespace to see if v1 of v2, if v2 call read below else
		// call a new v1 reader.
		for(NamespaceBinding n: document.getNamespaceBindings())
		{
			if(n.equals(Sbol1Terms.sbol1))
			{
				return readRdfV1(buffer, document);
			}
		}
		return readRdf(buffer, document);
	}

	public static SBOLDocument readTurtle(File file) throws Throwable
	{
		FileInputStream stream = new FileInputStream(file);
		BufferedInputStream buffer = new BufferedInputStream(stream);
		String inputStreamString = new Scanner(stream,"UTF-8").useDelimiter("\\A").next();

		DocumentRoot<QName> document= readTurtle(new StringReader(inputStreamString));
		return readTurtle(buffer, document);
	}

	public static SBOLDocument readJson(InputStream in, DocumentRoot<QName> document)
	{
		SBOLDocument SBOLDoc = new SBOLDocument();
		for(NamespaceBinding n : document.getNamespaceBindings())
		{
			SBOLDoc.addNameSpaceBinding(URI.create(n.getNamespaceURI()), n.getPrefix());
		}
		readTopLevelDocs(SBOLDoc,document);
		return SBOLDoc;
	}

	public static SBOLDocument read(InputStream in, DocumentRoot<QName> document)
	{
		SBOLDocument SBOLDoc = new SBOLDocument();
		for(NamespaceBinding n : document.getNamespaceBindings())
		{
			SBOLDoc.addNameSpaceBinding(URI.create(n.getNamespaceURI()), n.getPrefix());
		}
		readTopLevelDocs(SBOLDoc,document);
		return SBOLDoc;
	}

	public static SBOLDocument readRdf(InputStream in, DocumentRoot<QName> document)
	{
		SBOLDocument SBOLDoc = new SBOLDocument();
		for(NamespaceBinding n : document.getNamespaceBindings())
		{
			SBOLDoc.addNameSpaceBinding(URI.create(n.getNamespaceURI()), n.getPrefix());
		}
		readTopLevelDocs(SBOLDoc,document);
		return SBOLDoc;
	}

	// TODO: readV1 should copy namespaces except sbol v1 namespace which is replaced with sbol v2
	// call readTopLevelDocsV1
	public static SBOLDocument readRdfV1(InputStream in, DocumentRoot<QName> document)
	{
		SBOLDocument SBOLDoc = new SBOLDocument();
		for(NamespaceBinding n : document.getNamespaceBindings())
		{
			if(n.equals(Sbol1Terms.sbol1))
				SBOLDoc.addNameSpaceBinding(URI.create(Sbol2Terms.sbol2.getNamespaceURI()), Sbol2Terms.sbol2.getPrefix());
			else
				SBOLDoc.addNameSpaceBinding(URI.create(n.getNamespaceURI()), n.getPrefix());
		}
		readTopLevelDocsV1(SBOLDoc, document);
		return SBOLDoc;
	}

	public static SBOLDocument readTurtle(InputStream in, DocumentRoot<QName> document)
	{
		SBOLDocument SBOLDoc = new SBOLDocument();
		for(NamespaceBinding n : document.getNamespaceBindings())
		{
			SBOLDoc.addNameSpaceBinding(URI.create(n.getNamespaceURI()), n.getPrefix());
		}
		readTopLevelDocs(SBOLDoc,document);
		return SBOLDoc;
	}

	private static DocumentRoot<QName> readJson(Reader stream) throws Exception
	{
		JsonReader reader = Json.createReaderFactory(Collections.<String, Object> emptyMap()).createReader(stream);<commit_msg>merging new changes made on sbol-data. <commit_after>package org.sbolstandard.core2;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.net.URI;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import javax.json.Json;
import javax.json.JsonReader;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

import org.sbolstandard.core2.abstract_classes.Location;

import uk.ac.intbio.core.io.turtle.TurtleIo;
import uk.ac.ncl.intbio.core.datatree.DocumentRoot;
import uk.ac.ncl.intbio.core.datatree.Literal;
import uk.ac.ncl.intbio.core.datatree.NamedProperty;
import uk.ac.ncl.intbio.core.datatree.NamespaceBinding;
import uk.ac.ncl.intbio.core.datatree.NestedDocument;
import uk.ac.ncl.intbio.core.datatree.TopLevelDocument;
import uk.ac.ncl.intbio.core.io.IoReader;
import uk.ac.ncl.intbio.core.io.json.JsonIo;
import uk.ac.ncl.intbio.core.io.json.StringifyQName;
import uk.ac.ncl.intbio.core.io.rdf.RdfIo;

public class SBOLReader {

	public static SBOLDocument read(String fileName) throws Throwable
	{
		FileInputStream fis = new FileInputStream(fileName);
		String inputStreamString = new Scanner(fis,"UTF-8").useDelimiter("\\A").next();
		return readRdf(new File(fileName));
	}

	public static SBOLDocument readJson(String fileName) throws Throwable
	{
		return readJson(new File(fileName));
	}

	public static SBOLDocument readRdf(String fileName) throws Throwable
	{
		return readRdf(new File(fileName));
	}

	public static SBOLDocument readTurtle(String fileName) throws Throwable
	{
		return readTurtle(new File(fileName));
	}

	public static SBOLDocument readJson(File file) throws Throwable
	{
		FileInputStream stream = new FileInputStream(file);
		BufferedInputStream buffer = new BufferedInputStream(stream);
		String inputStreamString = new Scanner(stream,"UTF-8").useDelimiter("\\A").next();

		DocumentRoot<QName> document= readJson(new StringReader(inputStreamString));
		return readJson(buffer, document);
	}

	public static SBOLDocument read(File file) throws Throwable
	{
		FileInputStream stream = new FileInputStream(file);
		BufferedInputStream buffer = new BufferedInputStream(stream);
		String inputStreamString = new Scanner(stream,"UTF-8").useDelimiter("\\A").next();

		DocumentRoot<QName> document= readRdf(new StringReader(inputStreamString));
		return read(buffer, document);
	}

	/**
	 * Takes in the given .rdf file and converts the file to an SBOLDocument
	 */
	public static SBOLDocument readRdf(File file) throws Throwable
	{
		FileInputStream stream = new FileInputStream(file);
		BufferedInputStream buffer = new BufferedInputStream(stream);
		String inputStreamString = new Scanner(stream,"UTF-8").useDelimiter("\\A").next();

		DocumentRoot<QName> document= readRdf(new StringReader(inputStreamString));
		// TODO: search namespace to see if v1 of v2, if v2 call read below else
		// call a new v1 reader.
		for(NamespaceBinding n: document.getNamespaceBindings())
		{
			if(n.equals(Sbol1Terms.sbol1))
			{
				return readRdfV1(buffer, document);
			}
		}
		return readRdf(buffer, document);
	}

	public static SBOLDocument readTurtle(File file) throws Throwable
	{
		FileInputStream stream = new FileInputStream(file);
		BufferedInputStream buffer = new BufferedInputStream(stream);
		String inputStreamString = new Scanner(stream,"UTF-8").useDelimiter("\\A").next();

		DocumentRoot<QName> document= readTurtle(new StringReader(inputStreamString));
		return readTurtle(buffer, document);
	}

	public static SBOLDocument readJson(InputStream in, DocumentRoot<QName> document)
	{
		SBOLDocument SBOLDoc = new SBOLDocument();
		for(NamespaceBinding n : document.getNamespaceBindings())
		{
			SBOLDoc.addNameSpaceBinding(URI.create(n.getNamespaceURI()), n.getPrefix());
		}
		readTopLevelDocs(SBOLDoc,document);
		return SBOLDoc;
	}

	public static SBOLDocument read(InputStream in, DocumentRoot<QName> document)
	{
		SBOLDocument SBOLDoc = new SBOLDocument();
		for(NamespaceBinding n : document.getNamespaceBindings())
		{
			SBOLDoc.addNameSpaceBinding(URI.create(n.getNamespaceURI()), n.getPrefix());
		}
		readTopLevelDocs(SBOLDoc,document);
		return SBOLDoc;
	}

	public static SBOLDocument readRdf(InputStream in, DocumentRoot<QName> document)
	{
		SBOLDocument SBOLDoc = new SBOLDocument();
		for(NamespaceBinding n : document.getNamespaceBindings())
		{
			SBOLDoc.addNameSpaceBinding(URI.create(n.getNamespaceURI()), n.getPrefix());
		}
		readTopLevelDocs(SBOLDoc,document);
		return SBOLDoc;
	}

	// TODO: readV1 should copy namespaces except sbol v1 namespace which is replaced with sbol v2
	// call readTopLevelDocsV1
	public static SBOLDocument readRdfV1(InputStream in, DocumentRoot<QName> document)
	{
		SBOLDocument SBOLDoc = new SBOLDocument();
		for(NamespaceBinding n : document.getNamespaceBindings())
		{
			if(n.getNamespaceURI().equals(Sbol1Terms.sbol1.getNamespaceURI()))
				SBOLDoc.addNameSpaceBinding(URI.create(Sbol2Terms.sbol2.getNamespaceURI()), Sbol2Terms.sbol2.getPrefix());
			else
				SBOLDoc.addNameSpaceBinding(URI.create(n.getNamespaceURI()), n.getPrefix());
		}
		readTopLevelDocsV1(SBOLDoc, document);
		return SBOLDoc;
	}

	public static SBOLDocument readTurtle(InputStream in, DocumentRoot<QName> document)
	{
		SBOLDocument SBOLDoc = new SBOLDocument();
		for(NamespaceBinding n : document.getNamespaceBindings())
		{
			SBOLDoc.addNameSpaceBinding(URI.create(n.getNamespaceURI()), n.getPrefix());
		}
		readTopLevelDocs(SBOLDoc,document);
		return SBOLDoc;
	}

	private static DocumentRoot<QName> readJson(Reader stream) throws Exception
	{
		JsonReader reader = Json.createReaderFactory(Collections.<String, Object> emptyMap()).createReader(stream);
{'fraction_non_alphanumeric': 0.10921727618732417, 'fraction_numerical': 0.004302498758894589, 'mean_word_length': 5.637561779242175, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7148026', 'n_tokens_mistral': 4043, 'n_tokens_neox': 3833, 'n_words': 804}