|
@@ -23,6 +23,7 @@
|
|
|
#include <QBuffer>
|
|
|
#include <QDebug>
|
|
|
#include <QFile>
|
|
|
+#include <QXmlQuery>
|
|
|
#include <QXmlSchema>
|
|
|
#include <QXmlSchemaValidator>
|
|
|
#include <QXmlFormatter>
|
|
@@ -31,19 +32,86 @@
|
|
|
#include "ctkCmdLineModuleXslTransform.h"
|
|
|
#include "ctkCmdLineModuleXmlMsgHandler_p.h"
|
|
|
|
|
|
+class ctkCmdLineModuleXslTransformPrivate
|
|
|
+{
|
|
|
+public:
|
|
|
+
|
|
|
+ ctkCmdLineModuleXslTransformPrivate(QIODevice *output)
|
|
|
+ : Validate(false)
|
|
|
+ , Format(false)
|
|
|
+ , OutputSchema(0)
|
|
|
+ , Transformation(0)
|
|
|
+ , Output(output)
|
|
|
+ , XslTransform(QXmlQuery::XSLT20)
|
|
|
+ {
|
|
|
+ this->XslTransform.setMessageHandler(&this->MsgHandler);
|
|
|
+ }
|
|
|
+
|
|
|
+ bool validateOutput();
|
|
|
+
|
|
|
+ bool Validate;
|
|
|
+ bool Format;
|
|
|
+
|
|
|
+ QIODevice* OutputSchema;
|
|
|
+ QIODevice* Transformation;
|
|
|
+ QIODevice* Output;
|
|
|
+
|
|
|
+ QXmlQuery XslTransform;
|
|
|
+ QList<QIODevice*> ExtraTransformations;
|
|
|
+ ctkCmdLineModuleXmlMsgHandler MsgHandler;
|
|
|
+
|
|
|
+ QString ErrorStr;
|
|
|
+};
|
|
|
+
|
|
|
+bool ctkCmdLineModuleXslTransformPrivate::validateOutput()
|
|
|
+{
|
|
|
+ this->ErrorStr.clear();
|
|
|
+
|
|
|
+ QIODevice* outputSchema = this->OutputSchema;
|
|
|
+
|
|
|
+ // If there is no custom schema for validating the output, we just return.
|
|
|
+ // The QtDesigner.xsd schema below (which should be the default) exhausts
|
|
|
+ // the memory during validation.
|
|
|
+ if (!outputSchema) return true;
|
|
|
+
|
|
|
+ QScopedPointer<QIODevice> defaultOutputSchema(new QFile(":/QtDesigner.xsd"));
|
|
|
+ if (!outputSchema)
|
|
|
+ {
|
|
|
+ outputSchema = defaultOutputSchema.data();
|
|
|
+ outputSchema->open(QIODevice::ReadOnly);
|
|
|
+ }
|
|
|
+ outputSchema->reset();
|
|
|
+
|
|
|
+ QXmlSchema schema;
|
|
|
+
|
|
|
+ ctkCmdLineModuleXmlMsgHandler msgHandler;
|
|
|
+ schema.setMessageHandler(&msgHandler);
|
|
|
+
|
|
|
+ if (!schema.load(outputSchema))
|
|
|
+ {
|
|
|
+ QString msg("Invalid output schema at line %1, column %2: %3");
|
|
|
+ ErrorStr = msg.arg(msgHandler.line()).arg(msgHandler.column()).arg(msgHandler.statusMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ QXmlSchemaValidator validator(schema);
|
|
|
+ validator.setMessageHandler(&msgHandler);
|
|
|
+
|
|
|
+ if (!validator.validate(Output))
|
|
|
+ {
|
|
|
+ QString msg("Error validating transformed XML input, at line %1, column %2: %3");
|
|
|
+ ErrorStr = msg.arg(msgHandler.line()).arg(msgHandler.column())
|
|
|
+ .arg(msgHandler.statusMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
ctkCmdLineModuleXslTransform::ctkCmdLineModuleXslTransform(QIODevice *input, QIODevice *output)
|
|
|
: ctkCmdLineModuleXmlValidator(input)
|
|
|
- , Validate(false)
|
|
|
- , Format(false)
|
|
|
- , OutputSchema(0)
|
|
|
- , Transformation(0)
|
|
|
- , Output(output)
|
|
|
- , XslTransform(QXmlQuery::XSLT20)
|
|
|
- , MsgHandler(0)
|
|
|
+ , d(new ctkCmdLineModuleXslTransformPrivate(output))
|
|
|
{
|
|
|
- this->MsgHandler = new ctkCmdLineModuleXmlMsgHandler();
|
|
|
- this->XslTransform.setMessageHandler(this->MsgHandler);
|
|
|
-
|
|
|
this->bindVariable("executableWidget", QVariant(QString("QWidget")));
|
|
|
this->bindVariable("parametersWidget", QVariant(QString("ctkCollapsibleGroupBox")));
|
|
|
this->bindVariable("booleanWidget", QVariant(QString("QCheckBox")));
|
|
@@ -59,47 +127,46 @@ ctkCmdLineModuleXslTransform::ctkCmdLineModuleXslTransform(QIODevice *input, QIO
|
|
|
|
|
|
ctkCmdLineModuleXslTransform::~ctkCmdLineModuleXslTransform()
|
|
|
{
|
|
|
- delete this->MsgHandler;
|
|
|
}
|
|
|
|
|
|
void ctkCmdLineModuleXslTransform::setOutput(QIODevice* output)
|
|
|
{
|
|
|
- this->Output = output;
|
|
|
+ d->Output = output;
|
|
|
}
|
|
|
|
|
|
QIODevice* ctkCmdLineModuleXslTransform::output() const
|
|
|
{
|
|
|
- return this->Output;
|
|
|
+ return d->Output;
|
|
|
}
|
|
|
|
|
|
void ctkCmdLineModuleXslTransform::setOutputSchema(QIODevice *output)
|
|
|
{
|
|
|
- this->OutputSchema = output;
|
|
|
+ d->OutputSchema = output;
|
|
|
}
|
|
|
|
|
|
bool ctkCmdLineModuleXslTransform::formatXmlOutput() const
|
|
|
{
|
|
|
- return this->Format;
|
|
|
+ return d->Format;
|
|
|
}
|
|
|
|
|
|
void ctkCmdLineModuleXslTransform::setFormatXmlOutput(bool format)
|
|
|
{
|
|
|
- this->Format = format;
|
|
|
+ d->Format = format;
|
|
|
}
|
|
|
|
|
|
bool ctkCmdLineModuleXslTransform::transform()
|
|
|
{
|
|
|
- this->ErrorStr.clear();
|
|
|
+ d->ErrorStr.clear();
|
|
|
|
|
|
- if (!Output)
|
|
|
+ if (!d->Output)
|
|
|
{
|
|
|
- this->ErrorStr = "No output device set";
|
|
|
+ d->ErrorStr = "No output device set";
|
|
|
return false;
|
|
|
}
|
|
|
QIODevice* inputDevice = this->input();
|
|
|
if (!inputDevice)
|
|
|
{
|
|
|
- this->ErrorStr = "No input set for deriving an output.";
|
|
|
+ d->ErrorStr = "No input set for deriving an output.";
|
|
|
return false;
|
|
|
}
|
|
|
else if (!(inputDevice->openMode() & QIODevice::ReadOnly))
|
|
@@ -109,14 +176,14 @@ bool ctkCmdLineModuleXslTransform::transform()
|
|
|
inputDevice->reset();
|
|
|
|
|
|
|
|
|
- if (!this->XslTransform.setFocus(inputDevice))
|
|
|
+ if (!d->XslTransform.setFocus(inputDevice))
|
|
|
{
|
|
|
QString msg("Error transforming XML input: %1");
|
|
|
- this->ErrorStr = msg.arg(this->MsgHandler->statusMessage());
|
|
|
+ d->ErrorStr = msg.arg(d->MsgHandler.statusMessage());
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- QIODevice* transformation = this->Transformation;
|
|
|
+ QIODevice* transformation = d->Transformation;
|
|
|
QScopedPointer<QIODevice> defaultTransform(new QFile(":/ctkCmdLineModuleXmlToQtUi.xsl"));
|
|
|
if (!transformation)
|
|
|
{
|
|
@@ -125,7 +192,7 @@ bool ctkCmdLineModuleXslTransform::transform()
|
|
|
}
|
|
|
QString query(transformation->readAll());
|
|
|
QString extra;
|
|
|
- foreach(QIODevice* extraIODevice, this->ExtraTransformations)
|
|
|
+ foreach(QIODevice* extraIODevice, d->ExtraTransformations)
|
|
|
{
|
|
|
extraIODevice->open(QIODevice::ReadOnly);
|
|
|
extra += extraIODevice->readAll();
|
|
@@ -134,61 +201,61 @@ bool ctkCmdLineModuleXslTransform::transform()
|
|
|
#if 0
|
|
|
qDebug() << query;
|
|
|
#endif
|
|
|
- this->XslTransform.setQuery(query);
|
|
|
+ d->XslTransform.setQuery(query);
|
|
|
|
|
|
bool closeOutput = false;
|
|
|
- if (!(this->Output->openMode() & QIODevice::WriteOnly))
|
|
|
+ if (!(d->Output->openMode() & QIODevice::WriteOnly))
|
|
|
{
|
|
|
- this->Output->open(QIODevice::WriteOnly);
|
|
|
+ d->Output->open(QIODevice::WriteOnly);
|
|
|
closeOutput = true;
|
|
|
}
|
|
|
|
|
|
QScopedPointer<QXmlSerializer> xmlSerializer;
|
|
|
- if (Format)
|
|
|
+ if (d->Format)
|
|
|
{
|
|
|
- xmlSerializer.reset(new QXmlFormatter(this->XslTransform, this->Output));
|
|
|
+ xmlSerializer.reset(new QXmlFormatter(d->XslTransform, d->Output));
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- xmlSerializer.reset(new QXmlSerializer(this->XslTransform, this->Output));
|
|
|
+ xmlSerializer.reset(new QXmlSerializer(d->XslTransform, d->Output));
|
|
|
}
|
|
|
|
|
|
- if (!this->XslTransform.evaluateTo(xmlSerializer.data()))
|
|
|
+ if (!d->XslTransform.evaluateTo(xmlSerializer.data()))
|
|
|
{
|
|
|
QString msg("Error transforming XML input, at line %1, column %2: %3");
|
|
|
- this->ErrorStr = msg.arg(this->MsgHandler->line()).arg(this->MsgHandler->column())
|
|
|
- .arg(this->MsgHandler->statusMessage());
|
|
|
+ d->ErrorStr = msg.arg(d->MsgHandler.line()).arg(d->MsgHandler.column())
|
|
|
+ .arg(d->MsgHandler.statusMessage());
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
#if 0
|
|
|
- qDebug() << this->Output;
|
|
|
+ qDebug() << d->Output;
|
|
|
#endif
|
|
|
|
|
|
if (closeOutput)
|
|
|
{
|
|
|
- this->Output->close();
|
|
|
+ d->Output->close();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- this->Output->reset();
|
|
|
+ d->Output->reset();
|
|
|
}
|
|
|
|
|
|
- if (this->Validate)
|
|
|
+ if (d->Validate)
|
|
|
{
|
|
|
- return this->validateOutput();
|
|
|
+ return d->validateOutput();
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
void ctkCmdLineModuleXslTransform::setXslTransformation(QIODevice *transformation)
|
|
|
{
|
|
|
- this->Transformation = transformation;
|
|
|
+ d->Transformation = transformation;
|
|
|
}
|
|
|
|
|
|
void ctkCmdLineModuleXslTransform::bindVariable(const QString& name, const QVariant& value)
|
|
|
{
|
|
|
- this->XslTransform.bindVariable(name, value);
|
|
|
+ d->XslTransform.bindVariable(name, value);
|
|
|
}
|
|
|
|
|
|
void ctkCmdLineModuleXslTransform::setXslExtraTransformation(QIODevice* transformation)
|
|
@@ -200,62 +267,17 @@ void ctkCmdLineModuleXslTransform::setXslExtraTransformation(QIODevice* transfor
|
|
|
|
|
|
void ctkCmdLineModuleXslTransform::setXslExtraTransformations(const QList<QIODevice *>& transformations)
|
|
|
{
|
|
|
- this->ExtraTransformations = transformations;
|
|
|
+ d->ExtraTransformations = transformations;
|
|
|
}
|
|
|
|
|
|
void ctkCmdLineModuleXslTransform::setValidateOutput(bool validate)
|
|
|
{
|
|
|
- this->Validate = validate;
|
|
|
-}
|
|
|
-
|
|
|
-bool ctkCmdLineModuleXslTransform::validateOutput()
|
|
|
-{
|
|
|
- this->ErrorStr.clear();
|
|
|
-
|
|
|
- QIODevice* outputSchema = this->OutputSchema;
|
|
|
-
|
|
|
- // If there is no custom schema for validating the output, we just return.
|
|
|
- // The QtDesigner.xsd schema below (which should be the default) exhausts
|
|
|
- // the memory during validation.
|
|
|
- if (!outputSchema) return true;
|
|
|
-
|
|
|
- QScopedPointer<QIODevice> defaultOutputSchema(new QFile(":/QtDesigner.xsd"));
|
|
|
- if (!outputSchema)
|
|
|
- {
|
|
|
- outputSchema = defaultOutputSchema.data();
|
|
|
- outputSchema->open(QIODevice::ReadOnly);
|
|
|
- }
|
|
|
- outputSchema->reset();
|
|
|
-
|
|
|
- QXmlSchema schema;
|
|
|
-
|
|
|
- ctkCmdLineModuleXmlMsgHandler msgHandler;
|
|
|
- schema.setMessageHandler(&msgHandler);
|
|
|
-
|
|
|
- if (!schema.load(outputSchema))
|
|
|
- {
|
|
|
- QString msg("Invalid output schema at line %1, column %2: %3");
|
|
|
- ErrorStr = msg.arg(msgHandler.line()).arg(msgHandler.column()).arg(msgHandler.statusMessage());
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- QXmlSchemaValidator validator(schema);
|
|
|
- validator.setMessageHandler(&msgHandler);
|
|
|
-
|
|
|
- if (!validator.validate(Output))
|
|
|
- {
|
|
|
- QString msg("Error validating transformed XML input, at line %1, column %2: %3");
|
|
|
- ErrorStr = msg.arg(msgHandler.line()).arg(msgHandler.column())
|
|
|
- .arg(msgHandler.statusMessage());
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- return true;
|
|
|
+ d->Validate = validate;
|
|
|
}
|
|
|
|
|
|
bool ctkCmdLineModuleXslTransform::error() const
|
|
|
{
|
|
|
- return ctkCmdLineModuleXmlValidator::error() || !this->ErrorStr.isEmpty();
|
|
|
+ return ctkCmdLineModuleXmlValidator::error() || !d->ErrorStr.isEmpty();
|
|
|
}
|
|
|
|
|
|
QString ctkCmdLineModuleXslTransform::errorString() const
|
|
@@ -263,7 +285,7 @@ QString ctkCmdLineModuleXslTransform::errorString() const
|
|
|
QString errStr = this->ctkCmdLineModuleXmlValidator::errorString();
|
|
|
if (errStr.isEmpty())
|
|
|
{
|
|
|
- return this->ErrorStr;
|
|
|
+ return d->ErrorStr;
|
|
|
}
|
|
|
return errStr;
|
|
|
}
|