2003-09-11 12:26:53 +02:00
|
|
|
#ifndef ARABICA_SAX_EXPAT_H
|
|
|
|
#define ARABICA_SAX_EXPAT_H
|
2002-06-21 13:16:28 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// A SAX2 wrapper class for expat.
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2003-09-09 13:14:48 +02:00
|
|
|
#include <SAX/ArabicaConfig.h>
|
2002-06-21 13:16:28 +02:00
|
|
|
#include <SAX/XMLReader.h>
|
|
|
|
#include <expat.h>
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <SAX/InputSource.h>
|
|
|
|
#include <SAX/ContentHandler.h>
|
|
|
|
#include <SAX/SAXParseException.h>
|
|
|
|
#include <SAX/SAXNotRecognizedException.h>
|
|
|
|
#include <SAX/SAXNotSupportedException.h>
|
|
|
|
#include <SAX/helpers/DefaultHandler.h>
|
|
|
|
#include <SAX/helpers/AttributesImpl.h>
|
|
|
|
#include <SAX/helpers/NamespaceSupport.h>
|
|
|
|
#include <SAX/helpers/InputSourceResolver.h>
|
|
|
|
#include <SAX/helpers/FeatureNames.h>
|
|
|
|
#include <SAX/helpers/PropertyNames.h>
|
2005-08-15 23:09:13 +02:00
|
|
|
#include <Utils/StringAdaptor.h>
|
2002-06-21 13:16:28 +02:00
|
|
|
#include <SAX/helpers/AttributeDefaults.h>
|
|
|
|
#include <typeinfo>
|
|
|
|
#include <map>
|
2005-12-07 11:51:00 +01:00
|
|
|
#include <Utils/getparam.hpp>
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
namespace SAX
|
|
|
|
{
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// the callback functions for expat
|
|
|
|
namespace expat_wrapper_impl_mumbojumbo
|
|
|
|
{
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
void ewim_charHandler(void* data, const char* txt, int txtlen);
|
|
|
|
void ewim_startElement(void* data, const char* qName, const char** atts);
|
|
|
|
void ewim_endElement(void* data, const char* qName);
|
|
|
|
void ewim_processingInstruction(void* userData, const char* target, const char* data);
|
|
|
|
void ewim_elementDeclaration(void* userData,
|
|
|
|
const XML_Char* name,
|
|
|
|
XML_Content* model);
|
|
|
|
void ewim_attListDeclaration(void* userData,
|
2002-11-23 21:09:25 +01:00
|
|
|
const XML_Char* elname,
|
|
|
|
const XML_Char* attname,
|
|
|
|
const XML_Char* att_type,
|
|
|
|
const XML_Char* dflt,
|
|
|
|
int isrequired);
|
2002-06-21 13:16:28 +02:00
|
|
|
void ewim_entityDeclaration(void* userData,
|
|
|
|
const XML_Char* entityName,
|
2002-11-23 21:09:25 +01:00
|
|
|
int is_parameter_entity,
|
|
|
|
const XML_Char* value,
|
|
|
|
int value_length,
|
|
|
|
const XML_Char* base,
|
|
|
|
const XML_Char* systemId,
|
|
|
|
const XML_Char* publicId,
|
|
|
|
const XML_Char* notationName);
|
2002-06-21 13:16:28 +02:00
|
|
|
void ewim_notationDeclaration(void* userData,
|
2002-11-23 21:09:25 +01:00
|
|
|
const XML_Char* notationName,
|
|
|
|
const XML_Char* base,
|
|
|
|
const XML_Char* systemId,
|
|
|
|
const XML_Char* publicId);
|
2002-06-21 13:16:28 +02:00
|
|
|
void ewim_startDoctypeDecl(void* userData,
|
2002-11-23 21:09:25 +01:00
|
|
|
const XML_Char* doctypeName,
|
|
|
|
const XML_Char* sysid,
|
|
|
|
const XML_Char* pubid,
|
|
|
|
int has_internal_subset);
|
2002-06-21 13:16:28 +02:00
|
|
|
void ewim_endDoctypeDecl(void* userData);
|
|
|
|
void ewim_startCdataSection(void* userData);
|
|
|
|
void ewim_endCdataSection(void* userData);
|
|
|
|
void ewim_commentHandler(void* userData, const XML_Char* data);
|
|
|
|
int ewim_externalEntityRefHandler(XML_Parser parser,
|
|
|
|
const XML_Char* context,
|
|
|
|
const XML_Char* base,
|
|
|
|
const XML_Char* systemId,
|
|
|
|
const XML_Char* publicId);
|
|
|
|
|
|
|
|
} // extern "C"
|
|
|
|
|
|
|
|
class expat2base
|
|
|
|
{
|
2005-08-22 17:04:27 +02:00
|
|
|
protected:
|
|
|
|
virtual ~expat2base() { }
|
|
|
|
|
2002-06-21 13:16:28 +02:00
|
|
|
private:
|
|
|
|
virtual void charHandler(const char* txt, int txtlen) = 0;
|
|
|
|
virtual void startElement(const char* qName, const char** atts) = 0;
|
|
|
|
virtual void endElement(const char* qName) = 0;
|
|
|
|
virtual void processingInstruction(const char* target, const char* data) = 0;
|
|
|
|
virtual void elementDeclaration(const XML_Char* name,
|
|
|
|
const XML_Content* model) = 0;
|
|
|
|
virtual void attListDeclaration(const XML_Char* elname,
|
2002-11-23 21:09:25 +01:00
|
|
|
const XML_Char* attname,
|
|
|
|
const XML_Char* att_type,
|
|
|
|
const XML_Char* dflt,
|
2004-09-14 22:51:36 +02:00
|
|
|
int isrequired) = 0;
|
2002-06-21 13:16:28 +02:00
|
|
|
virtual void entityDeclaration(const XML_Char* entityName,
|
2002-11-23 21:09:25 +01:00
|
|
|
int is_parameter_entity,
|
|
|
|
const XML_Char* value,
|
|
|
|
int value_length,
|
|
|
|
const XML_Char* base,
|
|
|
|
const XML_Char* systemId,
|
|
|
|
const XML_Char* publicId,
|
|
|
|
const XML_Char* notationName) = 0;
|
2002-06-21 13:16:28 +02:00
|
|
|
virtual void notationDeclaration(const XML_Char* notationName,
|
2002-11-23 21:09:25 +01:00
|
|
|
const XML_Char* base,
|
|
|
|
const XML_Char* systemId,
|
|
|
|
const XML_Char* publicId) = 0;
|
2002-06-21 13:16:28 +02:00
|
|
|
virtual void startDoctypeDecl(const XML_Char* doctypeName,
|
2002-11-23 21:09:25 +01:00
|
|
|
const XML_Char* sysid,
|
|
|
|
const XML_Char* pubid,
|
|
|
|
int has_internal_subset) = 0;
|
2002-06-21 13:16:28 +02:00
|
|
|
virtual void endDoctypeDecl() = 0;
|
|
|
|
virtual void startCdataSection() = 0;
|
|
|
|
virtual void endCdataSection() = 0;
|
|
|
|
virtual void commentHandler(const XML_Char* data) = 0;
|
|
|
|
virtual int externalEntityRefHandler(XML_Parser parser,
|
|
|
|
const XML_Char* context,
|
|
|
|
const XML_Char* base,
|
|
|
|
const XML_Char* systemId,
|
|
|
|
const XML_Char* publicId) = 0;
|
|
|
|
|
|
|
|
|
|
|
|
friend void ewim_charHandler(void*, const char*, int);
|
|
|
|
friend void ewim_startElement(void*, const char*, const char**);
|
|
|
|
friend void ewim_endElement(void*, const char*);
|
|
|
|
friend void ewim_processingInstruction(void*, const char*, const char*);
|
|
|
|
friend void ewim_elementDeclaration(void*, const XML_Char*, XML_Content*);
|
2004-09-14 22:51:36 +02:00
|
|
|
friend void ewim_attListDeclaration(void*, const XML_Char*, const XML_Char*, const XML_Char*, const XML_Char*, int);
|
2002-06-21 13:16:28 +02:00
|
|
|
friend void ewim_entityDeclaration(void*, const XML_Char*, int, const XML_Char*, int, const XML_Char*, const XML_Char*, const XML_Char*, const XML_Char*);
|
|
|
|
friend void ewim_notationDeclaration(void*, const XML_Char*, const XML_Char*, const XML_Char*, const XML_Char*);
|
|
|
|
friend void ewim_startDoctypeDecl(void*, const XML_Char*, const XML_Char*, const XML_Char*, int);
|
|
|
|
friend void ewim_endDoctypeDecl(void*);
|
|
|
|
friend void ewim_startCdataSection(void*);
|
|
|
|
friend void ewim_endCdataSection(void*);
|
|
|
|
friend void ewim_commentHandler(void*, const XML_Char*);
|
|
|
|
friend int ewim_externalEntityRefHandler(XML_Parser, const XML_Char*, const XML_Char*, const XML_Char*, const XML_Char*);
|
|
|
|
|
|
|
|
}; // class expat2base
|
|
|
|
|
|
|
|
} // namespace expat_wrapper_impl_mumbojumbo
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// A SAX2 wrapper for expat. expat has an event-based interface of its own,
|
|
|
|
// so the SAX wrapper maps more or less directly to it.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* expat_wrapper puts an {@link basic_XMLReader XMLReader} interface
|
2003-03-20 17:31:09 +01:00
|
|
|
* around <a href='http://www.libexpat.org/'>Expat</a>.
|
2002-06-21 13:16:28 +02:00
|
|
|
* <p>
|
|
|
|
* For general usage:<br>
|
|
|
|
* <code>
|
|
|
|
* expat_wrapper<std::string> parser;<br>
|
|
|
|
* ... <br>
|
|
|
|
* parser.parse(filename);<br>
|
|
|
|
* </code>
|
|
|
|
* expat_wrapper<std::string> will hand out std::strings containing UTF-8 encoded
|
|
|
|
* data (expat's native format).
|
|
|
|
* <p>
|
|
|
|
* If you need std::wstring containing UCS-2 or perhaps you want to use some custom
|
|
|
|
* string type, then you can simply instantiate expat_wrapper on your string type and
|
|
|
|
* a policy class which knows how to convert UTF-8 const char*s to your type.
|
|
|
|
* <p>
|
|
|
|
* The default policy is:
|
|
|
|
* <code>
|
|
|
|
* template<class stringT><br>
|
|
|
|
* class default_expat_string_convertor<br>
|
|
|
|
* {<br>
|
|
|
|
* public:<br>
|
|
|
|
* stringT makeStringT(const char* str) const<br>
|
|
|
|
* {<br>
|
|
|
|
* if(!str)<br>
|
|
|
|
* return stringT();<br>
|
|
|
|
* return stringT(str);<br>
|
|
|
|
* } // makeStringT<br>
|
|
|
|
* stringT makeStringT(const char* str, int length) const<br>
|
|
|
|
* {<br>
|
|
|
|
* return stringT(str, length);<br>
|
|
|
|
* } // makeStringT<br>
|
|
|
|
* <br>
|
|
|
|
* std::string asStdString(const stringT& str) const<br>
|
|
|
|
* {<br>
|
|
|
|
* return str;<br>
|
|
|
|
* } // asStdString<br>
|
|
|
|
* }; // class default_string_convertor<br>
|
|
|
|
* </code>
|
|
|
|
* <p>
|
|
|
|
* A custom policy needs to provide the equivalent makeString and asStdString functions.
|
|
|
|
* <p>
|
|
|
|
* @author Jez Higgins
|
|
|
|
* <a href="mailto:jez@jezuk.co.uk">jez@jezuk.co.uk</a>
|
|
|
|
* @version $Id$
|
|
|
|
* @see SAX::basic_XMLReader
|
|
|
|
*/
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class string_type,
|
|
|
|
class T0 = Arabica::nil_t,
|
|
|
|
class T1 = Arabica::nil_t>
|
2002-06-21 13:16:28 +02:00
|
|
|
class expat_wrapper : public SAX::basic_XMLReader<string_type>,
|
|
|
|
public SAX::basic_Locator<string_type>,
|
|
|
|
public expat_wrapper_impl_mumbojumbo::expat2base
|
|
|
|
{
|
2004-09-14 22:51:36 +02:00
|
|
|
public:
|
2002-06-21 13:16:28 +02:00
|
|
|
typedef string_type stringT;
|
2005-12-07 11:51:00 +01:00
|
|
|
typedef typename Arabica::get_param<Arabica::string_adaptor_tag,
|
|
|
|
Arabica::default_string_adaptor<string_type>,
|
|
|
|
T0,
|
|
|
|
T1>::type string_adaptor_type;
|
2002-06-21 13:16:28 +02:00
|
|
|
typedef string_adaptor_type string_adaptorT;
|
2005-11-09 22:13:22 +01:00
|
|
|
typedef string_adaptor_type SA;
|
2002-06-21 13:16:28 +02:00
|
|
|
typedef SAX::basic_EntityResolver<stringT> entityResolverT;
|
|
|
|
typedef SAX::basic_DTDHandler<stringT> dtdHandlerT;
|
|
|
|
typedef SAX::basic_ContentHandler<stringT> contentHandlerT;
|
|
|
|
typedef SAX::basic_DeclHandler<stringT> declHandlerT;
|
|
|
|
typedef SAX::basic_LexicalHandler<stringT> lexicalHandlerT;
|
|
|
|
typedef SAX::basic_InputSource<stringT> inputSourceT;
|
|
|
|
typedef SAX::basic_Locator<stringT> locatorT;
|
|
|
|
typedef SAX::basic_NamespaceSupport<stringT, string_adaptorT> namespaceSupportT;
|
2003-09-10 10:47:37 +02:00
|
|
|
typedef SAX::basic_ErrorHandler<stringT> errorHandlerT;
|
|
|
|
typedef SAX::basic_SAXParseException<stringT> SAXParseExceptionT;
|
2004-09-14 22:51:36 +02:00
|
|
|
typedef SAX::basic_XMLReader<stringT> XMLReaderT;
|
|
|
|
typedef typename XMLReaderT::PropertyBase PropertyBaseT;
|
|
|
|
typedef typename XMLReaderT::template Property<lexicalHandlerT*> getLexicalHandlerT;
|
|
|
|
typedef typename XMLReaderT::template Property<lexicalHandlerT&> setLexicalHandlerT;
|
|
|
|
typedef typename XMLReaderT::template Property<declHandlerT*> getDeclHandlerT;
|
|
|
|
typedef typename XMLReaderT::template Property<declHandlerT&> setDeclHandlerT;
|
2002-06-21 13:16:28 +02:00
|
|
|
|
2004-09-14 22:51:36 +02:00
|
|
|
expat_wrapper();
|
|
|
|
virtual ~expat_wrapper();
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////
|
|
|
|
// Configuration
|
|
|
|
virtual bool getFeature(const stringT& name) const;
|
|
|
|
virtual void setFeature(const stringT& name, bool value);
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////
|
|
|
|
// Event Handlers
|
|
|
|
virtual void setEntityResolver(entityResolverT& resolver) { entityResolver_ = &resolver; }
|
|
|
|
virtual entityResolverT* getEntityResolver() const { return entityResolver_; }
|
|
|
|
virtual void setDTDHandler(dtdHandlerT& handler) { dtdHandler_ = &handler; }
|
|
|
|
virtual dtdHandlerT* getDTDHandler() const { return dtdHandler_; }
|
|
|
|
virtual void setContentHandler(contentHandlerT& handler) { contentHandler_ = &handler; }
|
|
|
|
virtual contentHandlerT* getContentHandler() const { return contentHandler_; }
|
2003-09-10 10:47:37 +02:00
|
|
|
virtual void setErrorHandler(errorHandlerT& handler) { errorHandler_ = &handler; }
|
|
|
|
virtual errorHandlerT* getErrorHandler() const { return errorHandler_; }
|
2007-01-15 15:21:33 +01:00
|
|
|
virtual void setDeclHandler(declHandlerT& handler) { declHandler_ = &handler; }
|
|
|
|
virtual declHandlerT* getDeclHandler() const { return declHandler_; }
|
|
|
|
virtual void setLexicalHandler(lexicalHandlerT& handler) { lexicalHandler_ = &handler; }
|
|
|
|
virtual lexicalHandlerT* getLexicalHandler() const { return lexicalHandler_; }
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
// Parsing
|
|
|
|
virtual void parse(inputSourceT& input);
|
|
|
|
private:
|
|
|
|
bool do_parse(inputSourceT& source, XML_Parser parser);
|
|
|
|
|
|
|
|
public:
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
// Locator
|
|
|
|
virtual stringT getPublicId() const;
|
|
|
|
virtual stringT getSystemId() const;
|
|
|
|
virtual int getLineNumber() const;
|
|
|
|
virtual int getColumnNumber() const;
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////
|
|
|
|
// properties
|
|
|
|
protected:
|
2002-09-20 12:16:35 +02:00
|
|
|
virtual std::auto_ptr<PropertyBaseT> doGetProperty(const stringT& name);
|
|
|
|
virtual void doSetProperty(const stringT& name, std::auto_ptr<PropertyBaseT> value);
|
2004-09-14 22:51:36 +02:00
|
|
|
private:
|
2002-09-20 12:11:57 +02:00
|
|
|
typename namespaceSupportT::Parts processName(const stringT& qName, bool isAttribute);
|
2002-06-21 13:16:28 +02:00
|
|
|
void reportError(const std::string& message, bool fatal = false);
|
|
|
|
void checkNotParsing(const stringT& type, const stringT& name) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual void charHandler(const char* txt, int txtlen);
|
|
|
|
virtual void startElement(const char* qName, const char** atts);
|
|
|
|
virtual void endElement(const char* qName);
|
|
|
|
virtual void processingInstruction(const char* target, const char* data);
|
|
|
|
virtual void elementDeclaration(const XML_Char* name,
|
|
|
|
const XML_Content* model);
|
|
|
|
virtual void attListDeclaration(const XML_Char* elname,
|
2002-11-23 21:09:25 +01:00
|
|
|
const XML_Char* attname,
|
|
|
|
const XML_Char* att_type,
|
|
|
|
const XML_Char* dflt,
|
2004-09-14 22:51:36 +02:00
|
|
|
int isrequired);
|
2002-06-21 13:16:28 +02:00
|
|
|
virtual void entityDeclaration(const XML_Char* entityName,
|
2002-11-23 21:09:25 +01:00
|
|
|
int is_parameter_entity,
|
|
|
|
const XML_Char* value,
|
|
|
|
int value_length,
|
|
|
|
const XML_Char* base,
|
|
|
|
const XML_Char* systemId,
|
|
|
|
const XML_Char* publicId,
|
|
|
|
const XML_Char* notationName);
|
2002-06-21 13:16:28 +02:00
|
|
|
virtual void notationDeclaration(const XML_Char* notationName,
|
2002-11-23 21:09:25 +01:00
|
|
|
const XML_Char* base,
|
|
|
|
const XML_Char* systemId,
|
|
|
|
const XML_Char* publicId);
|
2002-06-21 13:16:28 +02:00
|
|
|
virtual void startDoctypeDecl(const XML_Char* doctypeName,
|
2002-11-23 21:09:25 +01:00
|
|
|
const XML_Char* sysid,
|
|
|
|
const XML_Char* pubid,
|
|
|
|
int has_internal_subset);
|
2002-06-21 13:16:28 +02:00
|
|
|
virtual void endDoctypeDecl();
|
|
|
|
virtual void startCdataSection();
|
|
|
|
virtual void endCdataSection();
|
|
|
|
virtual void commentHandler(const XML_Char* data);
|
|
|
|
virtual int externalEntityRefHandler(XML_Parser parser,
|
|
|
|
const XML_Char* context,
|
|
|
|
const XML_Char* base,
|
|
|
|
const XML_Char* systemId,
|
|
|
|
const XML_Char* publicId);
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
void startElementNoNS(const char* qName, const char** atts);
|
|
|
|
void endElementNoNS(const char* qName);
|
|
|
|
void convertXML_Content(std::ostream& os, const XML_Content* model, bool isChild = false);
|
|
|
|
|
|
|
|
// member variables
|
|
|
|
entityResolverT* entityResolver_;
|
2002-11-23 21:09:25 +01:00
|
|
|
dtdHandlerT* dtdHandler_;
|
|
|
|
contentHandlerT* contentHandler_;
|
2003-09-10 10:47:37 +02:00
|
|
|
errorHandlerT* errorHandler_;
|
2002-06-21 13:16:28 +02:00
|
|
|
declHandlerT* declHandler_;
|
|
|
|
lexicalHandlerT* lexicalHandler_;
|
|
|
|
namespaceSupportT nsSupport_;
|
|
|
|
|
|
|
|
XML_Parser parser_;
|
|
|
|
stringT publicId_;
|
|
|
|
stringT systemId_;
|
|
|
|
bool parsing_;
|
|
|
|
|
|
|
|
// features
|
|
|
|
bool namespaces_;
|
|
|
|
bool prefixes_;
|
|
|
|
bool externalResolving_;
|
|
|
|
|
|
|
|
stringT emptyString_;
|
|
|
|
const SAX::FeatureNames<stringT, string_adaptorT> features_;
|
|
|
|
const SAX::PropertyNames<stringT, string_adaptorT> properties_;
|
|
|
|
const SAX::NamespaceConstants<stringT, string_adaptorT> nsc_;
|
|
|
|
const SAX::AttributeDefaults<stringT, string_adaptorT> attrDefaults_;
|
|
|
|
|
|
|
|
std::map<stringT, stringT> declaredExternalEnts_;
|
|
|
|
}; // class expat_wrapper
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// expat wrapper definition
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
expat_wrapper<stringT, T0, T1>::expat_wrapper() :
|
2004-09-14 22:51:36 +02:00
|
|
|
entityResolver_(0),
|
|
|
|
dtdHandler_(0),
|
|
|
|
contentHandler_(0),
|
|
|
|
errorHandler_(0),
|
2002-06-21 13:16:28 +02:00
|
|
|
declHandler_(0),
|
|
|
|
lexicalHandler_(0),
|
|
|
|
parser_(XML_ParserCreate(0)),
|
|
|
|
parsing_(false),
|
|
|
|
namespaces_(true),
|
|
|
|
prefixes_(true),
|
|
|
|
externalResolving_(false)
|
|
|
|
{
|
|
|
|
XML_SetUserData(parser_, reinterpret_cast<void*>(static_cast<expat_wrapper_impl_mumbojumbo::expat2base*>(this)));
|
|
|
|
XML_SetCharacterDataHandler(parser_, expat_wrapper_impl_mumbojumbo::ewim_charHandler);
|
|
|
|
XML_SetElementHandler(parser_, expat_wrapper_impl_mumbojumbo::ewim_startElement, expat_wrapper_impl_mumbojumbo::ewim_endElement);
|
|
|
|
XML_SetElementDeclHandler(parser_, expat_wrapper_impl_mumbojumbo::ewim_elementDeclaration);
|
|
|
|
XML_SetAttlistDeclHandler(parser_, expat_wrapper_impl_mumbojumbo::ewim_attListDeclaration);
|
|
|
|
XML_SetEntityDeclHandler(parser_, expat_wrapper_impl_mumbojumbo::ewim_entityDeclaration);
|
|
|
|
XML_SetNotationDeclHandler(parser_, expat_wrapper_impl_mumbojumbo::ewim_notationDeclaration);
|
|
|
|
XML_SetDoctypeDeclHandler(parser_, expat_wrapper_impl_mumbojumbo::ewim_startDoctypeDecl, expat_wrapper_impl_mumbojumbo::ewim_endDoctypeDecl);
|
|
|
|
XML_SetCdataSectionHandler(parser_, expat_wrapper_impl_mumbojumbo::ewim_startCdataSection, expat_wrapper_impl_mumbojumbo::ewim_endCdataSection);
|
|
|
|
XML_SetCommentHandler(parser_, expat_wrapper_impl_mumbojumbo::ewim_commentHandler);
|
|
|
|
XML_SetExternalEntityRefHandler(parser_, expat_wrapper_impl_mumbojumbo::ewim_externalEntityRefHandler);
|
|
|
|
XML_SetProcessingInstructionHandler(parser_, expat_wrapper_impl_mumbojumbo::ewim_processingInstruction);
|
|
|
|
} // expat
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
expat_wrapper<stringT, T0, T1>::~expat_wrapper()
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
XML_ParserFree(parser_);
|
|
|
|
} // ~expat
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::setFeature(const stringT& name, bool value)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(name == features_.namespaces)
|
|
|
|
{
|
2005-11-09 22:13:22 +01:00
|
|
|
checkNotParsing(SA::construct_from_utf8("feature"), name);
|
2002-06-21 13:16:28 +02:00
|
|
|
namespaces_ = value;
|
|
|
|
if(!namespaces_ && !prefixes_)
|
|
|
|
prefixes_ = true;
|
|
|
|
return;
|
|
|
|
} // namespaces
|
|
|
|
|
|
|
|
if(name == features_.namespace_prefixes)
|
|
|
|
{
|
2005-11-09 22:13:22 +01:00
|
|
|
checkNotParsing(SA::construct_from_utf8("feature"), name);
|
2002-06-21 13:16:28 +02:00
|
|
|
prefixes_ = value;
|
|
|
|
if(prefixes_ && !namespaces_)
|
|
|
|
namespaces_ = true;
|
|
|
|
return;
|
|
|
|
} // namespace prefixes
|
|
|
|
|
|
|
|
if(name == features_.external_general || name == features_.external_parameter)
|
|
|
|
{
|
2005-11-09 22:13:22 +01:00
|
|
|
checkNotParsing(SA::construct_from_utf8("feature"), name);
|
2002-06-21 13:16:28 +02:00
|
|
|
externalResolving_ = value;
|
|
|
|
return;
|
|
|
|
} // external entity resolution
|
|
|
|
|
2005-12-07 17:09:37 +01:00
|
|
|
#ifndef __BORLANDC__
|
|
|
|
// this is a hack features validation problem with BCB6 => need to be investigated
|
2002-06-21 13:16:28 +02:00
|
|
|
if(name == features_.validation)
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
2005-11-09 22:13:22 +01:00
|
|
|
os << "Feature not supported " << SA::asStdString(name);
|
2002-06-21 13:16:28 +02:00
|
|
|
throw SAX::SAXNotSupportedException(os.str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
2005-11-09 22:13:22 +01:00
|
|
|
os << "Feature not recognized " << SA::asStdString(name);
|
2002-06-21 13:16:28 +02:00
|
|
|
throw SAX::SAXNotRecognizedException(os.str());
|
|
|
|
}
|
2005-12-07 17:09:37 +01:00
|
|
|
#endif
|
2002-06-21 13:16:28 +02:00
|
|
|
} // setFeature
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
bool expat_wrapper<stringT, T0, T1>::getFeature(const stringT& name) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(name == features_.namespaces)
|
|
|
|
return namespaces_;
|
|
|
|
|
|
|
|
if(name == features_.namespace_prefixes)
|
|
|
|
return prefixes_;
|
|
|
|
|
|
|
|
if(name == features_.external_general || name == features_.external_parameter)
|
|
|
|
return externalResolving_;
|
|
|
|
|
|
|
|
if(name == features_.validation)
|
|
|
|
return false;
|
|
|
|
|
2005-11-09 22:13:22 +01:00
|
|
|
throw SAX::SAXNotRecognizedException(std::string("Feature not recognized ") + SA::asStdString(name));
|
2002-06-21 13:16:28 +02:00
|
|
|
} // getFeature
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::parse(inputSourceT& source)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
publicId_ = source.getPublicId();
|
|
|
|
systemId_ = source.getSystemId();
|
|
|
|
|
|
|
|
if(contentHandler_)
|
|
|
|
contentHandler_->setDocumentLocator(*this);
|
|
|
|
|
|
|
|
parsing_ = true;
|
|
|
|
|
|
|
|
if(contentHandler_)
|
2004-09-14 22:51:36 +02:00
|
|
|
contentHandler_->startDocument();
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
XML_SetParamEntityParsing(parser_, externalResolving_ ? XML_PARAM_ENTITY_PARSING_ALWAYS : XML_PARAM_ENTITY_PARSING_NEVER);
|
|
|
|
|
|
|
|
do_parse(source, parser_);
|
|
|
|
|
|
|
|
if(contentHandler_)
|
2004-09-14 22:51:36 +02:00
|
|
|
contentHandler_->endDocument();
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
parsing_ = false;
|
|
|
|
} // parse
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
bool expat_wrapper<stringT, T0, T1>::do_parse(inputSourceT& source, XML_Parser parser)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2005-11-09 22:13:22 +01:00
|
|
|
InputSourceResolver is(source, string_adaptorT());
|
2004-09-14 22:51:36 +02:00
|
|
|
if(is.resolve() == 0)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
reportError("Could not resolve XML document", true);
|
2004-09-14 22:51:36 +02:00
|
|
|
return false;
|
2002-06-21 13:16:28 +02:00
|
|
|
} // if(is.resolver() == 0)
|
|
|
|
|
|
|
|
const int BUFF_SIZE = 10*1024;
|
|
|
|
while(!is.resolve()->eof())
|
|
|
|
{
|
|
|
|
char* buffer = (char*)XML_GetBuffer(parser, BUFF_SIZE);
|
|
|
|
if(buffer == NULL)
|
|
|
|
{
|
|
|
|
reportError("Could not acquire expat buffer", true);
|
|
|
|
return false;
|
|
|
|
} // if ...
|
|
|
|
|
|
|
|
is.resolve()->read(buffer, BUFF_SIZE);
|
|
|
|
if(XML_ParseBuffer(parser, is.resolve()->gcount(), is.resolve()->eof()) == 0)
|
|
|
|
{
|
|
|
|
// error
|
|
|
|
reportError(XML_ErrorString(XML_GetErrorCode(parser_)), true);
|
|
|
|
return false;
|
|
|
|
} // if ...
|
|
|
|
} // while
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} // do_parse
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
2003-09-15 18:04:37 +02:00
|
|
|
#ifndef ARABICA_VS6_WORKAROUND
|
2005-12-07 11:51:00 +01:00
|
|
|
std::auto_ptr<typename expat_wrapper<stringT, T0, T1>::PropertyBaseT> expat_wrapper<stringT, T0, T1>::doGetProperty(const stringT& name)
|
2003-09-15 18:04:37 +02:00
|
|
|
#else
|
2005-12-07 11:51:00 +01:00
|
|
|
std::auto_ptr<expat_wrapper<stringT, T0, T1>::PropertyBaseT> expat_wrapper<stringT, T0, T1>::doGetProperty(const stringT& name)
|
2003-09-15 18:04:37 +02:00
|
|
|
#endif
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(name == properties_.lexicalHandler)
|
|
|
|
{
|
2004-09-14 22:51:36 +02:00
|
|
|
getLexicalHandlerT* prop = new getLexicalHandlerT(lexicalHandler_);
|
2002-09-20 12:16:35 +02:00
|
|
|
return std::auto_ptr<PropertyBaseT>(prop);
|
2002-06-21 13:16:28 +02:00
|
|
|
}
|
2004-09-14 22:51:36 +02:00
|
|
|
if(name == properties_.declHandler)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2004-09-14 22:51:36 +02:00
|
|
|
getDeclHandlerT* prop = new getDeclHandlerT(declHandler_);
|
2002-09-20 12:16:35 +02:00
|
|
|
return std::auto_ptr<PropertyBaseT>(prop);
|
2002-06-21 13:16:28 +02:00
|
|
|
}
|
2004-09-14 22:51:36 +02:00
|
|
|
|
2005-11-09 22:13:22 +01:00
|
|
|
throw SAX::SAXNotRecognizedException(std::string("Property not recognized ") + SA::asStdString(name));
|
2002-06-21 13:16:28 +02:00
|
|
|
} // doGetProperty
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::doSetProperty(const stringT& name, std::auto_ptr<PropertyBaseT> value)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(name == properties_.lexicalHandler)
|
|
|
|
{
|
2004-09-14 22:51:36 +02:00
|
|
|
setLexicalHandlerT* prop = dynamic_cast<setLexicalHandlerT*>(value.get());
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
if(!prop)
|
|
|
|
throw std::bad_cast();
|
|
|
|
|
|
|
|
lexicalHandler_ = &(prop->get());
|
|
|
|
}
|
|
|
|
else if(name == properties_.declHandler)
|
|
|
|
{
|
2004-09-14 22:51:36 +02:00
|
|
|
setDeclHandlerT* prop = dynamic_cast<setDeclHandlerT*>(value.get());
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
if(!prop)
|
|
|
|
throw std::bad_cast();
|
|
|
|
|
|
|
|
declHandler_ = &(prop->get());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
2005-11-09 22:13:22 +01:00
|
|
|
os << "Property not recognized " << SA::asStdString(name);
|
2002-06-21 13:16:28 +02:00
|
|
|
throw SAX::SAXNotRecognizedException(os.str());
|
|
|
|
}
|
|
|
|
} // doSetProperty
|
|
|
|
|
|
|
|
// Locator implementation
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
stringT expat_wrapper<stringT, T0, T1>::getPublicId() const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
return publicId_;
|
|
|
|
} // getPublicId
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
stringT expat_wrapper<stringT, T0, T1>::getSystemId() const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
return systemId_;
|
|
|
|
} // getSystemId
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
int expat_wrapper<stringT, T0, T1>::getLineNumber() const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
return XML_GetCurrentLineNumber(parser_);
|
|
|
|
} // getLineNumber
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
int expat_wrapper<stringT, T0, T1>::getColumnNumber() const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
return XML_GetCurrentColumnNumber(parser_);
|
|
|
|
} // getColumnNumber
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
typename SAX::basic_NamespaceSupport<stringT, typename expat_wrapper<stringT, T0, T1>::string_adaptorT>::Parts expat_wrapper<stringT, T0, T1>::processName(const stringT& qName, bool isAttribute)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2002-09-20 12:11:57 +02:00
|
|
|
typename namespaceSupportT::Parts p = nsSupport_.processName(qName, isAttribute);
|
2005-11-17 13:45:49 +01:00
|
|
|
if(SA::empty(p.URI) && !SA::empty(p.prefix))
|
2005-11-09 22:13:22 +01:00
|
|
|
reportError(std::string("Undeclared prefix ") + SA::asStdString(qName));
|
2002-06-21 13:16:28 +02:00
|
|
|
return p;
|
|
|
|
} // processName
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::reportError(const std::string& message, bool fatal)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(!errorHandler_)
|
|
|
|
return;
|
|
|
|
|
2003-09-10 10:47:37 +02:00
|
|
|
SAXParseExceptionT e(message,
|
|
|
|
publicId_,
|
2004-09-14 22:51:36 +02:00
|
|
|
systemId_,
|
|
|
|
XML_GetCurrentLineNumber(parser_),
|
|
|
|
XML_GetCurrentColumnNumber(parser_));
|
2002-06-21 13:16:28 +02:00
|
|
|
if(fatal)
|
|
|
|
errorHandler_->fatalError(e);
|
|
|
|
else
|
|
|
|
errorHandler_->error(e);
|
|
|
|
} // reportError
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::checkNotParsing(const stringT& type, const stringT& name) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(parsing_)
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
2005-11-09 22:13:22 +01:00
|
|
|
os << "Can't change " << SA::asStdString(type) << " " << SA::asStdString(name) << " while parsing";
|
2002-06-21 13:16:28 +02:00
|
|
|
throw SAX::SAXNotSupportedException(os.str());
|
|
|
|
} // if(parsing_)
|
|
|
|
} // checkNotParsing
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::charHandler(const char* txt, int txtlen)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(!contentHandler_)
|
|
|
|
return;
|
2005-11-09 22:13:22 +01:00
|
|
|
contentHandler_->characters(SA::construct_from_utf8(txt, txtlen));
|
2002-06-21 13:16:28 +02:00
|
|
|
} // charHandler
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::startElement(const char* qName, const char** atts)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(!contentHandler_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!namespaces_)
|
|
|
|
{
|
|
|
|
startElementNoNS(qName, atts);
|
|
|
|
return;
|
|
|
|
} // if(!namespaces)
|
|
|
|
|
|
|
|
// OK we're doing Namespaces
|
|
|
|
nsSupport_.pushContext();
|
|
|
|
SAX::basic_AttributesImpl<stringT> attributes;
|
|
|
|
|
|
|
|
// take a first pass and copy all the attributes, noting any declarations
|
|
|
|
if(atts && *atts != 0)
|
|
|
|
{
|
2005-11-17 16:45:59 +01:00
|
|
|
const char** a1 = atts;
|
|
|
|
while(*a1 != 0)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2005-11-17 16:45:59 +01:00
|
|
|
stringT attQName = SA::construct_from_utf8(*a1++);
|
|
|
|
stringT value = SA::construct_from_utf8(*a1++);
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
// declaration?
|
2005-11-17 13:45:49 +01:00
|
|
|
if(SA::find(attQName, nsc_.xmlns) == 0)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
stringT prefix;
|
2005-11-17 16:45:59 +01:00
|
|
|
typename SA::size_type n = SA::find(attQName, nsc_.colon);
|
2006-06-08 11:51:18 +02:00
|
|
|
if(n != SA::npos())
|
2005-11-17 13:45:49 +01:00
|
|
|
prefix = SA::construct(SA::begin(attQName) + n + 1, SA::end(attQName));
|
2002-06-21 13:16:28 +02:00
|
|
|
if(!nsSupport_.declarePrefix(prefix, value))
|
2005-11-09 22:13:22 +01:00
|
|
|
reportError(std::string("Illegal Namespace prefix ") + SA::asStdString(prefix));
|
2002-06-21 13:16:28 +02:00
|
|
|
contentHandler_->startPrefixMapping(prefix, value);
|
|
|
|
if(prefixes_)
|
|
|
|
attributes.addAttribute(emptyString_,
|
|
|
|
emptyString_,
|
|
|
|
attQName,
|
|
|
|
emptyString_,
|
|
|
|
value);
|
|
|
|
}
|
2005-11-17 16:45:59 +01:00
|
|
|
} // while
|
|
|
|
|
|
|
|
while(*atts != 0)
|
|
|
|
{
|
|
|
|
stringT attQName = SA::construct_from_utf8(*atts++);
|
|
|
|
stringT value = SA::construct_from_utf8(*atts++);
|
|
|
|
|
|
|
|
// declaration?
|
|
|
|
if(SA::find(attQName, nsc_.xmlns) != 0)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2002-09-20 12:11:57 +02:00
|
|
|
typename namespaceSupportT::Parts attName = processName(attQName, true);
|
2002-06-21 13:16:28 +02:00
|
|
|
attributes.addAttribute(attName.URI, attName.localName, attName.rawName, emptyString_, value);
|
|
|
|
}
|
|
|
|
} // while ...
|
|
|
|
} // if ...
|
|
|
|
|
|
|
|
// at last! report the event
|
2005-11-09 22:13:22 +01:00
|
|
|
typename namespaceSupportT::Parts name = processName(SA::construct_from_utf8(qName), false);
|
2002-06-21 13:16:28 +02:00
|
|
|
contentHandler_->startElement(name.URI, name.localName, name.rawName, attributes);
|
|
|
|
} // startElement
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::startElementNoNS(const char* qName, const char** atts)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
SAX::basic_AttributesImpl<stringT> attributes;
|
|
|
|
|
|
|
|
if(atts && *atts != 0)
|
|
|
|
{
|
|
|
|
while(*atts != 0)
|
|
|
|
{
|
2005-11-09 22:13:22 +01:00
|
|
|
stringT attQName = SA::construct_from_utf8(*atts++);
|
|
|
|
stringT value = SA::construct_from_utf8(*atts++);
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
attributes.addAttribute(emptyString_, emptyString_, attQName, emptyString_, value);
|
|
|
|
} // while ..
|
|
|
|
} // if ...
|
|
|
|
|
2005-11-09 22:13:22 +01:00
|
|
|
contentHandler_->startElement(emptyString_, emptyString_, SA::construct_from_utf8(qName), attributes);
|
2002-06-21 13:16:28 +02:00
|
|
|
} // startElementNoNS
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::endElement(const char* qName)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(!contentHandler_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!namespaces_)
|
|
|
|
{
|
|
|
|
endElementNoNS(qName);
|
|
|
|
return;
|
|
|
|
} // if(!namespaces_)
|
|
|
|
|
2005-11-09 22:13:22 +01:00
|
|
|
typename namespaceSupportT::Parts name = processName(SA::construct_from_utf8(qName), false);
|
2002-06-21 13:16:28 +02:00
|
|
|
contentHandler_->endElement(name.URI, name.localName, name.rawName);
|
2002-09-20 12:11:57 +02:00
|
|
|
typename namespaceSupportT::stringListT prefixes = nsSupport_.getDeclaredPrefixes();
|
2007-01-15 15:21:33 +01:00
|
|
|
for(size_t i = 0, end = prefixes.size(); i < end; ++i)
|
2002-06-21 13:16:28 +02:00
|
|
|
contentHandler_->endPrefixMapping(prefixes[i]);
|
|
|
|
nsSupport_.popContext();
|
|
|
|
} // endElement
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::endElementNoNS(const char* qName)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(contentHandler_)
|
2005-11-09 22:13:22 +01:00
|
|
|
contentHandler_->endElement(emptyString_, emptyString_, SA::construct_from_utf8(qName));
|
2002-06-21 13:16:28 +02:00
|
|
|
} // endElementNoNS
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::processingInstruction(const char* target, const char* data)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(contentHandler_)
|
2005-11-09 22:13:22 +01:00
|
|
|
contentHandler_->processingInstruction(SA::construct_from_utf8(target), SA::construct_from_utf8(data));
|
2002-06-21 13:16:28 +02:00
|
|
|
} // processingInstruction
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::elementDeclaration(const XML_Char* name,
|
2002-06-21 13:16:28 +02:00
|
|
|
const XML_Content* model)
|
|
|
|
{
|
|
|
|
if(!declHandler_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::ostringstream os;
|
|
|
|
convertXML_Content(os, model);
|
2005-11-09 22:13:22 +01:00
|
|
|
declHandler_->elementDecl(SA::construct_from_utf8(name), SA::construct_from_utf8(os.str().c_str()));
|
2002-06-21 13:16:28 +02:00
|
|
|
} // elementDeclaration
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::convertXML_Content(std::ostream& os, const XML_Content* model, bool isChild)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
enum XML_Content_Type {
|
|
|
|
XML_CTYPE_EMPTY = 1,
|
|
|
|
XML_CTYPE_ANY,
|
|
|
|
XML_CTYPE_MIXED,
|
|
|
|
XML_CTYPE_NAME,
|
|
|
|
XML_CTYPE_CHOICE,
|
|
|
|
XML_CTYPE_SEQ
|
|
|
|
};
|
|
|
|
|
|
|
|
enum XML_Content_Quant {
|
|
|
|
XML_CQUANT_NONE,
|
|
|
|
XML_CQUANT_OPT,
|
|
|
|
XML_CQUANT_REP,
|
|
|
|
XML_CQUANT_PLUS
|
|
|
|
};
|
|
|
|
|
|
|
|
If type == XML_CTYPE_EMPTY or XML_CTYPE_ANY, then quant will be
|
|
|
|
XML_CQUANT_NONE, and the other fields will be zero or NULL.
|
|
|
|
If type == XML_CTYPE_MIXED, then quant will be NONE or REP and
|
|
|
|
numchildren will contain number of elements that may be mixed in
|
|
|
|
and children point to an array of XML_Content cells that will be
|
|
|
|
all of XML_CTYPE_NAME type with no quantification.
|
|
|
|
|
|
|
|
If type == XML_CTYPE_NAME, then the name points to the name, and
|
|
|
|
the numchildren field will be zero and children will be NULL. The
|
|
|
|
quant fields indicates any quantifiers placed on the name.
|
|
|
|
|
|
|
|
CHOICE and SEQ will have name NULL, the number of children in
|
|
|
|
numchildren and children will point, recursively, to an array
|
|
|
|
of XML_Content cells.
|
|
|
|
|
|
|
|
The EMPTY, ANY, and MIXED types will only occur at top level.
|
|
|
|
|
|
|
|
typedef struct XML_cp XML_Content;
|
|
|
|
|
|
|
|
struct XML_cp {
|
2004-09-14 22:51:36 +02:00
|
|
|
enum XML_Content_Type type;
|
|
|
|
enum XML_Content_Quant quant;
|
|
|
|
const XML_Char * name;
|
|
|
|
unsigned int numchildren;
|
|
|
|
XML_Content * children;
|
2002-06-21 13:16:28 +02:00
|
|
|
};
|
|
|
|
*/
|
|
|
|
char concatenator = ' ';
|
|
|
|
|
|
|
|
switch(model->type)
|
|
|
|
{
|
|
|
|
case XML_CTYPE_EMPTY:
|
|
|
|
os << "EMPTY";
|
|
|
|
break;
|
|
|
|
case XML_CTYPE_ANY:
|
|
|
|
os << "ANY";
|
|
|
|
break;
|
|
|
|
case XML_CTYPE_MIXED:
|
|
|
|
if(model->numchildren == 0)
|
|
|
|
os << "(#PCDATA)";
|
|
|
|
else
|
|
|
|
os << "(#PCDATA";
|
|
|
|
concatenator = '|';
|
|
|
|
break;
|
|
|
|
case XML_CTYPE_NAME:
|
|
|
|
if(!isChild)
|
|
|
|
os << '(' << model->name << ')';
|
|
|
|
else
|
|
|
|
os << model->name;
|
|
|
|
break;
|
|
|
|
case XML_CTYPE_CHOICE:
|
|
|
|
concatenator = '|';
|
|
|
|
break;
|
|
|
|
case XML_CTYPE_SEQ:
|
|
|
|
concatenator = ',';
|
|
|
|
break;
|
|
|
|
} // switch
|
|
|
|
|
|
|
|
// do children here
|
|
|
|
if(model->numchildren)
|
|
|
|
{
|
|
|
|
if(model->type != XML_CTYPE_MIXED)
|
|
|
|
os << '(';
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < model->numchildren; ++i)
|
|
|
|
{
|
|
|
|
if(i != 0)
|
|
|
|
os << concatenator;
|
|
|
|
convertXML_Content(os, &(model->children[i]), true);
|
|
|
|
} // for ...
|
|
|
|
|
|
|
|
os << ')';
|
|
|
|
} // if ...
|
|
|
|
|
|
|
|
switch(model->quant)
|
|
|
|
{
|
|
|
|
case XML_CQUANT_NONE:
|
|
|
|
break;
|
|
|
|
case XML_CQUANT_OPT:
|
|
|
|
os << "?";
|
|
|
|
break;
|
|
|
|
case XML_CQUANT_REP:
|
|
|
|
os << "*";
|
|
|
|
break;
|
|
|
|
case XML_CQUANT_PLUS:
|
|
|
|
os << "+";
|
|
|
|
break;
|
|
|
|
} // switch
|
|
|
|
} // convertXML_Content
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::attListDeclaration(const XML_Char* elname,
|
2002-06-21 13:16:28 +02:00
|
|
|
const XML_Char* attname,
|
|
|
|
const XML_Char* att_type,
|
|
|
|
const XML_Char* dflt,
|
|
|
|
int isrequired)
|
|
|
|
{
|
|
|
|
/* The Attlist declaration handler is called for *each* attribute. So
|
|
|
|
a single Attlist declaration with multiple attributes declared will
|
|
|
|
generate multiple calls to this handler. The "default" parameter
|
|
|
|
may be NULL in the case of the "#IMPLIED" or "#REQUIRED" keyword.
|
|
|
|
The "isrequired" parameter will be true and the default value will
|
|
|
|
be NULL in the case of "#REQUIRED". If "isrequired" is true and
|
|
|
|
default is non-NULL, then this is a "#FIXED" default. */
|
|
|
|
if(declHandler_)
|
|
|
|
{
|
|
|
|
const stringT* defType = &attrDefaults_.implied;
|
|
|
|
if(isrequired)
|
|
|
|
defType = dflt ? &attrDefaults_.fixed : &attrDefaults_.required;
|
2005-11-09 22:13:22 +01:00
|
|
|
declHandler_->attributeDecl(SA::construct_from_utf8(elname),
|
|
|
|
SA::construct_from_utf8(attname),
|
|
|
|
SA::construct_from_utf8(att_type),
|
2002-06-21 13:16:28 +02:00
|
|
|
*defType,
|
2005-11-09 22:13:22 +01:00
|
|
|
SA::construct_from_utf8(dflt));
|
2002-06-21 13:16:28 +02:00
|
|
|
}
|
|
|
|
} // attListDeclaration
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::entityDeclaration(const XML_Char* entityName,
|
2004-09-14 22:51:36 +02:00
|
|
|
int is_parameter_entity,
|
|
|
|
const XML_Char* value,
|
|
|
|
int value_length,
|
|
|
|
const XML_Char* base,
|
|
|
|
const XML_Char* systemId,
|
|
|
|
const XML_Char* publicId,
|
|
|
|
const XML_Char* notationName)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
/* For internal entities (<!ENTITY foo "bar">), value will
|
|
|
|
be non-null and systemId, publicID, and notationName will be null.
|
|
|
|
The value string is NOT null terminated; the length is provided in
|
|
|
|
the value_length argument. Since it is legal to have zero-length
|
|
|
|
values, do not use this argument to test for internal entities.
|
|
|
|
|
|
|
|
For external entities, value will be null and systemId will be non-null.
|
|
|
|
The publicId argument will be null unless a public identifier was
|
|
|
|
provided. The notationName argument will have a non-null value only
|
|
|
|
for unparsed entity declarations. */
|
|
|
|
|
2005-11-09 22:13:22 +01:00
|
|
|
const stringT s_entityName(SA::construct_from_utf8(entityName));
|
2002-06-21 13:16:28 +02:00
|
|
|
if(!systemId && !publicId && !notationName)
|
|
|
|
{
|
|
|
|
// internal entity!
|
|
|
|
if(declHandler_)
|
2005-11-09 22:13:22 +01:00
|
|
|
declHandler_->internalEntityDecl(s_entityName, SA::construct_from_utf8(value, value_length));
|
2002-06-21 13:16:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-11-09 22:13:22 +01:00
|
|
|
const stringT s_publicId(SA::construct_from_utf8(publicId));
|
|
|
|
const stringT s_systemId(SA::construct_from_utf8(systemId));
|
2002-06-21 13:16:28 +02:00
|
|
|
if(notationName == 0)
|
|
|
|
{
|
|
|
|
if(declHandler_)
|
|
|
|
declHandler_->externalEntityDecl(s_entityName, s_publicId, s_systemId);
|
|
|
|
declaredExternalEnts_.insert(std::make_pair(s_publicId, s_entityName));
|
|
|
|
declaredExternalEnts_.insert(std::make_pair(s_systemId, s_entityName));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(dtdHandler_)
|
2005-11-09 22:13:22 +01:00
|
|
|
dtdHandler_->unparsedEntityDecl(s_entityName, s_publicId, s_systemId, SA::construct_from_utf8(notationName));
|
2002-06-21 13:16:28 +02:00
|
|
|
}
|
|
|
|
} // entityDeclaration
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::notationDeclaration(const XML_Char* notationName,
|
2004-09-14 22:51:36 +02:00
|
|
|
const XML_Char* base,
|
|
|
|
const XML_Char* systemId,
|
|
|
|
const XML_Char* publicId)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(!dtdHandler_)
|
|
|
|
return;
|
2005-11-09 22:13:22 +01:00
|
|
|
dtdHandler_->notationDecl(SA::construct_from_utf8(notationName),
|
|
|
|
SA::construct_from_utf8(publicId),
|
|
|
|
SA::construct_from_utf8(systemId));
|
2002-06-21 13:16:28 +02:00
|
|
|
} // notationDeclaration
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::startDoctypeDecl(const XML_Char *doctypeName,
|
2004-09-14 22:51:36 +02:00
|
|
|
const XML_Char *systemId,
|
|
|
|
const XML_Char *publicId,
|
|
|
|
int has_internal_subset)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(!lexicalHandler_)
|
|
|
|
return;
|
|
|
|
|
2005-11-09 22:13:22 +01:00
|
|
|
stringT s_publicId = SA::construct_from_utf8(publicId);
|
|
|
|
stringT s_systemId = SA::construct_from_utf8(systemId);
|
|
|
|
stringT dtd = SA::construct_from_utf8("[dtd]");
|
2002-06-21 13:16:28 +02:00
|
|
|
declaredExternalEnts_.insert(std::make_pair(s_publicId, dtd));
|
|
|
|
declaredExternalEnts_.insert(std::make_pair(s_systemId, dtd));
|
2005-11-09 22:13:22 +01:00
|
|
|
lexicalHandler_->startDTD(SA::construct_from_utf8(doctypeName),
|
2002-06-21 13:16:28 +02:00
|
|
|
s_publicId,
|
|
|
|
s_systemId);
|
|
|
|
} // startDoctypeDecl
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::endDoctypeDecl()
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(lexicalHandler_)
|
|
|
|
lexicalHandler_->endDTD();
|
|
|
|
} // endDoctypeDecl
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::startCdataSection()
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(lexicalHandler_)
|
|
|
|
lexicalHandler_->startCDATA();
|
|
|
|
} // startCdataSection
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::endCdataSection()
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(lexicalHandler_)
|
|
|
|
lexicalHandler_->endCDATA();
|
|
|
|
} // endCdataSection
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
void expat_wrapper<stringT, T0, T1>::commentHandler(const XML_Char *data)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(lexicalHandler_)
|
2005-11-09 22:13:22 +01:00
|
|
|
lexicalHandler_->comment(SA::construct_from_utf8(data));
|
2002-06-21 13:16:28 +02:00
|
|
|
} // commentHandler
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
template<class stringT, class T0, class T1>
|
|
|
|
int expat_wrapper<stringT, T0, T1>::externalEntityRefHandler(XML_Parser parser,
|
2002-06-21 13:16:28 +02:00
|
|
|
const XML_Char* context,
|
|
|
|
const XML_Char* base,
|
|
|
|
const XML_Char* systemId,
|
|
|
|
const XML_Char* publicId)
|
|
|
|
{
|
2005-11-09 22:13:22 +01:00
|
|
|
stringT pubId(SA::construct_from_utf8(publicId));
|
|
|
|
stringT sysId(SA::construct_from_utf8(systemId));
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
stringT entityName;
|
|
|
|
if(systemId)
|
|
|
|
entityName = declaredExternalEnts_[sysId];
|
|
|
|
else if(publicId)
|
|
|
|
entityName = declaredExternalEnts_[pubId];
|
|
|
|
|
|
|
|
if(!externalResolving_)
|
|
|
|
{
|
2005-11-17 13:45:49 +01:00
|
|
|
if(!SA::empty(entityName) && contentHandler_)
|
2002-06-21 13:16:28 +02:00
|
|
|
contentHandler_->skippedEntity(entityName);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
} // if ...
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
// resolve external entity
|
2005-11-17 13:45:49 +01:00
|
|
|
if(!SA::empty(entityName) && lexicalHandler_)
|
2002-06-21 13:16:28 +02:00
|
|
|
lexicalHandler_->startEntity(entityName);
|
|
|
|
|
|
|
|
inputSourceT source;
|
|
|
|
if(entityResolver_)
|
|
|
|
{
|
|
|
|
source = entityResolver_->resolveEntity(pubId, sysId);
|
2005-11-17 13:45:49 +01:00
|
|
|
if(SA::empty(source.getPublicId()) && SA::empty(source.getSystemId()))
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
source.setPublicId(pubId);
|
|
|
|
source.setSystemId(sysId);
|
|
|
|
} // if ...
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
source.setPublicId(pubId);
|
|
|
|
source.setSystemId(sysId);
|
|
|
|
} // if ...
|
|
|
|
|
|
|
|
XML_Parser externalParser = XML_ExternalEntityParserCreate(parser, context, 0);
|
|
|
|
|
|
|
|
bool ok = do_parse(source, externalParser);
|
|
|
|
|
|
|
|
XML_ParserFree(externalParser);
|
|
|
|
|
2005-11-17 13:45:49 +01:00
|
|
|
if(!SA::empty(entityName) && lexicalHandler_)
|
2002-06-21 13:16:28 +02:00
|
|
|
lexicalHandler_->endEntity(entityName);
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
} // externalEntityRefHandler
|
|
|
|
|
|
|
|
}; // namespace SAX
|
|
|
|
|
|
|
|
#endif
|
|
|
|
// end of file
|