<commit_before>/*=========================================================================

  Program:   Dionysus
  Module:    mwsRestXMLParser.cxx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Kitware Inc. All rights reserved.
  See Copyright.txt or http://www.Kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "mwsRestXMLParser.h"

namespace mws{

/** Constructor */
RestXMLParser::RestXMLParser()
{
  m_ErrorCode = 0;
  m_ErrorMessage = "";
  m_CurrentTag = "";
  m_FoundXMLTag = false;
  m_PreBuffer = "";
}
 
//----------------------------------------------------------------------------
static void itkXMLParserStartElement(void* parser, const char *name,
                                    const char **atts)
{
  // Begin element handler that is registered with the XML_Parser.
  // This just casts the user data to a itkXMLParser and calls
  // StartElement.
  static_cast<RestXMLParser*>(parser)->StartElement(name, atts);
}

//----------------------------------------------------------------------------
static void itkXMLParserEndElement(void* parser, const char *name)
{
  // End element handler that is registered with the XML_Parser.  This
  // just casts the user data to a itkXMLParser and calls EndElement.
  static_cast<RestXMLParser*>(parser)->EndElement(name);
}

//--------------------------------------------------------------------------------------------------
static void itkXMLParserCharacterDataHandler(void* parser, const char* data,
                                      int length)
  {
  // Character data handler that is registered with the XML_Parser.
  // This just casts the user data to a itkXMLParser and calls
  // CharacterDataHandler.
  static_cast<RestXMLParser*>(parser)->CharacterDataHandler(data, length);
  }

/** Initialize **/
void RestXMLParser::Initialize(void)
{
  m_Parser = XML_ParserCreate(0);

  XML_SetElementHandler(m_Parser,
                        &itkXMLParserStartElement,
                        &itkXMLParserEndElement);

  XML_SetCharacterDataHandler(m_Parser,
                              &itkXMLParserCharacterDataHandler);

  XML_SetUserData(m_Parser,this);

  this->m_ErrorMessage = "";
  this->m_CurrentTag = "";
  this->m_ErrorCode = 0;
}
  
//--------------------------------------------------------------------------------------------------
bool RestXMLParser::Parse(const char* buffer,unsigned long length)
{
  if(!m_Parser)
    {
    return false;
    }
  if(!m_FoundXMLTag)
    {
    m_PreBuffer.append(buffer);
    size_t pos;
    if((pos = m_PreBuffer.find("<?xml")) != std::string::npos)
      {
      m_FoundXMLTag = true;
      m_PreBuffer = m_PreBuffer.substr(pos);
      buffer = m_PreBuffer.c_str();
      length = m_PreBuffer.length();
      }
    }
  else
    {
    int result = XML_Parse(m_Parser, buffer, length, false);
    if(result==0)
      {
      m_ErrorCode = 1;
      m_ErrorMessage = "Cannot parse XML";
      return false;
      }<commit_msg>BUG: Wrong changes<commit_after>/*=========================================================================

  Program:   Dionysus
  Module:    mwsRestXMLParser.cxx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Kitware Inc. All rights reserved.
  See Copyright.txt or http://www.Kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "mwsRestXMLParser.h"

namespace mws{

/** Constructor */
RestXMLParser::RestXMLParser()
{
  m_ErrorCode = 0;
  m_ErrorMessage = "";
  m_CurrentTag = "";
  m_FoundXMLTag = false;
  m_PreBuffer = "";
}
 
//----------------------------------------------------------------------------
static void itkXMLParserStartElement(void* parser, const char *name,
                                    const char **atts)
{
  // Begin element handler that is registered with the XML_Parser.
  // This just casts the user data to a itkXMLParser and calls
  // StartElement.
  static_cast<RestXMLParser*>(parser)->StartElement(name, atts);
}

//----------------------------------------------------------------------------
static void itkXMLParserEndElement(void* parser, const char *name)
{
  // End element handler that is registered with the XML_Parser.  This
  // just casts the user data to a itkXMLParser and calls EndElement.
  static_cast<RestXMLParser*>(parser)->EndElement(name);
}

//--------------------------------------------------------------------------------------------------
static void itkXMLParserCharacterDataHandler(void* parser, const char* data,
                                      int length)
  {
  // Character data handler that is registered with the XML_Parser.
  // This just casts the user data to a itkXMLParser and calls
  // CharacterDataHandler.
  static_cast<RestXMLParser*>(parser)->CharacterDataHandler(data, length);
  }

/** Initialize **/
void RestXMLParser::Initialize(void)
{
  m_Parser = XML_ParserCreate(0);

  XML_SetElementHandler(m_Parser,
                        &itkXMLParserStartElement,
                        &itkXMLParserEndElement);

  XML_SetCharacterDataHandler(m_Parser,
                              &itkXMLParserCharacterDataHandler);

  XML_SetUserData(m_Parser,this);

  this->m_ErrorMessage = "";
  this->m_CurrentTag = "";
  this->m_ErrorCode = 0;
}
  
//--------------------------------------------------------------------------------------------------
bool RestXMLParser::Parse(const char* buffer,unsigned long length)
{
  if(!m_Parser)
    {
    return false;
    }
    
  if(!m_FoundXMLTag)
    {
    m_PreBuffer.append(buffer);
    size_t pos;
    if((pos = m_PreBuffer.find("<?xml")) != std::string::npos)
      {
      m_FoundXMLTag = true;
      m_PreBuffer = m_PreBuffer.substr(pos);
      buffer = m_PreBuffer.c_str();
      length = m_PreBuffer.length();
      }
    }
    
  if(m_FoundXMLTag)
    {
    int result = XML_Parse(m_Parser, buffer, length, false);
    if(result==0)
      {
      m_ErrorCode = 1;
      m_ErrorMessage = "Cannot parse XML";
      return false;
      }
{'fraction_non_alphanumeric': 0.2379278728606357, 'fraction_numerical': 0.001528117359413203, 'mean_word_length': 2.062704726251755, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2099763', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1699, 'n_words': 432}