File indexing completed on 2024-05-12 05:10:14

0001 /***************************************************************************
0002     Copyright (C) 2008-2009 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #ifndef TELLICO_IMPORT_XMLSTATEHANDLER_H
0026 #define TELLICO_IMPORT_XMLSTATEHANDLER_H
0027 
0028 #include <QXmlStreamAttributes>
0029 #include <QUrl>
0030 
0031 #include "../datavectors.h"
0032 
0033 namespace Tellico {
0034   namespace Import {
0035     namespace SAX {
0036 
0037 class StateData {
0038 public:
0039   StateData() : syntaxVersion(0), collType(0), defaultFields(false), loadImages(false), hasImages(false), showImageLoadErrors(true) {}
0040   QString text;
0041   QString error;
0042   QString ns; // namespace
0043   QString textBuffer;
0044   uint syntaxVersion;
0045   QString collTitle;
0046   int collType;
0047   QString entryName;
0048   Data::CollPtr coll;
0049   Data::FieldList fields;
0050   Data::FieldPtr currentField;
0051   Data::EntryList entries;
0052   QString modifiedDate;
0053   FilterPtr filter;
0054   Data::BorrowerPtr borrower;
0055   bool defaultFields;
0056   bool loadImages;
0057   bool hasImages;
0058   bool showImageLoadErrors;
0059   QUrl baseUrl;
0060 };
0061 
0062 class StateHandler {
0063 public:
0064   StateHandler(StateData* data) : d(data) {}
0065   virtual ~StateHandler() {}
0066 
0067   virtual bool start(const QStringRef& nsUri, const QStringRef& localName, const QXmlStreamAttributes& attributes) = 0;
0068   virtual bool   end(const QStringRef& nsUri, const QStringRef& localName) = 0;
0069 
0070   StateHandler* nextHandler(const QStringRef& nsUri, const QStringRef& localName);
0071 protected:
0072   StateData* d;
0073 private:
0074   Q_DISABLE_COPY(StateHandler)
0075   virtual StateHandler* nextHandlerImpl(const QStringRef&, const QStringRef&)  { return nullptr; }
0076 };
0077 
0078 class NullHandler : public StateHandler {
0079 public:
0080   NullHandler(StateData* data) : StateHandler(data) {}
0081   virtual ~NullHandler() {}
0082 
0083   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE { return true; }
0084   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE { return true; }
0085 };
0086 
0087 class RootHandler : public StateHandler {
0088 public:
0089   RootHandler(StateData* data) : StateHandler(data) {}
0090   virtual ~RootHandler() {}
0091 
0092   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE { return true; }
0093   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE { return true; }
0094 
0095 private:
0096   virtual StateHandler* nextHandlerImpl(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0097 };
0098 
0099 class DocumentHandler : public StateHandler {
0100 public:
0101   DocumentHandler(StateData* data) : StateHandler(data) {}
0102   virtual ~DocumentHandler() {}
0103 
0104   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0105   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0106 
0107 private:
0108   virtual StateHandler* nextHandlerImpl(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0109 };
0110 
0111 class CollectionHandler : public StateHandler {
0112 public:
0113   CollectionHandler(StateData* data) : StateHandler(data) {}
0114   virtual ~CollectionHandler() {}
0115 
0116   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0117   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0118 
0119 private:
0120   virtual StateHandler* nextHandlerImpl(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0121 };
0122 
0123 class FieldsHandler : public StateHandler {
0124 public:
0125   FieldsHandler(StateData* data) : StateHandler(data) {}
0126   virtual ~FieldsHandler() {}
0127 
0128   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0129   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0130 
0131 private:
0132   virtual StateHandler* nextHandlerImpl(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0133 };
0134 
0135 class FieldHandler : public StateHandler {
0136 public:
0137   FieldHandler(StateData* data) : StateHandler(data), isI18n(false) {}
0138   virtual ~FieldHandler() {}
0139 
0140   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0141   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0142 
0143 private:
0144   virtual StateHandler* nextHandlerImpl(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0145   bool isI18n;
0146 };
0147 
0148 class FieldPropertyHandler : public StateHandler {
0149 public:
0150   FieldPropertyHandler(StateData* data) : StateHandler(data) {}
0151   virtual ~FieldPropertyHandler() {}
0152 
0153   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0154   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0155 
0156 private:
0157   QString m_propertyName;
0158 };
0159 
0160 class BibtexPreambleHandler : public StateHandler {
0161 public:
0162   BibtexPreambleHandler(StateData* data) : StateHandler(data) {}
0163   virtual ~BibtexPreambleHandler() {}
0164 
0165   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0166   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0167 };
0168 
0169 class BibtexMacrosHandler : public StateHandler {
0170 public:
0171   BibtexMacrosHandler(StateData* data) : StateHandler(data) {}
0172   virtual ~BibtexMacrosHandler() {}
0173 
0174   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0175   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0176 
0177 private:
0178   virtual StateHandler* nextHandlerImpl(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0179 };
0180 
0181 class BibtexMacroHandler : public StateHandler {
0182 public:
0183   BibtexMacroHandler(StateData* data) : StateHandler(data) {}
0184   virtual ~BibtexMacroHandler() {}
0185 
0186   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0187   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0188 
0189 private:
0190   QString m_macroName;
0191 };
0192 
0193 class EntryHandler : public StateHandler {
0194 public:
0195   EntryHandler(StateData* data) : StateHandler(data) {}
0196   virtual ~EntryHandler() {}
0197 
0198   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0199   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0200 
0201 private:
0202   virtual StateHandler* nextHandlerImpl(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0203 };
0204 
0205 class FieldValueContainerHandler : public StateHandler {
0206 public:
0207   FieldValueContainerHandler(StateData* data) : StateHandler(data) {}
0208   virtual ~FieldValueContainerHandler() {}
0209 
0210   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0211   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0212 
0213 private:
0214   virtual StateHandler* nextHandlerImpl(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0215 };
0216 
0217 class FieldValueHandler : public StateHandler {
0218 public:
0219   FieldValueHandler(StateData* data) : StateHandler(data)
0220     , m_i18n(false), m_validateISBN(false) {}
0221   virtual ~FieldValueHandler() {}
0222 
0223   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0224   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0225 
0226 private:
0227   virtual StateHandler* nextHandlerImpl(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0228   bool m_i18n;
0229   bool m_validateISBN;
0230 };
0231 
0232 class DateValueHandler : public StateHandler {
0233 public:
0234   DateValueHandler(StateData* data) : StateHandler(data) {}
0235   virtual ~DateValueHandler() {}
0236 
0237   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0238   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0239 };
0240 
0241 class TableColumnHandler : public StateHandler {
0242 public:
0243   TableColumnHandler(StateData* data) : StateHandler(data) {}
0244   virtual ~TableColumnHandler() {}
0245 
0246   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0247   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0248 };
0249 
0250 class ImagesHandler : public StateHandler {
0251 public:
0252   ImagesHandler(StateData* data) : StateHandler(data) {}
0253   virtual ~ImagesHandler() {}
0254 
0255   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0256   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0257 
0258 private:
0259   virtual StateHandler* nextHandlerImpl(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0260 };
0261 
0262 class ImageHandler : public StateHandler {
0263 public:
0264   ImageHandler(StateData* data) : StateHandler(data)
0265     , m_link(false), m_width(0), m_height(0) {}
0266   virtual ~ImageHandler() {}
0267 
0268   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0269   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0270 
0271 private:
0272   QString m_format;
0273   bool m_link;
0274   QString m_imageId;
0275   int m_width;
0276   int m_height;
0277 };
0278 
0279 class FiltersHandler : public StateHandler {
0280 public:
0281   FiltersHandler(StateData* data) : StateHandler(data) {}
0282   virtual ~FiltersHandler() {}
0283 
0284   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0285   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0286 
0287 private:
0288   virtual StateHandler* nextHandlerImpl(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0289 };
0290 
0291 class FilterHandler : public StateHandler {
0292 public:
0293   FilterHandler(StateData* data) : StateHandler(data) {}
0294   virtual ~FilterHandler() {}
0295 
0296   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0297   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0298 
0299 private:
0300   virtual StateHandler* nextHandlerImpl(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0301 };
0302 
0303 class FilterRuleHandler : public StateHandler {
0304 public:
0305   FilterRuleHandler(StateData* data) : StateHandler(data) {}
0306   virtual ~FilterRuleHandler() {}
0307 
0308   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0309   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0310 };
0311 
0312 class BorrowersHandler : public StateHandler {
0313 public:
0314   BorrowersHandler(StateData* data) : StateHandler(data) {}
0315   virtual ~BorrowersHandler() {}
0316 
0317   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0318   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0319 
0320 private:
0321   virtual StateHandler* nextHandlerImpl(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0322 };
0323 
0324 class BorrowerHandler : public StateHandler {
0325 public:
0326   BorrowerHandler(StateData* data) : StateHandler(data) {}
0327   virtual ~BorrowerHandler() {}
0328 
0329   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0330   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0331 
0332 private:
0333   virtual StateHandler* nextHandlerImpl(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0334 };
0335 
0336 class LoanHandler : public StateHandler {
0337 public:
0338   LoanHandler(StateData* data) : StateHandler(data)
0339     , m_id(-1), m_inCalendar(false) {}
0340   virtual ~LoanHandler() {}
0341 
0342   virtual bool start(const QStringRef&, const QStringRef&, const QXmlStreamAttributes&) Q_DECL_OVERRIDE;
0343   virtual bool   end(const QStringRef&, const QStringRef&) Q_DECL_OVERRIDE;
0344 
0345 private:
0346   int m_id;
0347   QString m_uid;
0348   QString m_loanDate;
0349   QString m_dueDate;
0350   bool m_inCalendar;
0351 };
0352 
0353     }
0354   }
0355 }
0356 #endif