|
From: Christian P. <cp...@us...> - 2005-01-11 14:47:59
|
Update of /cvsroot/pclasses/pclasses2/src/XML In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9267/src/XML Added Files: Makefile.am XMLParseError.cpp XMLPushParser.expat.cpp Log Message: Added XMLParseError, XMLPushParser. --- NEW FILE: XMLPushParser.expat.cpp --- /*************************************************************************** * Copyright (C) 2004 by Christian Prochnow * * cp...@se... * * * * This program 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 program 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 General Public License for more details. * * * * You should have received a copy of the GNU Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "pclasses/XML/XMLPushParser.h" #include <expat.h> namespace P { namespace XML { struct XMLPushParser::ParserImpl { XML_Parser parser; std::string encoding; ParserImpl(XMLPushParser* p, const std::string& enc, int flags, char sep) { if(flags & XMLPushParser::XMLNS) parser = XML_ParserCreateNS(enc.empty() ? 0 : enc.c_str(), sep); else parser = XML_ParserCreate(enc.empty() ? 0 : enc.c_str()); XML_SetUserData(parser, p); XML_SetNamespaceDeclHandler(parser, namespaceDeclStart, namespaceDeclEnd); XML_SetElementHandler(parser, elementStart, elementEnd); XML_SetCommentHandler(parser, comment); XML_SetCharacterDataHandler(parser, characterData); XML_SetCdataSectionHandler(parser, cDataStart, cDataEnd); XML_SetExternalEntityRefHandler(parser, externalEntityRef); XML_SetNotationDeclHandler(parser, notationDecl); encoding = enc; } ParserImpl(ParserImpl* impl, const std::string& enc, const std::string& context) { parser = XML_ExternalEntityParserCreate(impl->parser, enc.empty() ? 0 : enc.c_str(), context.c_str()); encoding = impl->encoding; } ~ParserImpl() { XML_ParserFree(parser); } static void namespaceDeclStart(void* userData, const char* prefix, const char* uri) { XMLPushParser* p = static_cast<XMLPushParser*>(userData); std::string prefixTmp(prefix); std::string uriTmp(uri); p->namespaceDeclStart(prefix, uri); p->sig_namespaceDeclStart.fire(prefix, uri); } static void namespaceDeclEnd(void* userData, const char* prefix) { XMLPushParser* p = static_cast<XMLPushParser*>(userData); std::string prefixTmp(prefix); p->namespaceDeclEnd(prefix); p->sig_namespaceDeclEnd.fire(prefix); } static void elementStart(void* userData, const char* elem, const char** attrs) { XMLPushParser* p = static_cast<XMLPushParser*>(userData); std::string elemTmp(elem); XMLPushParser::AttributeMap attrMap; for(int i = 0; attrs[i]; i+=2) attrMap[attrs[i]] = attrs[i+1]; p->elementStart(elemTmp, attrMap); p->sig_elementStart.fire(elemTmp, attrMap); } static void elementEnd(void* userData, const char* elem) { XMLPushParser* p = static_cast<XMLPushParser*>(userData); std::string tmp(elem); p->elementEnd(tmp); p->sig_elementEnd.fire(tmp); } static void comment(void* userData, const char* comment) { XMLPushParser* p = static_cast<XMLPushParser*>(userData); std::string tmp(comment); p->comment(tmp); p->sig_comment.fire(tmp); } static void characterData(void* userData, const char* cdata, int len) { XMLPushParser* p = static_cast<XMLPushParser*>(userData); std::string tmp(cdata, len); p->characterData(tmp); p->sig_characterData.fire(tmp); } static void cDataStart(void* userData) { XMLPushParser* p = static_cast<XMLPushParser*>(userData); p->cDataStart(); p->sig_cDataStart.fire(); } static void cDataEnd(void* userData) { XMLPushParser* p = static_cast<XMLPushParser*>(userData); p->cDataEnd(); p->sig_cDataEnd.fire(); } static int externalEntityRef(XML_Parser p, const char* context, const char* base, const char* systemId, const char* publicId) { XMLPushParser* parser = static_cast<XMLPushParser*>(XML_GetUserData(p)); std::string tmpContext(context); std::string tmpBase(base); std::string tmpSystemId(systemId); std::string tmpPublicId(publicId); return parser->externalEntityRef(tmpContext, tmpBase, tmpSystemId, tmpPublicId) ? 1 : 0; } static void notationDecl(void* userData, const char* notationName, const char* base, const char* systemId, const char* publicId) { XMLPushParser* p = static_cast<XMLPushParser*>(userData); std::string tmpNotName(notationName); std::string tmpBase(base); std::string tmpSystemId(systemId); std::string tmpPublicId(publicId); p->notationDecl(tmpNotName, tmpBase, tmpSystemId, tmpPublicId); } }; XMLPushParser::XMLPushParser(const std::string& encoding, int flags) : _impl(new XMLPushParser::ParserImpl(this, encoding, flags, ';')) { } XMLPushParser::XMLPushParser(const std::string& encoding, int flags, char separator) : _impl(new XMLPushParser::ParserImpl(this, encoding, flags, separator)) { } XMLPushParser::XMLPushParser(XMLPushParser& p, const std::string& encoding, const std::string& context) : _impl(new XMLPushParser::ParserImpl(p._impl, encoding, context)) { } XMLPushParser::~XMLPushParser() throw() { delete _impl; } void XMLPushParser::setParamEntityParsing(EntityParsingMode m) { XML_ParamEntityParsing c; switch(m) { case Never: c = XML_PARAM_ENTITY_PARSING_NEVER; break; case UnlessStandalone: c = XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE; break; case Always: c = XML_PARAM_ENTITY_PARSING_ALWAYS; break; } XML_SetParamEntityParsing(_impl->parser, c); } void XMLPushParser::parse(const char* buffer, size_t count) throw(XMLParseError) { if(XML_Parse(_impl->parser, buffer, count, false) == 0) { XML_Error err = XML_GetErrorCode(_impl->parser); throw XMLParseError(err, XML_ErrorString(err), "Error parsing XML data", P_SOURCEINFO); } } void XMLPushParser::finish() throw() { XML_Parse(_impl->parser, 0, 0, true); } void XMLPushParser::reset() throw() { XML_ParserReset(_impl->parser, _impl->encoding.empty() ? 0 : _impl->encoding.c_str()); } void XMLPushParser::namespaceDeclStart(const std::string& prefix, const std::string& uri) { } void XMLPushParser::namespaceDeclEnd(const std::string& prefix) { } void XMLPushParser::comment(const std::string& comment) { } void XMLPushParser::elementStart(const std::string& el, const AttributeMap& attrs) { } void XMLPushParser::elementEnd(const std::string& el) { } void XMLPushParser::characterData(const std::string& data) { } void XMLPushParser::cDataStart() { } void XMLPushParser::cDataEnd() { } void XMLPushParser::notationDecl(const std::string& notationName, const std::string& base, const std::string& systemId, const std::string& publicId) { } bool XMLPushParser::externalEntityRef(const std::string& context, const std::string& base, const std::string& systemId, const std::string& publicId) { return false; } } // !namespace XML } // !namespace P --- NEW FILE: XMLParseError.cpp --- /*************************************************************************** * Copyright (C) 2004 by Christian Prochnow * * cp...@se... * * * * This program 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 program 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 General Public License for more details. * * * * You should have received a copy of the GNU Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "pclasses/XMLParseError.h" namespace P { namespace XML { XMLParseError::XMLParseError(int err, const std::string& text, const char* what, const SourceInfo& si) : RuntimeError(what, si), _errNo(err), _text(text) { } XMLParseError::~XMLParseError() { } int XMLParseError::errNo() const throw() { return _errNo; } const std::string& XMLParseError::text() const throw() { return _text; } } // !namespace XML } // !namespace P --- NEW FILE: Makefile.am --- INCLUDES = -I$(top_srcdir)/include -I$(top_builddir)/include $(all_includes) METASOURCES = AUTO lib_LTLIBRARIES = libpclasses_xml.la libpclasses_xml_la_SOURCES = XMLPushParser.expat.cpp libpclasses_xml_la_LDFLAGS = -no-undefined libpclasses_xml_la_LIBADD = $(top_builddir)/src/libpclasses.la \ $(top_builddir)/src/System/libpclasses_system.la -lexpat |