Syndication Library
parsercollectionimpl.h
00001 /* 00002 * This file is part of the syndication library 00003 * 00004 * Copyright (C) 2005 Frank Osterfeld <osterfeld@kde.org> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Library General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Library General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Library General Public License 00017 * along with this library; see the file COPYING.LIB. If not, write to 00018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 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 //@cond PRIVATE 00043 // default implementation of ParserCollection. This is separated 00044 // from the interface to move the implementation out of the public API 00045 // (template classes require implementations to be in the header) 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 //@endcond 00078 00079 //template <class T> 00080 //class ParserCollectionImpl<T>::ParserCollectionImplPrivate 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 } // namespace Syndication 00183 00184 #endif // SYNDICATION_PARSERCOLLECTIONIMPL_H
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 7 2012 23:57:26 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 7 2012 23:57:26 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.