package edu.vt.marian.Document;

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

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

import gnu.regexp.*;

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

import org.xml.sax.Parser;
import org.xml.sax.DocumentHandler;
import org.xml.sax.helpers.ParserFactory;
import edu.vt.marian.search.Wrapper.*;
import org.xml.sax.InputSource;



/**
 * A set of data encapsulated in a SOIF
 */
public class OAMSDocument implements Document
{

    /** this string contains all the information of this document
		the format of the string is in SGML
	*/
	private String OAMSString = null;

	 /**
     * Property list for the OAMS
     */
	//private Hashtable attributeValues;
    
	static final String parserClass = "com.jclark.xml.sax.Driver";
	
    /**
     * Allows us to pull things out of the property list in the order they
     * were inserted.
     */
	
	public String title; 
    public String fullID;
    public String author; 
    public String organization; 
    public String Abstract; 
    public String subject; 
	public String displayID;

	
	private Vector keyList;

    /**
     * Common delimitter that separates SOIF attributes and values
     */
	static final String soifDelimitter = ":: ";

    
	
	Debug debug;
	
    /**
        Build a new SOIF object
    */
	
	
	
	public OAMSDocument(Debug debug) {
		this.debug = debug;
		//attributeValues = new Hashtable();
		keyList = new Vector();
	}

  
	
	
	
  /**
   * Build a new SOIF object from an input stream
   * @param soifInput input stream from which the SOIF should be built.
   */
	public OAMSDocument(String OAMSInput, Debug debug) {
		this(debug);

		OAMSString = OAMSInput;
		System.out.println(OAMSString);
		ParseOAMSBody();
	}

  
  /**
   * Parse the body of the SOIF extracting the attribute/value pairs
   * @param body the body to parse
   */
	public void ParseOAMSBody() {
	
	 
			try{
				  
				  
				  
				  //InputStreamReader in = new InputStreamReader(new DataInputStream(new ByteArrayInputStream(OAMSString.getBytes())));
				  StringReader in = new StringReader(OAMSString);
				  Parser parser = ParserFactory.makeParser(parserClass);
				  DocumentHandler handler = new OAiHandler();
				  parser.setDocumentHandler(handler);
				  parser.parse(new InputSource(in));
				  author = OAiHandler.author();
				  title = OAiHandler.title();
				  subject = OAiHandler.subject();
				  displayID = OAiHandler.displayID();
				  Abstract = OAiHandler.Abstract();
				} catch (Exception e) {};	
			
 
	}




 /**
        Return a short description 
            (probably only one sentence) of the document this represents.
        @param    markupType -- specifies the charater set type need to be returned
        @return    the short description of this object as a string
    */      
    public String presentShort(int markupType)
    {
        StringWriter sw = new StringWriter();
        BufferedWriter out = new BufferedWriter( sw );
        int Err;
        try {
            if ( (Err = presentShort(markupType, out)) != ReturnCodes.OK )
                return( null );
			out.flush();
       } catch( Exception e ) { return( null ); }
        return(sw.toString());
    }

   public int presentShort(int markupType, BufferedWriter out) throws IOException
    {
        
        int Err;
		String AttributeValue,AUTHOR, TITLE;
		String shortPresentation;
		shortPresentation = "";
		AUTHOR= null;
		TITLE = null;
		switch ( markupType )
        {
         default:
            // only ASCII and XML are supported now.  ASCII produces the sort
            //  of single-line description used in results lists.  XML produces
            //  an OAMS description of the document.
            debug.dumpTrace("MarcDocument.presentShort(): unsupported markup type:  using ASCII.");
            //  FALL THROUGH:
            
         case DigInfObj.ASCII:
            boolean seenAuthor = false;
        
                if(author != null)
				{
					seenAuthor = true;
					shortPresentation = shortPresentation +  author;
				}	
				//Main title	
                if(title!=null)
				 { 
				   	if (seenAuthor)
                    {
                        // add separator
						shortPresentation = shortPresentation + ", ";
					}
				       shortPresentation = shortPresentation +  title;
                    
					
            	}
				                
        
		     shortPresentation = shortPresentation + "\n";
			 out.write(shortPresentation);
        
        } // end -- switch
        
        return( ReturnCodes.OK );
    }
		
   public String presentLong(int markupType)
    {
        StringWriter sw = new StringWriter();
        BufferedWriter out = new BufferedWriter( sw );
         int Err;
        try {
            if ( (Err = presentLong(markupType, out)) != ReturnCodes.OK )
                return( null );
			out.flush();
        } catch( Exception e ) { return( null ); }
        return(sw.toString());
    }


    public int presentLong(int markupType, BufferedWriter out) throws IOException
    {
        String lineBreak="";;
        String paraBreak;
        int Err;
		String AttributeValue;
		String  presentLong = null;
		
		
		
        switch ( markupType )
        {
         case DigInfObj.HTML:
			lineBreak = new String("<BR>\n");
            paraBreak = new String("<P>\n");
			//System.out.println("ENTREI AQUI");
			
           // out.write("<B>Call Number:  </B>");
            break;

         default:
                        
			debug.dumpTrace("MarcDocument.presentLong(): unsupported markup type:  using ASCII.");
            //  FALL THROUGH.
          break;
         case DigInfObj.ASCII: 
         case DigInfObj.ANSEL:
            lineBreak = System.getProperty("line.separator");
            paraBreak = new String(System.getProperty("line.separator") +
                                   System.getProperty("line.separator"));
           
            break;
        }
		
       // out.write(fullID);
        //out.write(paraBreak);
		//out.write(lineBreak);
		if(title != null){
     		out.write(title);
			out.write(lineBreak);
			out.write(lineBreak);
			
          }
		if (author !=null)
		{
		out.write(author);	
		out.write(lineBreak);
		out.write(lineBreak);
        }
		if (Abstract !=null)
		{
		out.write(Abstract);	
		out.write(lineBreak);
		out.write(lineBreak);
        }
		if (displayID !=null)
		{
		out.write("<A HREF=\"" + displayID + "\">" + displayID + "</A>");	
		out.write(lineBreak);
		out.write(lineBreak);
        }
		
		//this.presentShort(markupType, out);
		
		//presentLong= OAMSString;	
		//out.write(presentLong);
		return( ReturnCodes.OK );
    }

public boolean isValid()
	{
		return( true );
	}
	
	public DigInfObj copy()
	{
	 return null;
	}
	
 public String presentFull(int markupType)
    {
        StringWriter sw = new StringWriter();
        BufferedWriter out = new BufferedWriter( sw );
        int Err;
        try {
            if ( (Err = presentFull(markupType, out)) != ReturnCodes.OK )
                return( null );
			out.flush();
        } catch( Exception e ) { return( null ); }
        return(sw.toString());
    }  
 
 public Vector attributes()
	{
	return null;
	}
 
 public Vector attributes(int markupType)
 {
return null;
 }
 
 public Vector presentAttributes(int markupType)
	{
	return null;	
	}
 
 public Object presentAttribute(int attrID, int markupType)
	{
		return null;
	}

 public int presentFull(int markupType, BufferedWriter out) throws IOException
	{
		return( ReturnCodes.NOT_YET_IMPLEMENTED );
	}
   
}