📄 parserengine.h
字号:
//
// ParserEngine.h
//
// $Id: //poco/Main/XML/include/XML/ParserEngine.h#5 $
//
// Definition of the ParseEngine class.
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#ifndef XML_ParserEngine_INCLUDED
#define XML_ParserEngine_INCLUDED
#ifndef XML_XML_INCLUDED
#include "XML/XML.h"
#endif
#ifndef XmlParse_INCLUDED
#include "XML/expat.h"
#endif
#ifndef XML_XMLString_INCLUDED
#include "XML/XMLString.h"
#endif
#ifndef XML_XMLStream_INCLUDED
#include "XML/XMLStream.h"
#endif
#ifndef SAX_Locator_INCLUDED
#include "SAX/Locator.h"
#endif
#ifndef Foundation_TextEncoding_INCLUDED
#include "Foundation/TextEncoding.h"
#endif
#ifndef STD_MAP_INCLUDED
#include <map>
#define STD_MAP_INCLUDED
#endif
#ifndef STD_VECTOR_INCLUDED
#include <vector>
#define STD_VECTOR_INCLUDED
#endif
XML_BEGIN
class InputSource;
class EntityResolver;
class DTDHandler;
class DeclHandler;
class ContentHandler;
class LexicalHandler;
class ErrorHandler;
class NamespaceStrategy;
class ContextLocator;
class XML_API ParserEngine: public Locator
/// This class provides an object-oriented, stream-based,
/// low-level interface to the XML Parser Toolkit (expat).
/// It is strongly recommended, that you use the
/// SAX parser classes (which are based on this
/// class) instead of this class, since they provide
/// a standardized, higher-level interface to the parser.
{
public:
ParserEngine();
/// Creates the parser engine.
ParserEngine(const XMLString& encoding);
/// Creates the parser engine and passes the encoding
/// to the underlying parser.
~ParserEngine();
/// Destroys the parser.
void setEncoding(const XMLString& encoding);
/// Sets the encoding used by expat. The encoding must be
/// set before parsing begins, otherwise it will be ignored.
const XMLString& getEncoding() const;
/// Returns the encoding used by expat.
void addEncoding(const XMLString& name, Foundation::TextEncoding* pEncoding);
/// Adds an encoding to the parser.
void setNamespaceStrategy(NamespaceStrategy* pStrategy);
/// Sets the NamespaceStrategy used by the parser.
/// The parser takes ownership of the strategy object
/// and deletes it when it's no longer needed.
/// The default is NoNamespacesStrategy.
NamespaceStrategy* getNamespaceStrategy() const;
/// Returns the NamespaceStrategy currently in use.
void setExpandInternalEntities(bool flag = true);
/// Enables/disables expansion of internal entities (enabled by
/// default). If entity expansion is disabled, internal entities
/// are reported via the default handler.
/// Must be set before parsing begins, otherwise it will be
/// ignored.
bool getExpandInternalEntities() const;
/// Returns true if internal entities will be expanded automatically,
/// which is the default.
void setExternalGeneralEntities(bool flag = true);
/// Enable or disable processing of external general entities.
bool getExternalGeneralEntities() const;
/// Returns true if external general entities will be processed; false otherwise.
void setExternalParameterEntities(bool flag = true);
/// Enable or disable processing of external parameter entities.
bool getExternalParameterEntities() const;
/// Returns true if external parameter entities will be processed; false otherwise.
void setEntityResolver(EntityResolver* pResolver);
/// Allow an application to register an entity resolver.
EntityResolver* getEntityResolver() const;
/// Return the current entity resolver.
void setDTDHandler(DTDHandler* pDTDHandler);
/// Allow an application to register a DTD event handler.
DTDHandler* getDTDHandler() const;
/// Return the current DTD handler.
void setDeclHandler(DeclHandler* pDeclHandler);
/// Allow an application to register a DTD declarations event handler.
DeclHandler* getDeclHandler() const;
/// Return the current DTD declarations handler.
void setContentHandler(ContentHandler* pContentHandler);
/// Allow an application to register a content event handler.
ContentHandler* getContentHandler() const;
/// Return the current content handler.
void setLexicalHandler(LexicalHandler* pLexicalHandler);
/// Allow an application to register a lexical event handler.
LexicalHandler* getLexicalHandler() const;
/// Return the current lexical handler.
void setErrorHandler(ErrorHandler* pErrorHandler);
/// Allow an application to register an error event handler.
ErrorHandler* getErrorHandler() const;
/// Return the current error handler.
void parse(InputSource* pInputSource);
/// Parse an XML document from the given InputSource.
// Locator
XMLString getPublicId() const;
/// Return the public identifier for the current document event.
XMLString getSystemId() const;
/// Return the system identifier for the current document event.
int getLineNumber() const;
/// Return the line number where the current document event ends.
int getColumnNumber() const;
/// Return the column number where the current document event ends.
protected:
void init();
/// initializes expat
void parseByteInputStream(XMLByteInputStream& istr);
/// Parses an entity from the given stream.
void parseCharInputStream(XMLCharInputStream& istr);
/// Parses an entity from the given stream.
void handleError(int errorNo);
/// Throws an XMLException with a message corresponding
/// to the given Expat error code.
void parseExternal(XML_Parser extParser, InputSource* pInputSource);
/// Parse an XML document from the given InputSource.
void parseExternalByteInputStream(XML_Parser extParser, XMLByteInputStream& istr);
/// Parses an external entity from the given stream, with a separate parser.
void parseExternalCharInputStream(XML_Parser extParser, XMLCharInputStream& istr);
/// Parses an external entity from the given stream, with a separate parser.
void pushContext(XML_Parser parser, InputSource* pInputSource);
/// Pushes a new entry to the context stack.
void popContext();
/// Pops the top-most entry from the context stack.
void resetContext();
/// Resets and clears the context stack.
const Locator& locator() const;
/// Returns a locator denoting the current parse location.
// expat handler procedures
static void handleStartElement(void* userData, const XML_Char* name, const XML_Char** atts);
static void handleEndElement(void* userData, const XML_Char* name);
static void handleCharacterData(void* userData, const XML_Char* s, int len);
static void handleProcessingInstruction(void* userData, const XML_Char* target, const XML_Char* data);
static void handleDefault(void* userData, const XML_Char* s, int len);
static void handleUnparsedEntityDecl(void* userData, const XML_Char* entityName, const XML_Char* base, const XML_Char* systemId, const XML_Char* publicId, const XML_Char* notationName);
static void handleNotationDecl(void* userData, const XML_Char* notationName, const XML_Char* base, const XML_Char* systemId, const XML_Char* publicId);
static int handleExternalEntityRef(XML_Parser parser, const XML_Char* openEntityNames, const XML_Char* base, const XML_Char* systemId, const XML_Char* publicId);
static int handleUnknownEncoding(void* encodingHandlerData, const XML_Char* name, XML_Encoding* info);
static void handleComment(void* userData, const XML_Char* data);
static void handleStartCdataSection(void* userData);
static void handleEndCdataSection(void* userData);
static void handleStartNamespaceDecl(void* userData, const XML_Char* prefix, const XML_Char* uri);
static void handleEndNamespaceDecl(void* userData, const XML_Char* prefix);
static void handleStartDoctypeDecl(void* userData, const XML_Char* doctypeName, const XML_Char *systemId, const XML_Char* publicId, int hasInternalSubset);
static void handleEndDoctypeDecl(void* userData);
static void handleEntityDecl(void *userData, const XML_Char *entityName, int isParamEntity, const XML_Char *value, int valueLength,
const XML_Char *base, const XML_Char *systemId, const XML_Char *publicId, const XML_Char *notationName);
static void handleExternalParsedEntityDecl(void* userData, const XML_Char* entityName, const XML_Char* base, const XML_Char* systemId, const XML_Char* publicId);
static void handleInternalParsedEntityDecl(void* userData, const XML_Char* entityName, const XML_Char* replacementText, int replacementTextLength);
static void handleSkippedEntity(void* userData, const XML_Char* entityName, int isParameterEntity);
// encoding support
static int convert(void *data, const char *s);
private:
typedef std::map<XMLString, Foundation::TextEncoding*> EncodingMap;
typedef std::vector<ContextLocator*> ContextStack;
XML_Parser _parser;
char* _pBuffer;
bool _encodingSpecified;
XMLString _encoding;
bool _expandInternalEntities;
bool _externalGeneralEntities;
bool _externalParameterEntities;
NamespaceStrategy* _pNamespaceStrategy;
EncodingMap _encodings;
ContextStack _context;
EntityResolver* _pEntityResolver;
DTDHandler* _pDTDHandler;
DeclHandler* _pDeclHandler;
ContentHandler* _pContentHandler;
LexicalHandler* _pLexicalHandler;
ErrorHandler* _pErrorHandler;
static const int PARSE_BUFFER_SIZE;
static const XMLString EMPTY_STRING;
};
//
// inlines
//
inline const XMLString& ParserEngine::getEncoding() const
{
return _encoding;
}
inline NamespaceStrategy* ParserEngine::getNamespaceStrategy() const
{
return _pNamespaceStrategy;
}
inline bool ParserEngine::getExpandInternalEntities() const
{
return _expandInternalEntities;
}
inline bool ParserEngine::getExternalGeneralEntities() const
{
return _externalGeneralEntities;
}
inline bool ParserEngine::getExternalParameterEntities() const
{
return _externalParameterEntities;
}
inline EntityResolver* ParserEngine::getEntityResolver() const
{
return _pEntityResolver;
}
inline DTDHandler* ParserEngine::getDTDHandler() const
{
return _pDTDHandler;
}
inline DeclHandler* ParserEngine::getDeclHandler() const
{
return _pDeclHandler;
}
inline ContentHandler* ParserEngine::getContentHandler() const
{
return _pContentHandler;
}
inline LexicalHandler* ParserEngine::getLexicalHandler() const
{
return _pLexicalHandler;
}
inline ErrorHandler* ParserEngine::getErrorHandler() const
{
return _pErrorHandler;
}
XML_END
#endif // XML_ParserEngine_INCLUDED
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -