#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/framework/LocalFileInputSource.hpp>
#ifdef XERCES_HAS_CPP_NAMESPACE
XERCES_CPP_NAMESPACE_USE
#endif

#include <BasicUtils/BasicException.h>
#include <BasicUtils/BasicSmartPointer.h>

#include <XercesUtils/BasicErrorHandler.h>
#include <XercesUtils/XMLCompare.h>
#include <XercesUtils/XercesStr.h>

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>

using namespace std;


void Syntax(const char *name) {
  cerr << "Syntax: " << name << " <file1> <file2>" << endl;
  exit(1);
}


int main(int argc, char *argv[]) {
  if (argc < 3) Syntax(argv[0]);

  try {
    XMLPlatformUtils::Initialize();

    XercesStr filename1(argv[1]);
    XercesStr filename2(argv[2]);
    
    LocalFileInputSource input1(filename1.genericForm());
    LocalFileInputSource input2(filename2.genericForm());
    
    XercesDOMParser parser;
    BasicErrorHandler errorHandler;
    parser.setErrorHandler(&errorHandler);
    parser.setDoNamespaces(true);
    
    parser.parse(input1);
    BasicSmartPointer<DOMDocument> doc1 = parser.adoptDocument();
    parser.parse(input2);
    BasicSmartPointer<DOMDocument> doc2 = parser.adoptDocument();

    if (xmlCompare(doc1.get(), doc2.get())) {
      cout << "XML files match!" << endl;
      return 0;

    } else {
      cout << "XML files DON'T match!" << endl;
      return 1;
    }
  } catch (BasicException &e) {
    cerr << e << endl;
    return 2;
  } catch (...) {
    cerr << "Unknown exception!" << endl;
    return 2;
  }

  return 0;
}
