📄 saxparser.cpp
字号:
//
// SAXParser.cpp
//
// $Id: //poco/Main/XML/src/SAXParser.cpp#5 $
//
// 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.
//
#include "SAX/SAXParser.h"
#include "SAX/SAXException.h"
#include "SAX/EntityResolverImpl.h"
#include "SAX/InputSource.h"
#include "XML/NamespaceStrategy.h"
#include <sstream>
XML_BEGIN
SAXParser::SAXParser():
_namespaces(true),
_namespacePrefixes(false)
{
}
SAXParser::SAXParser(const XMLString& encoding):
_engine(encoding),
_namespaces(true),
_namespacePrefixes(false)
{
}
SAXParser::~SAXParser()
{
}
void SAXParser::setEncoding(const XMLString& encoding)
{
_engine.setEncoding(encoding);
}
const XMLString& SAXParser::getEncoding() const
{
return _engine.getEncoding();
}
void SAXParser::addEncoding(const XMLString& name, Foundation::TextEncoding* pEncoding)
{
_engine.addEncoding(name, pEncoding);
}
void SAXParser::setEntityResolver(EntityResolver* pResolver)
{
_engine.setEntityResolver(pResolver);
}
EntityResolver* SAXParser::getEntityResolver() const
{
return _engine.getEntityResolver();
}
void SAXParser::setDTDHandler(DTDHandler* pDTDHandler)
{
_engine.setDTDHandler(pDTDHandler);
}
DTDHandler* SAXParser::getDTDHandler() const
{
return _engine.getDTDHandler();
}
void SAXParser::setContentHandler(ContentHandler* pContentHandler)
{
_engine.setContentHandler(pContentHandler);
}
ContentHandler* SAXParser::getContentHandler() const
{
return _engine.getContentHandler();
}
void SAXParser::setErrorHandler(ErrorHandler* pErrorHandler)
{
_engine.setErrorHandler(pErrorHandler);
}
ErrorHandler* SAXParser::getErrorHandler() const
{
return _engine.getErrorHandler();
}
void SAXParser::setFeature(const XMLString& featureId, bool state)
{
if (featureId == XMLReader::FEATURE_VALIDATION || featureId == XMLReader::FEATURE_STRING_INTERNING)
throw SAXNotSupportedException(fromXMLString(XMLReader::FEATURE_VALIDATION));
else if (featureId == XMLReader::FEATURE_EXTERNAL_GENERAL_ENTITIES)
_engine.setExternalGeneralEntities(state);
else if (featureId == XMLReader::FEATURE_EXTERNAL_PARAMETER_ENTITIES)
_engine.setExternalParameterEntities(state);
else if (featureId == XMLReader::FEATURE_NAMESPACES)
_namespaces = state;
else if (featureId == XMLReader::FEATURE_NAMESPACE_PREFIXES)
_namespacePrefixes = state;
else throw SAXNotRecognizedException(fromXMLString(featureId));
}
bool SAXParser::getFeature(const XMLString& featureId) const
{
if (featureId == XMLReader::FEATURE_VALIDATION || featureId == XMLReader::FEATURE_STRING_INTERNING)
throw SAXNotSupportedException(fromXMLString(XMLReader::FEATURE_VALIDATION));
else if (featureId == XMLReader::FEATURE_EXTERNAL_GENERAL_ENTITIES)
return _engine.getExternalGeneralEntities();
else if (featureId == XMLReader::FEATURE_EXTERNAL_PARAMETER_ENTITIES)
return _engine.getExternalParameterEntities();
else if (featureId == XMLReader::FEATURE_NAMESPACES)
return _namespaces;
else if (featureId == XMLReader::FEATURE_NAMESPACE_PREFIXES)
return _namespacePrefixes;
else throw SAXNotRecognizedException(fromXMLString(featureId));
}
void SAXParser::setProperty(const XMLString& propertyId, const XMLString& value)
{
if (propertyId == XMLReader::PROPERTY_DECLARATION_HANDLER || propertyId == XMLReader::PROPERTY_LEXICAL_HANDLER)
throw SAXNotSupportedException(std::string("property does not take a string value: ") + fromXMLString(propertyId));
else
throw SAXNotRecognizedException(fromXMLString(propertyId));
}
void SAXParser::setProperty(const XMLString& propertyId, void* value)
{
if (propertyId == XMLReader::PROPERTY_DECLARATION_HANDLER)
_engine.setDeclHandler(reinterpret_cast<DeclHandler*>(value));
else if (propertyId == XMLReader::PROPERTY_LEXICAL_HANDLER)
_engine.setLexicalHandler(reinterpret_cast<LexicalHandler*>(value));
else throw SAXNotRecognizedException(fromXMLString(propertyId));
}
void* SAXParser::getProperty(const XMLString& propertyId) const
{
if (propertyId == XMLReader::PROPERTY_DECLARATION_HANDLER)
return _engine.getDeclHandler();
else if (propertyId == XMLReader::PROPERTY_LEXICAL_HANDLER)
return _engine.getLexicalHandler();
else throw SAXNotSupportedException(fromXMLString(propertyId));
}
void SAXParser::parse(InputSource* pInputSource)
{
if (pInputSource->getByteStream() || pInputSource->getCharacterStream())
{
setupParse();
_engine.parse(pInputSource);
}
else parse(pInputSource->getSystemId());
}
void SAXParser::parse(const XMLString& systemId)
{
setupParse();
EntityResolverImpl entityResolver;
InputSource* pInputSource = entityResolver.resolveEntity(0, systemId);
if (pInputSource)
{
try
{
_engine.parse(pInputSource);
}
catch (...)
{
entityResolver.releaseInputSource(pInputSource);
throw;
}
entityResolver.releaseInputSource(pInputSource);
}
else throw XMLException("Cannot resolve system identifier", systemId);
}
void SAXParser::parseString(const std::string& xml)
{
std::istringstream istr(xml);
InputSource src(istr);
parse(&src);
}
void SAXParser::setupParse()
{
if (_namespaces && !_namespacePrefixes)
_engine.setNamespaceStrategy(new NoNamespacePrefixesStrategy);
else if (_namespaces && _namespacePrefixes)
_engine.setNamespaceStrategy(new NamespacePrefixesStrategy);
else
_engine.setNamespaceStrategy(new NoNamespacesStrategy);
}
XML_END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -