// file: $isip/class/mmedia/XMLParser/xp_05.cc
// version: $Id: xp_05.cc 10284 2005-10-27 20:52:14Z wholland $

#if defined(HAVE_EXPAT)

#include "XMLParser.h"

// method: getIndent
//
// arguments:
//   long depth: (input) the depth of an XML token relative to the
//            left margin
//
// return: a String containing 2 spaces for every unit of depth
//
// gets the proper amount of white space to pad before
// an XMLToken for the indention to be right when printing
// the token in XML format
//
const String XMLParser::getIndent(long depth_a) {
  
  // declare a string to store space to be output
  // in front of xml tokens
  //
  String pad;
  
  // if tag type = start and depth = 0, add a new line.
  //
  for(int i = 0; i < depth_a; i++) {
    pad.concat(L"  ");
  }
  
  // return the white space
  //
  return pad;
}

// method: toXML
//
// arguments: none
//
// return: a String containing the XML rule
//
// converts the the member vector of xml tokens
// into an xml format string
// 
String XMLParser::toXML() {
  
  // return the parser's contents
  // in XML format (call the master
  // method)
  //
  return toXML(getTokenVector());
}

// method: toXML
//
// arguments: none
//
// return: a String containing the XML rule
//
// converts a vector of xml tokens into an xml
// format string
// 
const String XMLParser::toXML(Vector<XMLToken>& token_vector_a) {
  
  // declare a string to store the output
  //
  String xml_output;
  
  // declare a variable to store the depth of the tokens
  //
  long depth = 0;
  
  // loop over all tokens in this rule's expansion
  //
  for(long i = 0; i < token_vector_a.length(); i++) {
    
    // if the current token is a start tag, indention increases
    //
    if(token_vector_a(i).getType() == XMLToken::START_TAG) {
      
      xml_output.concat(getIndent(depth));
      xml_output.concat(token_vector_a(i).toXML());
      xml_output.concat(SysChar::NEWLINE);
      depth++;
      
    }
    // if current token is an end tag, indention decreases
    //
    else if(token_vector_a(i).getType() == XMLToken::END_TAG) {
      
      depth--;
      xml_output.concat(getIndent(depth));
      xml_output.concat(token_vector_a(i).toXML());
      xml_output.concat(SysChar::NEWLINE);
      
    }
    // if current token is both start and end tag (as in  <tag/>),
    // indention remains constant
    //
    else if(token_vector_a(i).getType() == XMLToken::START_AND_END_TAG) {
      
      xml_output.concat(getIndent(depth));
      xml_output.concat(token_vector_a(i).toXML());
      xml_output.concat(SysChar::NEWLINE);
      
    }
    // if current token is CDATA, or UNHANDLED
    // indention remains constant
    //
    else if(token_vector_a(i).getType() == XMLToken::CDATA ||
	    token_vector_a(i).getType() == XMLToken::UNHANDLED) {
      
      xml_output.concat(getIndent(depth));
      xml_output.concat(token_vector_a(i).toXML());
      xml_output.concat(SysChar::NEWLINE);
      
    }
  }
  
  // return the XML format string
  //
  return xml_output;
}

#endif
