<commit_before>/****************** <VPR heading BEGIN do not edit this line> *****************
 *
 * VR Juggler Portable Runtime
 *
 * Original Authors:
 *   <NAME>, <NAME>, <NAME>, <NAME>
 *
 * -----------------------------------------------------------------
 * File:          $RCSfile$
 * Date modified: $Date$
 * Version:       $Revision$
 * -----------------------------------------------------------------
 *
 ****************** <VPR heading END do not edit this line> ******************/

/*************** <auto-copyright.pl BEGIN do not edit this line> **************
 *
 * VR Juggler is (C) Copyright 1998-2003 by Iowa State University
 *
 * Original Authors:
 *   <NAME>, <NAME>,
 *   <NAME>, <NAME>,
 *   <NAME>, <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 *************** <auto-copyright.pl END do not edit this line> ***************/

#include <vpr/IO/XMLObjectReader.h>

#include <vector>

#include <boost/static_assert.hpp>
#include <vpr/IO/ObjectReader.h>

#include <vpr/Util/Assert.h>
#include <vpr/Util/Debug.h>

#include <cppdom/cppdom.h>
#include <cppdom/version.h>
#include <string>
#include <sstream>
#include <iterator>

#define XML_OR_LEVEL vprDBG_HEX_LVL


namespace
{
 /** Helper to read the data from the current string. */
 template<class T>
 vpr::ReturnStatus readValueStringRep(T& val, std::stringstream* inStream)
 {
    vprASSERT(inStream != NULL);

    // Just to be safe do this again for the heck of it. :)
    //in_stream->exceptions( std::ios::eofbit | std::ios::failbit | std::ios::badbit );
    vprASSERT(!inStream->bad() && "Bad stream, BAAADDD stream.");
    vprASSERT(!inStream->fail() && "Stream failed.");
    vprASSERT(!inStream->eof() && "Stream EOF'd.");
    vprASSERT(inStream->good());

    //stream_content = (*in_stream).str();

    (*inStream) >> val;

    return vpr::ReturnStatus::Succeed;
 }

}

namespace vpr
{

XMLObjectReader::XMLObjectReader(std::vector<vpr::Uint8> data)
   : mCurSource(CdataSource)
{
   initCppDomTree(data);
   //mAttribSource.exceptions( std::ios::eofbit | std::ios::failbit | std::ios::badbit );
}

XMLObjectReader::XMLObjectReader(cppdom::NodePtr rootNode)
   : mCurSource(CdataSource)
{
   mRootNode = rootNode;
   //mAttribSource.exceptions( std::ios::eofbit | std::ios::failbit | std::ios::badbit );
}



XMLObjectReader::NodeState::NodeState(cppdom::Node* cur_node)
   : node(cur_node)
{
   nextChild_i = node->getChildren().begin();     // Initialize to the first child
   endChild_i = node->getChildren().end();
   //cdataSource.exceptions( std::ios::eofbit | std::ios::failbit | std::ios::badbit );
   cdataSource.str(node->getFullCdata());
}

XMLObjectReader::NodeState::NodeState(const NodeState& rhs)
{
   node = rhs.node;
   nextChild_i = rhs.nextChild_i;
   endChild_i = rhs.endChild_i;
   //cdataSource.exceptions( std::ios::eofbit | std::ios::failbit | std::ios::badbit );
   cdataSource.str(rhs.cdataSource.str());
}

XMLObjectReader::NodeState& XMLObjectReader::NodeState::operator=(const NodeState& rhs)
{
   node = rhs.node;
   nextChild_i = rhs.nextChild_i;
   endChild_i = rhs.endChild_i;
   cdataSource.str(rhs.cdataSource.str());
   return *this;
}

void XMLObjectReader::NodeState::debugDump(int debug_level)
{
   vprDEBUG(vprDBG_ALL, debug_level) << "Node:" << node->getName() << "  num children:" << node->getChildren().size()
                                     << "  distance: nextChild-->endChild:" << std::distance(nextChild_i,endChild_i) << std::endl << vprDEBUG_FLUSH;
}


/** Get the current string source we are reading from.
*/
std::stringstream* XMLObjectReader::getCurSource()
{
   std::stringstream* in_stream(NULL);

   if(AttribSource == mCurSource)
   {
      in_stream = &mAttribSource;
   }
   else
   {
      in_stream = &(mCurNodeStack.back().cdataSource);
   }

   return in_stream;
}

/**
 * Initialize the members based on a serialized version of something in the
 * data buffer.
 */
void XMLObjectReader::initCppDomTree(std::vector<vpr::Uint8> data)
{
   std::string xml_data(data.begin(), data.end());
   std::stringstream xml_stream(xml_data);
   cppdom::ContextPtr cxt(new cppdom::Context);
   cppdom::NodePtr new_node = cppdom::NodePtr(new cppdom::Node(cxt));
   new_node->load(xml_stream, cxt);

   mRootNode = new_node;

   std::string root_name = mRootNode->getName();   // Debugging

   mCurNodeStack.clear();
}

void XMLObjectReader::debugDumpStack(int debug_level)
{
   vprDEBUG_OutputGuard(vprDBG_ALL, debug_level, "mCurNodeStack:\n", "end node stack\n");

   for(unsigned i=0; i<mCurNodeStack.size(); ++i)
   {
      mCurNodeStack[i].debugDump(debug_level);
   }
}

/**
 * Starting a new tag.
 * Get the local cdata into the current data source.
 */
vpr::ReturnStatus XMLObjectReader::beginTag(std::string tagName)
{
   vprDEBUG_OutputGuard(vprDBG_ALL, XML_OR_LEVEL, std::string("beginTag:[") + tagName + std::string("]\n"), "endTag");

   vprDEBUG(vprDBG_ALL,XML_OR_LEVEL) << "mCurNodeStack: size: " << mCurNodeStack.size() << std::endl << vprDEBUG_FLUSH;
   debugDumpStack(XML_OR_LEVEL);

   // Find the correct child
   if(mCurNodeStack.empty())  // We should enter the root
   {
      vprDEBUG(vprDBG_ALL,XML_OR_LEVEL) << "mCurNodeStack is empty. Push on root.\n" << vprDEBUG_FLUSH;
      mCurNodeStack.push_back(NodeState(mRootNode.get()));<commit_msg>Silenced an ICC compiler warning.  I don't know why GCC didn't complain about this one.<commit_after>/****************** <VPR heading BEGIN do not edit this line> *****************
 *
 * VR Juggler Portable Runtime
 *
 * Original Authors:
 *   <NAME>, <NAME>, <NAME>, <NAME>
 *
 * -----------------------------------------------------------------
 * File:          $RCSfile$
 * Date modified: $Date$
 * Version:       $Revision$
 * -----------------------------------------------------------------
 *
 ****************** <VPR heading END do not edit this line> ******************/

/*************** <auto-copyright.pl BEGIN do not edit this line> **************
 *
 * VR Juggler is (C) Copyright 1998-2003 by Iowa State University
 *
 * Original Authors:
 *   <NAME>, <NAME>,
 *   <NAME>, <NAME>,
 *   <NAME>, <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 *************** <auto-copyright.pl END do not edit this line> ***************/

#include <vpr/IO/XMLObjectReader.h>

#include <vector>

#include <boost/concept_check.hpp>
#include <boost/static_assert.hpp>
#include <vpr/IO/ObjectReader.h>

#include <vpr/Util/Assert.h>
#include <vpr/Util/Debug.h>

#include <cppdom/cppdom.h>
#include <cppdom/version.h>
#include <string>
#include <sstream>
#include <iterator>

#define XML_OR_LEVEL vprDBG_HEX_LVL


namespace
{
 /** Helper to read the data from the current string. */
 template<class T>
 vpr::ReturnStatus readValueStringRep(T& val, std::stringstream* inStream)
 {
    vprASSERT(inStream != NULL);

    // Just to be safe do this again for the heck of it. :)
    //in_stream->exceptions( std::ios::eofbit | std::ios::failbit | std::ios::badbit );
    vprASSERT(!inStream->bad() && "Bad stream, BAAADDD stream.");
    vprASSERT(!inStream->fail() && "Stream failed.");
    vprASSERT(!inStream->eof() && "Stream EOF'd.");
    vprASSERT(inStream->good());

    //stream_content = (*in_stream).str();

    (*inStream) >> val;

    return vpr::ReturnStatus::Succeed;
 }

}

namespace vpr
{

XMLObjectReader::XMLObjectReader(std::vector<vpr::Uint8> data)
   : mCurSource(CdataSource)
{
   initCppDomTree(data);
   //mAttribSource.exceptions( std::ios::eofbit | std::ios::failbit | std::ios::badbit );
}

XMLObjectReader::XMLObjectReader(cppdom::NodePtr rootNode)
   : mCurSource(CdataSource)
{
   mRootNode = rootNode;
   //mAttribSource.exceptions( std::ios::eofbit | std::ios::failbit | std::ios::badbit );
}



XMLObjectReader::NodeState::NodeState(cppdom::Node* cur_node)
   : node(cur_node)
{
   nextChild_i = node->getChildren().begin();     // Initialize to the first child
   endChild_i = node->getChildren().end();
   //cdataSource.exceptions( std::ios::eofbit | std::ios::failbit | std::ios::badbit );
   cdataSource.str(node->getFullCdata());
}

XMLObjectReader::NodeState::NodeState(const NodeState& rhs)
{
   node = rhs.node;
   nextChild_i = rhs.nextChild_i;
   endChild_i = rhs.endChild_i;
   //cdataSource.exceptions( std::ios::eofbit | std::ios::failbit | std::ios::badbit );
   cdataSource.str(rhs.cdataSource.str());
}

XMLObjectReader::NodeState& XMLObjectReader::NodeState::operator=(const NodeState& rhs)
{
   node = rhs.node;
   nextChild_i = rhs.nextChild_i;
   endChild_i = rhs.endChild_i;
   cdataSource.str(rhs.cdataSource.str());
   return *this;
}

void XMLObjectReader::NodeState::debugDump(int debug_level)
{
   vprDEBUG(vprDBG_ALL, debug_level) << "Node:" << node->getName() << "  num children:" << node->getChildren().size()
                                     << "  distance: nextChild-->endChild:" << std::distance(nextChild_i,endChild_i) << std::endl << vprDEBUG_FLUSH;
}


/** Get the current string source we are reading from.
*/
std::stringstream* XMLObjectReader::getCurSource()
{
   std::stringstream* in_stream(NULL);

   if(AttribSource == mCurSource)
   {
      in_stream = &mAttribSource;
   }
   else
   {
      in_stream = &(mCurNodeStack.back().cdataSource);
   }

   return in_stream;
}

/**
 * Initialize the members based on a serialized version of something in the
 * data buffer.
 */
void XMLObjectReader::initCppDomTree(std::vector<vpr::Uint8> data)
{
   std::string xml_data(data.begin(), data.end());
   std::stringstream xml_stream(xml_data);
   cppdom::ContextPtr cxt(new cppdom::Context);
   cppdom::NodePtr new_node = cppdom::NodePtr(new cppdom::Node(cxt));
   new_node->load(xml_stream, cxt);

   mRootNode = new_node;

#ifdef VPR_DEBUG
   std::string root_name = mRootNode->getName();   // Debugging
   boost::ignore_unused_variable_warning(root_name);
#endif

   mCurNodeStack.clear();
}

void XMLObjectReader::debugDumpStack(int debug_level)
{
   vprDEBUG_OutputGuard(vprDBG_ALL, debug_level, "mCurNodeStack:\n", "end node stack\n");

   for(unsigned i=0; i<mCurNodeStack.size(); ++i)
   {
      mCurNodeStack[i].debugDump(debug_level);
   }
}

/**
 * Starting a new tag.
 * Get the local cdata into the current data source.
 */
vpr::ReturnStatus XMLObjectReader::beginTag(std::string tagName)
{
   vprDEBUG_OutputGuard(vprDBG_ALL, XML_OR_LEVEL, std::string("beginTag:[") + tagName + std::string("]\n"), "endTag");

   vprDEBUG(vprDBG_ALL,XML_OR_LEVEL) << "mCurNodeStack: size: " << mCurNodeStack.size() << std::endl << vprDEBUG_FLUSH;
   debugDumpStack(XML_OR_LEVEL);

   // Find the correct child
   if(mCurNodeStack.empty())  // We should enter the root
   {
      vprDEBUG(vprDBG_ALL,XML_OR_LEVEL) << "mCurNodeStack is empty. Push on root.\n" << vprDEBUG_FLUSH;
      mCurNodeStack.push_back(NodeState(mRootNode.get()));
{'fraction_non_alphanumeric': 0.1782437745740498, 'fraction_numerical': 0.004259501965923985, 'mean_word_length': 3.8855542216886754, 'pattern_counts': {'":': 0, '<': 118, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6633405', 'n_tokens_mistral': 4023, 'n_tokens_neox': 3957, 'n_words': 939}