#include "xmloutput.hh"

XMLOutput::XMLOutput () 
{
  std::cout << "xmloutput has been created" << std::endl;
}

XMLOutput::~XMLOutput() 
{
  if (xmlfile != NULL) {
    xmlfile.close();
  }
}

void XMLOutput::openFile(std::string name) 
{
  xmlfile.open(name.c_str());
  //    xmlfile.open("testxmlfile.xml");
}

void XMLOutput::tagout (const std::string tag, const std::string attribute)
{
  //  verify_tag(tag);
  xmlfile << "<" << tag;
  if (attribute.size() > 0)
    xmlfile << " " << attribute;
  xmlfile << ">";
}

void XMLOutput::spaces() 
{
  for (u_int i = 0; i < tag_stack.size(); i++) {
    xmlfile << "  ";
  }
}

void XMLOutput::push(const std::string tag, const std::string attribute) 
{
  spaces();
  tag_stack.push(tag);
  tagout(tag, attribute);
  xmlfile << '\n';
}

void XMLOutput::pop()
{
  //    assert(tag_stack.size()>0);
  std::string tag = tag_stack.top();
  tag_stack.pop();
  spaces();
  tagout("/"+tag,"");
  xmlfile << '\n';
}

void XMLOutput::xmloutputstring(std::string tag, std::string value, std::string attribute) 
{
  spaces();
  if (value.size() == 0) {
    tagout(tag, attribute+"/");
  } else {
    tagout(tag, attribute);
    xmlfile << value;
    tagout("/"+tag, "");
  }
  xmlfile << "\n";
}

void XMLOutput::print (std::string str) {
  xmlfile << str;
}
