Skip to content

Commit 587c494

Browse files
committed
issue #56 updated XPath search query build.
1 parent 43fa8a4 commit 587c494

9 files changed

+159
-13
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
0.9.18
2+
Issue #56, fixed XPath query search building.
23
Propose the same folder of parent when saving an inner XML.
34
New algorithm to anonymize data.
45
Issue #84, added options for metadata installation folders.

src/findtextparams.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**************************************************************************
22
* This file is part of QXmlEdit *
3-
* Copyright (C) 2011-2018 by Luca Bellonda and individual contributors *
3+
* Copyright (C) 2011-2023 by Luca Bellonda and individual contributors *
44
* as indicated in the AUTHORS file *
55
* lbellonda _at_ gmail.com *
66
* *
@@ -24,9 +24,9 @@
2424
#define QXMLEDITWIDGET_FINDTEXTPARAMS_H
2525

2626
#include "libQXmlEdit_global.h"
27+
#include "xmlEdit.h"
2728
#include <QApplication>
2829
#include <QUndoCommand>
29-
#include "xmlEdit.h"
3030

3131
class Element;
3232
class Attribute;
@@ -222,6 +222,10 @@ class LIBQXMLEDITSHARED_EXPORT FindTextParams
222222
void setCaseSensitive(bool value);
223223

224224
FindTextParams *cloneFind();
225+
226+
#ifdef QXMLEDIT_TEST
227+
friend class TestSearch;
228+
#endif
225229
};
226230

227231
class TextChunk;
@@ -275,6 +279,10 @@ class LIBQXMLEDITSHARED_EXPORT ReplaceTextParams : public FindTextParams
275279
virtual bool isExploreAllItems();
276280

277281
void setCommandGroup(QUndoCommand *undoCommandGroup);
282+
283+
#ifdef QXMLEDIT_TEST
284+
friend class TestSearch;
285+
#endif
278286
};
279287

280288
#endif // QXMLEDITWIDGET_FINDTEXTPARAMS_H

src/modules/search/searchxquery.cpp

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**************************************************************************
22
* This file is part of QXmlEdit *
3-
* Copyright (C) 2013-2018 by Luca Bellonda and individual contributors *
3+
* Copyright (C) 2013-2023 by Luca Bellonda and individual contributors *
44
* as indicated in the AUTHORS file *
55
* lbellonda _at_ gmail.com *
66
* *
@@ -55,12 +55,27 @@ QString SearchXQuery::composeQueryString(Regola *regola, FindTextParams &searchI
5555
queryString += QString("declare namespace %1 = \"%2\";\n").arg(ns).arg(nss[ns]);
5656
}
5757
}
58+
const QString textToFind = getTextToFind(searchInfo);
59+
const QString textToFindAdjusted = adjustTextToFind(textToFind);
5860

59-
queryString += QString("declare variable $root external;\n$root%1").arg(searchInfo.getTextToFind());
61+
queryString += QString("declare variable $root external;\n$root%1").arg(textToFindAdjusted);
6062

6163
return queryString ;
6264
}
6365

66+
QString SearchXQuery::adjustTextToFind(const QString &textToFind)
67+
{
68+
if(textToFind.startsWith("/")) {
69+
return textToFind;
70+
}
71+
return "/"+textToFind;
72+
}
73+
74+
QString SearchXQuery::getTextToFind(FindTextParams &searchInfo)
75+
{
76+
return searchInfo.getTextToFind().trimmed();
77+
}
78+
6479
void SearchXQuery::search(Regola *regola, Element *element, FindTextParams &searchInfo)
6580
{
6681
QXmlResultItems result;

src/modules/search/searchxquery.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**************************************************************************
22
* This file is part of QXmlEdit *
3-
* Copyright (C) 2013-2018 by Luca Bellonda and individual contributors *
3+
* Copyright (C) 2013-2023 by Luca Bellonda and individual contributors *
44
* as indicated in the AUTHORS file *
55
* lbellonda _at_ gmail.com *
66
* *
@@ -41,6 +41,9 @@ class LIBQXMLEDITSHARED_EXPORT SearchXQuery : public QAbstractMessageHandler
4141

4242
void searchAndDisplay(Regola *regola, const QString expression);
4343
QString composeQueryString(Regola *regola, FindTextParams &searchInfo);
44+
45+
static QString adjustTextToFind(const QString &textToFind);
46+
static QString getTextToFind(FindTextParams &searchInfo);
4447
public:
4548
explicit SearchXQuery(QObject *parent = 0);
4649
virtual ~SearchXQuery();

test/testbase.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**************************************************************************
22
* This file is part of QXmlEdit *
3-
* Copyright (C) 2012-2022 by Luca Bellonda and individual contributors *
3+
* Copyright (C) 2012-2023 by Luca Bellonda and individual contributors *
44
* as indicated in the AUTHORS file *
55
* lbellonda _at_ gmail.com *
66
* *
@@ -141,6 +141,7 @@ class TestBase
141141

142142
#define DELTELMS(object) foreach( Element *var, object) { delete var; } object.clear();
143143

144-
#define TEST_ORDIE(x,y) do { if (!(x)) { return error((y)); } }while(false)
144+
#define TEST_OR_DIE2(x,y) do { if (!(x)) { return error((y)); } }while(false)
145+
#define TEST_OR_DIE(x) do { if (!(x)) { return false; } }while(false)
145146

146147
#endif // TESTBASE_H

test/testsearch.cpp

+112-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**************************************************************************
22
* This file is part of QXmlEdit *
3-
* Copyright (C) 2013-2019 by Luca Bellonda and individual contributors *
3+
* Copyright (C) 2013-2023 by Luca Bellonda and individual contributors *
44
* as indicated in the AUTHORS file *
55
* lbellonda _at_ gmail.com *
66
* *
@@ -997,6 +997,13 @@ bool TestSearch::testLiteralSearch()
997997

998998
bool TestSearch::testXQuerySearch()
999999
{
1000+
if( !xqueryGetTextToSearch()) {
1001+
return false;
1002+
}
1003+
if( !xqueryAdjustTextToSearch()) {
1004+
return false;
1005+
}
1006+
10001007
if( !xquerySearchPathExact()) {
10011008
return false;
10021009
}
@@ -1051,11 +1058,115 @@ bool TestSearch::testXQuerySearch()
10511058
if( !xquerySearchTextInChildren()) {
10521059
return false;
10531060
}
1061+
if( !xqueryVerifyAdjustTextToSearch()) {
1062+
return false;
1063+
}
10541064
//---
10551065
return true;
10561066
}
10571067

10581068

1069+
//------------------------------------------------------------
1070+
1071+
bool TestSearch::xqueryVerifyAdjustTextToSearch()
1072+
{
1073+
_subTestName = "xqueryVerifyAdjustTextToSearch";
1074+
TEST_OR_DIE(innerXqueryVerifyAdjustTextToSearch("//*[@data eq 'www']", false, 2));
1075+
TEST_OR_DIE(innerXqueryVerifyAdjustTextToSearch("./*[@data eq 'www']", false, 1));
1076+
TEST_OR_DIE(innerXqueryVerifyAdjustTextToSearch("/*[@data eq 'www']", false, 1));
1077+
TEST_OR_DIE(innerXqueryVerifyAdjustTextToSearch(".//*[@data eq 'www']", false, 2));
1078+
TEST_OR_DIE(innerXqueryVerifyAdjustTextToSearch(" .//*[@data eq 'www']", false, 2));
1079+
TEST_OR_DIE(innerXqueryVerifyAdjustTextToSearch(" .//*[@data eq 'www'] ", false, 2));
1080+
//--
1081+
TEST_OR_DIE(innerXqueryVerifyAdjustTextToSearch("//*[@data eq 'www']", true, 2));
1082+
TEST_OR_DIE(innerXqueryVerifyAdjustTextToSearch("./*[@data eq 'www']", true, 0));
1083+
TEST_OR_DIE(innerXqueryVerifyAdjustTextToSearch("/*[@data eq 'www']", true, 0));
1084+
TEST_OR_DIE(innerXqueryVerifyAdjustTextToSearch(".//*[@data eq 'www']", true, 2));
1085+
TEST_OR_DIE(innerXqueryVerifyAdjustTextToSearch(" .//*[@data eq 'www']", true, 2));
1086+
TEST_OR_DIE(innerXqueryVerifyAdjustTextToSearch(" .//*[@data eq 'www'] ", true, 2));
1087+
return true;
1088+
}
1089+
1090+
bool TestSearch::innerXqueryVerifyAdjustTextToSearch(const QString &toSearch, const bool isGlobal, const int expectedSelCount)
1091+
{
1092+
_subTestName = QString("xqueryVerifyAdjustTextToSearch:%1:%2").arg(toSearch).arg(isGlobal);
1093+
TestSearchHelper helper(true);
1094+
testAStdSearchWithParamsInit("xqueryVerifyAdjustTextToSearch", helper);
1095+
helper.initFind(toSearch);
1096+
if(!isGlobal) {
1097+
QList<int> selList;
1098+
selList.append(0); //root
1099+
selList.append(0); //h1
1100+
selList.append(0); //h11
1101+
selList.append(1); //h21
1102+
selList.append(1); //h31
1103+
Element *element = helper.app.mainWindow()->getRegola()->findElementByArray(selList);
1104+
helper.selectedItem = element ;
1105+
if( NULL == element ) {
1106+
return error("No element");
1107+
}
1108+
helper.findArgs.setOnlyChildren(true);
1109+
}
1110+
helper.search();
1111+
QStringList expected;
1112+
if(expectedSelCount>0) {
1113+
expected << "h4" ;
1114+
}
1115+
if(expectedSelCount>1) {
1116+
expected << "h5" ;
1117+
}
1118+
return checkResults(helper, expectedSelCount, expectedSelCount, &expected);
1119+
}
1120+
1121+
bool TestSearch::xqueryGetTextToSearch()
1122+
{
1123+
_subTestName = "xqueryGetTextToSearch";
1124+
TEST_OR_DIE(innerXqueryGetTextToSearch(" ", ""));
1125+
TEST_OR_DIE(innerXqueryGetTextToSearch(" a", "a"));
1126+
TEST_OR_DIE(innerXqueryGetTextToSearch("a ", "a"));
1127+
TEST_OR_DIE(innerXqueryGetTextToSearch(" a ", "a"));
1128+
TEST_OR_DIE(innerXqueryGetTextToSearch(" a b ", "a b"));
1129+
TEST_OR_DIE(innerXqueryGetTextToSearch(" a vvvv b ", "a vvvv b"));
1130+
return true;
1131+
}
1132+
1133+
bool TestSearch::xqueryAdjustTextToSearch()
1134+
{
1135+
_subTestName = "xqueryAdjustTextToSearch";
1136+
TEST_OR_DIE(innerXqueryAdjustTextToSearch("", "/"));
1137+
TEST_OR_DIE(innerXqueryAdjustTextToSearch("/", "/"));
1138+
TEST_OR_DIE(innerXqueryAdjustTextToSearch(".", "/."));
1139+
TEST_OR_DIE(innerXqueryAdjustTextToSearch("/abc", "/abc"));
1140+
TEST_OR_DIE(innerXqueryAdjustTextToSearch("//abc", "//abc"));
1141+
TEST_OR_DIE(innerXqueryAdjustTextToSearch("./abc", "/./abc"));
1142+
TEST_OR_DIE(innerXqueryAdjustTextToSearch(".//abc", "/.//abc"));
1143+
TEST_OR_DIE(innerXqueryAdjustTextToSearch("./abc/", "/./abc/"));
1144+
return true;
1145+
}
1146+
1147+
//------------------------------------------------------------
1148+
bool TestSearch::innerXqueryAdjustTextToSearch(const QString &input, const QString &expected)
1149+
{
1150+
const QString result = SearchXQuery::adjustTextToFind(input);
1151+
if(expected != result ) {
1152+
return error(QString("innerXqueryAdjustTextToSearch for input: '%1' expected: '%2' found: '%3' ")
1153+
.arg(input).arg(expected).arg(result));
1154+
}
1155+
return true ;
1156+
}
1157+
1158+
bool TestSearch::innerXqueryGetTextToSearch(const QString &input, const QString &expected)
1159+
{
1160+
FindTextParams searchInfo;
1161+
searchInfo.mTextToFind = input;
1162+
const QString result = SearchXQuery::getTextToFind(searchInfo);
1163+
if(expected != result ) {
1164+
return error(QString("innerXqueryGetTextToSearch for input: '%1' expected: '%2' found: '%3' ")
1165+
.arg(input).arg(expected).arg(result));
1166+
}
1167+
return true ;
1168+
}
1169+
10591170
//------------------------------------------------------------
10601171

10611172
bool TestSearch::innerSearchForNext(const QString &testName, const bool isNext)

test/testsearch.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**************************************************************************
22
* This file is part of QXmlEdit *
3-
* Copyright (C) 2013-2019 by Luca Bellonda and individual contributors *
3+
* Copyright (C) 2013-2023 by Luca Bellonda and individual contributors *
44
* as indicated in the AUTHORS file *
55
* lbellonda _at_ gmail.com *
66
* *
@@ -32,6 +32,13 @@ class TestSearch : public TestBase
3232
{
3333
bool testDoubleSearch();
3434
//---
35+
bool xqueryGetTextToSearch();
36+
bool xqueryAdjustTextToSearch();
37+
bool innerXqueryAdjustTextToSearch(const QString &input, const QString &expected);
38+
bool innerXqueryGetTextToSearch(const QString &input, const QString &expected);
39+
bool xqueryVerifyAdjustTextToSearch();
40+
bool innerXqueryVerifyAdjustTextToSearch(const QString &toSearch, const bool isGlobal, const int expectedSelCount);
41+
//---
3542
bool literalSearchNoItemFound();
3643
bool literalSearchNonExact();
3744
bool literalSearchExact();

test/testtestmainwindow.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**************************************************************************
22
* This file is part of QXmlEdit *
3-
* Copyright (C) 2022 by Luca Bellonda and individual contributors *
3+
* Copyright (C) 2023 by Luca Bellonda and individual contributors *
44
* as indicated in the AUTHORS file *
55
* lbellonda _at_ gmail.com *
66
* *
@@ -103,11 +103,10 @@ bool TestTestMainWindow::testFileNameOrFolder()
103103
return true ;
104104
}
105105

106-
#define TEST_FN(e) TEST_ORDIE((mainWindow->getRegola()->fileName()==FILE_1), e);
106+
#define TEST_FN(e) TEST_OR_DIE2((mainWindow->getRegola()->fileName()==FILE_1), e);
107107

108108
bool TestTestMainWindow::testSaveFileName()
109109
{
110-
Utils::TODO_THIS_RELEASE("commenti sotto");
111110
_subTestName = "testSaveFileName";
112111
App app;
113112
app.setUseTestWindowFile();

test/tst_qxmledit.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**************************************************************************
22
* This file is part of QXmlEdit *
3-
* Copyright (C) 2011-2022 by Luca Bellonda and individual contributors *
3+
* Copyright (C) 2011-2023 by Luca Bellonda and individual contributors *
44
* as indicated in the AUTHORS file *
55
* lbellonda _at_ gmail.com *
66
* *
@@ -1708,6 +1708,7 @@ void TestQXmlEdit::testNew()
17081708
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
17091709
qInstallMessageHandler(msgHandler);
17101710
#endif
1711+
testSearch();
17111712
testMainWindow();
17121713
testAnonymize();
17131714
testAbout();

0 commit comments

Comments
 (0)