arabica/examples/SAX/pyx.cpp

114 lines
3.2 KiB
C++
Raw Normal View History

2002-06-21 13:16:28 +02:00
//////////////////////////////////////////////////
//
// 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.
//
2007-09-13 23:21:55 +02:00
// For more information on PYX see
// http://www.xml.com/pub/a/2000/03/15/feature
2002-06-21 13:16:28 +02:00
//
// $Id$
//
//////////////////////////////////////////////////
#ifdef _MSC_VER
#pragma warning(disable : 4786)
#endif
2007-09-05 00:55:47 +02:00
#include <SAX/helpers/DefaultHandler.hpp>
#include <SAX/InputSource.hpp>
#include <SAX/XMLReader.hpp>
2002-06-21 13:16:28 +02:00
#include <iostream>
class SAX2PYX : public Arabica::SAX::DefaultHandler<std::string>
2002-06-21 13:16:28 +02:00
{
public:
virtual void startElement(const std::string& namespaceURI, const std::string& localName,
const std::string& qName, const Arabica::SAX::Attributes<std::string>& atts);
2002-06-21 13:16:28 +02:00
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 Arabica::SAX::SAXParseException<std::string>& e) { fatalError(e); }
virtual void error(const Arabica::SAX::SAXParseException<std::string>& e) { fatalError(e); }
2002-06-21 13:16:28 +02:00
private:
std::string escape(const std::string& str) const;
}; // class SimpleHandler
int main(int argc, char* argv[])
{
if(argc == 1)
2004-05-12 21:38:54 +02:00
{
std::cout << "Usage : " << argv[0] << " xmlfile ... " << std::endl;
return 0;
} // if(argc == 0)
2002-06-21 13:16:28 +02:00
2004-05-12 21:38:54 +02:00
SAX2PYX handler;
2002-06-21 13:16:28 +02:00
2004-05-12 21:38:54 +02:00
for(int i = 1; i < argc; ++i)
{
try
{
2007-09-05 11:49:18 +02:00
Arabica::SAX::XMLReader<std::string> myParser;
2004-05-12 21:38:54 +02:00
myParser.setContentHandler(handler);
myParser.setErrorHandler(handler);
Arabica::SAX::InputSource<std::string> is(argv[i]);
myParser.parse(is);
} // try
catch(std::runtime_error& e)
{
std::cerr << "Parse problem " << e.what() << std::endl;
} // catch
2004-05-12 21:38:54 +02:00
} // for ...
2002-06-21 13:16:28 +02:00
2004-05-12 21:38:54 +02:00
return 0;
2002-06-21 13:16:28 +02:00
} // main
void SAX2PYX::startElement(const std::string&, const std::string& localName,
const std::string&, const Arabica::SAX::Attributes<std::string>& atts)
2002-06-21 13:16:28 +02:00
{
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&)
2002-06-21 13:16:28 +02:00
{
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