23#ifndef SYNDICATION_PARSERCOLLECTIONIMPL_H
24#define SYNDICATION_PARSERCOLLECTIONIMPL_H
26#include <syndication/specificdocument.h>
27#include <syndication/abstractparser.h>
28#include <syndication/documentsource.h>
29#include <syndication/parsercollection.h>
30#include <syndication/feed.h>
31#include <syndication/global.h>
32#include <syndication/mapper.h>
34#include <QtXml/QDomDocument>
35#include <QtCore/QHash>
36#include <QtCore/QString>
48class SYNDICATION_EXPORT ParserCollectionImpl :
public ParserCollection<T>
52 ParserCollectionImpl();
54 virtual ~ParserCollectionImpl();
56 boost::shared_ptr<T>
parse(
const DocumentSource& source,
57 const QString& formatHint=QString());
60 bool registerParser(AbstractParser* parser, Mapper<T>* mapper);
62 void changeMapper(
const QString& format, Mapper<T>* mapper);
68 ParserCollectionImpl(
const ParserCollectionImpl&);
69 ParserCollectionImpl& operator=(
const ParserCollectionImpl&);
70 QHash<QString, AbstractParser*> m_parsers;
71 QHash<QString, Mapper<T>*> m_mappers;
72 QList<AbstractParser*> m_parserList;
83ParserCollectionImpl<T>::ParserCollectionImpl()
88ParserCollectionImpl<T>::~ParserCollectionImpl()
90 QList<AbstractParser*> list = m_parsers.values();
91 QList<AbstractParser*>::ConstIterator it = list.constBegin();
92 QList<AbstractParser*>::ConstIterator end = list.constEnd();
94 for ( ; it != end; ++it)
97 QList<QString> m = m_mappers.keys();
98 QList<QString>::ConstIterator itm = m.constBegin();
99 QList<QString>::ConstIterator endm = m.constEnd();
101 for ( ; itm != endm; ++itm)
102 delete m_mappers[*itm];
107bool ParserCollectionImpl<T>::registerParser(AbstractParser* parser, Mapper<T>* mapper)
109 if (m_parsers.contains(parser->format()))
112 m_parserList.append(parser);
113 m_parsers.insert(parser->format(), parser);
114 m_mappers.insert(parser->format(), mapper);
118void ParserCollectionImpl<T>::changeMapper(
const QString& format, Mapper<T>* mapper)
120 m_mappers[format] = mapper;
124boost::shared_ptr<T> ParserCollectionImpl<T>::parse(
const DocumentSource& source,
const QString& formatHint)
128 if (!formatHint.isNull() && m_parsers.contains(formatHint))
130 if (m_parsers[formatHint]->accept(source))
132 SpecificDocumentPtr doc = m_parsers[formatHint]->parse(source);
139 return m_mappers[formatHint]->map(doc);
143 Q_FOREACH (AbstractParser* i, m_parserList)
145 if (i->accept(source))
147 SpecificDocumentPtr doc = i->parse(source);
154 return m_mappers[i->format()]->map(doc);
157 if (source.asDomDocument().isNull())
172ParserCollectionImpl<T>::ParserCollectionImpl(
const ParserCollectionImpl&)
177ParserCollectionImpl<T>& ParserCollectionImpl<T>::operator=(
const ParserCollectionImpl&)
ErrorCode
error code indicating fetching or parsing errors
@ InvalidXml
The XML is invalid.
@ XmlNotAccepted
The source is valid XML, but no parser accepted it.
@ InvalidFormat
the source was accepted by a parser, but the actual parsing failed.
@ Success
No error occurred, feed was fetched and parsed successfully.
FeedPtr parse(const DocumentSource &src, const QString &formatHint)
parses a document from a source and returns a new Feed object wrapping the feed content.