Skip to content

Commit ea9b8b1

Browse files
committed
Generation of XSD from XML
1 parent 1241af6 commit ea9b8b1

9 files changed

+157
-20
lines changed

src/mainwndcontroller.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,9 @@ void MainWndController::testXML()
440440

441441
MainWindow* MainWndController::generateXSDFromData()
442442
{
443+
if(!XMLToXSD::checkForConfiguration(_w->appData(), _w)) {
444+
return NULL;
445+
}
443446
MainWindow *xsdWindow = NULL ;
444447
XMLToXSD * xmlToXsd = _XMLVsXSDFactory->newXML2XSD(_w->appData());
445448
OperationResult result;
@@ -450,7 +453,6 @@ MainWindow* MainWndController::generateXSDFromData()
450453
// open new window with XSD
451454
xsdWindow = _w->execNew();
452455
QByteArray byteData = xmlToXsd->schemaData().toUtf8();
453-
Utils::message(_w, xmlToXsd->schemaData());
454456
QBuffer xsdAsIO(&byteData);
455457
xsdAsIO.open(QIODevice::ReadOnly);
456458
if(!xsdWindow->loadFileInnerStream(&xsdAsIO, "", false, true, false)) {
@@ -468,7 +470,6 @@ MainWindow* MainWndController::generateXSDFromData()
468470

469471
QString MainWndController::selectTopLevelSchemaElement(Regola *regola)
470472
{
471-
Utils::TODO_THIS_RELEASE("Make provider to test it");
472473
QString nameResult ;
473474
OperationResult result;
474475
XSDSchema *schema = XSDSchema::loadXSDFromString(result, regola->getAsText());
@@ -512,7 +513,6 @@ MainWindow* MainWndController::createEditorFromXSD2XML(XSDToXML *xsdToXml)
512513
// open new window with XML
513514
xsdWindow = _w->execNew();
514515
QByteArray byteData = xsdToXml->data().toUtf8();
515-
Utils::message(_w, xsdToXml->data());
516516
QBuffer xsdAsIO(&byteData);
517517
xsdAsIO.open(QIODevice::ReadOnly);
518518
if(!xsdWindow->loadFileInnerStream(&xsdAsIO, "", false, true, false)) {
@@ -526,6 +526,9 @@ MainWindow* MainWndController::createEditorFromXSD2XML(XSDToXML *xsdToXml)
526526

527527
MainWindow* MainWndController::generateDataFromXSD()
528528
{
529+
if(!XSDToXML::checkForConfiguration(_w->appData(), _w)) {
530+
return NULL;
531+
}
529532
MainWindow* xsdWindow = NULL;
530533
XSDToXML *xsdToXml = _XMLVsXSDFactory->newXSD2XML(_w->appData());
531534
OperationResult result;

src/modules/xml/xmltoxsd.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,13 @@ QString XMLToXSD::sourceFilePath()
190190
{
191191
return _sourceFilePath;
192192
}
193+
194+
bool XMLToXSD::checkForConfiguration(ApplicationData *appData, QWidget *parent)
195+
{
196+
QString path = appData->inst2XSDPath();
197+
if(!QFile::exists(path)) {
198+
Utils::error(parent, QObject::tr("This feature requires Apache XMLBeans. Please open configuration panel and set the path to xsd2inst"));
199+
return false;
200+
}
201+
return true ;
202+
}

src/modules/xml/xmltoxsd.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ class XMLToXSD
5454

5555
bool generateXSD(OperationResult *result, Regola *regola, const GenXSDOption option, const int enumerationThreshold, const bool simpleContentTypeSmart);
5656
QString schemaData();
57-
private:
57+
static bool checkForConfiguration(ApplicationData *appData, QWidget *parent);
5858

59+
private:
5960
bool saveData(Regola *regola);
6061
void deleteData();
6162
bool addError(OperationResult *result, const QString &msgText);

src/modules/xml/xsdtoxml.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
XSDToXML::XSDToXML(ApplicationData *appData)
2727
{
28-
Utils::TODO_THIS_RELEASE("TODO: remove unused vars");
2928
_started = false;
3029
_appData = appData ;
3130
_result = NULL ;
@@ -154,3 +153,13 @@ QString XSDToXML::data()
154153
{
155154
return _instanceData;
156155
}
156+
157+
bool XSDToXML::checkForConfiguration(ApplicationData *appData, QWidget *parent)
158+
{
159+
QString path = appData->xsd2InstPath();
160+
if(!QFile::exists(path)) {
161+
Utils::error(parent, QObject::tr("This feature requires Apache XMLBeans. Please open configuration panel and set the path to inst2xsd"));
162+
return false;
163+
}
164+
return true ;
165+
}

src/modules/xml/xsdtoxml.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ class XSDToXML
4848

4949
bool generateData(OperationResult *result, Regola *regola, const QString &localNameOfGlobalElement);
5050
QString data();
51-
private:
51+
static bool checkForConfiguration(ApplicationData *appData, QWidget *parent);
5252

53+
private:
5354
bool saveData(Regola *regola);
5455
void deleteData();
5556
bool addError(OperationResult *result, const QString &msgText);

test/testbase.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -867,3 +867,11 @@ QString TestBase::loadTextFile(const QString &filePath)
867867
}
868868
return result ;
869869
}
870+
871+
bool TestBase::compare(const QString &code, const QString &expected, const QString &current)
872+
{
873+
if(expected != current) {
874+
return error(QString("%1 expected: '%2; found '%3'").arg(code).arg(expected).arg(current));
875+
}
876+
return true ;
877+
}

test/testbase.h

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class TestBase
134134
bool assertEquals(const QString &msg, const QString &expected, const QString &current);
135135
bool checkBoolSetting(const QString &setting, const bool expected);
136136
QString normalizeCR(QString in) const ;
137+
bool compare(const QString &code, const QString &expected, const QString &current);
137138
public:
138139
static QString loadTextFile(const QString &filePath);
139140
};

test/testxmlbeans.cpp

+116-14
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,13 @@ bool TestXMLBeans::testXML2XSD()
7373
if(!testXML2XSDParameters()) {
7474
return false;
7575
}
76-
if(!testXML2XSDLoadXSD()) {
76+
if(!testXML2XSDConfig()) {
7777
return false;
7878
}
79-
if(!testXML2XSDConfig()) {
79+
if(!testXML2XSDConfigCheck()) {
80+
return false;
81+
}
82+
if(!testXML2XSDLoadXSD()) {
8083
return false;
8184
}
8285
return true ;
@@ -88,10 +91,13 @@ bool TestXMLBeans::testXSD2XML()
8891
if(!testXSD2XMLParameters()) {
8992
return false;
9093
}
91-
if(!testXSD2XMLRun()) {
94+
if(!testXSD2XMLConfig()) {
9295
return false;
9396
}
94-
if(!testXSD2XMLConfig()) {
97+
if(!testXSD2XMLConfigCheck()) {
98+
return false;
99+
}
100+
if(!testXSD2XMLRun()) {
95101
return false;
96102
}
97103
return true ;
@@ -172,6 +178,11 @@ bool TestXMLBeans::testXML2XSDRunInner(const QString &code,
172178
app.mainWindow()->loadFile(fileInput);
173179
_factoryFileToRead = expectedDataFile ;
174180
_factoryErrorInExecution = errorInExecution ;
181+
QTemporaryFile file;
182+
file.open();
183+
file.write("a");
184+
file.close();
185+
app.data()->setInst2XSDPath(file.fileName());
175186
app.mainWindow()->controller()->setXMLVsXSDFactory(this);
176187
MainWindow *resultWindow = app.mainWindow()->controller()->generateXSDFromData();
177188
const bool result = NULL != resultWindow ;
@@ -254,6 +265,11 @@ bool TestXMLBeans::testXSD2XMLRunInner(const QString &code,
254265
_factoryChooseElement = elementChosen ;
255266
_factoryFileToRead = expectedDataFile ;
256267
_factoryErrorInExecution = errorInExecution ;
268+
QTemporaryFile file;
269+
file.open();
270+
file.write("a");
271+
file.close();
272+
app.data()->setXsd2InstPath(file.fileName());
257273
app.mainWindow()->controller()->setXMLVsXSDFactory(this);
258274
MainWindow *resultWindow = app.mainWindow()->controller()->generateDataFromXSD();
259275
const bool result = NULL != resultWindow ;
@@ -289,15 +305,58 @@ bool TestXMLBeans::testXML2XSDConfig()
289305
if(!app.init()) {
290306
return error("init");
291307
}
308+
const QString ExpectedValue ="xyz2";
309+
const QString ExpectedValue2 ="xyz3";
310+
app.data()->setInst2XSDPath(ExpectedValue);
311+
if(!compare( "Config 1", ExpectedValue, Config::getString(Config::KEY_TOOLS_XMLBEANS_INST2XSD, "") ) ) {
312+
return false;
313+
}
314+
if(!compare( "Config 2", ExpectedValue, app.data()->inst2XSDPath()) ) {
315+
return false;
316+
}
317+
app.data()->setInst2XSDPath("");
292318
ConfigValidation configValidation;
293-
Config::saveString(Config::KEY_TOOLS_XMLBEANS_INST2XSD, "xyz");
294319
configValidation.init(app.data());
295-
Config::saveString(Config::KEY_TOOLS_XMLBEANS_INST2XSD, "aaa");
320+
if(!setTextWidget(&configValidation, "pathInst2Xsd", ExpectedValue2)) {
321+
return error("No widget");
322+
}
296323
configValidation.save();
297324
//
298-
QString savedData = app.data()->inst2XSDPath();
299-
if(savedData != "xyz") {
300-
return error(QString("Expected %1 found '%2'").arg("xyz").arg(savedData));
325+
if(!compare( "Config 3", ExpectedValue2, app.data()->inst2XSDPath()) ) {
326+
return false;
327+
}
328+
XMLToXSD testObject(app.data());
329+
if(!compare( "Config 4", ExpectedValue2, testObject.getInst2XSD()) ) {
330+
return false;
331+
}
332+
return true ;
333+
}
334+
335+
bool TestXMLBeans::testXML2XSDConfigCheck()
336+
{
337+
_subTestName = "TestXMLBeans/testXML2XSDConfigCheck";
338+
App app;
339+
if(!app.init()) {
340+
return error("init");
341+
}
342+
app.data()->setInst2XSDPath("");
343+
if(XMLToXSD::checkForConfiguration(app.data(), NULL)) {
344+
return error("Empty");
345+
}
346+
QString fileName ;
347+
{
348+
QTemporaryFile file;
349+
file.open();
350+
file.write("a");
351+
file.close();
352+
fileName = file.fileName();
353+
app.data()->setInst2XSDPath(fileName);
354+
if(!XMLToXSD::checkForConfiguration(app.data(), NULL)) {
355+
return error("Existent:"+fileName);
356+
}
357+
}
358+
if(XMLToXSD::checkForConfiguration(app.data(), NULL)) {
359+
return error("Non Existent:"+fileName);
301360
}
302361
return true ;
303362
}
@@ -309,15 +368,58 @@ bool TestXMLBeans::testXSD2XMLConfig()
309368
if(!app.init()) {
310369
return error("init");
311370
}
371+
const QString ExpectedValue ="xyz2";
372+
const QString ExpectedValue2 ="xyz3";
373+
app.data()->setXsd2InstPath(ExpectedValue);
374+
if(!compare( "Config 1", ExpectedValue, Config::getString(Config::KEY_TOOLS_XMLBEANS_XSD2INST, "") ) ) {
375+
return false;
376+
}
377+
if(!compare( "Config 2", ExpectedValue, app.data()->xsd2InstPath()) ) {
378+
return false;
379+
}
380+
app.data()->setXsd2InstPath("");
312381
ConfigValidation configValidation;
313-
Config::saveString(Config::KEY_TOOLS_XMLBEANS_XSD2INST, "xyz");
314382
configValidation.init(app.data());
315-
Config::saveString(Config::KEY_TOOLS_XMLBEANS_XSD2INST, "aaa");
383+
if(!setTextWidget(&configValidation, "pathXsd2Inst", ExpectedValue2)) {
384+
return error("No widget");
385+
}
316386
configValidation.save();
317387
//
318-
QString savedData = app.data()->xsd2InstPath();
319-
if(savedData != "xyz") {
320-
return error(QString("Expected %1 found '%2'").arg("xyz").arg(savedData));
388+
if(!compare( "Config 3", ExpectedValue2, app.data()->xsd2InstPath()) ) {
389+
return false;
390+
}
391+
XSDToXML testObject(app.data());
392+
if(!compare( "Config 4", ExpectedValue2, testObject.getXSD2Inst()) ) {
393+
return false;
394+
}
395+
return true ;
396+
}
397+
398+
bool TestXMLBeans::testXSD2XMLConfigCheck()
399+
{
400+
_subTestName = "TestXMLBeans/testXSD2XMLConfigCheck";
401+
App app;
402+
if(!app.init()) {
403+
return error("init");
404+
}
405+
app.data()->setXsd2InstPath("");
406+
if(XSDToXML::checkForConfiguration(app.data(), NULL)) {
407+
return error("Empty");
408+
}
409+
QString fileName ;
410+
{
411+
QTemporaryFile file;
412+
file.open();
413+
file.write("a");
414+
file.close();
415+
fileName = file.fileName();
416+
app.data()->setXsd2InstPath(fileName);
417+
if(!XSDToXML::checkForConfiguration(app.data(), NULL)) {
418+
return error("Existent:"+fileName);
419+
}
420+
}
421+
if(XSDToXML::checkForConfiguration(app.data(), NULL)) {
422+
return error("Non Existent:"+fileName);
321423
}
322424
return true ;
323425
}

test/testxmlbeans.h

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class TestXMLBeans : public TestBase, XSDTopElementChooser, XMLVsXSDFactory
3131
bool testXML2XSDParameters();
3232
bool testXML2XSDLoadXSD();
3333
bool testXML2XSDConfig();
34+
bool testXML2XSDConfigCheck();
3435
bool testXML2XSD();
3536
bool testXML2XSDRunInner(const QString &code,
3637
const QString &fileInput, const bool expectedResult,
@@ -40,6 +41,7 @@ class TestXMLBeans : public TestBase, XSDTopElementChooser, XMLVsXSDFactory
4041
bool testXSD2XMLParameters();
4142
bool testXSD2XMLRun();
4243
bool testXSD2XMLConfig();
44+
bool testXSD2XMLConfigCheck();
4345
bool testXSD2XMLRunInner(const QString &code,
4446
const QString &fileInput, const bool expectedResult,
4547
const QString &elementChosen, const bool errorInExecution,

0 commit comments

Comments
 (0)