<commit_before>//////////////////////////////////////////////////
//
// An example application which generates PYX notation.
// It's a pretty literal translation of the Sean McGrath's
// sax2pyx.py code presented in his XML Processing In Python book.
//
// For more information on PYX see http://pyxie.sourceforce.net/
//
// $Id$
//
//////////////////////////////////////////////////

#ifdef _MSC_VER
#pragma warning(disable : 4786)
#endif

#include <SAX/helpers/DefaultHandler.h>
#include <SAX/InputSource.h>
#include <SAX/XMLReader.h>
#include <iostream>

class SAX2PYX : public SAX::DefaultHandler
{
  public:
    virtual void startElement(const std::string& namespaceURI, const std::string& localName,
                              const std::string& qName, const SAX::Attributes& atts);
    virtual void endElement(const std::string& namespaceURI, const std::string& localName,
                            const std::string& qName);
    virtual void characters(const std::string& ch);
    virtual void processingInstruction(const std::string& target, const std::string& data);

    virtual void warning(const SAX::SAXException& e) { fatalError(e); }
    virtual void error(const SAX::SAXException& e) { fatalError(e); }
    virtual void fatalError(const SAX::SAXException& e);

  private:
    std::string escape(const std::string& str) const;
}; // class SimpleHandler

int main(int argc, char* argv[])
{
  if(argc == 1) 
	{
		std::cout << "Usage : " << argv[0] << " xmlfile ... " << std::endl;
		return 0;
	} // if(argc == 0)

	SAX2PYX handler;

	for(int i = 1; i < argc; ++i)
	{
    SAX::XMLReader<std::string> myParser;
		myParser.setContentHandler(handler);
		myParser.setErrorHandler(handler);

    SAX::InputSource is(argv[i]);
    myParser.parse(is);
	}

	return 0;
} // main


void SAX2PYX::startElement(const std::string& namespaceURI, const std::string& localName,
                           const std::string& qName, const SAX::Attributes& atts)
{
  std::cout << '(' << localName << std::endl;

  for(int i = 0; i < atts.getLength(); ++i)
  {
    std::cout << 'A' << atts.getLocalName(i)
              << ' ' << escape(atts.getValue(i))
              << std::endl;
  } // for ...

} // startElement

void SAX2PYX::endElement(const std::string& namespaceURI, const std::string& localName,
                       const std::string& qName)
{
  std::cout << ')' << localName << std::endl;
} // endElement

void SAX2PYX::characters(const std::string& ch)
{
  std::cout << '-' << escape(ch) << std::endl;
} // characters

void SAX2PYX::processingInstruction(const std::string& target, const std::string& data)
{
  std::cout << '?' << target
            << ' ' << data << std::endl;
} // processingInstruction

std::string SAX2PYX::escape(const std::string& str) const
{
  std::string estr(str);
  std::string::size_type i(estr.find("\n"));
  while(i != std::string::npos)
  {
    estr.replace(i, 1, "\\n", 2);
    i = estr.find("\n", i);
  } // while ...
  return estr;
} // escape

void SAX2PYX::fatalError(const SAX::SAXException& e)
{
  std::cerr << "Parse problem " << e.what() << std::endl;
  exit(0);
} // fatalError

// end of file<commit_msg>Used DefaultHandler::fatalError and catch thrown exception<commit_after>//////////////////////////////////////////////////
//
// An example application which generates PYX notation.
// It's a pretty literal translation of the Sean McGrath's
// sax2pyx.py code presented in his XML Processing In Python book.
//
// For more information on PYX see http://pyxie.sourceforce.net/
//
// $Id$
//
//////////////////////////////////////////////////

#ifdef _MSC_VER
#pragma warning(disable : 4786)
#endif

#include <SAX/helpers/DefaultHandler.h>
#include <SAX/InputSource.h>
#include <SAX/XMLReader.h>
#include <iostream>

class SAX2PYX : public SAX::DefaultHandler
{
  public:
    virtual void startElement(const std::string& namespaceURI, const std::string& localName,
                              const std::string& qName, const SAX::Attributes& atts);
    virtual void endElement(const std::string& namespaceURI, const std::string& localName,
                            const std::string& qName);
    virtual void characters(const std::string& ch);
    virtual void processingInstruction(const std::string& target, const std::string& data);

    virtual void warning(const SAX::SAXParseException& e) { fatalError(e); }
    virtual void error(const SAX::SAXParseException& e) { fatalError(e); }

  private:
    std::string escape(const std::string& str) const;
}; // class SimpleHandler

int main(int argc, char* argv[])
{
  if(argc == 1) 
	{
		std::cout << "Usage : " << argv[0] << " xmlfile ... " << std::endl;
		return 0;
	} // if(argc == 0)

	SAX2PYX handler;

	for(int i = 1; i < argc; ++i)
	{
    try
    {
      SAX::XMLReader<std::string> myParser;
		  myParser.setContentHandler(handler);
		  myParser.setErrorHandler(handler);

      SAX::InputSource is(argv[i]);
      myParser.parse(is);
    } // try
    catch(std::runtime_error& e)
    {
      std::cerr << "Parse problem " << e.what() << std::endl;
    } // catch  
	} // for ...

	return 0;
} // main


void SAX2PYX::startElement(const std::string&, const std::string& localName,
                           const std::string&, const SAX::Attributes& atts)
{
  std::cout << '(' << localName << std::endl;

  for(int i = 0; i < atts.getLength(); ++i)
  {
    std::cout << 'A' << atts.getLocalName(i)
              << ' ' << escape(atts.getValue(i))
              << std::endl;
  } // for ...

} // startElement

void SAX2PYX::endElement(const std::string&, const std::string& localName,
                       const std::string&)
{
  std::cout << ')' << localName << std::endl;
} // endElement

void SAX2PYX::characters(const std::string& ch)
{
  std::cout << '-' << escape(ch) << std::endl;
} // characters

void SAX2PYX::processingInstruction(const std::string& target, const std::string& data)
{
  std::cout << '?' << target
            << ' ' << data << std::endl;
} // processingInstruction

std::string SAX2PYX::escape(const std::string& str) const
{
  std::string estr(str);
  std::string::size_type i(estr.find("\n"));
  while(i != std::string::npos)
  {
    estr.replace(i, 1, "\\n", 2);
    i = estr.find("\n", i);
  } // while ...
  return estr;
} // escape

// end of file
{'fraction_non_alphanumeric': 0.19381443298969073, 'fraction_numerical': 0.006978588421887391, 'mean_word_length': 2.8033775633293123, 'pattern_counts': {'":': 0, '<': 121, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7207709', 'n_tokens_mistral': 2079, 'n_tokens_neox': 2024, 'n_words': 495}