/* This file is part of KDE. SPDX-FileCopyrightText: 2009 Eckhart Wörner SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL */ #include "parser.h" #include using namespace Attica; template Parser::~Parser() { } template T Parser::parse(const QString &xmlString) { QStringList elements = xmlElement(); T item; QXmlStreamReader xml(xmlString); while (!xml.atEnd()) { xml.readNext(); if (xml.isStartElement()) { if (xml.name() == QLatin1String("meta")) { parseMetadataXml(xml); } else if (elements.contains(xml.name())) { item = parseXml(xml); } } } if (xml.hasError()) { // TODO: error handling in metadata? qWarning() << "parse():: XML Error: " << xml.errorString() << "\nIn XML:\n" << xmlString; } return item; } template typename T::List Parser::parseList(const QString &xmlString) { /* QString testxml = QString("\ \ \ ok\ 100\ \ \ \ \ obs\ openSUSE Build Service\ foobar.com\ \ openSUSE 11.2 32bit Intel\ openSUSE 11.3 64bit Intel\ openSUSE 11.3 32bit Intel\ openSUSE 11.3 64bit Intel\ \ \ \ mbs\ MeeGo Build Service\ foobar42.com\ \ MeeGo 1.0 Intel\ MeeGo 1.0 ARM\ MeeGo 1.1 Intel\ MeeGo 1.1 ARM\ \ \ \ sbs\ Sebas' Build Service\ foobar42.com\ \ sebasix 1.3 33bit\ sebasis 4.4 14bit\ sebasix 1.3 65bit\ sebasis 4.4 37bit\ \ \ \ \ "); qCDebug(ATTICA) << "parsing list:" << xmlString; */ QStringList elements = xmlElement(); typename T::List items; // QXmlStreamReader xml( xmlString ); QXmlStreamReader xml(xmlString); while (!xml.atEnd()) { xml.readNext(); // qCDebug(ATTICA) << "parseList():: Looking for:" << xml.name().toString(); if (xml.isStartElement()) { if (xml.name() == QLatin1String("data")) { while (!xml.atEnd()) { xml.readNext(); if (xml.isEndElement() && xml.name() == QLatin1String("data")) { break; } if (xml.isStartElement() && elements.contains(xml.name())) { // qCDebug(ATTICA) << "xxxxxxxxx New Item!" << xml.name().toString(); items.append(parseXml(xml)); } } } else if (xml.name() == QLatin1String("meta")) { parseMetadataXml(xml); } } } if (xml.hasError()) { // TODO: error handling in metadata? qWarning() << "parseList():: XML Error: " << xml.errorString() << "\nIn xml name" << xml.name() << "with text" << xml.text() << "at offset:\n" << xml.characterOffset() << "\nIn XML:\n" << xmlString; } return items; } template void Parser::parseMetadataXml(QXmlStreamReader &xml) { while (!xml.atEnd()) { xml.readNext(); if (xml.isEndElement() && xml.name() == QLatin1String("meta")) { break; } else if (xml.isStartElement()) { if (xml.name() == QLatin1String("status")) { m_metadata.setStatusString(xml.readElementText()); } else if (xml.name() == QLatin1String("statuscode")) { m_metadata.setStatusCode(xml.readElementText().toInt()); } else if (xml.name() == QLatin1String("message")) { m_metadata.setMessage(xml.readElementText()); } else if (xml.name() == QLatin1String("totalitems")) { m_metadata.setTotalItems(xml.readElementText().toInt()); } else if (xml.name() == QLatin1String("itemsperpage")) { m_metadata.setItemsPerPage(xml.readElementText().toInt()); } } } if (xml.hasError()) { // TODO: error handling in metadata? qWarning() << "XML Error: " << xml.errorString(); } } template Metadata Parser::metadata() const { return m_metadata; }