package edu.vt.marian.Document;

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

import edu.vt.marian.common.*;


/**
    A "variable field" in a US MARC record that contains a title:
	that is, a 130, 24x, 440, 730, 740, 830, or 840 field.
    @author	Robert France

*/

public class MarcTitleField extends PresentableMarcVarField
{
    /** Labels for subfields reported in a long presentation (NOT USED: 
	instead, all subfields are currently reported in a long presentation).
    */
    private final static String title_labels = "abcdfklnpst";


    /**
        Create a MarcTitleField object with no subfields.
        @param	id -- the variable field id.
        @param	xMap -- used to convert ANSEL to (e.g.) XML.
        @param	debug -- used for debugging
    */      
    public MarcTitleField(int id, EntityMap xMap, Debug debug)
    {
        super(id, xMap, debug);
        isTitle = true;
     }


    /**
        "Long" presentation for a title variable field:  use all subfields
            with slash 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:	// Use default.
         case DigInfObj.SGML:
	        return( super.presentLong(markupType, out) );

        case DigInfObj.HTML:
            // Presentation:  make each 'a' or 'b' subfield (main title or
            //  subtitle) a separate hot link with itself as (approximate)
            //  query text.
            try { while ( true )
            {
                MarcSubField sf = (MarcSubField) subfld.nextElement();
                if ( (sf.getLabel() == 'a') || (sf.getLabel() == 'b') )
                {
                    // Build URL.
                    StringWriter sw = new StringWriter();
                    BufferedWriter bsw = new BufferedWriter( sw );
                    if ( (Err = sf.present(DigInfObj.ANSEL, bsw))
                              != ReturnCodes.OK )
                        return( Err );
			        bsw.flush();
                    String queryString = "text1_type=Words in Title/text1=" +
                                            sw.toString();
                    presentUrl(queryString, out);
                    if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
						return( Err );
                    out.write("</A>");
                }
                else
                {
                    if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
						return( Err );
                }
                if ( subfld.hasMoreElements() )
                   out.write(" / ");
            } } catch( NoSuchElementException e) {};
            return( ReturnCodes.OK );
 
        default:
            debug.dumpTrace("MarcTitleField.presentLong(): Unexpected markup type " +
                            markupType + ":  treating as ASCII.");
            // Fall through:

        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 );
        }
    }

}

