2003-09-11 12:26:53 +02:00
|
|
|
#ifndef ARABICA_SAX_MSXML2_H
|
|
|
|
#define ARABICA_SAX_MSXML2_H
|
2002-06-21 13:16:28 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// A SAX2 wrapper class for MSXML component.
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-09-05 00:55:47 +02:00
|
|
|
#include <SAX/ArabicaConfig.hpp>
|
|
|
|
#include <SAX/XMLReader.hpp>
|
|
|
|
#include <SAX/InputSource.hpp>
|
|
|
|
#include <SAX/SAXParseException.hpp>
|
|
|
|
#include <SAX/SAXNotRecognizedException.hpp>
|
|
|
|
#include <SAX/SAXNotSupportedException.hpp>
|
|
|
|
#include <SAX/helpers/PropertyNames.hpp>
|
|
|
|
#include <Utils/StringAdaptor.hpp>
|
2002-09-03 15:19:14 +02:00
|
|
|
#include <iostream>
|
2005-12-07 11:51:00 +01:00
|
|
|
#include <Utils/getparam.hpp>
|
2002-09-03 15:19:14 +02:00
|
|
|
|
|
|
|
// Include the MSXML definitions.
|
|
|
|
#include <msxml2.h>
|
2002-06-21 13:16:28 +02:00
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
//
|
|
|
|
// Declare the 'smart pointer' type to simplify COM handling.
|
|
|
|
#include <comdef.h>
|
|
|
|
_COM_SMARTPTR_TYPEDEF(ISAXXMLReader, __uuidof(ISAXXMLReader));
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
namespace SAX
|
|
|
|
{
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
struct COMInitializer_tag { };
|
|
|
|
|
2002-06-21 13:16:28 +02:00
|
|
|
/**
|
|
|
|
* use this as COMInitializer_type if you call
|
|
|
|
* CoInitialize/CoInitializeEx in your own code
|
|
|
|
*/
|
2005-12-07 11:51:00 +01:00
|
|
|
class COMExternalInitializer : public COMInitializer_tag
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
COMExternalInitializer() { }
|
|
|
|
~COMExternalInitializer() { }
|
|
|
|
}; // COMExternalInitializer
|
|
|
|
|
2005-12-07 11:51:00 +01:00
|
|
|
class COMSingleThreadInitializer : public COMInitializer_tag
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
public:
|
2006-09-20 15:57:50 +02:00
|
|
|
COMSingleThreadInitializer() { ::CoInitialize(NULL); }
|
2002-06-21 13:16:28 +02:00
|
|
|
~COMSingleThreadInitializer() { ::CoUninitialize(); }
|
|
|
|
}; // COMSingleThreadInitializer
|
|
|
|
|
|
|
|
#if(_WIN32_WINNT >= 0x0400 ) || defined(_WIN32_DCOM)
|
2005-12-07 11:51:00 +01:00
|
|
|
class COMMultiThreadInitializer : public COMInitializer_tag
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
COMMultiThreadInitializer() { ::CoInitializeEx(NULL, COINIT_MULTITHREADED); }
|
|
|
|
~COMMultiThreadInitializer() { ::CoUninitialize(); }
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template<class string_type,
|
2005-12-07 11:51:00 +01:00
|
|
|
class T0 = Arabica::nil_t,
|
|
|
|
class T1 = Arabica::nil_t>
|
2002-06-21 13:16:28 +02:00
|
|
|
class msxml2_wrapper : public SAX::basic_XMLReader<string_type>
|
|
|
|
{
|
2006-09-20 15:57:50 +02:00
|
|
|
typedef typename Arabica::get_param<Arabica::string_adaptor_tag,
|
|
|
|
Arabica::default_string_adaptor<string_type>,
|
|
|
|
T0,
|
|
|
|
T1>::type string_adaptor_type;
|
|
|
|
typedef typename Arabica::get_param<COMInitializer_tag,
|
|
|
|
COMSingleThreadInitializer,
|
2006-10-06 23:25:55 +02:00
|
|
|
T1,
|
|
|
|
T0>::type COMInitializer_type;
|
2005-12-07 11:51:00 +01:00
|
|
|
|
2005-11-17 23:06:12 +01:00
|
|
|
public:
|
2006-09-20 15:57:50 +02:00
|
|
|
typedef SAX::basic_EntityResolver<string_type> entityResolverT;
|
|
|
|
typedef SAX::basic_DTDHandler<string_type> dtdHandlerT;
|
|
|
|
typedef SAX::basic_ContentHandler<string_type> contentHandlerT;
|
|
|
|
typedef SAX::basic_DeclHandler<string_type> declHandlerT;
|
|
|
|
typedef SAX::basic_LexicalHandler<string_type> lexicalHandlerT;
|
|
|
|
typedef SAX::basic_InputSource<string_type> inputSourceT;
|
|
|
|
typedef SAX::basic_Locator<string_type> locatorT;
|
|
|
|
typedef SAX::basic_ErrorHandler<string_type> errorHandlerT;
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
msxml2_wrapper();
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual ~msxml2_wrapper();
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////
|
|
|
|
// Configuration
|
2006-09-20 15:57:50 +02:00
|
|
|
virtual bool getFeature(const string_type& name) const;
|
|
|
|
virtual void setFeature(const string_type& name, bool value);
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////
|
|
|
|
// Event Handlers
|
|
|
|
/* MSXML does not use EntityResolver currently */
|
2006-09-20 15:57:50 +02:00
|
|
|
virtual void setEntityResolver(SAX::basic_EntityResolver<string_type>& resolver) { }
|
|
|
|
virtual SAX::basic_EntityResolver<string_type>* getEntityResolver() const { return 0; }
|
|
|
|
virtual void setDTDHandler(SAX::basic_DTDHandler<string_type>& handler) { dtdHandler_.setDTDHandler(handler); }
|
|
|
|
virtual SAX::basic_DTDHandler<string_type>* getDTDHandler() const { return dtdHandler_.getDTDHandler(); }
|
|
|
|
virtual void setContentHandler(SAX::basic_ContentHandler<string_type>& handler) { contentHandler_.setContentHandler(handler); }
|
2007-01-15 15:02:52 +01:00
|
|
|
virtual SAX::basic_ContentHandler<string_type>* getContentHandler() const { return contentHandler_.getContentHandler(); }
|
2006-09-20 15:57:50 +02:00
|
|
|
virtual void setErrorHandler(SAX::basic_ErrorHandler<string_type>& handler);
|
|
|
|
virtual SAX::basic_ErrorHandler<string_type>* getErrorHandler() const;
|
2007-01-15 15:21:33 +01:00
|
|
|
virtual void setDeclHandler(declHandlerT& handler) { declHandler_.setDeclHandler(handler); }
|
|
|
|
virtual declHandlerT* getDeclHandler() const { return declHandler_.getDeclHandler(); }
|
|
|
|
virtual void setLexicalHandler(lexicalHandlerT& handler) { lexicalHandler_.setLexicalHandler(handler); }
|
|
|
|
virtual lexicalHandlerT* getLexicalHandler() const { return lexicalHandler_.getLexicalHandler(); }
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
// Parsing
|
2006-09-20 15:57:50 +02:00
|
|
|
virtual void parse(SAX::basic_InputSource<string_type>& input);
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
protected:
|
2007-08-10 12:35:17 +02:00
|
|
|
virtual std::auto_ptr<typename SAX::basic_XMLReader<string_type>::PropertyBase> doGetProperty(const string_type& name)
|
|
|
|
{
|
|
|
|
if(name == properties_.lexicalHandler)
|
|
|
|
{
|
|
|
|
Property<SAX::basic_LexicalHandler<string_type>*>* prop = new Property<SAX::basic_LexicalHandler<string_type>*>(lexicalHandler_.getLexicalHandler());
|
|
|
|
return std::auto_ptr<SAX::basic_XMLReader<string_type>::PropertyBase>(prop);
|
|
|
|
}
|
|
|
|
if(name == properties_.declHandler)
|
|
|
|
{
|
|
|
|
Property<SAX::basic_DeclHandler<string_type>*>* prop = new Property<SAX::basic_DeclHandler<string_type>*>(declHandler_.getDeclHandler());
|
|
|
|
return std::auto_ptr<SAX::basic_XMLReader<string_type>::PropertyBase>(prop);
|
|
|
|
}
|
|
|
|
throw SAX::SAXNotRecognizedException("Property not recognized ");
|
|
|
|
} // doGetProperty
|
|
|
|
|
|
|
|
virtual void doSetProperty(const string_type& name, std::auto_ptr<typename SAX::basic_XMLReader<string_type>::PropertyBase> value)
|
|
|
|
{
|
|
|
|
if(name == properties_.lexicalHandler)
|
|
|
|
{
|
|
|
|
Property<SAX::basic_LexicalHandler<string_type>&>* prop = dynamic_cast<Property<SAX::basic_LexicalHandler<string_type>&>*>(value.get());
|
|
|
|
|
|
|
|
if(!prop)
|
|
|
|
throw std::runtime_error("bad_cast: Property LexicalHandler is wrong type, should be SAX::LexicalHandler&");
|
|
|
|
|
|
|
|
lexicalHandler_.setLexicalHandler(prop->get());
|
|
|
|
return;
|
|
|
|
} // if ...
|
|
|
|
if(name == properties_.declHandler)
|
|
|
|
{
|
|
|
|
Property<SAX::basic_DeclHandler<string_type>&>* prop = dynamic_cast<Property<SAX::basic_DeclHandler<string_type>&>*>(value.get());
|
|
|
|
|
|
|
|
if(!prop)
|
|
|
|
throw std::runtime_error("bad_cast: Property DeclHandler is wrong type, should be SAX::DeclHandler&");
|
|
|
|
|
|
|
|
declHandler_.setDeclHandler(prop->get());
|
|
|
|
return;
|
|
|
|
} // if ...
|
|
|
|
throw SAX::SAXNotRecognizedException("Property not recognized ");
|
|
|
|
} // doSetProperty
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
// COM interface -> C++ interface adaptors
|
2006-09-20 15:57:50 +02:00
|
|
|
class LocatorAdaptor : public SAX::basic_Locator<string_type>
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
LocatorAdaptor() : locator_(0) { }
|
2002-09-03 15:19:14 +02:00
|
|
|
LocatorAdaptor(ISAXLocator __RPC_FAR* locator) : locator_(0) { setLocator(locator); }
|
2002-06-21 13:16:28 +02:00
|
|
|
~LocatorAdaptor() { setLocator(0); }
|
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
string_type getPublicId() const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(!locator_)
|
2006-09-20 15:57:50 +02:00
|
|
|
return string_type();
|
2002-06-21 13:16:28 +02:00
|
|
|
|
2006-10-06 23:17:17 +02:00
|
|
|
const wchar_t* pwchPublicId;
|
2002-09-03 15:19:14 +02:00
|
|
|
locator_->getPublicId(&pwchPublicId);
|
2006-09-20 15:57:50 +02:00
|
|
|
string_type publicId(string_adaptor_type::construct_from_utf16(pwchPublicId));
|
2002-06-21 13:16:28 +02:00
|
|
|
return publicId;
|
|
|
|
} // getPublicId
|
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
string_type getSystemId() const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(!locator_)
|
2006-09-20 15:57:50 +02:00
|
|
|
return string_type();
|
2002-06-21 13:16:28 +02:00
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
const wchar_t* pwchSystemId;
|
|
|
|
locator_->getSystemId(&pwchSystemId);
|
2006-09-20 15:57:50 +02:00
|
|
|
string_type systemId(string_adaptor_type::construct_from_utf16(pwchSystemId));
|
2002-06-21 13:16:28 +02:00
|
|
|
return systemId;
|
|
|
|
} // getSystemId
|
|
|
|
|
|
|
|
int getLineNumber() const
|
|
|
|
{
|
|
|
|
if(!locator_)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
int lineNumber;
|
2002-09-03 15:19:14 +02:00
|
|
|
locator_->getLineNumber(&lineNumber);
|
2002-06-21 13:16:28 +02:00
|
|
|
return lineNumber;
|
|
|
|
} // getLineNumber
|
|
|
|
|
2006-10-06 23:17:17 +02:00
|
|
|
int getColumnNumber() const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(!locator_)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
int columnNumber;
|
2002-09-03 15:19:14 +02:00
|
|
|
locator_->getColumnNumber(&columnNumber);
|
2002-06-21 13:16:28 +02:00
|
|
|
return columnNumber;
|
|
|
|
} // getColumnNumber
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
void setLocator(ISAXLocator __RPC_FAR* locator)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2002-09-03 15:19:14 +02:00
|
|
|
locator_ = locator;
|
|
|
|
// no need to AddRef or Release as the ISAXLocator points to the MSXML instance
|
|
|
|
// and we'll clean that up properly anyway
|
2002-06-21 13:16:28 +02:00
|
|
|
} // setLocator
|
|
|
|
|
|
|
|
private:
|
2002-09-03 15:19:14 +02:00
|
|
|
ISAXLocator __RPC_FAR *locator_;
|
2002-06-21 13:16:28 +02:00
|
|
|
}; // class LocatorAdaptor
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
class DTDHandlerAdaptor : public ISAXDTDHandler
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
DTDHandlerAdaptor() : dtdHandler_(0) { }
|
|
|
|
~DTDHandlerAdaptor() { }
|
|
|
|
|
2006-10-06 23:17:17 +02:00
|
|
|
void setDTDHandler(SAX::basic_DTDHandler<string_type>& handler) { dtdHandler_ = &handler; }
|
2006-09-20 15:57:50 +02:00
|
|
|
SAX::basic_DTDHandler<string_type>* getDTDHandler() const { return dtdHandler_; }
|
2002-06-21 13:16:28 +02:00
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE notationDecl(
|
|
|
|
/* [in] */ const wchar_t *pwchName,
|
|
|
|
/* [in] */ int cchName,
|
|
|
|
/* [in] */ const wchar_t *pwchPublicId,
|
|
|
|
/* [in] */ int cchPublicId,
|
|
|
|
/* [in] */ const wchar_t *pwchSystemId,
|
|
|
|
/* [in] */ int cchSystemId)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(dtdHandler_)
|
2006-09-20 15:57:50 +02:00
|
|
|
dtdHandler_->notationDecl(string_adaptor_type::construct_from_utf16(pwchName, cchName),
|
|
|
|
string_adaptor_type::construct_from_utf16(pwchPublicId, cchPublicId),
|
|
|
|
string_adaptor_type::construct_from_utf16(pwchSystemId, cchSystemId));
|
2002-06-21 13:16:28 +02:00
|
|
|
return S_OK;
|
|
|
|
} // notationDecl
|
2002-09-03 15:19:14 +02:00
|
|
|
|
|
|
|
virtual HRESULT STDMETHODCALLTYPE unparsedEntityDecl(
|
|
|
|
/* [in] */ const wchar_t *pwchName,
|
|
|
|
/* [in] */ int cchName,
|
|
|
|
/* [in] */ const wchar_t *pwchPublicId,
|
|
|
|
/* [in] */ int cchPublicId,
|
|
|
|
/* [in] */ const wchar_t *pwchSystemId,
|
|
|
|
/* [in] */ int cchSystemId,
|
|
|
|
/* [in] */ const wchar_t *pwchNotationName,
|
|
|
|
/* [in] */ int cchNotationName)
|
2006-10-06 23:17:17 +02:00
|
|
|
{
|
2002-06-21 13:16:28 +02:00
|
|
|
if(dtdHandler_)
|
2006-09-20 15:57:50 +02:00
|
|
|
dtdHandler_->unparsedEntityDecl(string_adaptor_type::construct_from_utf16(pwchName, cchName),
|
|
|
|
string_adaptor_type::construct_from_utf16(pwchPublicId, cchPublicId),
|
|
|
|
string_adaptor_type::construct_from_utf16(pwchSystemId, cchSystemId),
|
|
|
|
string_adaptor_type::construct_from_utf16(pwchNotationName, cchNotationName));
|
2002-06-21 13:16:28 +02:00
|
|
|
return S_OK;
|
|
|
|
} // unparsedEntityDecl
|
|
|
|
|
|
|
|
// satify COM interface even if we're not a COM object
|
|
|
|
long __stdcall QueryInterface(const struct _GUID &riid,void **ppvObject) { return 0; }
|
|
|
|
unsigned long __stdcall AddRef() { return 0; }
|
|
|
|
unsigned long __stdcall Release() { return 0; }
|
|
|
|
|
|
|
|
private:
|
2006-09-20 15:57:50 +02:00
|
|
|
SAX::basic_DTDHandler<string_type>* dtdHandler_;
|
2002-06-21 13:16:28 +02:00
|
|
|
}; // class DTDHandlerAdaptor
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
class ContentHandlerAdaptor : public ISAXContentHandler
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
ContentHandlerAdaptor() : contentHandler_(0) { }
|
|
|
|
~ContentHandlerAdaptor() { }
|
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
void setContentHandler(SAX::basic_ContentHandler<string_type>& handler) { contentHandler_ = &handler; }
|
|
|
|
SAX::basic_ContentHandler<string_type>* getContentHandler() const { return contentHandler_; }
|
2002-06-21 13:16:28 +02:00
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
|
|
|
|
virtual HRESULT STDMETHODCALLTYPE putDocumentLocator(
|
|
|
|
/* [in] */ ISAXLocator *pLocator)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
locator_.setLocator(pLocator);
|
|
|
|
if(contentHandler_) contentHandler_->setDocumentLocator(locator_);
|
|
|
|
return S_OK;
|
|
|
|
} // putDocumentLocation
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE startDocument( void)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(contentHandler_) contentHandler_->startDocument();
|
|
|
|
return S_OK;
|
|
|
|
} // startDocument
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE endDocument( void)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(contentHandler_) contentHandler_->endDocument();
|
|
|
|
return S_OK;
|
|
|
|
} // endDocument
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE startPrefixMapping(
|
|
|
|
/* [in] */ const wchar_t *pwchPrefix,
|
|
|
|
/* [in] */ int cchPrefix,
|
|
|
|
/* [in] */ const wchar_t *pwchUri,
|
|
|
|
/* [in] */ int cchUri)
|
2002-06-21 13:16:28 +02:00
|
|
|
|
2006-10-06 23:17:17 +02:00
|
|
|
{
|
2002-06-21 13:16:28 +02:00
|
|
|
if(contentHandler_)
|
2006-09-20 15:57:50 +02:00
|
|
|
contentHandler_->startPrefixMapping(string_adaptor_type::construct_from_utf16(pwchPrefix, cchPrefix),
|
|
|
|
string_adaptor_type::construct_from_utf16(pwchUri, cchUri));
|
2002-06-21 13:16:28 +02:00
|
|
|
return S_OK;
|
|
|
|
} // startPrefixMapping
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE endPrefixMapping(
|
|
|
|
/* [in] */ const wchar_t *pwchPrefix,
|
|
|
|
/* [in] */ int cchPrefix)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2006-09-20 15:57:50 +02:00
|
|
|
if(contentHandler_) contentHandler_->endPrefixMapping(string_adaptor_type::construct_from_utf16(pwchPrefix, cchPrefix));
|
2002-06-21 13:16:28 +02:00
|
|
|
return S_OK;
|
|
|
|
} // endPrefixMapping
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE startElement(
|
|
|
|
/* [in] */ const wchar_t *pwchNamespaceUri,
|
|
|
|
/* [in] */ int cchNamespaceUri,
|
|
|
|
/* [in] */ const wchar_t *pwchLocalName,
|
|
|
|
/* [in] */ int cchLocalName,
|
|
|
|
/* [in] */ const wchar_t *pwchQName,
|
|
|
|
/* [in] */ int cchQName,
|
|
|
|
/* [in] */ ISAXAttributes *pAttributes)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(contentHandler_)
|
|
|
|
{
|
|
|
|
AttributesAdaptor attrs(pAttributes);
|
2006-10-06 23:17:17 +02:00
|
|
|
contentHandler_->startElement(string_adaptor_type::construct_from_utf16(pwchNamespaceUri, cchNamespaceUri),
|
2006-09-20 15:57:50 +02:00
|
|
|
string_adaptor_type::construct_from_utf16(pwchLocalName, cchLocalName),
|
|
|
|
string_adaptor_type::construct_from_utf16(pwchQName, cchQName),
|
2002-06-21 13:16:28 +02:00
|
|
|
attrs);
|
|
|
|
} // if ...
|
|
|
|
return S_OK;
|
|
|
|
} // startElement
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE endElement(
|
|
|
|
/* [in] */ const wchar_t *pwchNamespaceUri,
|
|
|
|
/* [in] */ int cchNamespaceUri,
|
|
|
|
/* [in] */ const wchar_t *pwchLocalName,
|
|
|
|
/* [in] */ int cchLocalName,
|
|
|
|
/* [in] */ const wchar_t *pwchQName,
|
|
|
|
/* [in] */ int cchQName)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(contentHandler_)
|
2006-09-20 15:57:50 +02:00
|
|
|
contentHandler_->endElement(string_adaptor_type::construct_from_utf16(pwchNamespaceUri, cchNamespaceUri),
|
|
|
|
string_adaptor_type::construct_from_utf16(pwchLocalName, cchLocalName),
|
|
|
|
string_adaptor_type::construct_from_utf16(pwchQName, cchQName));
|
2002-06-21 13:16:28 +02:00
|
|
|
return S_OK;
|
|
|
|
} // endElement
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE characters(
|
|
|
|
/* [in] */ const wchar_t *pwchChars,
|
|
|
|
/* [in] */ int cchChars)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2006-10-06 23:17:17 +02:00
|
|
|
if(contentHandler_) contentHandler_->characters(string_adaptor_type::construct_from_utf16(pwchChars, cchChars));
|
2002-06-21 13:16:28 +02:00
|
|
|
return S_OK;
|
|
|
|
} // characters
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE ignorableWhitespace(
|
|
|
|
/* [in] */ const wchar_t *pwchChars,
|
|
|
|
/* [in] */ int cchChars)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(contentHandler_)
|
2006-09-20 15:57:50 +02:00
|
|
|
contentHandler_->ignorableWhitespace(string_adaptor_type::construct_from_utf16(pwchChars, cchChars));
|
2002-06-21 13:16:28 +02:00
|
|
|
return S_OK;
|
|
|
|
} // ignorableWhitespace
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE processingInstruction(
|
|
|
|
/* [in] */ const wchar_t *pwchTarget,
|
|
|
|
/* [in] */ int cchTarget,
|
|
|
|
/* [in] */ const wchar_t *pwchData,
|
|
|
|
/* [in] */ int cchData)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(contentHandler_)
|
2006-09-20 15:57:50 +02:00
|
|
|
contentHandler_->processingInstruction(string_adaptor_type::construct_from_utf16(pwchTarget, cchTarget),
|
|
|
|
string_adaptor_type::construct_from_utf16(pwchData, cchData));
|
2002-06-21 13:16:28 +02:00
|
|
|
return S_OK;
|
|
|
|
} // processingInstruction
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE skippedEntity(
|
|
|
|
/* [in] */ const wchar_t *pwchName,
|
2006-10-06 23:17:17 +02:00
|
|
|
/* [in] */ int cchName)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(contentHandler_)
|
2006-09-20 15:57:50 +02:00
|
|
|
contentHandler_->skippedEntity(string_adaptor_type::construct_from_utf16(pwchName, cchName));
|
2002-06-21 13:16:28 +02:00
|
|
|
return S_OK;
|
|
|
|
} // skippedEntity
|
|
|
|
|
|
|
|
// satisfy COM interface even if we're not a COM object
|
|
|
|
long __stdcall QueryInterface(const struct _GUID &riid,void **ppvObject) { return 0; }
|
|
|
|
unsigned long __stdcall AddRef() { return 0; }
|
|
|
|
unsigned long __stdcall Release() { return 0; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
// member varaibles
|
2006-09-20 15:57:50 +02:00
|
|
|
SAX::basic_ContentHandler<string_type>* contentHandler_;
|
2002-06-21 13:16:28 +02:00
|
|
|
LocatorAdaptor locator_;
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
// COM interface -> C++ interface adaptors
|
2006-09-20 15:57:50 +02:00
|
|
|
class AttributesAdaptor : public SAX::basic_Attributes<string_type>
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
public:
|
2002-09-03 15:19:14 +02:00
|
|
|
AttributesAdaptor(ISAXAttributes __RPC_FAR *pAttributes) : attributes_(pAttributes) { }
|
2002-06-21 13:16:28 +02:00
|
|
|
~AttributesAdaptor() { }
|
|
|
|
|
|
|
|
/////////////////////////
|
2006-10-06 23:17:17 +02:00
|
|
|
// indexed access
|
2002-06-21 13:16:28 +02:00
|
|
|
virtual int getLength() const
|
|
|
|
{
|
|
|
|
int length;
|
2002-09-03 15:19:14 +02:00
|
|
|
attributes_->getLength(&length);
|
2002-06-21 13:16:28 +02:00
|
|
|
return length;
|
|
|
|
} // getLength
|
2002-09-03 15:19:14 +02:00
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
virtual string_type getURI(unsigned int index) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2002-09-03 15:19:14 +02:00
|
|
|
const wchar_t* pwchUri;
|
2002-06-21 13:16:28 +02:00
|
|
|
int cchUri;
|
2002-09-03 15:19:14 +02:00
|
|
|
HRESULT hr = attributes_->getURI(index, &pwchUri, &cchUri);
|
2002-06-21 13:16:28 +02:00
|
|
|
if (FAILED(hr))
|
2006-09-20 15:57:50 +02:00
|
|
|
return string_type();
|
|
|
|
string_type uri(string_adaptor_type::construct_from_utf16(pwchUri, cchUri));
|
2002-06-21 13:16:28 +02:00
|
|
|
return uri;
|
|
|
|
} // getURI
|
2002-09-03 15:19:14 +02:00
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
virtual string_type getLocalName(unsigned int index) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2002-09-03 15:19:14 +02:00
|
|
|
const wchar_t* pwchLocalName;
|
2002-06-21 13:16:28 +02:00
|
|
|
int cchLocalName;
|
2002-09-03 15:19:14 +02:00
|
|
|
HRESULT hr = attributes_->getLocalName(index, &pwchLocalName, &cchLocalName);
|
2002-06-21 13:16:28 +02:00
|
|
|
if (FAILED(hr))
|
2006-09-20 15:57:50 +02:00
|
|
|
return string_type();
|
|
|
|
string_type localName(string_adaptor_type::construct_from_utf16(pwchLocalName, cchLocalName));
|
2006-10-06 23:17:17 +02:00
|
|
|
return localName;
|
2002-06-21 13:16:28 +02:00
|
|
|
} // getLocalName
|
2002-09-03 15:19:14 +02:00
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
virtual string_type getQName(unsigned int index) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2002-09-03 15:19:14 +02:00
|
|
|
const wchar_t* pwchQName;
|
2002-06-21 13:16:28 +02:00
|
|
|
int cchQName;
|
2002-09-03 15:19:14 +02:00
|
|
|
HRESULT hr = attributes_->getQName(index, &pwchQName, &cchQName);
|
2002-06-21 13:16:28 +02:00
|
|
|
if (FAILED(hr))
|
2006-09-20 15:57:50 +02:00
|
|
|
return string_type();
|
|
|
|
string_type qName(string_adaptor_type::construct_from_utf16(pwchQName, cchQName));
|
2002-06-21 13:16:28 +02:00
|
|
|
return qName;
|
|
|
|
} // getQName
|
2002-09-03 15:19:14 +02:00
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
virtual string_type getType(unsigned int index) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2002-09-03 15:19:14 +02:00
|
|
|
const wchar_t* pwchType;
|
2002-06-21 13:16:28 +02:00
|
|
|
int cchType;
|
2002-09-03 15:19:14 +02:00
|
|
|
HRESULT hr = attributes_->getType(index, &pwchType, &cchType);
|
2002-06-21 13:16:28 +02:00
|
|
|
if (FAILED(hr))
|
2006-09-20 15:57:50 +02:00
|
|
|
return string_type();
|
|
|
|
string_type type(string_adaptor_type::construct_from_utf16(pwchType, cchType));
|
2002-06-21 13:16:28 +02:00
|
|
|
return type;
|
|
|
|
} // getType
|
2002-09-03 15:19:14 +02:00
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
virtual string_type getValue(unsigned int index) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2006-10-06 23:17:17 +02:00
|
|
|
const wchar_t* pwchValue;
|
2002-06-21 13:16:28 +02:00
|
|
|
int cchValue;
|
2002-09-03 15:19:14 +02:00
|
|
|
HRESULT hr = attributes_->getValue(index, &pwchValue, &cchValue);
|
2002-06-21 13:16:28 +02:00
|
|
|
if (FAILED(hr))
|
2006-09-20 15:57:50 +02:00
|
|
|
return string_type();
|
|
|
|
string_type value(string_adaptor_type::construct_from_utf16(pwchValue, cchValue));
|
2002-06-21 13:16:28 +02:00
|
|
|
return value;
|
|
|
|
} // getValue
|
|
|
|
|
|
|
|
/////////////////////////
|
|
|
|
// name based query
|
2006-09-20 15:57:50 +02:00
|
|
|
virtual int getIndex(const string_type& uri, const string_type& localName) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
int index = -1;
|
2006-09-20 15:57:50 +02:00
|
|
|
std::wstring wUri(string_adaptor_type::asStdWString(uri));
|
|
|
|
std::wstring wLocalName(string_adaptor_type::asStdWString(localName));
|
2003-08-27 16:28:01 +02:00
|
|
|
HRESULT hr = attributes_->getIndexFromName(wUri.data(), static_cast<int>(wUri.length()),
|
|
|
|
wLocalName.data(), static_cast<int>(wLocalName.length()),
|
2002-06-21 13:16:28 +02:00
|
|
|
&index);
|
|
|
|
return index;
|
|
|
|
} // getIndex
|
2002-09-03 15:19:14 +02:00
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
virtual int getIndex(const string_type& qName) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
int index = -1;
|
2006-09-20 15:57:50 +02:00
|
|
|
std::wstring wQName(string_adaptor_type::asStdWString(qName));
|
2003-08-27 16:28:01 +02:00
|
|
|
attributes_->getIndexFromQName(wQName.data(), static_cast<int>(wQName.length()), &index);
|
2006-10-06 23:17:17 +02:00
|
|
|
return index;
|
2002-06-21 13:16:28 +02:00
|
|
|
} // getIndex
|
2002-09-03 15:19:14 +02:00
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
virtual string_type getType(const string_type& uri, const string_type& localName) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2002-09-03 15:19:14 +02:00
|
|
|
const wchar_t* pwchType;
|
2002-06-21 13:16:28 +02:00
|
|
|
int cchType;
|
2006-09-20 15:57:50 +02:00
|
|
|
std::wstring wUri(string_adaptor_type::asStdWString(uri));
|
|
|
|
std::wstring wLocalName(string_adaptor_type::asStdWString(localName));
|
2003-08-27 16:28:01 +02:00
|
|
|
HRESULT hr = attributes_->getTypeFromName(wUri.data(), static_cast<int>(wUri.length()),
|
|
|
|
wLocalName.data(), static_cast<int>(wLocalName.length()),
|
2002-06-21 13:16:28 +02:00
|
|
|
&pwchType, &cchType);
|
|
|
|
if (FAILED(hr))
|
2006-09-20 15:57:50 +02:00
|
|
|
return string_type();
|
|
|
|
string_type type(string_adaptor_type::construct_from_utf16(pwchType, cchType));
|
2002-06-21 13:16:28 +02:00
|
|
|
return type;
|
|
|
|
} // getType
|
2002-09-03 15:19:14 +02:00
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
virtual string_type getType(const string_type& qName) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2002-09-03 15:19:14 +02:00
|
|
|
const wchar_t* pwchType;
|
2002-06-21 13:16:28 +02:00
|
|
|
int cchType;
|
2006-09-20 15:57:50 +02:00
|
|
|
std::wstring wQName(string_adaptor_type::asStdWString(qName));
|
2003-08-27 16:28:01 +02:00
|
|
|
HRESULT hr = attributes_->getTypeFromQName(wQName.data(), static_cast<int>(wQName.length()),
|
2002-06-21 13:16:28 +02:00
|
|
|
&pwchType, &cchType);
|
|
|
|
if (FAILED(hr))
|
2006-09-20 15:57:50 +02:00
|
|
|
return string_type();
|
2006-10-06 23:17:17 +02:00
|
|
|
string_type type(string_adaptor_type::construct_from_utf16(pwchType, cchType));
|
2002-06-21 13:16:28 +02:00
|
|
|
return type;
|
|
|
|
} // getType
|
2002-09-03 15:19:14 +02:00
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
virtual string_type getValue(const string_type& uri, const string_type& localName) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2002-09-03 15:19:14 +02:00
|
|
|
const wchar_t* pwchValue;
|
2002-06-21 13:16:28 +02:00
|
|
|
int cchValue;
|
2006-09-20 15:57:50 +02:00
|
|
|
std::wstring wUri(string_adaptor_type::asStdWString(uri));
|
|
|
|
std::wstring wLocalName(string_adaptor_type::asStdWString(localName));
|
2003-08-27 16:28:01 +02:00
|
|
|
HRESULT hr = attributes_->getValueFromName(wUri.data(), static_cast<int>(wUri.length()),
|
|
|
|
wLocalName.data(), static_cast<int>(wLocalName.length()),
|
2002-06-21 13:16:28 +02:00
|
|
|
&pwchValue, &cchValue);
|
|
|
|
if (FAILED(hr))
|
2006-09-20 15:57:50 +02:00
|
|
|
return string_type();
|
|
|
|
string_type value(string_adaptor_type::construct_from_utf16(pwchValue, cchValue));
|
2002-06-21 13:16:28 +02:00
|
|
|
return value;
|
|
|
|
} // getValue
|
2002-09-03 15:19:14 +02:00
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
virtual string_type getValue(const string_type& qname) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2002-09-03 15:19:14 +02:00
|
|
|
const wchar_t* pwchValue;
|
2002-06-21 13:16:28 +02:00
|
|
|
int cchValue;
|
2006-09-20 15:57:50 +02:00
|
|
|
std::wstring wQName(string_adaptor_type::asStdWString(qname));
|
2003-08-27 16:28:01 +02:00
|
|
|
HRESULT hr = attributes_->getValueFromQName(wQName.data(), static_cast<int>(wQName.length()),
|
2002-06-21 13:16:28 +02:00
|
|
|
&pwchValue, &cchValue);
|
|
|
|
if (FAILED(hr))
|
2006-10-06 23:17:17 +02:00
|
|
|
return string_type();
|
2006-09-20 15:57:50 +02:00
|
|
|
string_type value(string_adaptor_type::construct_from_utf16(pwchValue, cchValue));
|
2002-06-21 13:16:28 +02:00
|
|
|
return value;
|
|
|
|
} // getValue
|
|
|
|
|
|
|
|
private:
|
2002-09-03 15:19:14 +02:00
|
|
|
ISAXAttributes __RPC_FAR *attributes_;
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
AttributesAdaptor();
|
|
|
|
}; // class AttributesAdaptor
|
|
|
|
}; // class ContentHandlerAdaptor
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
class ErrorHandlerAdaptor : public ISAXErrorHandler
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
public:
|
2004-02-24 10:52:18 +01:00
|
|
|
ErrorHandlerAdaptor() : errorHandler_(0),
|
|
|
|
bWarning_(false), bError_(false), bFatal_(false),
|
|
|
|
eWarning_("none"), eError_("none"), eFatal_("none")
|
|
|
|
{ }
|
2002-06-21 13:16:28 +02:00
|
|
|
virtual ~ErrorHandlerAdaptor() { }
|
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
void setErrorHandler(SAX::basic_ErrorHandler<string_type>& handler) { errorHandler_ = &handler; }
|
|
|
|
SAX::basic_ErrorHandler<string_type>* getErrorHandler() const { return errorHandler_; }
|
2002-06-21 13:16:28 +02:00
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE error(
|
|
|
|
/* [in] */ ISAXLocator *pLocator,
|
|
|
|
/* [in] */ const wchar_t *pwchErrorMessage,
|
2006-10-06 23:17:17 +02:00
|
|
|
/* [in] */ HRESULT hrErrorCode)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2004-02-24 10:52:18 +01:00
|
|
|
bError_ = true;
|
2006-09-20 15:57:50 +02:00
|
|
|
string_type errorMsg(string_adaptor_type::construct_from_utf16(pwchErrorMessage));
|
|
|
|
eError_ = SAXParseExceptionT(string_adaptor_type::asStdString(errorMsg), LocatorAdaptor(pLocator));
|
2002-06-21 13:16:28 +02:00
|
|
|
return S_OK;
|
|
|
|
} // error
|
2002-09-03 15:19:14 +02:00
|
|
|
|
|
|
|
virtual HRESULT STDMETHODCALLTYPE fatalError(
|
|
|
|
/* [in] */ ISAXLocator *pLocator,
|
|
|
|
/* [in] */ const wchar_t *pwchErrorMessage,
|
|
|
|
/* [in] */ HRESULT hrErrorCode)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2004-02-24 10:52:18 +01:00
|
|
|
bFatal_ = true;
|
2006-09-20 15:57:50 +02:00
|
|
|
string_type errorMsg(string_adaptor_type::construct_from_utf16(pwchErrorMessage));
|
|
|
|
eFatal_ = SAXParseExceptionT(string_adaptor_type::asStdString(errorMsg), LocatorAdaptor(pLocator));
|
2004-02-24 10:52:18 +01:00
|
|
|
return S_FALSE;
|
2002-06-21 13:16:28 +02:00
|
|
|
} // fatalError
|
2002-09-03 15:19:14 +02:00
|
|
|
|
|
|
|
virtual HRESULT STDMETHODCALLTYPE ignorableWarning(
|
|
|
|
/* [in] */ ISAXLocator *pLocator,
|
|
|
|
/* [in] */ const wchar_t *pwchErrorMessage,
|
|
|
|
/* [in] */ HRESULT hrErrorCode)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2004-02-24 10:52:18 +01:00
|
|
|
bWarning_ = true;
|
2006-09-20 15:57:50 +02:00
|
|
|
string_type errorMsg(string_adaptor_type::construct_from_utf16(pwchErrorMessage));
|
|
|
|
eWarning_ = SAXParseExceptionT(string_adaptor_type::asStdString(errorMsg), LocatorAdaptor(pLocator));
|
2006-10-06 23:17:17 +02:00
|
|
|
return S_OK;
|
2002-06-21 13:16:28 +02:00
|
|
|
} // ignorableWarning
|
|
|
|
|
2004-02-24 10:52:18 +01:00
|
|
|
void report()
|
|
|
|
{
|
|
|
|
if(!errorHandler_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool bWarning = bWarning_;
|
|
|
|
bool bError = bError_;
|
|
|
|
bool bFatal = bFatal_;
|
|
|
|
|
|
|
|
bWarning_ = bError_ = bFatal_ = false;
|
|
|
|
|
|
|
|
if(bFatal)
|
|
|
|
errorHandler_->fatalError(eFatal_);
|
|
|
|
if(bError)
|
|
|
|
errorHandler_->error(eError_);
|
|
|
|
if(bWarning)
|
|
|
|
errorHandler_->warning(eWarning_);
|
|
|
|
} // report
|
|
|
|
|
2002-06-21 13:16:28 +02:00
|
|
|
// satisfy COM interface even if we're not a COM object
|
|
|
|
long __stdcall QueryInterface(const struct _GUID &riid,void **ppvObject) { return 0; }
|
|
|
|
unsigned long __stdcall AddRef() { return 0; }
|
|
|
|
unsigned long __stdcall Release() { return 0; }
|
|
|
|
|
2006-10-06 23:17:17 +02:00
|
|
|
private:
|
2006-09-20 15:57:50 +02:00
|
|
|
typedef SAX::basic_SAXParseException<string_type> SAXParseExceptionT;
|
2004-02-24 10:52:18 +01:00
|
|
|
bool bWarning_;
|
|
|
|
bool bError_;
|
|
|
|
bool bFatal_;
|
|
|
|
SAXParseExceptionT eWarning_;
|
|
|
|
SAXParseExceptionT eError_;
|
|
|
|
SAXParseExceptionT eFatal_;
|
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
SAX::basic_ErrorHandler<string_type>* errorHandler_;
|
2002-06-21 13:16:28 +02:00
|
|
|
}; // class ErrorHandlerAdaptor
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
class LexicalHandlerAdaptor : public ISAXLexicalHandler
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
LexicalHandlerAdaptor() : lexicalHandler_(0) { }
|
|
|
|
virtual ~LexicalHandlerAdaptor() { }
|
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
void setLexicalHandler(SAX::basic_LexicalHandler<string_type>& handler) { lexicalHandler_ = &handler; }
|
|
|
|
SAX::basic_LexicalHandler<string_type>* getLexicalHandler() const { return lexicalHandler_; }
|
2002-06-21 13:16:28 +02:00
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE startDTD(
|
|
|
|
/* [in] */ const wchar_t *pwchName,
|
|
|
|
/* [in] */ int cchName,
|
|
|
|
/* [in] */ const wchar_t *pwchPublicId,
|
|
|
|
/* [in] */ int cchPublicId,
|
|
|
|
/* [in] */ const wchar_t *pwchSystemId,
|
2006-10-06 23:17:17 +02:00
|
|
|
/* [in] */ int cchSystemId)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(lexicalHandler_)
|
2006-09-20 15:57:50 +02:00
|
|
|
lexicalHandler_->startDTD(string_adaptor_type::construct_from_utf16(pwchName, cchName),
|
|
|
|
string_adaptor_type::construct_from_utf16(pwchPublicId, cchPublicId),
|
|
|
|
string_adaptor_type::construct_from_utf16(pwchSystemId, cchSystemId));
|
2002-06-21 13:16:28 +02:00
|
|
|
return S_OK;
|
|
|
|
} // startDTD
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE endDTD( void)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(lexicalHandler_)
|
|
|
|
lexicalHandler_->endDTD();
|
|
|
|
return S_OK;
|
|
|
|
} // endDTD
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE startEntity(
|
|
|
|
/* [in] */ const wchar_t *pwchName,
|
|
|
|
/* [in] */ int cchName)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(lexicalHandler_)
|
2006-09-20 15:57:50 +02:00
|
|
|
lexicalHandler_->startEntity(string_adaptor_type::construct_from_utf16(pwchName, cchName));
|
2002-06-21 13:16:28 +02:00
|
|
|
return S_OK;
|
|
|
|
} // startEntity
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE endEntity(
|
|
|
|
/* [in] */ const wchar_t *pwchName,
|
2006-10-06 23:17:17 +02:00
|
|
|
/* [in] */ int cchName)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(lexicalHandler_)
|
2006-09-20 15:57:50 +02:00
|
|
|
lexicalHandler_->endEntity(string_adaptor_type::construct_from_utf16(pwchName, cchName));
|
2002-06-21 13:16:28 +02:00
|
|
|
return S_OK;
|
|
|
|
} // endEntity
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT __stdcall startCDATA()
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(lexicalHandler_)
|
|
|
|
lexicalHandler_->startCDATA();
|
|
|
|
return S_OK;
|
|
|
|
} // startCDATA
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT __stdcall endCDATA()
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(lexicalHandler_)
|
|
|
|
lexicalHandler_->endCDATA();
|
|
|
|
return S_OK;
|
|
|
|
} // endCDATA
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE comment(
|
|
|
|
/* [in] */ const wchar_t *pwchChars,
|
|
|
|
/* [in] */ int cchChars)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(lexicalHandler_)
|
2006-09-20 15:57:50 +02:00
|
|
|
lexicalHandler_->comment(string_adaptor_type::construct_from_utf16(pwchChars, cchChars));
|
2006-10-06 23:17:17 +02:00
|
|
|
return S_OK;
|
2002-06-21 13:16:28 +02:00
|
|
|
} // comment
|
|
|
|
|
|
|
|
// satisfy COM interface even if we're not a COM object
|
|
|
|
long __stdcall QueryInterface(const struct _GUID &riid,void **ppvObject)
|
|
|
|
{
|
|
|
|
// we have to implement this, because we pass this as an IUnknown but it needs an
|
|
|
|
// ISAXLexicalHandler interface
|
2002-09-03 15:19:14 +02:00
|
|
|
if(riid == __uuidof(ISAXLexicalHandler))
|
2002-06-21 13:16:28 +02:00
|
|
|
*ppvObject = this;
|
|
|
|
return 0;
|
|
|
|
} // QueryInterface
|
|
|
|
unsigned long __stdcall AddRef() { return 0; }
|
|
|
|
unsigned long __stdcall Release() { return 0; }
|
|
|
|
|
|
|
|
private:
|
2006-09-20 15:57:50 +02:00
|
|
|
SAX::basic_LexicalHandler<string_type>* lexicalHandler_;
|
2002-06-21 13:16:28 +02:00
|
|
|
}; // class LexicalHandlerAdaptor
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
class DeclHandlerAdaptor : public ISAXDeclHandler
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
DeclHandlerAdaptor() : declHandler_(0) { }
|
|
|
|
virtual ~DeclHandlerAdaptor() { }
|
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
void setDeclHandler(SAX::basic_DeclHandler<string_type>& handler) { declHandler_ = &handler; }
|
|
|
|
SAX::basic_DeclHandler<string_type>* getDeclHandler() const { return declHandler_; }
|
2002-06-21 13:16:28 +02:00
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE elementDecl(
|
|
|
|
/* [in] */ const wchar_t *pwchName,
|
|
|
|
/* [in] */ int cchName,
|
|
|
|
/* [in] */ const wchar_t *pwchModel,
|
|
|
|
/* [in] */ int cchModel)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(declHandler_)
|
2006-09-20 15:57:50 +02:00
|
|
|
declHandler_->elementDecl(string_adaptor_type::construct_from_utf16(pwchName, cchName),
|
|
|
|
string_adaptor_type::construct_from_utf16(pwchModel, cchModel));
|
2002-06-21 13:16:28 +02:00
|
|
|
return S_OK;
|
|
|
|
} // elementDecl
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE attributeDecl(
|
|
|
|
/* [in] */ const wchar_t *pwchElementName,
|
|
|
|
/* [in] */ int cchElementName,
|
|
|
|
/* [in] */ const wchar_t *pwchAttributeName,
|
|
|
|
/* [in] */ int cchAttributeName,
|
|
|
|
/* [in] */ const wchar_t *pwchType,
|
|
|
|
/* [in] */ int cchType,
|
|
|
|
/* [in] */ const wchar_t *pwchValueDefault,
|
|
|
|
/* [in] */ int cchValueDefault,
|
|
|
|
/* [in] */ const wchar_t *pwchValue,
|
|
|
|
/* [in] */ int cchValue)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(declHandler_)
|
2006-09-20 15:57:50 +02:00
|
|
|
declHandler_->attributeDecl(string_adaptor_type::construct_from_utf16(pwchElementName, cchElementName),
|
2006-10-06 23:17:17 +02:00
|
|
|
string_adaptor_type::construct_from_utf16(pwchAttributeName, cchAttributeName),
|
2006-09-20 15:57:50 +02:00
|
|
|
string_adaptor_type::construct_from_utf16(pwchType, cchType),
|
|
|
|
string_adaptor_type::construct_from_utf16(pwchValueDefault, cchValueDefault),
|
|
|
|
string_adaptor_type::construct_from_utf16(pwchValue, cchValue));
|
2002-06-21 13:16:28 +02:00
|
|
|
return S_OK;
|
|
|
|
} // attributeDecl
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE internalEntityDecl(
|
|
|
|
/* [in] */ const wchar_t *pwchName,
|
|
|
|
/* [in] */ int cchName,
|
|
|
|
/* [in] */ const wchar_t *pwchValue,
|
|
|
|
/* [in] */ int cchValue)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(declHandler_)
|
2006-09-20 15:57:50 +02:00
|
|
|
declHandler_->internalEntityDecl(string_adaptor_type::construct_from_utf16(pwchName, cchName),
|
|
|
|
string_adaptor_type::construct_from_utf16(pwchValue, cchValue));
|
2002-06-21 13:16:28 +02:00
|
|
|
return S_OK;
|
|
|
|
} // internalEntityDecl
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE externalEntityDecl(
|
|
|
|
/* [in] */ const wchar_t *pwchName,
|
|
|
|
/* [in] */ int cchName,
|
|
|
|
/* [in] */ const wchar_t *pwchPublicId,
|
|
|
|
/* [in] */ int cchPublicId,
|
|
|
|
/* [in] */ const wchar_t *pwchSystemId,
|
|
|
|
/* [in] */ int cchSystemId)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2006-10-06 23:17:17 +02:00
|
|
|
if(declHandler_)
|
2006-09-20 15:57:50 +02:00
|
|
|
declHandler_->externalEntityDecl(string_adaptor_type::construct_from_utf16(pwchName, cchName),
|
|
|
|
string_adaptor_type::construct_from_utf16(pwchPublicId, cchPublicId),
|
|
|
|
string_adaptor_type::construct_from_utf16(pwchSystemId, cchSystemId));
|
2002-06-21 13:16:28 +02:00
|
|
|
return S_OK;
|
|
|
|
} // externalEntityDecl
|
|
|
|
|
|
|
|
// satisfy COM interface even if we're not a COM object
|
|
|
|
long __stdcall QueryInterface(const struct _GUID &riid,void **ppvObject)
|
|
|
|
{
|
|
|
|
// we have to implement this, because we pass this as an IUnknown but it needs an
|
|
|
|
// ISAXDeclHandler interface
|
2002-09-03 15:19:14 +02:00
|
|
|
if(riid == __uuidof(ISAXDeclHandler))
|
2002-06-21 13:16:28 +02:00
|
|
|
*ppvObject = reinterpret_cast<void*>(this);
|
|
|
|
return 0;
|
|
|
|
} // QueryInterface
|
|
|
|
|
|
|
|
unsigned long __stdcall AddRef() { return 0; }
|
|
|
|
unsigned long __stdcall Release() { return 0; }
|
|
|
|
|
|
|
|
private:
|
2006-09-20 15:57:50 +02:00
|
|
|
SAX::basic_DeclHandler<string_type>* declHandler_;
|
2002-06-21 13:16:28 +02:00
|
|
|
}; // class DeclHandlerAdaptor
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
class StreamAdaptor : public ISequentialStream
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
public:
|
2006-10-06 23:17:17 +02:00
|
|
|
StreamAdaptor(SAX::basic_InputSource<string_type>& source) :
|
2002-06-21 13:16:28 +02:00
|
|
|
source_(source)
|
|
|
|
{
|
|
|
|
} // StreamAdaptor
|
|
|
|
|
|
|
|
virtual HRESULT __stdcall Read(void* pv, ULONG cb, ULONG* pcbRead)
|
|
|
|
{
|
|
|
|
source_.getByteStream()->read(reinterpret_cast<char*>(pv), cb);
|
|
|
|
*pcbRead = source_.getByteStream()->gcount();
|
|
|
|
return S_OK;
|
|
|
|
} // Read
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
virtual HRESULT __stdcall Write(const void __RPC_FAR *pv, ULONG cb, ULONG __RPC_FAR *pcbWritten)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
return S_FALSE;
|
|
|
|
} // Write
|
|
|
|
|
|
|
|
// satisfy COM interface even if we're not a COM object
|
|
|
|
long __stdcall QueryInterface(const struct _GUID &riid,void **ppvObject)
|
|
|
|
{
|
|
|
|
// we have to implement this, because we pass this as an IUnknown but it needs an
|
|
|
|
// IStream interface
|
2002-09-03 15:19:14 +02:00
|
|
|
if(riid == __uuidof(ISequentialStream))
|
|
|
|
{
|
2002-06-21 13:16:28 +02:00
|
|
|
*ppvObject = reinterpret_cast<void*>(this);
|
2002-09-03 15:19:14 +02:00
|
|
|
return S_OK;
|
|
|
|
} // if ...
|
2006-10-06 23:17:17 +02:00
|
|
|
return E_NOINTERFACE;
|
2002-06-21 13:16:28 +02:00
|
|
|
} // QueryInterface
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
unsigned long __stdcall AddRef() { return 1; }
|
|
|
|
unsigned long __stdcall Release() { return 1; }
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
private:
|
2006-09-20 15:57:50 +02:00
|
|
|
SAX::basic_InputSource<string_type>& source_;
|
2002-06-21 13:16:28 +02:00
|
|
|
}; // StreamAdaptor
|
|
|
|
|
2002-09-03 15:19:14 +02:00
|
|
|
|
2002-06-21 13:16:28 +02:00
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
// member variables
|
|
|
|
COMInitializer_type init;
|
|
|
|
DTDHandlerAdaptor dtdHandler_;
|
|
|
|
ContentHandlerAdaptor contentHandler_;
|
|
|
|
ErrorHandlerAdaptor errorHandler_;
|
|
|
|
LexicalHandlerAdaptor lexicalHandler_;
|
|
|
|
DeclHandlerAdaptor declHandler_;
|
2002-09-03 15:19:14 +02:00
|
|
|
|
|
|
|
ISAXXMLReaderPtr reader_;
|
2006-09-20 15:57:50 +02:00
|
|
|
SAX::PropertyNames<string_type, string_adaptor_type> properties_;
|
2002-06-21 13:16:28 +02:00
|
|
|
}; // class msxml
|
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
template<class string_type, class T0, class T1>
|
|
|
|
msxml2_wrapper<string_type, T0, T1>::msxml2_wrapper()
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2007-01-15 15:02:52 +01:00
|
|
|
reader_.CreateInstance("Msxml2.SAXXMLReader.6.0");
|
|
|
|
if(reader_.GetInterfacePtr() == 0)
|
|
|
|
reader_.CreateInstance("Msxml2.SAXXMLReader.5.0");
|
|
|
|
if(reader_.GetInterfacePtr() == 0)
|
|
|
|
reader_.CreateInstance("Msxml2.SAXXMLReader.4.0");
|
2005-08-15 11:05:56 +02:00
|
|
|
if(reader_.GetInterfacePtr() == 0)
|
|
|
|
reader_.CreateInstance("Msxml2.SAXXMLReader.3.0");
|
|
|
|
if(reader_.GetInterfacePtr() == 0)
|
2002-09-03 15:19:14 +02:00
|
|
|
reader_.CreateInstance(__uuidof(ISAXXMLReader));
|
2005-08-15 11:05:56 +02:00
|
|
|
|
|
|
|
if(reader_.GetInterfacePtr() == 0)
|
|
|
|
throw SAXException("MSXML SAX Reader (pre-4.0) could not be instantiated");
|
|
|
|
|
2002-06-21 13:16:28 +02:00
|
|
|
reader_->putContentHandler(&contentHandler_);
|
|
|
|
reader_->putErrorHandler(&errorHandler_);
|
|
|
|
reader_->putDTDHandler(&dtdHandler_);
|
|
|
|
|
|
|
|
VARIANT wrapper;
|
|
|
|
wrapper.vt = VT_UNKNOWN;
|
2002-09-03 15:19:14 +02:00
|
|
|
wrapper.punkVal = static_cast<ISAXLexicalHandler*>(&lexicalHandler_);
|
2002-06-21 13:16:28 +02:00
|
|
|
reader_->putProperty(L"http://xml.org/sax/properties/lexical-handler", wrapper);
|
2002-09-03 15:19:14 +02:00
|
|
|
wrapper.punkVal = static_cast<ISAXDeclHandler*>(&declHandler_);
|
2002-06-21 13:16:28 +02:00
|
|
|
reader_->putProperty(L"http://xml.org/sax/properties/declaration-handler", wrapper);
|
|
|
|
} // msxml2_wrapper
|
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
template<class string_type, class T0, class T1>
|
|
|
|
msxml2_wrapper<string_type, T0, T1>::~msxml2_wrapper()
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2005-08-15 11:05:56 +02:00
|
|
|
if(reader_.GetInterfacePtr())
|
|
|
|
reader_.Release();
|
2002-09-03 15:19:14 +02:00
|
|
|
} // ~msxml2_wrapper
|
2002-06-21 13:16:28 +02:00
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
template<class string_type, class T0, class T1>
|
|
|
|
bool msxml2_wrapper<string_type, T0, T1>::getFeature(const string_type& name) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
VARIANT_BOOL feature;
|
2006-09-20 15:57:50 +02:00
|
|
|
std::wstring wName(string_adaptor_type::asStdWString(name));
|
2003-08-27 16:28:01 +02:00
|
|
|
reader_->getFeature(wName.c_str(), &feature);
|
2002-06-21 13:16:28 +02:00
|
|
|
return (feature == VARIANT_TRUE) ? true : false;
|
2002-09-03 15:19:14 +02:00
|
|
|
} // msxml2_wrapper::getFeature
|
2002-06-21 13:16:28 +02:00
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
template<class string_type, class T0, class T1>
|
|
|
|
void msxml2_wrapper<string_type, T0, T1>::setFeature(const string_type& name, bool value)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2006-09-20 15:57:50 +02:00
|
|
|
std::wstring wName(string_adaptor_type::asStdWString(name));
|
2003-08-27 16:28:01 +02:00
|
|
|
reader_->putFeature(wName.c_str(), value);
|
2002-06-21 13:16:28 +02:00
|
|
|
} // setFeature
|
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
template<class string_type, class T0, class T1>
|
|
|
|
void msxml2_wrapper<string_type, T0, T1>::setErrorHandler(SAX::basic_ErrorHandler<string_type>& handler)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
errorHandler_.setErrorHandler(handler);
|
|
|
|
} // setErrorHandler
|
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
template<class string_type, class T0, class T1>
|
|
|
|
SAX::basic_ErrorHandler<string_type>* msxml2_wrapper<string_type, T0, T1>::getErrorHandler() const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
return errorHandler_.getErrorHandler();
|
|
|
|
} // getErrorHandler
|
|
|
|
|
2006-09-20 15:57:50 +02:00
|
|
|
template<class string_type, class T0, class T1>
|
|
|
|
void msxml2_wrapper<string_type, T0, T1>::parse(SAX::basic_InputSource<string_type>& source)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(source.getByteStream() == 0)
|
|
|
|
{
|
2006-09-20 15:57:50 +02:00
|
|
|
std::wstring wSysId(string_adaptor_type::asStdWString(source.getSystemId()));
|
2003-08-27 16:28:01 +02:00
|
|
|
reader_->parseURL(wSysId.c_str());
|
2002-06-21 13:16:28 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
StreamAdaptor sa(source);
|
|
|
|
VARIANT wrapper;
|
|
|
|
wrapper.vt = VT_UNKNOWN;
|
2002-09-03 15:19:14 +02:00
|
|
|
wrapper.punkVal = static_cast<ISequentialStream*>(&sa);
|
|
|
|
reader_->parse(wrapper);
|
2002-06-21 13:16:28 +02:00
|
|
|
} // if ...
|
2004-02-24 10:52:18 +01:00
|
|
|
errorHandler_.report();
|
2002-06-21 13:16:28 +02:00
|
|
|
} // parse
|
|
|
|
|
|
|
|
} // namespace SAX
|
|
|
|
|
|
|
|
#endif
|
2002-09-03 15:19:14 +02:00
|
|
|
// end of file
|
2003-04-28 16:51:48 +02:00
|
|
|
|