package it.unibz.krdb.odba;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.Iterator;

import org.callimachusproject.io.TurtleStreamWriter;
import org.junit.Test;
import org.openrdf.model.Model;
import org.openrdf.model.Statement;
import org.openrdf.model.impl.LinkedHashModel;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFParser;
import org.openrdf.rio.Rio;
import org.openrdf.rio.helpers.StatementCollector;
import org.openrdf.rio.turtle.TurtleWriter;

import eu.optique.api.mapping.R2RMLMappingManager;
import eu.optique.api.mapping.R2RMLMappingManagerFactory;

public class PrettyR2RMLTest {
	@Test
	public void test1() throws Exception {
		FileInputStream fis = new FileInputStream(new File("src/test/resources/r2rml/npd.ttl"));
		
		R2RMLMappingManager mm = R2RMLMappingManagerFactory.getSesameMappingManager();
		
		// Read the file into a model.
		RDFParser rdfParser = Rio.createParser(RDFFormat.TURTLE);
		Model m = new LinkedHashModel();
		rdfParser.setRDFHandler(new StatementCollector(m));
		rdfParser.parse(fis, "http://example.org");
		
		FileWriter fw = new FileWriter(new File("src/test/resources/r2rml/npd.pretty.ttl"));
		
		TurtleWriter writer = new TurtleStreamWriter(fw, null);
		
		writer.startRDF();
		
		writer.handleNamespace("rr", "http://www.w3.org/ns/r2rml#");
		writer.handleNamespace("npd", "http://sws.ifi.uio.no/data/npd-v2");
		writer.handleNamespace("void", "http://rdfs.org/ns/void#");
		writer.handleNamespace("xsd", "http://www.w3.org/2001/XMLSchema#");
		writer.handleNamespace("owl", "http://www.w3.org/2002/07/owl#");
		writer.handleNamespace("ex", "http://example.org/ex#");
		writer.handleNamespace("mastro", "http://www.dis.uniroma1.it/mastro#");
		
		
		
		Iterator<Statement> it = m.iterator();
		while(it.hasNext()){
			writer.handleStatement(it.next());
		}
		
		writer.endRDF();
		
			
	}
}

@@ -22,16 +22,61 @@
 .odba;%0A%0A
+import it.unibz.krdb.obda.io.PrefixManager;%0A%0A
 import j
@@ -164,24 +164,94 @@
 va.util.
-Iterator
+HashMap;%0Aimport java.util.Iterator;%0Aimport java.util.Map;%0Aimport java.util.Set
 ;%0A%0Aimpor
@@ -349,24 +349,60 @@
 odel.Model;%0A
+import org.openrdf.model.Namespace;%0A
 import org.o
@@ -506,16 +506,95 @@
 Format;%0A
+import org.openrdf.rio.RDFHandler;%0Aimport org.openrdf.rio.RDFHandlerException;%0A
 import o
@@ -1025,16 +1025,22 @@
 %22));%0A%09%09%0A
+%09%09%0A%09%09%0A
 %09%09R2RMLM
@@ -1214,138 +1214,569 @@
 ;%0A%09%09
-Model m = new LinkedHashModel();%0A%09%09rdfParser.setRDFHandler(new StatementCollector(m));%0A%09%09rdfParser.parse(fis, %22http://example.org%22
+%0A%09%09//RDFNamespaceCollector rdfNamespacesCollector = new RDFNamespaceCollector();%0A%09%09%0A%09%09//rdfParser.setRDFHandler(rdfNamespacesCollector);%0A%09%09%0A%09%09Model m = new LinkedHashModel();%0A%09%09%0A%09%09//Set%3CNamespace%3E namespaces = m.getNamespaces();%0A%09%09%0A%09%09%0A%09%09%0A%09%09StatementCollector collector = new StatementCollector(m);%0A%09%09rdfParser.setRDFHandler(collector);%0A%09%09rdfParser.parse(fis, %22http://example.org%22);%0A%09%09%0A%09%09Map%3CString, String%3E namespaces = collector.getNamespaces();%0A%09%09%0A%09%09//Map%3CString, String%3E namespaces = rdfNamespacesCollector.getNamespaces();%0A%09%09%0A%09%09System.out.println(namespaces
 );%0A%09
@@ -2563,10 +2563,13 @@
 %0A%09%09%09%0A%09%7D%0A
+%09%0A%0A
 %7D%0A

extract namespaces