📄 whitespacefilter.cpp
字号:
//
// WhitespaceFilter.cpp
//
// $Id: //poco/Main/XML/src/WhitespaceFilter.cpp#5 $
//
// Copyright (c) 2004-2005, 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/WhitespaceFilter.h"
#include "SAX/SAXException.h"
XML_BEGIN
WhitespaceFilter::WhitespaceFilter():
_pLexicalHandler(0),
_filter(true)
{
}
WhitespaceFilter::WhitespaceFilter(XMLReader* pReader):
XMLFilterImpl(pReader),
_pLexicalHandler(0),
_filter(true)
{
}
WhitespaceFilter::~WhitespaceFilter()
{
}
void WhitespaceFilter::setProperty(const XMLString& propertyId, const XMLString& value)
{
if (propertyId == XMLReader::PROPERTY_LEXICAL_HANDLER)
throw SAXNotSupportedException(std::string("property does not take a string value: ") + fromXMLString(propertyId));
else
XMLFilterImpl::setProperty(propertyId, value);
}
void WhitespaceFilter::setProperty(const XMLString& propertyId, void* value)
{
if (propertyId == XMLReader::PROPERTY_LEXICAL_HANDLER)
_pLexicalHandler = reinterpret_cast<LexicalHandler*>(value);
else
XMLFilterImpl::setProperty(propertyId, value);
}
void* WhitespaceFilter::getProperty(const XMLString& propertyId) const
{
if (propertyId == XMLReader::PROPERTY_LEXICAL_HANDLER)
return _pLexicalHandler;
else
return XMLFilterImpl::getProperty(propertyId);
}
void WhitespaceFilter::startDocument()
{
XMLFilterImpl::startDocument();
_filter = true;
_data.clear();
}
void WhitespaceFilter::endDocument()
{
XMLFilterImpl::endDocument();
_filter = true;
_data.clear();
}
void WhitespaceFilter::startElement(const XMLString& uri, const XMLString& localName, const XMLString& qname, const Attributes& attrList)
{
XMLFilterImpl::startElement(uri, localName, qname, attrList);
_filter = true;
_data.clear();
}
void WhitespaceFilter::endElement(const XMLString& uri, const XMLString& localName, const XMLString& qname)
{
XMLFilterImpl::endElement(uri, localName, qname);
_filter = true;
_data.clear();
}
void WhitespaceFilter::characters(const XMLChar ch[], int start, int length)
{
if (_filter)
{
bool ws = true;
const XMLChar* it = ch + start;
const XMLChar* end = ch + start + length;
_data.append(it, end);
while (it != end)
{
if (*it != '\r' && *it != '\n' && *it != '\t' && *it != ' ')
{
ws = false;
break;
}
++it;
}
if (!ws)
{
XMLFilterImpl::characters(_data.data(), 0, (int) _data.length());
_filter = false;
_data.clear();
}
}
else XMLFilterImpl::characters(ch, start, length);
}
void WhitespaceFilter::ignorableWhitespace(const XMLChar ch[], int start, int length)
{
// the handler name already says that this data can be ignored
}
void WhitespaceFilter::processingInstruction(const XMLString& target, const XMLString& data)
{
XMLFilterImpl::processingInstruction(target, data);
_filter = true;
_data.clear();
}
void WhitespaceFilter::startDTD(const XMLString& name, const XMLString& publicId, const XMLString& systemId)
{
if (_pLexicalHandler)
_pLexicalHandler->startDTD(name, publicId, systemId);
}
void WhitespaceFilter::endDTD()
{
if (_pLexicalHandler)
_pLexicalHandler->endDTD();
}
void WhitespaceFilter::startEntity(const XMLString& name)
{
if (_pLexicalHandler)
_pLexicalHandler->startEntity(name);
_filter = true;
_data.clear();
}
void WhitespaceFilter::endEntity(const XMLString& name)
{
if (_pLexicalHandler)
_pLexicalHandler->endEntity(name);
_filter = true;
_data.clear();
}
void WhitespaceFilter::startCDATA()
{
if (_pLexicalHandler)
_pLexicalHandler->startCDATA();
_filter = false;
_data.clear();
}
void WhitespaceFilter::endCDATA()
{
if (_pLexicalHandler)
_pLexicalHandler->endCDATA();
_filter = true;
_data.clear();
}
void WhitespaceFilter::comment(const XMLChar ch[], int start, int length)
{
if (_pLexicalHandler)
_pLexicalHandler->comment(ch, start, length);
_filter = true;
_data.clear();
}
void WhitespaceFilter::setupParse()
{
XMLFilterImpl::setupParse();
parent()->setProperty(XMLReader::PROPERTY_LEXICAL_HANDLER, static_cast<LexicalHandler*>(this));
}
XML_END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -