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 imprint data:
	that is, a 260 field.
    @author	Robert France

*/

public class Marc856Field 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 MarcImprintField 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 Marc856Field(int id, EntityMap xMap, Debug debug)
    {
        super(id, xMap, debug);
    }


    /**
        "Short" presentation for a corporate name variable field.
        @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("Marc856Field.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:
            // Go for the URL only.  Should all be ASCII characters anyway.
            Enumeration subfld = subfields.elements();
            try { while ( true )
            {
                MarcSubField sf = (MarcSubField) subfld.nextElement();
                if ( sf.getLabel() == 'u' )
                {
                    if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                        return( Err );
                    break;
                }
            } } catch( NoSuchElementException e) {};
        }
     return( ReturnCodes.OK );
    }


    /**
        "Long" presentation for a conference name variable field.
        @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 
    {
        Enumeration subfld = subfields.elements();
		boolean closeAnchor;
		int Err;
        switch ( markupType )
        {
         case DigInfObj.XML:	// Use default.
         case DigInfObj.SGML:
	        return( super.presentLong(markupType, out) );

        case DigInfObj.HTML:
            try { while ( true )
            {
                MarcSubField sf = (MarcSubField) subfld.nextElement();
				closeAnchor = false;
				switch ( sf.getLabel() )
				{
				case 'a':
					out.write("<I>HostName: </I>");
					break;
				case 'b':
					out.write("<I>AccessNumber: </I>");
					break;
				case 'c':
					out.write("<I>Compression:&nbsp;&nbsp;</I>");
					break;
				case 'd':
					out.write("<I>Path:&nbsp;&nbsp;</I>");
					break;
				case 'f':
					out.write("<I>FileName:&nbsp;&nbsp;</I>");
					break;
				case 'g':
					out.write("<I>URN:&nbsp;&nbsp;</I>");
					break;
				case 'h':
					out.write("<I>UserName:&nbsp;&nbsp;</I>");
					break;
				case 'i':
					out.write("<I>Instruction:&nbsp;&nbsp;</I>");
					break;
				case 'p':
					out.write("<I>Port:&nbsp;&nbsp;</I>");
					break;
				case 'q':
					out.write("<I>Format:&nbsp;&nbsp;</I>");
					break;
				case 's':
					out.write("<I>FileSize:&nbsp;&nbsp;</I>");
					break;
				case 'u':
                    // Build URL.
					out.write("<I>URL:&nbsp;&nbsp;</I><A HREF=\"");
                    if ( (Err = sf.present(DigInfObj.ASCII, out)) != ReturnCodes.OK )
                        return( Err );
					out.write("\">");
					closeAnchor = true;
					break;
				 default:
					out.write("<I>|" + sf.getLabel() + "|:&nbsp;&nbsp; </I>");
				}
                if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                    return( Err );
				if ( closeAnchor )
					out.write("</A>");
                if ( subfld.hasMoreElements() )
                   out.write("&nbsp;&nbsp;&nbsp;");
            } } catch( NoSuchElementException e) {};
            return( ReturnCodes.OK );

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

		case DigInfObj.ASCII:	//**DEVEL: This is not right.  We should be at least
		case DigInfObj.ANSEL:	//**  as careful here as in presentShort().
            try { while ( true )
            {
                MarcSubField sf = (MarcSubField) subfld.nextElement();
				switch ( sf.getLabel() )
				{
				case 'a':
					out.write("HostName: ");
					break;
				case 'b':
					out.write("AccessNumber: ");
					break;
				case 'c':
					out.write("Compression: ");
					break;
				case 'd':
					out.write("Path: ");
					break;
				case 'f':
					out.write("FileName: ");
					break;
				case 'g':
					out.write("URN: ");
					break;
				case 'h':
					out.write("UserName: ");
					break;
				case 'i':
					out.write("Instruction: ");
					break;
				case 'p':
					out.write("Port: ");
					break;
				case 'q':
					out.write("Format: ");
					break;
				case 's':
					out.write("FileSize: ");
					break;
				case 'u':
					out.write("URL: ");
					break;
				 default:
					out.write("|" + sf.getLabel() + "|:");
				}
                if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                    return( Err );
                if ( subfld.hasMoreElements() )
                   out.write("  ");
            } } catch( NoSuchElementException e) {};
            return( ReturnCodes.OK );
        }
    }

}
