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

#if defined(HAVE_EXPAT)

// ISIP include files
//
#include "XMLParser.h"

// method: diagnose
//
// arguments:
//  Integral::DEBUG level: (input) debug level for diagnostics
//
// return: a boolean value indicating status
//
boolean XMLParser::diagnose(Integral::DEBUG level_a) {
  
  //---------------------------------------------------------------------------
  //
  // 0. preliminaries
  //
  //---------------------------------------------------------------------------
  
  // output the class name
  //
  if (level_a > Integral::NONE) {
    SysString output(L"diagnosing class ");
    output.concat(CLASS_NAME);
    output.concat(L": ");
    Console::put(output);
    Console::increaseIndention();
  }
  
  //--------------------------------------------------------------------------
  //
  // 1. required public methods
  //
  //--------------------------------------------------------------------------
  
  // set indention
  //
  if (level_a > Integral::NONE) {
    Console::put(L"testing required public methods...\n");
    Console::increaseIndention();
  }
  
  // test debug methods
  //
  XMLParser xml_parser;
  xml_parser.setDebug(level_a);
  if(xml_parser.getDebug() == level_a) {
    if(level_a > Integral::BRIEF) {
      Console::put(L"testing debug method...");
      xml_parser.debug(L"debug");
    }
  }
  else {
    return Error::handle(name(), L"setDebug", Error::TEST, __FILE__, __LINE__);
  }
  
  // reset indention
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  //--------------------------------------------------------------------------
  //
  // 2. class-specific public methods:
  //    parsing methods
  //
  //--------------------------------------------------------------------------
  
  
  // set indention
  //
  if (level_a > Integral::NONE) {
    Console::put(L"testing class-specific public methods: parsing methods...\n");
    Console::increaseIndention();
  }
  
  // make a string of xml to parse
  //
  String xml = L"<xml_tag> xml content </xml_tag>";
  
  // parse the xml string
  //
  xml_parser.parseXML(xml);
  
  // declare a token vector and a token to use for testing
  //
  Vector<XMLToken> token_vector;
  XMLToken token;
  
  // create and add to the token vector tokens identical to
  // those in the xml format string created above
  //
  token.init(XMLToken::START_TAG, L"xml_tag");
  token.setDepth(1);
  token_vector.concat(token);
  
  token.init(XMLToken::CDATA, L"xml");
  token.setDepth(1);
  token_vector.concat(token);
  
  token.init(XMLToken::CDATA, L"content");
  token.setDepth(1);
  token_vector.concat(token);
  
  token.init(XMLToken::END_TAG, L"xml_tag");
  token.setDepth(0);
  token_vector.concat(token);
  
  // compare the token vectors, they should be equal
  //
  if(!xml_parser.getTokenVector().eq(token_vector)) {
    
    debugTokenDepths(xml_parser.getTokenVector());
    debugTokenDepths(token_vector);
    
    // error
    //
    return Error::handle(name(), L"parseXML", Error::TEST, __FILE__, __LINE__);
    
  }
  
  // clear the parser, and set the manually generated token vector
  // as the parser's internal token vector
  //
  xml_parser.clear(Integral::RESET);
  xml_parser.setDebug(level_a);
  xml_parser.setTokenVector(token_vector);
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing get/setTokenVector methods...");
  }
  if(!xml_parser.getTokenVector().eq(token_vector)) {
    
    // error
    //
    return Error::handle(name(), L"get/setTokenVector", Error::TEST, __FILE__, __LINE__);
    
  }
  
  // add some unhandled stuff to the token vector
  //
  token.init(XMLToken::START_TAG, L"bad_tag");
  xml_parser.addToken(token);
  token.init(XMLToken::START_TAG, L"cdata between bad tags");
  xml_parser.addToken(token);
  token.init(XMLToken::START_TAG, L"bad_tag");
  xml_parser.addToken(token);
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing addToken method...");
  }
  if(xml_parser.getTokenVector().eq(token_vector)) {
    
    // error
    //
    return Error::handle(name(), L"addToken", Error::TEST, __FILE__, __LINE__);
  }
  
  Vector<String> valid_values;
  valid_values.concat(L"xml_tag");
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing setValidTokenValues method...");
  }
  if(!xml_parser.setValidTokenValues(valid_values)) {
    
    // error
    //
    return Error::handle(name(), L"setValidTokenValues", Error::TEST, __FILE__, __LINE__);
    
  }
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing markUnhandledToken method...");
  }
  if(!xml_parser.markUnhandledTokens()) {
    
    // error
    //
    return Error::handle(name(), L"markUnhandledTokens", Error::TEST, __FILE__, __LINE__);
  }
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing removeUnhandledTokens method...");
  }
  if(!xml_parser.removeUnhandledTokens()) {
    
    // error
    //
    return Error::handle(name(), L"removeUnhandledTokens", Error::TEST, __FILE__, __LINE__);
  }
  
  if(!xml_parser.getTokenVector().eq(token_vector)) {
    
    // error
    //
    return Error::handle(name(), L"removeUnhandledTokens", Error::TEST, __FILE__, __LINE__);    
  }
  
  // reset indention
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  //--------------------------------------------------------------------------
  //
  // 3. Clean up.
  //
  //--------------------------------------------------------------------------    
  // reset indention
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  if (level_a > Integral::NONE) {
    SysString output(L"diagnostics passed for class ");
    output.concat(name());
    output.concat(L"\n");
    Console::put(output);
  }
  
  // indicate success
  //
  return true;
}

#endif
