|
| 1 | +/************************************************************************** |
| 2 | + * This file is part of QXmlEdit * |
| 3 | + * Copyright (C) 2019 by Luca Bellonda and individual contributors * |
| 4 | + * as indicated in the AUTHORS file * |
| 5 | + * lbellonda _at_ gmail.com * |
| 6 | + * * |
| 7 | + * This library is free software; you can redistribute it and/or * |
| 8 | + * modify it under the terms of the GNU Library General Public * |
| 9 | + * License as published by the Free Software Foundation; either * |
| 10 | + * version 2 of the License, or (at your option) any later version. * |
| 11 | + * * |
| 12 | + * This library is distributed in the hope that it will be useful, * |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 15 | + * Library General Public License for more details. * |
| 16 | + * * |
| 17 | + * You should have received a copy of the GNU Library General Public * |
| 18 | + * License along with this library; if not, write to the * |
| 19 | + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * |
| 20 | + * Boston, MA 02110-1301 USA * |
| 21 | + **************************************************************************/ |
| 22 | + |
| 23 | +#include "xmltest.h" |
| 24 | +#include "utils.h" |
| 25 | +#include "modules/xml/xmlerrormanagerdialog.h" |
| 26 | + |
| 27 | +XMLTest::XMLTest(QObject *parent) : QObject(parent) |
| 28 | +{ |
| 29 | +} |
| 30 | + |
| 31 | +XMLTest::~XMLTest() |
| 32 | +{ |
| 33 | +} |
| 34 | + |
| 35 | +bool XMLTest::readTestXML(XMLLoadContext *context, QXmlStreamReader *xmlReader) |
| 36 | +{ |
| 37 | + while(!xmlReader->atEnd()) { |
| 38 | + xmlReader->readNext(); |
| 39 | + if(xmlReader->hasError()) { |
| 40 | + return context->setErrorFromReader(xmlReader); |
| 41 | + } |
| 42 | + switch(xmlReader->tokenType()) { |
| 43 | + default: |
| 44 | + break; |
| 45 | + case QXmlStreamReader::Invalid: |
| 46 | + return context->setErrorFromReader(xmlReader); |
| 47 | + case QXmlStreamReader::EntityReference: |
| 48 | + return context->setError(tr("This XML contains an entity reference.\nEntity references are not supported at the moment."), xmlReader); |
| 49 | + } |
| 50 | + }//for |
| 51 | + return true; |
| 52 | +} |
| 53 | + |
| 54 | +bool XMLTest::loadTestFileInnerStream(XMLLoadContext *context, QIODevice *ioDevice) |
| 55 | +{ |
| 56 | + QXmlStreamReader xmlReader ; |
| 57 | + xmlReader.setNamespaceProcessing(false); |
| 58 | + xmlReader.setDevice(ioDevice); |
| 59 | + readTestXML(context, &xmlReader); |
| 60 | + return !context->isError(); |
| 61 | +} |
| 62 | + |
| 63 | +bool XMLTest::testLoadFile(XMLLoadContext *context, const QString &filePath) |
| 64 | +{ |
| 65 | + if(!filePath.isEmpty()) { |
| 66 | + QFile file(filePath); |
| 67 | + if(file.open(QIODevice::ReadOnly)) { |
| 68 | + loadTestFileInnerStream(context, &file); |
| 69 | + file.close(); |
| 70 | + } else { |
| 71 | + context->setErrorWithMessage(tr("Unable to read data.\nError code is '%1', %2.").arg(file.error()).arg(file.errorString())); |
| 72 | + } |
| 73 | + } else { |
| 74 | + context->setErrorWithMessage(tr("File name empty.\nUnable to load.")); |
| 75 | + } |
| 76 | + return !context->isError(); |
| 77 | +} |
| 78 | + |
| 79 | +bool XMLTest::testXMLFile(UIDelegate *uiDelegate, const QString &filePath) |
| 80 | +{ |
| 81 | + XMLLoadContext context; |
| 82 | + if(!testLoadFile(&context, filePath) || context.isError()) { |
| 83 | + uiDelegate->error(context.errorMessage()); |
| 84 | + return false; |
| 85 | + } else { |
| 86 | + uiDelegate->message(tr("XML is valid.")); |
| 87 | + return true ; |
| 88 | + } |
| 89 | +} |
0 commit comments