File indexing completed on 2024-12-01 03:37:35

0001 /*
0002     This file is part of KDE.
0003 
0004     SPDX-FileCopyrightText: 2009 Eckhart Wörner <ewoerner@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "parser.h"
0010 #include <QDebug>
0011 
0012 using namespace Attica;
0013 
0014 template<class T>
0015 Parser<T>::~Parser()
0016 {
0017 }
0018 
0019 template<class T>
0020 T Parser<T>::parse(const QString &xmlString)
0021 {
0022     QStringList elements = xmlElement();
0023     T item;
0024 
0025     QXmlStreamReader xml(xmlString);
0026 
0027     while (!xml.atEnd()) {
0028         xml.readNext();
0029 
0030         if (xml.isStartElement()) {
0031             if (xml.name() == QLatin1String("meta")) {
0032                 parseMetadataXml(xml);
0033             } else if (elements.contains(xml.name())) {
0034                 item = parseXml(xml);
0035             }
0036         }
0037     }
0038     if (xml.hasError()) {
0039         // TODO: error handling in metadata?
0040         qWarning() << "parse():: XML Error: " << xml.errorString() << "\nIn XML:\n" << xmlString;
0041     }
0042 
0043     return item;
0044 }
0045 
0046 template<class T>
0047 typename T::List Parser<T>::parseList(const QString &xmlString)
0048 {
0049     /*
0050             QString testxml = QString("<?xml version=\"1.0\"?>\
0051     <ocs>\
0052      <meta>\
0053       <status>ok</status>\
0054       <statuscode>100</statuscode>\
0055       <message></message>\
0056      </meta>\
0057      <data>\
0058       <buildservice>\
0059        <id>obs</id>\
0060        <name>openSUSE Build Service</name>\
0061        <registrationurl>foobar.com</registrationurl>\
0062        <supportedtargets>\
0063         <target>openSUSE 11.2 32bit Intel</target>\
0064         <target>openSUSE 11.3 64bit Intel</target>\
0065         <target>openSUSE 11.3 32bit Intel</target>\
0066         <target>openSUSE 11.3 64bit Intel</target>\
0067        </supportedtargets>\
0068       </buildservice>\
0069       <buildservice>\
0070        <id>mbs</id>\
0071        <name>MeeGo Build Service</name>\
0072        <registrationurl>foobar42.com</registrationurl>\
0073        <supportedtargets>\
0074         <target>MeeGo 1.0 Intel</target>\
0075         <target>MeeGo 1.0 ARM</target>\
0076         <target>MeeGo 1.1 Intel</target>\
0077         <target>MeeGo 1.1 ARM</target>\
0078        </supportedtargets>\
0079       </buildservice>\
0080       <buildservice>\
0081        <id>sbs</id>\
0082        <name>Sebas' Build Service</name>\
0083        <registrationurl>foobar42.com</registrationurl>\
0084        <supportedtargets>\
0085         <target>sebasix 1.3 33bit</target>\
0086         <target>sebasis 4.4 14bit</target>\
0087         <target>sebasix 1.3 65bit</target>\
0088         <target>sebasis 4.4 37bit</target>\
0089        </supportedtargets>\
0090       </buildservice>\
0091      </data>\
0092     </ocs>\
0093      ");
0094 
0095         qCDebug(ATTICA) << "parsing list:" << xmlString;
0096         */
0097     QStringList elements = xmlElement();
0098     typename T::List items;
0099 
0100     // QXmlStreamReader xml( xmlString );
0101     QXmlStreamReader xml(xmlString);
0102 
0103     while (!xml.atEnd()) {
0104         xml.readNext();
0105         // qCDebug(ATTICA) << "parseList():: Looking for:" << xml.name().toString();
0106         if (xml.isStartElement()) {
0107             if (xml.name() == QLatin1String("data")) {
0108                 while (!xml.atEnd()) {
0109                     xml.readNext();
0110 
0111                     if (xml.isEndElement() && xml.name() == QLatin1String("data")) {
0112                         break;
0113                     }
0114 
0115                     if (xml.isStartElement() && elements.contains(xml.name())) {
0116                         // qCDebug(ATTICA) << "xxxxxxxxx New Item!" << xml.name().toString();
0117                         items.append(parseXml(xml));
0118                     }
0119                 }
0120             } else if (xml.name() == QLatin1String("meta")) {
0121                 parseMetadataXml(xml);
0122             }
0123         }
0124     }
0125     if (xml.hasError()) {
0126         // TODO: error handling in metadata?
0127         qWarning() << "parseList():: XML Error: " << xml.errorString() << "\nIn xml name" << xml.name() << "with text" << xml.text() << "at offset:\n"
0128                    << xml.characterOffset() << "\nIn XML:\n"
0129                    << xmlString;
0130     }
0131 
0132     return items;
0133 }
0134 
0135 template<class T>
0136 void Parser<T>::parseMetadataXml(QXmlStreamReader &xml)
0137 {
0138     while (!xml.atEnd()) {
0139         xml.readNext();
0140         if (xml.isEndElement() && xml.name() == QLatin1String("meta")) {
0141             break;
0142         } else if (xml.isStartElement()) {
0143             if (xml.name() == QLatin1String("status")) {
0144                 m_metadata.setStatusString(xml.readElementText());
0145             } else if (xml.name() == QLatin1String("statuscode")) {
0146                 m_metadata.setStatusCode(xml.readElementText().toInt());
0147             } else if (xml.name() == QLatin1String("message")) {
0148                 m_metadata.setMessage(xml.readElementText());
0149             } else if (xml.name() == QLatin1String("totalitems")) {
0150                 m_metadata.setTotalItems(xml.readElementText().toInt());
0151             } else if (xml.name() == QLatin1String("itemsperpage")) {
0152                 m_metadata.setItemsPerPage(xml.readElementText().toInt());
0153             }
0154         }
0155     }
0156     if (xml.hasError()) {
0157         // TODO: error handling in metadata?
0158         qWarning() << "XML Error: " << xml.errorString();
0159     }
0160 }
0161 
0162 template<class T>
0163 Metadata Parser<T>::metadata() const
0164 {
0165     return m_metadata;
0166 }