package edu.vt.marian.Document;

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

import edu.vt.marian.common.*;


/**
    An enhanced version of a "variable field" in a US MARC record, capable
	of supporting flexible presentation to a user.
    @author	Robert France
*/

public class PresentableMarcVarField extends MarcVarField
{
    protected boolean isTitle = false;	    // These are all false for undifferentiated
    protected boolean isSubject = false;	//  PresentableMarcVarField objects.  Objects
    protected boolean isNote = false;	    //  of the semantic subclasses MarcTitleField,
    protected boolean isPersName = false;	//  MarcPersNameField, etc. set one each to
    protected boolean isCorpName = false;	//  true.  **DEVEL:  Can they be static here
    protected boolean isConfName = false;	//**  and yet overridden in the subclasses?
    protected boolean isImprint = false;

	
    /** These next two items are actually used only by the subclasses.	*/

    /** Prefix (notably including substitution points for WebGate) used in
        constructing URLs implementing "Hot Links" in presented records as
        new searches.
    */
    protected static String urlPrefix = "<a href=\"@webgate:calling_program@/submit=query/@webgate:session@/@webgate:new_query_number@/";

    /**
     *       Certain chars (the space, for example), cannot appear in URLs.
     *       Instead, we use the hex value for that particular character
     *       to display it in a URL.
     *       <P>
     *       e.g. "Program Files" becomes "Program%20Files" for a URL.
     @author	Jon Pryor (translated from C++ by RKF)
     */
    protected void presentUrl(String urlBody, BufferedWriter out) throws IOException
    {
        int i;       // Index for urlBodyBuf.
        
        out.write(urlPrefix);	// ASSUMES no problem characters in urlPrefix.
        for (i=0; i<urlBody.length(); i++)
        {
            if ( urlBody.charAt(i) == ' ' )     // space char
                out.write("%20");
            else
                out.write(urlBody.charAt(i));
        }
        out.write("\">");
    }



    /**
        Create a PresentableMarcVarField object with no subfields from a field ID
            and a pair of indicator characters.  Used when creating
            a PresentableMarcVarField from an XML or Atomic format record, e.g.
        @param	id --- this will be the id of this object
        @param	data -- this string contains the indicators and all the
                subfields.  The format is specified in US MARC.
        @param	debug -- used for debugging
    */      
    public PresentableMarcVarField(int id, EntityMap xMap, Debug debug)
    {
        super(id, xMap, debug);
    }


    /**
        Default "short" presentation for any variable field:  just use the 'a' subfield.
        @param	markupType    see edu.vt.marian.common.DigInfObj
        @param	out	A BufferedWriter (presumably String or OutputStream) to present on.
        @return    OK -- everything jake.
        <BR>    IO_ERROR or PARSE_ERROR -- problems.
    */      
    public int presentShort(int markupType, BufferedWriter out) throws IOException
    {
		int Err;
        switch ( markupType )
        {
         default:
            debug.dumpTrace("MarcVarField.presentShort(): Unexpected markup type " +
                            markupType + ":  treating as ASCII.");
            // Fall through:

         case DigInfObj.XML:
         case DigInfObj.SGML:
         case DigInfObj.HTML:
         case DigInfObj.ASCII:
         case DigInfObj.ANSEL:
            Enumeration subfld = subfields.elements();
            try { while ( true )
            {
                MarcSubField sf = (MarcSubField) subfld.nextElement();
                if ( sf.getLabel() == 'a' )
                   if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
					   return( Err );
                if ( subfld.hasMoreElements() )
                   out.write(' ');
            } } catch( NoSuchElementException e) {};
        }
     return( ReturnCodes.OK );
    }


    /**
        Default "long" presentation for any variable field:  use all subfields
            with a minimal separator.
        @param	markupType    see edu.vt.marian.common.DigInfObj
        @param	out	A BufferedWriter (presumably String or OutputStream) to present on.
        @return    OK -- everything jake.
        <BR>    IO_ERROR or PARSE_ERROR -- problems.
    */      
    public int presentLong(int markupType, BufferedWriter out) throws IOException
    {
		int Err;
        Enumeration subfld = subfields.elements();
        switch ( markupType )
        {
         case DigInfObj.XML:	// treat as FULL until we figure out why you
         case DigInfObj.SGML:	//  would present LONG in XML.
	        out.write("<varfield id=" + id + " i1='" + indicator1 + 
                      "' i2='" + indicator2 + "'>");
            try { while ( true )
            {
                MarcSubField sf = (MarcSubField) subfld.nextElement();
                if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                    return( Err );
                out.newLine();
            } } catch( NoSuchElementException e) {};
            out.write("</varfield>");
            out.newLine();
            return( ReturnCodes.OK );

        default:
            debug.dumpTrace("MarcVarField.presentLong(): Unexpected markup type " +
                            markupType + ":  treating as ASCII.");
            // Fall through:

         case DigInfObj.HTML:
         case DigInfObj.ASCII:
         case DigInfObj.ANSEL:
            try { while ( true )
            {
                MarcSubField sf = (MarcSubField) subfld.nextElement();
                if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
				    return( Err );
                if ( subfld.hasMoreElements() )
                   out.write(' ');
            } } catch( NoSuchElementException e) {};
            return( ReturnCodes.OK );
        }
    }

}

