package util.kif;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import util.gdl.factory.GdlFactory;
import util.gdl.factory.exceptions.GdlFormatException;
import util.gdl.grammar.Gdl;
import util.symbol.factory.SymbolFactory;
import util.symbol.factory.exceptions.SymbolFormatException;
import util.symbol.grammar.SymbolList;

public class KifReader
{
	public static List<Gdl> read(String filename) throws IOException, SymbolFormatException, GdlFormatException
	{
		List<Gdl> contents = new ArrayList<Gdl>();

		String string = "(" + readFile(filename) + ")";
		SymbolList list = (SymbolList) SymbolFactory.create(string);
		for (int i = 0; i < list.size(); i++)
		{
			contents.add(GdlFactory.create(list.get(i)));
		}

		return contents;
	}

	protected static String readFile(String filename) throws IOException
	{
		StringBuilder sb = new StringBuilder();
		String line = null;

		BufferedReader br = new BufferedReader(new FileReader(filename));
		while ((line = br.readLine()) != null)
		{
			int comment = line.indexOf(';');
			int cutoff = (comment == -1) ? line.length() : comment;

			sb.append(line.substring(0, cutoff));
			sb.append(" ");
		}

		return sb.toString();
	}
}
@@ -94,24 +94,86 @@
 OException;%0A
+import java.io.InputStream;%0Aimport java.io.InputStreamReader;%0A
 import java.
@@ -612,110 +612,505 @@
 tion
-%0A%09%7B%0A%09%09List%3CGdl%3E contents = new ArrayList%3CGdl%3E();%0A%0A%09%09String string = %22(%22 + readFile(filename
+ %7B%0A%09    return convertIntoGdl(%22(%22 + readFile(filename) + %22)%22);%0A%09%7D%0A%09%0A    public static List%3CGdl%3E readStream(InputStream in) throws IOException, SymbolFormatException, GdlFormatException %7B%0A        return convertIntoGdl(%22(%22 + readStream(new BufferedReader(new InputStreamReader(in))
 ) + %22)%22
+)
 ;%0A
-%09%09
+    %7D%09%0A%09%0A%09protected static List%3CGdl%3E convertIntoGdl(String gameDescription) throws IOException, SymbolFormatException, GdlFormatException %7B%0A        List%3CGdl%3E contents = new ArrayList%3CGdl%3E();%0A        %0A        
 Symb
@@ -1161,19 +1161,34 @@
 ate(
-string);%0A%09%09
+gameDescription);%0A        
 for 
@@ -1221,23 +1221,34 @@
 ); i++)%0A
-%09%09%7B%0A%09%09%09
+        %7B%0A        
 contents
@@ -1285,23 +1285,43 @@
 t(i)));%0A
-%09%09%7D%0A%0A%09%09
+        %7D%0A        %0A        
 return c
@@ -1406,147 +1406,270 @@
 tion
-%0A%09
+ 
 %7B%0A%09%09
-StringBuil
+BufferedRea
 der 
-s
 b
+r
  = new 
-StringBuilder();%0A%09%09String line = null;%0A%0A%09%09BufferedReader br = new BufferedReader(new FileReader(filename));%0A%09%09
+BufferedReader(new FileReader(filename));%0A%09%09return readStream(br);%0A%09%7D%0A%09%0A%09protected static String readStream(BufferedReader br) throws IOException %7B%0A        StringBuilder sb = new StringBuilder();%0A        String line = null;%0A%0A        
 whil
@@ -1707,15 +1707,30 @@
 ll)%0A
-%09%09%7B%0A%09%09%09
+        %7B%0A            
 int 
@@ -1758,19 +1758,28 @@
 f(';');%0A
-%09%09%09
+            
 int cuto
@@ -1827,19 +1827,28 @@
 mment;%0A%0A
-%09%09%09
+            
 sb.appen
@@ -1881,11 +1881,20 @@
 ));%0A
-%09%09%09
+            
 sb.a
@@ -1909,15 +1909,27 @@
 %22);%0A
-%09%09%7D%0A%0A%09%09
+        %7D%0A%0A        
 retu

Separated out the KifReader functionality a little more cleanly.