📄 saxreaderbase.cpp
字号:
/*gpsmgr: A program for managing GPS informationCopyright (C) 2003 Austin BinghamThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.You can reach the author at: abingham@spamcop.net*/#include "saxReaderBase.h"#include "exceptions.h"using namespace gpsmgr::exceptions;namespace{ using namespace gpsmgr::io::util; SAXReaderBase::Phase* BASE_PHASE = new SAXReaderBase::Phase(); SAXReaderBase::Phase* UNKNOWN_PHASE = 0x00; SAXReaderBase::Phase* DEFAULT_CONTEXT = new SAXReaderBase::Phase();}namespace gpsmgr { namespace io { namespace util { SAXReaderBase::SAXReaderBase() : mLastError ("NO ERROR"), mIgnoreUnmappedTags (true) { mMainMap[DEFAULT_CONTEXT] = DataToPhaseMap(); // necessary? mCurrPhase.push(BASE_PHASE); } SAXReaderBase::~SAXReaderBase() {} QString SAXReaderBase::errorString() { return mLastError; } bool SAXReaderBase::warning(const QXmlParseException& exception) { throw Exception<ParseError>(exception.message()); } bool SAXReaderBase::error(const QXmlParseException& exception) { throw Exception<ParseError>(exception.message()); } bool SAXReaderBase::fatalError(const QXmlParseException& exception) { throw Exception<ParseError>(exception.message()); } void SAXReaderBase::addTagMapping(const QString& tag, Phase* phase) { addTagMapping(tag, phase, DEFAULT_CONTEXT); } void SAXReaderBase::addTagMapping(const QString& tag, Phase* phase, Phase* context) { DataToPhaseMap& tagmap = mMainMap[context]; tagmap[tag] = phase; } bool SAXReaderBase::getAttributeValue(const QString& name, QString& value, const QXmlAttributes& atts, bool required, const QString& errmsg) { int idx = atts.index(name); if (idx == -1) { if (required) setErrorMessage(errmsg); return false; } value = atts.value(idx); return true; } bool SAXReaderBase::parse(QXmlInputSource& is) { return parse(is, *this); } bool SAXReaderBase::parse(QXmlInputSource& is, QXmlErrorHandler& eh) { QXmlSimpleReader rdr; rdr.setContentHandler(this); rdr.setEntityResolver(this); rdr.setDTDHandler(this); rdr.setLexicalHandler(this); rdr.setDeclHandler(this); rdr.setErrorHandler(&eh); try { return rdr.parse(is); } catch (QXmlParseException e) { throw Exception<ParseError>("Error while parsing XML input stream"); } } SAXReaderBase::Phase* SAXReaderBase::getPhaseForTag(const QString& tag, Phase* context) const { // First, find the string->phase map for the given context PhaseMap::const_iterator tagmapitr = mMainMap.find(context); if (tagmapitr == mMainMap.end()) return UNKNOWN_PHASE; // Next, find the phase for 'tag' if it exists DataToPhaseMap::const_iterator itr = tagmapitr->second.find(tag); if (itr == tagmapitr->second.end()) return UNKNOWN_PHASE; else return itr->second; } SAXReaderBase::Phase* SAXReaderBase::getCurrentPhase() const { return mCurrPhase.top(); } void SAXReaderBase::setErrorMessage(const QString& msg) { mLastError = msg; } bool SAXReaderBase::endElementPrePhaseChange(const QString& namespaceURI, const QString& localName, const QString& rawName, Phase* phase) { return true; } bool SAXReaderBase::endElementPostPhaseChange(const QString& namespaceURI, const QString& localName, const QString& rawName, Phase* phase) { return true; } bool SAXReaderBase::endElement(const QString & namespaceURI, const QString & localName, const QString & qName) { if (!endElementPrePhaseChange(namespaceURI, localName, qName, getCurrentPhase())) return false; // revert to the previous phase on the stack mCurrPhase.pop(); assert(!mCurrPhase.empty()); return endElementPostPhaseChange(namespaceURI, localName, qName, getCurrentPhase()); } bool SAXReaderBase::startElementPrePhaseChange(const QString& namespaceURI, const QString& localName, const QString& rawName, const QXmlAttributes& atts, Phase* phase) { return true; } bool SAXReaderBase::startElementPostPhaseChange(const QString& namespaceURI, const QString& localName, const QString& rawName, const QXmlAttributes& atts, Phase* phase) { return true; } bool SAXReaderBase::startElement(const QString & namespaceURI, const QString & localName, const QString & qName, const QXmlAttributes & atts) { if (!startElementPrePhaseChange(namespaceURI, localName, qName, atts, getCurrentPhase())) return false; // First, try to find a phase using the current phase // as the context. If that doesn't exist, try to find // one in the default context. Phase* phase = getPhaseForTag(localName, getCurrentPhase()); if (phase == UNKNOWN_PHASE) phase = getPhaseForTag(localName, DEFAULT_CONTEXT); if (phase == UNKNOWN_PHASE) { if (!mIgnoreUnmappedTags) { setErrorMessage("Unmapped tag encountered while parsing [" + namespaceURI + ", " + localName + ", " + qName + "]"); return false; } } mCurrPhase.push(phase); return startElementPostPhaseChange(namespaceURI, localName, qName, atts, getCurrentPhase()); }} } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -