/*******************************************************************\

              Copyright (C) 2002, 2003 Joseph Coffland

    This program is free software; you can redistribute it and/or
     modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
        of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
             GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
     along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
                           02111-1307, USA.

            For information regarding this software email
                   jcofflan@users.sourceforge.net

\*******************************************************************/

#include "DOMHelpers.h"

#include "XercesStr.h"

#include <BasicUtils/BasicException.h>

using namespace std;

#include <xercesc/dom/DOM.hpp>
XERCES_CPP_NAMESPACE_USE;


string getDOMNodeText(DOMNode *node) {
  if (!node) return "";

  DOMNodeList *children = node->getChildNodes();

  switch (node->getNodeType()) {
  case DOMNode::ELEMENT_NODE:
  case DOMNode::ENTITY_NODE:
  case DOMNode::ENTITY_REFERENCE_NODE:
  case DOMNode::DOCUMENT_FRAGMENT_NODE: 
    {
      string text;

      for (unsigned int i = 0; i < children->getLength(); i++) {
	DOMNode *child = children->item(i);
	switch (child->getNodeType()) {
	case DOMNode::COMMENT_NODE:
	case DOMNode::PROCESSING_INSTRUCTION_NODE:
	  break;
	  
	default: text += getDOMNodeText(child);
	}
      }
      
      return text;
    }
    break;

  case DOMNode::ATTRIBUTE_NODE:
  case DOMNode::TEXT_NODE:
  case DOMNode::CDATA_SECTION_NODE:
  case DOMNode::COMMENT_NODE:
  case DOMNode::PROCESSING_INSTRUCTION_NODE:
    return XercesStr(node->getNodeValue()).localForm();
    break;

  default:
    return "";
  }
}

#include <iostream>

bool getBoolValue(const string str) {
  if (str == "" || str == "false" || str == "0") return false;
  return true;
}

const string DOMErrorCode2String(int code) {
  switch (code) {
  case DOMException::INDEX_SIZE_ERR: return "INDEX_SIZE_ERR";
  case DOMException::DOMSTRING_SIZE_ERR: return "DOMSTRING_SIZE_ERR";
  case DOMException::HIERARCHY_REQUEST_ERR: return "HIERARCHY_REQUEST_ERR";
  case DOMException::WRONG_DOCUMENT_ERR: return "WRONG_DOCUMENT_ERR";
  case DOMException::INVALID_CHARACTER_ERR: return "INVALID_CHARACTER_ERR";
  case DOMException::NO_DATA_ALLOWED_ERR: return "NO_DATA_ALLOWED_ERR";
  case DOMException::NO_MODIFICATION_ALLOWED_ERR: return "NO_MODIFICATION_ALLOWED_ERR";
  case DOMException::NOT_FOUND_ERR: return "NOT_FOUND_ERR";
  case DOMException::NOT_SUPPORTED_ERR: return "NOT_SUPPORTED_ERR";
  case DOMException::INUSE_ATTRIBUTE_ERR: return "INUSE_ATTRIBUTE_ERR";
  case DOMException::INVALID_STATE_ERR: return "INVALID_STATE_ERR";
  case DOMException::SYNTAX_ERR: return "SYNTAX_ERR";
  case DOMException::INVALID_MODIFICATION_ERR: return "INVALID_MODIFICATION_ERR";
  case DOMException::NAMESPACE_ERR: return "NAMESPACE_ERR";
  case DOMException::INVALID_ACCESS_ERR: return "INVALID_ACCESS_ERR";
  case DOMException::VALIDATION_ERR: return "VALIDATION_ERR";
  default:
    return "Unknown DOM Error code";
  }
}

ostream &operator<<(ostream &stream, const DOMException &e) {  
  stream << DOMErrorCode2String(e.code).c_str();
  if (e.msg)
    stream << " " << XercesStr(e.msg).localForm();

  return stream;
}
