package ch.tkuhn.nanopub;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.openrdf.model.Statement;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFWriter;
import org.openrdf.rio.RDFWriterRegistry;
import org.openrdf.rio.Rio;

public class NanopubUtils {

	static {
		RDFWriterRegistry.getInstance().add(new CustomTrigWriterFactory());
	}

	private NanopubUtils() {}  // no instances allowed

	public static List<Statement> getStatements(Nanopub nanopub) {
		List<Statement> s = new ArrayList<>();
		s.addAll(nanopub.getHead());
		s.addAll(nanopub.getAssertion());
		s.addAll(nanopub.getProvenance());
		s.addAll(nanopub.getPubinfo());
		return s;
	}

	public static void writeToStream(Nanopub nanopub, OutputStream out, RDFFormat format)
			throws RDFHandlerException {
		RDFWriter writer = Rio.createWriter(format, out);
		writer.startRDF();
		String s = nanopub.getUri().toString();
		if (nanopub instanceof NanopubImpl && !((NanopubImpl) nanopub).getNsPrefixes().isEmpty()) {
			NanopubImpl np = (NanopubImpl) nanopub;
			for (String p : np.getNsPrefixes()) {
				writer.handleNamespace(p, np.getNamespace(p));
			}
		} else {
			writer.handleNamespace("this", s);
			writer.handleNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
			writer.handleNamespace("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
			writer.handleNamespace("rdfg", "http://www.w3.org/2004/03/trix/rdfg-1/");
			writer.handleNamespace("xsd", "http://www.w3.org/2001/XMLSchema#");
			writer.handleNamespace("owl", "http://www.w3.org/2002/07/owl#");
			writer.handleNamespace("dc", "http://purl.org/dc/terms/");
			writer.handleNamespace("pav", "http://swan.mindinformatics.org/ontologies/1.2/pav/");
			writer.handleNamespace("np", "http://www.nanopub.org/nschema#");
		}
		for (Statement st : getStatements(nanopub)) {
			writer.handleStatement(st);
		}
		writer.endRDF();
	}

}

@@ -171,16 +171,51 @@
 Format;%0A
+import org.openrdf.rio.RDFHandler;%0A
 import o
@@ -953,20 +953,172 @@
 out);%0A%09%09
-writ
+propagateToHandler(nanopub, writer);%0A%09%7D%0A%0A%09public static void propagateToHandler(Nanopub nanopub, RDFHandler handler)%0A%09%09%09throws RDFHandlerException %7B%0A%09%09handl
 er.start
@@ -1348,20 +1348,21 @@
 ) %7B%0A%09%09%09%09
-writ
+handl
 er.handl
@@ -1411,28 +1411,29 @@
 %7D else %7B%0A%09%09%09
-writ
+handl
 er.handleNam
@@ -1450,28 +1450,29 @@
 is%22, s);%0A%09%09%09
-writ
+handl
 er.handleNam
@@ -1528,36 +1528,37 @@
 yntax-ns#%22);%0A%09%09%09
-writ
+handl
 er.handleNamespa
@@ -1605,36 +1605,37 @@
 f-schema#%22);%0A%09%09%09
-writ
+handl
 er.handleNamespa
@@ -1683,36 +1683,37 @@
 x/rdfg-1/%22);%0A%09%09%09
-writ
+handl
 er.handleNamespa
@@ -1755,36 +1755,37 @@
 MLSchema#%22);%0A%09%09%09
-writ
+handl
 er.handleNamespa
@@ -1828,28 +1828,29 @@
 /owl#%22);%0A%09%09%09
-writ
+handl
 er.handleNam
@@ -1887,36 +1887,37 @@
 dc/terms/%22);%0A%09%09%09
-writ
+handl
 er.handleNamespa
@@ -1981,28 +1981,29 @@
 /pav/%22);%0A%09%09%09
-writ
+handl
 er.handleNam
@@ -2106,20 +2106,21 @@
 )) %7B%0A%09%09%09
-writ
+handl
 er.handl
@@ -2141,20 +2141,21 @@
 ;%0A%09%09%7D%0A%09%09
-writ
+handl
 er.endRD

Add propagateToHandler utility method