Skip to content

Commit 5df1928

Browse files
committed
b-196 Test a XML file.
1 parent f3711ce commit 5df1928

21 files changed

+377
-2
lines changed

BRANCHES

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ devel - next release features
44

55
Branches:
66
---------
7+
196 - Test XML without loading it.
78
192 - Improve text editing.
89
191 - XMLTransforms: apply transformations while processing
910

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
0.9.14
2+
Test XML without loading it.
23
Fixed some menu labels.
34
Refactored project files for common options and create option to avoid transform warning in errors.
45
Issue #62, build fails with Qt 5.13.

src/MainWindow.ui

+9-1
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@
404404
<addaction name="actionBinaryViewer"/>
405405
<addaction name="actionCodePages"/>
406406
<addaction name="actionXSLExecutor"/>
407+
<addaction name="actionTestXML"/>
407408
</widget>
408409
<widget class="QMenu" name="menuView">
409410
<property name="title">
@@ -2591,6 +2592,14 @@
25912592
<string>Edit the current element in textual form.</string>
25922593
</property>
25932594
</action>
2595+
<action name="actionTestXML">
2596+
<property name="text">
2597+
<string>Test XML</string>
2598+
</property>
2599+
<property name="toolTip">
2600+
<string>Test a XML file.</string>
2601+
</property>
2602+
</action>
25942603
</widget>
25952604
<customwidgets>
25962605
<customwidget>
@@ -2614,7 +2623,6 @@
26142623
</customwidgets>
26152624
<resources>
26162625
<include location="risorse.qrc"/>
2617-
<include location="risorse.qrc"/>
26182626
</resources>
26192627
<connections/>
26202628
<slots>

src/QXmlEdit.pro

+3-1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ HEADERS = precompiled_app.h \
126126
snippet_interface.h \
127127
test.h \
128128
paths.h \
129+
xmltest.h \
129130
xsaxhandler.h \
130131
searchinfiles.h \
131132
scansax.h \
@@ -335,7 +336,8 @@ SOURCES = \
335336
modules/utils/animationutility.cpp \
336337
widgets/infooneditmode.cpp \
337338
modules/help/searchcommanddialog.cpp \
338-
widgets/shortcutinfo.cpp
339+
widgets/shortcutinfo.cpp \
340+
xmltest.cpp
339341

340342
FORMS = MainWindow.ui \
341343
configoptions.ui \

src/mainwindow.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -3958,3 +3958,9 @@ void MainWindow::on_actionEditAsText_triggered()
39583958
{
39593959
ui.editor->editSelectionAsText();
39603960
}
3961+
3962+
void MainWindow::on_actionTestXML_triggered()
3963+
{
3964+
_controller.testXML();
3965+
}
3966+

src/mainwindow.h

+1
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ private slots:
422422
void on_actionSearchCommand_triggered();
423423
void on_actionShowKeyboardShortcuts_triggered();
424424
void on_actionEditAsText_triggered();
425+
void on_actionTestXML_triggered();
425426
//----- other slots ------------------
426427

427428
void onClipboardDataChanged(bool isData);

src/mainwindowio.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,4 @@ bool MainWindow::showErrorAndAskUserIfContinue(QWidget * /*parent*/, XMLLoadCont
262262
}
263263
return false;
264264
}
265+

src/mainwndcontroller.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#ifdef QXMLEDIT_QT_SCXML_ENABLED
5353
#include <QtScxml/QScxmlStateMachine>
5454
#endif
55+
#include "xmltest.h"
5556
//----------
5657
ReplicaInfoProvider::ReplicaInfoProvider() {}
5758
ReplicaInfoProvider::~ReplicaInfoProvider() {}
@@ -418,3 +419,17 @@ bool MainWndController::checkSCXML()
418419
return returnValue ;
419420
#endif
420421
}
422+
423+
void MainWndController::testXML()
424+
{
425+
XMLTest testXML;
426+
QString filePath = QFileDialog::getOpenFileName(
427+
_w, tr("Open File"),
428+
QXmlEditData::sysFilePathForOperation(""),
429+
Utils::getFileFilterForOpenFile()
430+
);
431+
if(!filePath.isEmpty()) {
432+
testXML.testXMLFile(_w->uiDelegate, filePath);
433+
}
434+
}
435+

src/mainwndcontroller.h

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class MainWndController : public QObject, ReplicaInfoProvider
8484
bool checkSCXML();
8585
void sourceDecode(QBuffer *dataStream, QList<SourceMessage*> &errors, Regola *regola);
8686
QBuffer* getDataForSourceDecode();
87+
void testXML();
8788

8889
signals:
8990

src/modules/xml/xmlloadcontext.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ void XMLLoadContext::setErrorMessage(const QString &errorMessage)
6969
_errorMessage = errorMessage;
7070
}
7171

72+
bool XMLLoadContext::setErrorWithMessage(const QString &theErrorMessage)
73+
{
74+
setIsError(true);
75+
setErrorMessage(theErrorMessage);
76+
return false;
77+
}
78+
7279
void XMLLoadContext::copyErrorData(QXmlStreamReader *xmlReader)
7380
{
7481
setErrorPosition(xmlReader->lineNumber(), xmlReader->columnNumber(), xmlReader->characterOffset());

src/modules/xml/xmlloadcontext.h

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class LIBQXMLEDITSHARED_EXPORT XMLLoadContext
5050
void setIsError(bool isError);
5151
QString errorMessage() const;
5252
void setErrorMessage(const QString &errorMessage);
53+
bool setErrorWithMessage(const QString &errorMessage);
5354
void copyErrorData(QXmlStreamReader *xmlReader);
5455
/*!
5556
* \brief setError sets the error with the specified message and return false to chaing this method in a return

src/xmltest.cpp

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
}

src/xmltest.h

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
#ifndef XMLTEST_H
24+
#define XMLTEST_H
25+
26+
#include <xmlEdit.h>
27+
#include "modules/xml/xmlloadcontext.h"
28+
29+
class UIDelegate;
30+
31+
class XMLTest : public QObject
32+
{
33+
Q_OBJECT
34+
public:
35+
explicit XMLTest(QObject *parent = 0);
36+
virtual ~XMLTest();
37+
38+
bool readTestXML(XMLLoadContext *context, QXmlStreamReader *xmlReader);
39+
bool readTestFromStream(XMLLoadContext *context, QXmlStreamReader *xmlReader);
40+
bool loadTestFileInnerStream(XMLLoadContext *context, QIODevice *ioDevice);
41+
42+
public:
43+
bool testLoadFile(XMLLoadContext *context, const QString &filePath);
44+
bool testXMLFile(UIDelegate *uiDelegate, const QString &filePath);
45+
46+
signals:
47+
48+
public slots:
49+
};
50+
51+
#endif // XMLTEST_H

test/TestQXmlEdit.h

+2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
#include "testformattinginfo.h"
105105
#include "teststartupandbehavior.h"
106106
#include "testhelp.h"
107+
#include "testtestxmlfile.h"
107108

108109
class TestQXmlEdit : public QObject
109110
{
@@ -217,6 +218,7 @@ private Q_SLOTS:
217218
void testFormattingInfo();
218219
void testStartupAndBehavior();
219220
void testHelp();
221+
void testTestXMLFile();
220222
};
221223

222224

test/data/xml/test/fileko.xml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- this is a sample QXmlEdit data file -->
3+
<data>
4+
5+
<keywords>
6+
<resource id="1"/>
7+
<widget class="1"/><
8+
<class id="1" buildId="22" />
9+
<layout name="Albert"/>
10+
<window id="2"/>
11+
<other id="1"/>
12+
</keywords>
13+
14+
</data>

test/data/xml/test/filenotclosed.xml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- this is a sample QXmlEdit data file -->
3+
<data>
4+
5+
<keywords>
6+
<resource id="1"/>
7+
<widget class="1"/>
8+
<class id="1" buildId="22" />
9+
<layout name="Albert"/>
10+
<window id="2"/>
11+
<other id="1"/>
12+
</keywords
13+
14+
/data

test/data/xml/test/fileok.xml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- this is a sample QXmlEdit data file -->
3+
<data>
4+
5+
<keywords>
6+
<resource id="1"/>
7+
<widget class="1"/>
8+
<class id="1" buildId="22" />
9+
<layout name="Albert"/>
10+
<window id="2"/>
11+
<other id="1"/>
12+
</keywords>
13+
14+
</data>

test/test.pro

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ DESTDIR = ../build
7575
SOURCES += \
7676
searchInFilesLogic.cpp \
7777
comparexml.cpp \
78+
testtestxmlfile.cpp \
7879
tst_qxmledit.cpp \
7980
testcomment.cpp \
8081
testxsd.cpp \
@@ -193,6 +194,7 @@ include("../src/allsources.pri")
193194
HEADERS += \
194195
comparexml.h \
195196
testcomment.h \
197+
testtestxmlfile.h \
196198
testxsd.h \
197199
testpaste.h \
198200
testsplit.h \

0 commit comments

Comments
 (0)