arabica/examples/SAX/pyx.cpp

114 lines
3.1 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.
//
// 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>
2002-06-21 13:16:28 +02:00
#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); }
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
{
SAX::XMLReader<std::string> myParser;
2004-05-12 21:38:54 +02:00
myParser.setContentHandler(handler);
myParser.setErrorHandler(handler);
2007-06-18 14:01:47 +02:00
myParser.setFeature("prohibit-dtd", false);
SAX::InputSource 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 SAX::Attributes& 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