Initial import.
[dorian] / opshandler.h
1 #ifndef OPSHANDLER_H
2 #define OPSHANDLER_H
3
4 #include <QXmlContentHandler>
5
6 #include "book.h"
7
8 /** XML content handler for OPS format. */
9 class OpsHandler: public QXmlContentHandler
10 {
11 public:
12     OpsHandler(Book &book): mBook(book) {}
13     bool endDocument() {return true;}
14     bool endPrefixMapping(const QString &) {return true;}
15     QString errorString() const {return "";}
16     bool ignorableWhitespace(const QString &) {return true;}
17     bool processingInstruction(const QString &, const QString &) {
18         return true;
19     }
20     void setDocumentLocator(QXmlLocator *) {}
21     bool skippedEntity(const QString &) {return true;}
22     bool startDocument() {return true;}
23     bool startPrefixMapping(const QString &, const QString &) {return true;}
24
25     bool characters(const QString &ch)
26     {
27         mCurrentText += ch;
28         return true;
29     }
30
31     bool endElement(const QString &namespaceUri, const QString &name,
32                     const QString &qName)
33     {
34         (void)namespaceUri;
35         (void)qName;
36         if (mCurrentText != "") {
37             if (name == "title") {
38                 mBook.title = mCurrentText;
39             }
40             else if (name == "creator") {
41                 mBook.creators.append(mCurrentText);
42             }
43             else if (name == "publisher") {
44                 mBook.publisher = mCurrentText;
45             }
46             else if (name == "subject") {
47                 mBook.subject = mCurrentText;
48             }
49             else if (name == "source") {
50                 mBook.source = mCurrentText;
51             }
52             else if (name == "rights") {
53                 mBook.rights = mCurrentText;
54             }
55         }
56         return true;
57     }
58
59     bool startElement(const QString &namespaceUri, const QString &name,
60                       const QString &qName, const QXmlAttributes &attrs)
61     {
62         (void)namespaceUri;
63         (void)name;
64         (void)qName;
65         (void)attrs;
66         mCurrentText = "";
67
68         if (name == "item") {
69             Book::ContentItem item;
70             item.href = mBook.rootPath() + "/" + attrs.value("href");
71             item.type = attrs.value("media-type");
72             QString key = attrs.value("id");
73             mBook.content[key] = item;
74             qDebug() << "OpsHandler::startElement: item" << key << "type"
75                     << item.type << "href" << item.href;
76         }
77         else if (name == "itemref") {
78             mBook.toc.append(attrs.value("idref"));
79             qDebug() << "OpsHandler::startElement: itemref" << attrs.value("idref");
80         }
81         return true;
82     }
83
84 private:
85     Book &mBook;
86     QString mCurrentText;
87 };
88
89 #endif // OPSHANDLER_H