/* This file is part of the syndication library SPDX-FileCopyrightText: 2005 Frank Osterfeld SPDX-License-Identifier: LGPL-2.0-or-later */ #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(); ~ParserCollectionImpl() override; QSharedPointer parse(const DocumentSource &source, const QString &formatHint = QString()) override; bool registerParser(AbstractParser *parser, Mapper *mapper) override; void changeMapper(const QString &format, Mapper *mapper) override; ErrorCode lastError() const override; 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() { // Delete the values qDeleteAll(m_parsers); qDeleteAll(m_mappers); } 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 QSharedPointer 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); } } for (AbstractParser *i : std::as_const(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