package edu.vt.marian.Document;

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

import edu.vt.marian.common.*;


/**
    Unit tester for MarcDocument (and thus MarcRecord) class.
    @author	Robert France
*/

public class MarcFilter
{
	// Defaults:  can be reset by command line argument.
	static private String docFile = null;		// Default is System.in.
	static private String mapFile = "ansel_uni_comb.map";

	static private String XmlHeader =
		"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
		System.getProperty("line.separator") +
		"<sequence xmlns=\"http://www.dlib.vt.edu/projects/OAi/marcxml/container\"" + 
		System.getProperty("line.separator") +
		"     xmlns:xsi=\"http://www.w3.org/2000/10/XMLSchema-instance\"" + 
		System.getProperty("line.separator") +
		"      xsi:schemaLocation=\"http://www.openarchives.org/OAI/oai_marc" +
		System.getProperty("line.separator") +
		"            http://www.openarchives.org/OAI/oai_marc.xsd" +
		System.getProperty("line.separator") +
		"            http://www.dlib.vt.edu/projects/OAi/marcxml/container" +
		System.getProperty("line.separator") +
		"            http://www.dlib.vt.edu/projects/OAi/marcxml/container.xsd\">";
	static private String XmlTrailer = "</sequence>";

	static private boolean writeAscii = false;
	static private boolean writeHtml = false;
	static private boolean writeTapeFormat = false;
	static private boolean writeXml = false;
	static private boolean writeOams = false;
	
	static private void showUsage()
	{
		System.err.println("usage:  MarcFilter -o[achox] [ -i <inputFile>] [ -m <mapFile>]");
		System.err.println("	-o	output format:");
		System.err.println("		a==ascii, h==html, c==MARC communication format,");
		System.err.println("		o==OAMS, x=OAI MARC xml");
	}
	
	static private int parseCommandLine(String argv[])
	{
		for (int i=0; i<argv.length; i++)
		{
			if (argv[i].charAt(0) == '-')	// Flag.
			{
				switch(argv[i].charAt(1))
				{
				case 'o':	// Output format.
					for (int j=2; j<argv[i].length(); j++)
						switch (argv[i].charAt(j))
						{
						case 'a':
							writeAscii = true;
							break;
						case 'c':
							writeTapeFormat = true;
							break;
						case 'h':
							writeHtml = true;
							break;
						case 'o':
							writeOams = true;
							break;
						case 'x':
							writeXml = true;
							break;
						default:
							System.err.println("Undefined output format '" + argv[i].charAt(j) +
											   "' in argument #'" + i + ".  Ignoring.");
						}
					break;
				case 'i':
					docFile = argv[++i];
					break;
				case 'm':
					mapFile = argv[++i];
					break;
					
				default:
					showUsage();
					return( ReturnCodes.BAD_PARAMS );

				}
			}
		}
		return( ReturnCodes.OK );
	}
								 
    public static void main(String argv[]) throws Exception
    {
        int Err;
        Debug debug = new Debug();
        EntityMap testMap = new EntityMap(debug);
        
		if (parseCommandLine(argv) != ReturnCodes.OK)
			return;
		
		try {
			BufferedReader inFile = new BufferedReader(new FileReader(mapFile));
		    testMap.load(inFile);
		} catch (IOException e)
		{
			System.err.println("Cannot load entity table in '" + mapFile + "':  using default.");
		}
		
		BufferedReader in;
		if ( docFile == null )
			in = new BufferedReader( new InputStreamReader( System.in ));
		else
		{
		    try {
			in = new BufferedReader(new FileReader(docFile));
		    } catch (Exception e)
		    {
			System.err.println("Cannot open test file '" + docFile + "':  giving up.");
			return;
		    }
		}
		
		PrintWriter pw = new PrintWriter(System.out);
		BufferedWriter bw = new BufferedWriter(pw);
		int numDocs = 0;
		
		if (writeHtml)
		{
			System.out.println("<HTML>");
			System.out.println("<HEADER>");
			System.out.println("<TITLE>MARC Records from '" + docFile + "'</TITLE>");
			System.out.println("</HEADER>");
			System.out.println("<BODY BGCOLOR=WHITE>");
			System.out.println("<H2>MARC Records from '" + docFile + "'</H2>");
			System.out.println("<P>&nbsp;<P>");
		}
		if (writeXml)
		{
			System.out.println(XmlHeader);
		}

		try { while( true )
		{
			MarcDocument doc = new MarcDocument(5104, testMap, debug);
			if ( (Err = doc.setFromTapeFormat(in)) != ReturnCodes.OK)
			{
				System.out.println("Error (" + Err + ") on setFromTapeFormat().");
				continue;
			}
			
			numDocs++;
			if (writeAscii || writeHtml)
			{
				System.out.println("<P>&nbsp;<P>");
			}
			if (writeAscii)
			{
				if (writeHtml)
				{
					System.out.println("<H4>Record #" + numDocs + ":  MARIAN \"Long Description\" in ASCII:</H4>");
					System.out.println("<PRE>");
				}
				else
					System.out.println("Record #" + numDocs + ":  MARIAN \"Long Description\" in ASCII:");
					
				if ( (Err = doc.presentLong(DigInfObj.ASCII, bw)) != ReturnCodes.OK)
				{
					System.err.println("Error (" + Err + ") on presentLong(), doc #" + numDocs + ".");
				}
				bw.newLine();
				bw.flush();
				if (writeHtml)
				{
					System.out.println("</PRE>");
				}
			}
			
			if (writeHtml)
			{
				System.out.println("<P>&nbsp;<P>");
				System.out.println("<H4>Record #" + numDocs + ":  Long description in HTML:</H4>");
				if ( (Err = doc.presentLong(DigInfObj.HTML, bw)) != ReturnCodes.OK)
				{
					System.err.println("Error (" + Err + ") on presentAsHtml(), doc #" + numDocs + ".");
				}
				bw.newLine();
				bw.flush();
			}
			
			if (writeTapeFormat)
			{
				if (writeHtml)
				{
					System.out.println("<P>&nbsp;<P>");
					System.out.println("<H4>In Tape Format:</H4>");
					System.out.println("<PRE>");
				}
				if ( (Err = doc.presentAsTapeFormat(bw)) != ReturnCodes.OK)
				{
					System.err.println("Error (" + Err + ") on presentAsXml(), doc #" + numDocs + ".");
				}
				if (writeHtml)
				{
					System.out.println("</PRE>");
					bw.newLine();
					bw.newLine();
				}
				bw.flush();
			}

			if (writeXml)
			{
				if (writeHtml)
				{
					System.out.println("<P>&nbsp;<P>");
					System.out.println("<H4>In OAI XML:</H4>");
					System.out.println("<PRE>");
				}
				if ( (Err = doc.presentAsXml(bw)) != ReturnCodes.OK)
				{
					System.err.println("Error (" + Err + ") on presentAsXml(), doc #" + numDocs + ".");
				}
				if (writeHtml)
				{
					System.out.println("</PRE>");
					bw.newLine();
					bw.newLine();
				}
				bw.flush();
			}

			if (writeOams)
			{
				if (writeHtml)
				{
					System.out.println("<P>&nbsp;<P>");
					System.out.println("<H4>In OAMS:</H4>");
					System.out.println("<PRE>");
				}
				if ( (Err = doc.presentShort(DigInfObj.XML, bw)) != ReturnCodes.OK)
				{
					System.err.println("Error (" + Err + ") on presentAsXml(), doc #" + numDocs + ".");
				}
				if (writeHtml)
				{
					System.out.println("</PRE>");
					bw.newLine();
					bw.newLine();
				}
				bw.flush();
			}
		} } catch (EOFException e)
		{
		}
		
		if (writeHtml)
		{
			System.out.println("</BODY>");
			System.out.println("</HTML>");
		}
		if (writeXml)
		{
			System.out.println(XmlTrailer);
		}

		System.err.println("End of File.");
 
    }
}

