File indexing completed on 2024-09-15 06:35:52
0001 /* 0002 This file is part of the syndication library 0003 SPDX-FileCopyrightText: 2005 Frank Osterfeld <osterfeld@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #ifndef SYNDICATION_PARSERCOLLECTION_H 0009 #define SYNDICATION_PARSERCOLLECTION_H 0010 0011 #include "abstractparser.h" 0012 #include "documentsource.h" 0013 #include "feed.h" 0014 #include "global.h" 0015 #include "mapper.h" 0016 #include "specificdocument.h" 0017 0018 #include <QString> 0019 0020 namespace Syndication 0021 { 0022 /** 0023 * A collection of format-specific parser implementations. 0024 * To parse a feed source, pass it to the parse() method of this class. 0025 * In most cases, you should use the global singleton instance 0026 * Syndication::parserCollection(). 0027 * When loading the source from the web, use Loader instead of using 0028 * this class directly. 0029 * 0030 * Example code: 0031 * 0032 * @code 0033 * ... 0034 * QFile someFile(somePath); 0035 * ... 0036 * DocumentSource src(someFile.readAll()); 0037 * someFile.close(); 0038 * 0039 * FeedPtr feed = parserCollection()->parse(src); 0040 * 0041 * if (feed) 0042 * { 0043 * QString title = feed->title(); 0044 * QList<ItemPtr> items = feed->items(); 0045 * ... 0046 * } 0047 * @endcode 0048 * 0049 * The template parameter T is the abstraction class parsed documents 0050 * should be mapped to. If you want to use your own abstraction MyFeed, 0051 * implement ParserCollection<MyFeed> (Note that you have to provide 0052 * mapper implementations for every feed format then). 0053 * 0054 * @author Frank Osterfeld 0055 */ 0056 template<class T> 0057 class SYNDICATION_EXPORT ParserCollection 0058 { 0059 public: 0060 /** destructor */ 0061 virtual ~ParserCollection() 0062 { 0063 } 0064 0065 /** 0066 * tries to parse a given source with the parsers registered. 0067 * The source is passed to the first parser that accepts it. 0068 * 0069 * @param source The source to be parsed 0070 * @param formatHint An optional hint which parser to test first. If 0071 * there is a parser with the given hint as format string (e.g., 0072 * "rss2", "atom", "rdf"...), it is asked first to accept the source. 0073 * This can avoid unnecessary AbstractParser::accept() checks and speed 0074 * up parsing. See also AbstractParser::format(). 0075 * @return The feed document parsed from the source, or NULL if no 0076 * parser accepted the source. 0077 */ 0078 virtual QSharedPointer<T> parse(const DocumentSource &source, const QString &formatHint = QString()) = 0; 0079 0080 /** 0081 * returns the error code of the last parse() call. 0082 * 0083 * @return the last error, or Success if parse() was successful 0084 * or not yet called at all. 0085 */ 0086 virtual ErrorCode lastError() const = 0; 0087 0088 /** 0089 * Adds a parser and corresponding mapper to the collection. 0090 * AbstractParser::format() must be unique 0091 * in the collection. If there is already a parser with the same format 0092 * string, the parser isn't added. 0093 * 0094 * @note ownership for both @c parser and @c mapper is taken by the 0095 * implementation, so don't delete them in your code! 0096 * 0097 * @param parser The parser to be registered 0098 * @param mapper the mapper that should be used for building the 0099 * abstraction 0100 * @return whether the parser was successfully registered or not. 0101 */ 0102 virtual bool registerParser(AbstractParser *parser, Mapper<T> *mapper) = 0; 0103 0104 /** 0105 * Changes the specific format to abstraction mapping for a parser. 0106 * 0107 * @param format the format string of the parser whose 0108 * mapping should be changed. See AbstractParser::format. 0109 * @param mapper Mapper implementation doing the mapping from the 0110 * format specific representation to abstraction of type T. 0111 */ 0112 virtual void changeMapper(const QString &format, Mapper<T> *mapper) = 0; 0113 }; 0114 0115 } // namespace Syndication 0116 0117 #endif // SYNDICATION_PARSERCOLLECTION_H