package nl.idgis.geoportaal.conversie;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;

public class Converter {
	
	private String xmlEncoding;
	private OutDestination outType;
	
	public Converter(String xmlEncoding, OutDestination outType) throws IOException {
		this.xmlEncoding = xmlEncoding;
		this.outType = outType;
	}
	
	public void convertFiles(File[] files) throws IOException {		
		int i = 0;
		for (File file : files) {
			System.out.println(i++ + ": " + file.getName());
			try {
				outType.convertFile(file, parseDocument(file));
			} catch(Exception e) {
				System.out.println("File is invalid");
				e.printStackTrace();
			}
		}
	}
	
	private MetadataDocument parseDocument(File xmlFile) throws Exception {
		// parse xml to DOM
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		dbf.setNamespaceAware(true);
		
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document d = db.parse(new InputSource(new InputStreamReader(new FileInputStream(xmlFile), xmlEncoding)));
		
		// construct namespace context
		Map<String, String> ns = new HashMap<>();
		ns.put("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
		ns.put("dc", "http://purl.org/dc/elements/1.1/");
		ns.put("dcterms", "http://purl.org/dc/terms/");
		ns.put("ows", "http://www.opengis.net/ows");
		
		Map<String, String> pf = new HashMap<>();
		for(Map.Entry<String, String> e : ns.entrySet()) {
			pf.put(e.getValue(), e.getKey());
		}

		NamespaceContext nc = new NamespaceContext() {

			@Override
			public String getNamespaceURI(String pf) {
				return ns.get(pf);
			}

			@Override
			public String getPrefix(String ns) {
				return pf.get(ns);
			}
			
			@Override
			@SuppressWarnings("rawtypes")
			public Iterator getPrefixes(String ns) {
				return Arrays.asList(getPrefix(ns)).iterator();
			}
			
		};
		
		// construct XPath evaluator
		XPathFactory xf = XPathFactory.newInstance();
		XPath xp = xf.newXPath();
		xp.setNamespaceContext(nc);
		
		return new MetadataDocument(d, xp);
	}
}

@@ -150,24 +150,53 @@
 eamReader;%0D%0A
+import java.io.PrintWriter;%0D%0A
 import java.
@@ -806,25 +806,24 @@
 tType;%0D%0A%09%7D%0D%0A
-%09
 %0D%0A%09public vo
@@ -872,18 +872,75 @@
 eption %7B
-%09%09
+%0D%0A%09%09PrintWriter errorWriter = new PrintWriter(%22log.txt%22);%0D%0A
 %0D%0A%09%09int 
@@ -1158,17 +1158,36 @@
  invalid
-%22
+: %22 + file.getName()
 );%0D%0A%09%09%09%09
@@ -1215,21 +1215,175 @@
 %0A%09%09%09
-%7D%0D%0A%09%09%7D
+%09errorWriter.println(%22File is invalid: %22 + file.getName());%0D%0A%09%09%09%09e.printStackTrace(errorWriter);%0D%0A%09%09%09%09errorWriter.println();%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%0D%0A%09%09errorWriter.close();
 %0D%0A%09%7D%0D%0A
-%09
 %0D%0A%09p
@@ -1575,26 +1575,24 @@
 are(true);%0D%0A
-%09%09
 %0D%0A%09%09Document
@@ -1736,26 +1736,24 @@
 coding)));%0D%0A
-%09%09
 %0D%0A%09%09// const
@@ -2034,26 +2034,24 @@
 net/ows%22);%0D%0A
-%09%09
 %0D%0A%09%09Map%3CStri
@@ -2410,27 +2410,24 @@
 ns);%0D%0A%09%09%09%7D%0D%0A
-%09%09%09
 %0D%0A%09%09%09@Overri
@@ -2572,19 +2572,16 @@
 %09%7D%0D%0A
-%09%09%09
 %0D%0A%09%09%7D;%0D%0A
 %09%09%0D%0A
@@ -2576,18 +2576,16 @@
 %0D%0A%09%09%7D;%0D%0A
-%09%09
 %0D%0A%09%09// c
@@ -2719,18 +2719,16 @@
 t(nc);%0D%0A
-%09%09
 %0D%0A%09%09retu

print exceptions to log.txt