package org.petapico.npop;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPOutputStream;

import net.trustyuri.TrustyUriException;
import net.trustyuri.TrustyUriResource;

import org.nanopub.MalformedNanopubException;
import org.nanopub.MultiNanopubRdfHandler;
import org.nanopub.MultiNanopubRdfHandler.NanopubHandler;
import org.nanopub.Nanopub;
import org.nanopub.NanopubImpl;
import org.nanopub.NanopubUtils;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFParseException;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;

public class Run {

	@com.beust.jcommander.Parameter(description = "input-nanopubs", required = true)
	private List<File> inputNanopubs = new ArrayList<File>();

	public static void main(String[] args) {
		NanopubImpl.ensureLoaded();
		Run obj = new Run();
		JCommander jc = new JCommander(obj);
		try {
			jc.parse(args);
		} catch (ParameterException ex) {
			jc.usage();
			System.exit(1);
		}
		try {
			obj.run();
		} catch (Exception ex) {
			ex.printStackTrace();
			System.exit(1);
		}
	}

	private void run() throws IOException, RDFParseException, RDFHandlerException,
			MalformedNanopubException, TrustyUriException {
		for (File inputFile : inputNanopubs) {
			File outFile = new File(inputFile.getParent(), "op." + inputFile.getName());
			final OutputStream out;
			if (inputFile.getName().matches(".*\\.(gz|gzip)")) {
				out = new GZIPOutputStream(new FileOutputStream(outFile));
			} else {
				out = new FileOutputStream(outFile);
			}
			final RDFFormat format = new TrustyUriResource(inputFile).getFormat(RDFFormat.TRIG);
			MultiNanopubRdfHandler.process(format, inputFile, new NanopubHandler() {

				@Override
				public void handleNanopub(Nanopub np) {
					try {
						NanopubUtils.writeToStream(np, out, format);
					} catch (RDFHandlerException ex) {
						throw new RuntimeException(ex);
					}
				}

			});
			out.close();
		}
	}

}

@@ -545,16 +545,52 @@
 bUtils;%0A
+import org.openrdf.model.Statement;%0A
 import o
@@ -615,16 +615,16 @@
 Format;%0A
-
 import o
@@ -953,16 +953,137 @@
 le%3E();%0A%0A
+%09@com.beust.jcommander.Parameter(names = %22-f%22, description = %22Filter by URI or literal%22)%0A%09private String filter = null;%0A%0A
 %09public 
@@ -1410,16 +1410,71 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09private RDFFormat format;%0A%09private OutputStream out;%0A%0A
 %09private
@@ -1721,35 +1721,8 @@
 ));%0A
-%09%09%09final OutputStream out;%0A
 %09%09%09i
@@ -1901,24 +1901,8 @@
 %0A%09%09%09
-final RDFFormat 
 form
@@ -2122,50 +2122,18 @@
 %09%09%09%09
-NanopubUtils.writeToStream(np, out, format
+process(np
 );%0A%09
@@ -2246,18 +2246,18 @@
 lose();%0A
-
 %09%09%7D%0A%09%7D%0A%0A
 %7D%0A
@@ -2254,10 +2254,630 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09private void process(Nanopub np) throws RDFHandlerException %7B%0A%09%09if (filter != null) %7B%0A%09%09%09boolean keep = false;%0A%09%09%09for (Statement st : NanopubUtils.getStatements(np)) %7B%0A%09%09%09%09if (st.getSubject().stringValue().equals(filter)) %7B%0A%09%09%09%09%09keep = true;%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%09if (st.getPredicate().stringValue().equals(filter)) %7B%0A%09%09%09%09%09keep = true;%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%09if (st.getObject().stringValue().equals(filter)) %7B%0A%09%09%09%09%09keep = true;%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%09if (st.getContext().stringValue().equals(filter)) %7B%0A%09%09%09%09%09keep = true;%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09if (!keep) return;%0A%09%09%7D%0A%09%09NanopubUtils.writeToStream(np, out, format);%0A%09%7D%0A%0A
 %7D%0A

Support filtering