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>
38 namespace Syndication {
48 class 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;
83 ParserCollectionImpl<T>::ParserCollectionImpl()
88 ParserCollectionImpl<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];
107 bool 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);
118 void ParserCollectionImpl<T>::changeMapper(
const QString& format, Mapper<T>* mapper)
120 m_mappers[format] = mapper;
124 boost::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())
172 ParserCollectionImpl<T>::ParserCollectionImpl(
const ParserCollectionImpl&)
177 ParserCollectionImpl<T>& ParserCollectionImpl<T>::operator=(
const ParserCollectionImpl&)
184 #endif // SYNDICATION_PARSERCOLLECTIONIMPL_H