package org.deri.any23.writer;

import org.deri.any23.extractor.ExtractionContext;
import org.openrdf.model.Resource;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;

public class RepositoryWriter implements TripleHandler {
	private final RepositoryConnection conn;
	
	public RepositoryWriter(RepositoryConnection conn) {
		this.conn = conn;
	}
	
	public void startDocument(URI documentURI) {
		// ignore
	}
	
	public void openContext(ExtractionContext context) {
		// ignore
	}

	public void receiveTriple(Resource s, URI p, Value o,
			ExtractionContext context) {
		try {
			conn.add(conn.getValueFactory().createStatement(s, p, o), context.getDocumentURI());
		} catch (RepositoryException ex) {
			throw new RuntimeException(ex);
		}
	}

	public void receiveNamespace(String prefix, String uri,
			ExtractionContext context) {
		try {
			conn.setNamespace(prefix, uri);
		} catch (RepositoryException ex) {
			throw new RuntimeException(ex);
		}
	}
	
	public void closeContext(ExtractionContext context) {
		// ignore
	}

	public void close() {
		// ignore
	}

	@Override
	public void endDocument(URI documentURI) {
		// ignore
		;
	}	
	
	@Override
	public void setContentLength(long contentLength) {
//		_contentLength = contentLength;
		//ignore
		;
	}
}

@@ -376,16 +376,57 @@
 n conn;%0A
+%09private final Resource overrideContext;%0A
 %09%0A%09publi
@@ -483,20 +483,167 @@
 this
-.conn = conn
+(conn, null);%0A%09%7D%0A%09%0A%09public RepositoryWriter(RepositoryConnection conn, Resource overrideContext) %7B%0A%09%09this.conn = conn;%0A%09%09this.overrideContext = overrideContext
 ;%0A%09%7D
@@ -934,16 +934,35 @@
  p, o), 
+getContextResource(
 context.
@@ -978,16 +978,17 @@
 ntURI())
+)
 ;%0A%09%09%7D ca
@@ -1578,10 +1578,166 @@
 %0A%09%09;%0A%09%7D%0A
+%09%0A%09private Resource getContextResource(Resource fromExtractor) %7B%0A%09%09if (overrideContext != null) %7B%0A%09%09%09return overrideContext;%0A%09%09%7D%0A%09%09return fromExtractor;%0A%09%7D%0A
 %7D%0A

Add a constructor for overriding the OpenRDF context where triples are stored