File indexing completed on 2024-12-15 04:13:43

0001 /*
0002     Kchmviewer - a CHM and EPUB file viewer with broad language support
0003     SPDX-FileCopyrightText: 2004-2014 George Yunaev gyunaev@ulduzsoft.com
0004 
0005     SPDX-License-Identifier: GPL-3.0-or-later
0006 */
0007 
0008 #include "helperxmlhandler_epubcontent.h"
0009 
0010 HelperXmlHandler_EpubContent::HelperXmlHandler_EpubContent()
0011 {
0012     m_state = STATE_NONE;
0013 }
0014 
0015 bool HelperXmlHandler_EpubContent::startElement(const QString &, const QString &localName, const QString &, const QXmlAttributes &atts)
0016 {
0017     // <metadata> tag contains the medatada which goes into m_metadata
0018     if (localName == QLatin1String("metadata")) {
0019         m_state = STATE_IN_METADATA;
0020     } else if (localName == QLatin1String("manifest")) {
0021         m_state = STATE_IN_MANIFEST;
0022     } else if (localName == QLatin1String("spine")) {
0023         m_state = STATE_IN_SPINE;
0024     } else if (m_state == STATE_IN_METADATA) { // we don't need to store the first 'metadata' here
0025         // Now handle the states
0026         m_tagname = localName;
0027     } else if (m_state == STATE_IN_MANIFEST && localName == QLatin1String("item")) {
0028         int idx_id = atts.index(QLatin1String("id"));
0029         int idx_href = atts.index(QLatin1String("href"));
0030         int idx_mtype = atts.index(QLatin1String("media-type"));
0031 
0032         if (idx_id == -1 || idx_href == -1 || idx_mtype == -1) {
0033             return false;
0034         }
0035 
0036         manifest[atts.value(idx_id)] = atts.value(idx_href);
0037 
0038         if (atts.value(idx_mtype) == QLatin1String("application/x-dtbncx+xml")) {
0039             tocname = atts.value(idx_href);
0040         }
0041 
0042         // qDebug() << "MANIFEST: " << atts.value( idx_id ) << "->" << atts.value( idx_href );
0043     } else if (m_state == STATE_IN_SPINE && localName == QLatin1String("itemref")) {
0044         int idx = atts.index(QLatin1String("idref"));
0045 
0046         if (idx == -1) {
0047             return false;
0048         }
0049 
0050         spine.push_back(atts.value(idx));
0051         // qDebug() << "SPINE: " << atts.value( idx );
0052     }
0053 
0054     return true;
0055 }
0056 
0057 bool HelperXmlHandler_EpubContent::characters(const QString &ch)
0058 {
0059     if (m_state == STATE_IN_METADATA && !m_tagname.isEmpty() && !ch.trimmed().isEmpty()) {
0060         // Some metadata may be duplicated; we concantenate them with |
0061         if (metadata.contains(m_tagname)) {
0062             metadata[m_tagname].append(QStringLiteral("|"));
0063             metadata[m_tagname].append(ch.trimmed());
0064         } else {
0065             metadata[m_tagname] = ch.trimmed();
0066         }
0067 
0068         // qDebug() << "METATAG: " << m_tagname << " " << metadata[ m_tagname ];
0069     }
0070 
0071     return true;
0072 }
0073 
0074 bool HelperXmlHandler_EpubContent::endElement(const QString &, const QString &, const QString &qName)
0075 {
0076     if (qName == QLatin1String("manifest") || qName == QLatin1String("metadata") || qName == QLatin1String("spine")) {
0077         m_state = STATE_NONE;
0078     }
0079 
0080     return true;
0081 }