package edu.vt.marian.Document;

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

import edu.vt.marian.common.*;


/**
    A subfield of a variable field in a USMARC record.  A subfield is
         composed of a single-character label and a data string.
    @author    Robert France
    @author    Jianxin(Jason) Zhao (jxzhao@csgrad.cs.vt.edu)
*/

public class MarcSubField
{
    /** field label.
    */
    private char label;

    /** field value.
    */
    private String data;

    /** just used for debugging
    */
    private Debug debug;
	
	/** Device for mapping ANSEL (and some ASCII) characters to XML entities.
	 */
	private EntityMap xmlMap;


    /**
        create a marc_subfield object from label and data 
        @param    label --- this will be the label of this object
        @param    data -- this will be the data of this object
        @param    debug -- used for debugging
    */      
    public MarcSubField(char lbl, String dataStr, EntityMap xMap, Debug dbg)
    {
        debug = dbg;
        label = lbl;
		xmlMap = xMap;
        data = dataStr;
    }


    /**
        return the label of this object
        @return    the id of this object as a char
    */      
    public char getLabel()
    {
        return( label );
    }


    /**
        return the data of this object
        @return    the data of this object as a string
    */      
    public String getData()
    {
        return( data );
    }

    public int present(int markupType, BufferedWriter out) throws IOException 
    {
         switch( markupType )
        {
         case DigInfObj.XML:
         case DigInfObj.SGML:
            out.write("<subfield label=\"" + label + "\">");
            xmlMap.mapStringToEntities(data, out);
            out.write("</subfield>");
            return( ReturnCodes.OK );

         case DigInfObj.HTML:	//**DEVEL:  Do we need a separate map for HTML
            xmlMap.mapStringToEntities(data, out);	//**   entities, or are
            return( ReturnCodes.OK );				//**   these standard enough?

         case DigInfObj.ANSEL:
            out.write(data);
            return( ReturnCodes.OK );

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

         case DigInfObj.ASCII:
            MarcRecord.anselToCanonicalAscii(data, out);
            return( ReturnCodes.OK );
		}
    }

}

