File indexing completed on 2024-04-21 04:34:33

0001 /*
0002  * This file is part of KDevelop Krazy2 Plugin.
0003  *
0004  * Copyright 2012 Daniel Calviño Sánchez <danxuliu@gmail.com>
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License
0008  * as published by the Free Software Foundation; either version 2
0009  * of the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program. If not, see <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "checkerlistparser.h"
0021 
0022 #include <QRegExp>
0023 
0024 #include "checker.h"
0025 
0026 //public:
0027 
0028 CheckerListParser::CheckerListParser():
0029     m_checkerList(nullptr),
0030     m_extra(false) {
0031 }
0032 
0033 void CheckerListParser::setCheckerList(QList< const Checker* >* checkerList) {
0034     Q_ASSERT(checkerList);
0035     Q_ASSERT(checkerList->isEmpty());
0036 
0037     m_checkerList = checkerList;
0038 }
0039 
0040 void CheckerListParser::parse(const QString& data) {
0041     Q_ASSERT(m_checkerList);
0042 
0043     m_xmlStreamReader.addData(data);
0044 
0045     while (!m_xmlStreamReader.atEnd()) {
0046         m_xmlStreamReader.readNext();
0047 
0048         if (isStartElement("file-type")) {
0049             processFileTypeStart();
0050         }
0051         if (isStartElement("plugin")) {
0052             processPluginStart();
0053         }
0054         if (isEndElement("explanation")) {
0055             processExplanationEnd();
0056         }
0057         if (isStartElement("extra")) {
0058             processExtraStart();
0059         }
0060         if (isEndElement("extra")) {
0061             processExtraEnd();
0062         }
0063         if (m_xmlStreamReader.isCharacters()) {
0064             m_text = m_xmlStreamReader.text().toString().trimmed();
0065         }
0066     }
0067 }
0068 
0069 //private:
0070 
0071 bool CheckerListParser::isStartElement(const QString& elementName) {
0072     return m_xmlStreamReader.isStartElement() &&
0073            m_xmlStreamReader.name() == elementName;
0074 }
0075 
0076 bool CheckerListParser::isEndElement(const QString& elementName) {
0077     return m_xmlStreamReader.isEndElement() &&
0078            m_xmlStreamReader.name() == elementName;
0079 }
0080 
0081 void CheckerListParser::processFileTypeStart() {
0082     m_fileType = m_xmlStreamReader.attributes().value("value").toString();
0083 }
0084 
0085 void CheckerListParser::processPluginStart() {
0086     QString checkerDescription = m_xmlStreamReader.attributes().value("short-desc").toString();
0087     if (checkerDescription == "(no description available)") {
0088         checkerDescription = "";
0089     }
0090 
0091     auto  checker = new Checker();
0092     checker->setName(m_xmlStreamReader.attributes().value("name").toString());
0093     checker->setDescription(checkerDescription);
0094     checker->setFileType(m_fileType);
0095     checker->setExtra(m_extra);
0096 
0097     m_checkerBeingInitialized = checker;
0098 
0099     m_checkerList->append(checker);
0100 }
0101 
0102 void CheckerListParser::processExplanationEnd() {
0103     if (m_text != "(no explanation available)") {
0104         m_checkerBeingInitialized->setExplanation(m_text);
0105     }
0106 }
0107 
0108 void CheckerListParser::processExtraStart() {
0109     m_extra = true;
0110 }
0111 
0112 void CheckerListParser::processExtraEnd() {
0113     m_extra = false;
0114 }