/* * This file is part of the syndication library * * Copyright (C) 2005 Frank Osterfeld * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * */ #ifndef SYNDICATION_PARSERCOLLECTIONIMPL_H #define SYNDICATION_PARSERCOLLECTIONIMPL_H #include #include #include #include #include #include #include #include #include #include namespace Syndication { //@cond PRIVATE /** @internal */ // default implementation of ParserCollection. This is separated // from the interface to move the implementation out of the public API // (template classes require implementations to be in the header) template class SYNDICATION_EXPORT ParserCollectionImpl : public ParserCollection { public: ParserCollectionImpl(); virtual ~ParserCollectionImpl(); boost::shared_ptr parse(const DocumentSource& source, const QString& formatHint=QString()); bool registerParser(AbstractParser* parser, Mapper* mapper); void changeMapper(const QString& format, Mapper* mapper); ErrorCode lastError() const; private: ParserCollectionImpl(const ParserCollectionImpl&); ParserCollectionImpl& operator=(const ParserCollectionImpl&); QHash m_parsers; QHash*> m_mappers; QList m_parserList; ErrorCode m_lastError; }; //@endcond //template //class ParserCollectionImpl::ParserCollectionImplPrivate template ParserCollectionImpl::ParserCollectionImpl() { } template ParserCollectionImpl::~ParserCollectionImpl() { QList list = m_parsers.values(); QList::ConstIterator it = list.constBegin(); QList::ConstIterator end = list.constEnd(); for ( ; it != end; ++it) delete *it; QList m = m_mappers.keys(); QList::ConstIterator itm = m.constBegin(); QList::ConstIterator endm = m.constEnd(); for ( ; itm != endm; ++itm) delete m_mappers[*itm]; } template bool ParserCollectionImpl::registerParser(AbstractParser* parser, Mapper* mapper) { if (m_parsers.contains(parser->format())) return false; m_parserList.append(parser); m_parsers.insert(parser->format(), parser); m_mappers.insert(parser->format(), mapper); return true; } template void ParserCollectionImpl::changeMapper(const QString& format, Mapper* mapper) { m_mappers[format] = mapper; } template boost::shared_ptr ParserCollectionImpl::parse(const DocumentSource& source, const QString& formatHint) { m_lastError = Syndication::Success; if (!formatHint.isNull() && m_parsers.contains(formatHint)) { if (m_parsers[formatHint]->accept(source)) { SpecificDocumentPtr doc = m_parsers[formatHint]->parse(source); if (!doc->isValid()) { m_lastError = InvalidFormat; return FeedPtr(); } return m_mappers[formatHint]->map(doc); } } Q_FOREACH (AbstractParser* i, m_parserList) { if (i->accept(source)) { SpecificDocumentPtr doc = i->parse(source); if (!doc->isValid()) { m_lastError = InvalidFormat; return FeedPtr(); } return m_mappers[i->format()]->map(doc); } } if (source.asDomDocument().isNull()) m_lastError = InvalidXml; else m_lastError = XmlNotAccepted; return FeedPtr(); } template Syndication::ErrorCode ParserCollectionImpl::lastError() const { return m_lastError; } template ParserCollectionImpl::ParserCollectionImpl(const ParserCollectionImpl&) { } template ParserCollectionImpl& ParserCollectionImpl::operator=(const ParserCollectionImpl&) { return *this; } } // namespace Syndication #endif // SYNDICATION_PARSERCOLLECTIONIMPL_H