// file: $isip/class/mmedia/xt_03.cc
// version: $Id: xt_04.cc 9929 2004-12-31 02:42:05Z duncan $

#include "XMLToken.h"

// method: toXML
//
// arguments: none
//
// return: a String containing an XML representation of this token
//
String XMLToken::toXML() const {
  
  // declare a string to store the output
  //
  String xml_output;
  
  // add the appropriate trimmings for an XML tag,
  // depending on the tag's type
  // note that concatAttributesAsXML adds an attribute
  // /value pair as attribute='value' or attribute="value",
  // as appropriate
  //
  if(type_d == START_TAG) {
    xml_output.concat(L"<");
    xml_output.concat(value_d);
    concatAttributesAsXML(xml_output);
    xml_output.concat(L">");
  }
  else if (type_d == END_TAG) {
    xml_output.concat(L"</");
    xml_output.concat(value_d);
    xml_output.concat(L">");
  }
  else if (type_d == START_AND_END_TAG) {
    xml_output.concat(L"<");
    xml_output.concat(value_d);
    concatAttributesAsXML(xml_output);
    xml_output.concat(L"/>");
  }
  else if (type_d == CDATA) {
    xml_output.concat(value_d);
  }
  
  // return the string containing the token in
  // XML format
  //
  return xml_output;
}

// method: addQuotes
//
// arguments:
//   String attrib:(input) a string containing an attribute value
//
// return: a string containing an attribute wrapped in XML-safe quotes.
//
// This method checks an attribute for the presence of
// single or double quotes. If it contains single quotes,
// then double quotes are prepended and concatenated to the string.
// Otherwise, double quotes are used.
// It returns a string containing the attribute wrapped in the
// appropriate quote type.
//
String XMLToken::addQuotes(String attrib_a) const {
  
  // store the character we're looking for as the delimiter
  //
  String delim(L"'");
  
  // declare a string to store the output
  //
  String output;
  
  // declare a long to use as the argument to the firstChr method
  // and set it to zero, so the search begins from the start of the
  // string
  //
  long pos = 0;
  
  // if the attribute contains no single quotes, use single quotes
  //
  if(attrib_a.firstChr(delim, pos) == Integral::NO_POS) {
    output = L"'";
    output.concat(attrib_a);
    output.concat(L"'");  
  }
  // otherwise, use double quotes
  //
  else {
    output = L"\"";
    output.concat(attrib_a);
    output.concat(L"\"");
  }
  
  // return the attribute value
  //
  return output;
}

// method: concatAttributesAsXML
//
// arguments:
//   String& output: (input/output) the value of a token, to which all of
//             the token's attributes will be concatenated in
//             XML form
//
// returns: a boolean indicating status
//
boolean XMLToken::concatAttributesAsXML(String& output_a) const {
  
  // loop over all of the token's attributes
  //
  for(int i = 0; i<attributes_d.length(); i++) {
    
    // add a space to the string
    //
    output_a.concat(SysChar::SPACEC);
    
    // add the attribute value
    //
    output_a.concat(attributes_d(i).first());
    
    // insert an equals sign
    //
    output_a.concat(L"=");
    
    // add the value, with the appropriate quotes
    //
    output_a.concat(addQuotes(attributes_d(i).second()));
  }
  
  // indicate success
  //
  return true;
}
