package legends;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import legends.model.LegendsXml;
import legends.xml.handlers.AnnotationContentHandler;

public class LegendsReader {
	private static final Log LOG = LogFactory.getLog(LegendsReader.class);

	public static void read(Path path, Charset cs) throws SAXException, IOException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		LOG.info("load legends: "+path);
		XMLReader xmlReader = XMLReaderFactory.createXMLReader();
		AnnotationContentHandler contentHandler = new AnnotationContentHandler(LegendsXml.class);
		contentHandler.setXmlReader(xmlReader);
		xmlReader.setContentHandler(contentHandler);
		
		CharsetDecoder decoder = cs.newDecoder();
        decoder.onMalformedInput(CodingErrorAction.IGNORE);
        
		InputSource inputSource = new InputSource(new BufferedReader( new InputStreamReader(Files.newInputStream(path), decoder)));
		xmlReader.parse(inputSource);
		
		contentHandler.printMappedValues();
	}
}

@@ -96,32 +96,88 @@
 utStreamReader;%0A
+import java.io.PipedReader;%0Aimport java.io.PipedWriter;%0A
 import java.lang
@@ -204,32 +204,32 @@
 argetException;%0A
-
 import java.nio.
@@ -897,33 +897,36 @@
 iationException,
- 
+%0A%09%09%09
 IllegalAccessExc
@@ -1018,9 +1018,11 @@
 s: %22
-+
+ + 
 path
@@ -1265,19 +1265,17 @@
 ndler);%0A
-%09%09
 %0A
+
 %09%09Charse
@@ -1310,24 +1310,18 @@
 oder();%0A
-        
+%09%09
 decoder.
@@ -1368,140 +1368,947 @@
 E);%0A
-        %0A%09%09InputSource inputSource = new InputSource(new BufferedReader( new InputStreamReader(Files.newInputStream(path), decoder))
+%0A%09%09BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(path), decoder));%0A%09%09PipedWriter writer = new PipedWriter();%0A%09%09PipedReader fixedReader = new PipedReader(writer);%0A%0A%09%09new Thread() %7B%0A%09%09%09public void run() %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09String line = null;%0A%09%09%09%09%09while ((line = reader.readLine()) != null) %7B%0A%09%09%09%09%09%09if (line.contains(%22conspirator_hfid%22)) %7B%0A%09%09%09%09%09%09%09line = line.replaceAll(%22%3Cconspirator_hfid%3E(%5C%5Cd+)%3C%5C%5C/conspirator%3E%22,%0A%09%09%09%09%09%09%09%09%09%22%3Cconspirator_hfid%3E$1%3C/conspirator_hfid%3E%22);%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09if (line.contains(%22interrogator_hfid%22)) %7B%0A%09%09%09%09%09%09%09line = line.replaceAll(%22%3Cinterrogator_hfid%3E(%5C%5Cd+)%3C%5C%5C/convicted_hfid%3E%22,%0A%09%09%09%09%09%09%09%09%09%22%3Cinterrogator_hfid%3E$1%3C/interrogator_hfid%3E%22);%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09writer.write(line);%0A%09%09%09%09%09%09writer.write(%22%5Cr%5Cn%22);%0A%09%09%09%09%09%7D%0A%09%09%09%09%09writer.close();%0A%09%09%09%09%09reader.close();%0A%09%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09%09e.printStackTrace();%0A%09%09%09%09%7D%0A%09%09%09%7D;%0A%09%09%7D.start();%0A%0A%09%09InputSource inputSource = new InputSource(fixedReader
 );%0A%09
@@ -2338,18 +2338,16 @@
 ource);%0A
-%09%09
 %0A%09%09conte

Version 0.47 world can't be loaded, fixes #32