Syndication Library
parsercollectionimpl.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef SYNDICATION_PARSERCOLLECTIONIMPL_H
00024 #define SYNDICATION_PARSERCOLLECTIONIMPL_H
00025
00026 #include <syndication/specificdocument.h>
00027 #include <syndication/abstractparser.h>
00028 #include <syndication/documentsource.h>
00029 #include <syndication/parsercollection.h>
00030 #include <syndication/feed.h>
00031 #include <syndication/global.h>
00032 #include <syndication/mapper.h>
00033
00034 #include <QtXml/QDomDocument>
00035 #include <QtCore/QHash>
00036 #include <QtCore/QString>
00037
00038 namespace Syndication {
00039
00040
00043
00044
00045
00046
00047 template <class T>
00048 class SYNDICATION_EXPORT ParserCollectionImpl : public ParserCollection<T>
00049 {
00050 public:
00051
00052 ParserCollectionImpl();
00053
00054 virtual ~ParserCollectionImpl();
00055
00056 boost::shared_ptr<T> parse(const DocumentSource& source,
00057 const QString& formatHint=QString());
00058
00059
00060 bool registerParser(AbstractParser* parser, Mapper<T>* mapper);
00061
00062 void changeMapper(const QString& format, Mapper<T>* mapper);
00063
00064 ErrorCode lastError() const;
00065
00066 private:
00067
00068 ParserCollectionImpl(const ParserCollectionImpl&);
00069 ParserCollectionImpl& operator=(const ParserCollectionImpl&);
00070 QHash<QString, AbstractParser*> m_parsers;
00071 QHash<QString, Mapper<T>*> m_mappers;
00072 QList<AbstractParser*> m_parserList;
00073
00074 ErrorCode m_lastError;
00075 };
00076
00077
00078
00079
00080
00081
00082 template <class T>
00083 ParserCollectionImpl<T>::ParserCollectionImpl()
00084 {
00085 }
00086
00087 template <class T>
00088 ParserCollectionImpl<T>::~ParserCollectionImpl()
00089 {
00090 QList<AbstractParser*> list = m_parsers.values();
00091 QList<AbstractParser*>::ConstIterator it = list.constBegin();
00092 QList<AbstractParser*>::ConstIterator end = list.constEnd();
00093
00094 for ( ; it != end; ++it)
00095 delete *it;
00096
00097 QList<QString> m = m_mappers.keys();
00098 QList<QString>::ConstIterator itm = m.constBegin();
00099 QList<QString>::ConstIterator endm = m.constEnd();
00100
00101 for ( ; itm != endm; ++itm)
00102 delete m_mappers[*itm];
00103
00104 }
00105
00106 template <class T>
00107 bool ParserCollectionImpl<T>::registerParser(AbstractParser* parser, Mapper<T>* mapper)
00108 {
00109 if (m_parsers.contains(parser->format()))
00110 return false;
00111
00112 m_parserList.append(parser);
00113 m_parsers.insert(parser->format(), parser);
00114 m_mappers.insert(parser->format(), mapper);
00115 return true;
00116 }
00117 template <class T>
00118 void ParserCollectionImpl<T>::changeMapper(const QString& format, Mapper<T>* mapper)
00119 {
00120 m_mappers[format] = mapper;
00121 }
00122
00123 template <class T>
00124 boost::shared_ptr<T> ParserCollectionImpl<T>::parse(const DocumentSource& source, const QString& formatHint)
00125 {
00126 m_lastError = Syndication::Success;
00127
00128 if (!formatHint.isNull() && m_parsers.contains(formatHint))
00129 {
00130 if (m_parsers[formatHint]->accept(source))
00131 {
00132 SpecificDocumentPtr doc = m_parsers[formatHint]->parse(source);
00133 if (!doc->isValid())
00134 {
00135 m_lastError = InvalidFormat;
00136 return FeedPtr();
00137 }
00138
00139 return m_mappers[formatHint]->map(doc);
00140 }
00141 }
00142
00143 Q_FOREACH (AbstractParser* i, m_parserList)
00144 {
00145 if (i->accept(source))
00146 {
00147 SpecificDocumentPtr doc = i->parse(source);
00148 if (!doc->isValid())
00149 {
00150 m_lastError = InvalidFormat;
00151 return FeedPtr();
00152 }
00153
00154 return m_mappers[i->format()]->map(doc);
00155 }
00156 }
00157 if (source.asDomDocument().isNull())
00158 m_lastError = InvalidXml;
00159 else
00160 m_lastError = XmlNotAccepted;
00161
00162 return FeedPtr();
00163 }
00164
00165 template <class T>
00166 Syndication::ErrorCode ParserCollectionImpl<T>::lastError() const
00167 {
00168 return m_lastError;
00169 }
00170
00171 template <class T>
00172 ParserCollectionImpl<T>::ParserCollectionImpl(const ParserCollectionImpl&)
00173 {
00174 }
00175
00176 template <class T>
00177 ParserCollectionImpl<T>& ParserCollectionImpl<T>::operator=(const ParserCollectionImpl&)
00178 {
00179 return *this;
00180 }
00181
00182 }
00183
00184 #endif // SYNDICATION_PARSERCOLLECTIONIMPL_H