package edu.vt.marian.Document;

import java.io.*;
import java.net.*;
import java.util.*;

import edu.vt.marian.common.*;
// import edu.vt.marian.Document.*;


/**
    Unit tester for EntityMap class.
    @author	Robert France
*/

public class EntityMapTester
{
/******
	static String testStr1 = "&amp;&lt;";
	static String testStr2 = "This is &amp;nother &lt;test&gt; string.";
	static String testStr3 = "This is &Oslash;nothe&rgrave; &AElig;test&flat; string.";
	static String testStr4 = "&Lstrok;&Oslash;&Dstrok;&THORN;&agrave;&aacute;&acirc;&atilde;&amacr;&abreve;&cdot;&auml;";
********/
	static String testStr1 = "&#x0026;&#x003C;";
	static String testStr2 = "This is &#x0026;nother &#x003C;test&#x003E; string.";
	static String testStr3 = "This is an&#x00f8;the&#x0155; t&#233;st&#x266D; string.";
	static String testStr4 = "&#x0141;&#x00D8;&#x0189;&#x00DE;&#x00E0;&#x00E1;&#x00E2;&#x00E3;&#x0101;&#x0103;&#x010B;&#x00E4;&#x00E5;";

        static String failTest1 = "This &string has a spurious amp.";
        static String failTest2 = "This &string has a spurious ampersand and is more than thirty additional characters long.";
        static String failTest3 = "This &string has &#x003C; both > spurious and real &&#x0026;s.";
		
		static String failTest4 = "This string has a b&#x00G5;gus numeric entity.";


    public static void main(String argv[]) throws Exception
    {
        int Err;
        Debug debug = new Debug("debug.conf", "trace.log", false);
        EntityMap testMap = new EntityMap(debug);
        
		
		try {
			BufferedReader inFile = new BufferedReader(new FileReader("ansel_uni_comb.map"));
		    testMap.load(inFile);
		} catch (IOException e)
		{
			System.err.println("Cannot load table.");
		}
		
		
		PrintWriter pw = new PrintWriter(System.out);
		BufferedWriter bw = new BufferedWriter(pw);

        System.out.println("First test:  map entities to characters:");
        System.out.println("   Input:  " + testStr1);
        System.out.print("   Output: ");
        testMap.mapStringFromEntities(testStr1, bw);
		bw.flush();
        System.out.println(); System.out.println();

        System.out.println("Second test:  map characters back to entities:");
        System.out.println("   Input:  " + testStr4);
        StringWriter sw = new StringWriter();
        BufferedWriter bsw = new BufferedWriter( sw );
        testMap.mapStringFromEntities(testStr4, bsw);
		bsw.flush();
		String s0 = sw.toString();
		System.out.print("   Interm: " +  s0 + " [");
		for (int i=0; i<s0.length(); i++)
		{
			System.out.print((int) s0.charAt(i));
			if ( i < s0.length()-1 )
				System.out.print(", ");
		}
		System.out.println("]");
        System.out.print("   Output: ");
        testMap.mapStringToEntities(sw.toString(), bw);
		bw.flush();
		System.out.println();

        System.out.println("Third test:  read string from just entities:");
        System.out.println("   Input:  " + testStr2);
        StringReader sr = new StringReader( testStr2 );
        BufferedReader bsr = new BufferedReader( sr );
        String s1 = testMap.getStringFromEntityReader( bsr );
        System.out.println("   Output: " + s1);
        System.out.println("   Input:  " + testStr3);
        sr = new StringReader( testStr3 );
        bsr = new BufferedReader( sr );
        s1 = testMap.getStringFromEntityReader( bsr );
        System.out.println("   Output: " + s1);

        System.out.println("FAILURE TESTS:  Spurious ampersands:");
        System.out.println("   Input:  " + failTest1);
        sr = new StringReader( failTest1 );
        bsr = new BufferedReader( sr );
        try {
            s1 = testMap.getStringFromEntityReader( bsr );
            System.out.println("   Output: " + s1);
        } catch (EOFException e)
          {System.out.println("   Expected EOF: " + e.getMessage());}
        System.out.println("   Input:  " + failTest2);
        sr = new StringReader( failTest2 );
        bsr = new BufferedReader( sr );
        s1 = testMap.getStringFromEntityReader( bsr );
        System.out.println("   Output: " + s1);
        System.out.println("   Input:  " + failTest3);
        sr = new StringReader( failTest3 );
        bsr = new BufferedReader( sr );
        s1 = testMap.getStringFromEntityReader( bsr );
        System.out.println("   Output: " + s1);
        System.out.println("   Input:  " + failTest4);
        sr = new StringReader( failTest4 );
        bsr = new BufferedReader( sr );
        s1 = testMap.getStringFromEntityReader( bsr );
        System.out.println("   Output: " + s1);
    }
}

