Browse Source

Use pimpl idiom for public xml/xsl classes.

Sascha Zelzer 13 years ago
parent
commit
dae2f987ff

+ 32 - 13
Libs/CommandLineModules/Core/ctkCmdLineModuleXmlValidator.cpp

@@ -30,37 +30,56 @@
 
 #include <QDebug>
 
+class ctkCmdLineModuleXmlValidatorPrivate
+{
+public:
+
+  ctkCmdLineModuleXmlValidatorPrivate()
+    : Input(NULL), InputSchema(NULL)
+  {}
+
+  QIODevice* Input;
+  QIODevice* InputSchema;
+
+  QString ErrorStr;
+};
+
 ctkCmdLineModuleXmlValidator::ctkCmdLineModuleXmlValidator(QIODevice *input)
-  : Input(input), InputSchema(0)
+  : d(new ctkCmdLineModuleXmlValidatorPrivate)
+{
+  d->Input = input;
+}
+
+ctkCmdLineModuleXmlValidator::~ctkCmdLineModuleXmlValidator()
 {
 }
 
 void ctkCmdLineModuleXmlValidator::setInput(QIODevice *input)
 {
-  Input = input;
+  d->Input = input;
 }
 
 QIODevice* ctkCmdLineModuleXmlValidator::input() const
 {
-  return Input;
+  return d->Input;
 }
 
 void ctkCmdLineModuleXmlValidator::setInputSchema(QIODevice *input)
 {
-  InputSchema = input;
+  d->InputSchema = input;
 }
 
 bool ctkCmdLineModuleXmlValidator::validateInput()
 {
-  ErrorStr.clear();
+  d->ErrorStr.clear();
 
-  if (!Input)
+  if (!d->Input)
   {
-    ErrorStr = "No input set for validation.";
+    d->ErrorStr = "No input set for validation.";
     return false;
   }
 
-  QIODevice* inputSchema = InputSchema;
+  QIODevice* inputSchema = d->InputSchema;
   QScopedPointer<QIODevice> defaultInputSchema(new QFile(":/ctkCmdLineModule.xsd"));
   if (!inputSchema)
   {
@@ -76,16 +95,16 @@ bool ctkCmdLineModuleXmlValidator::validateInput()
   if (!schema.load(inputSchema))
   {
     QString msg("Invalid input schema at line %1, column %2: %3");
-    ErrorStr = msg.arg(errorHandler.line()).arg(errorHandler.column()).arg(errorHandler.statusMessage());
+    d->ErrorStr = msg.arg(errorHandler.line()).arg(errorHandler.column()).arg(errorHandler.statusMessage());
     return false;
   }
 
   QXmlSchemaValidator validator(schema);
 
-  if (!validator.validate(Input))
+  if (!validator.validate(d->Input))
   {
     QString msg("Error validating CLI XML description, at line %1, column %2: %3");
-    ErrorStr = msg.arg(errorHandler.line()).arg(errorHandler.column())
+    d->ErrorStr = msg.arg(errorHandler.line()).arg(errorHandler.column())
                 .arg(errorHandler.statusMessage());
     return false;
   }
@@ -95,10 +114,10 @@ bool ctkCmdLineModuleXmlValidator::validateInput()
 
 bool ctkCmdLineModuleXmlValidator::error() const
 {
-  return !this->ErrorStr.isEmpty();
+  return !d->ErrorStr.isEmpty();
 }
 
 QString ctkCmdLineModuleXmlValidator::errorString() const
 {
-  return this->ErrorStr;
+  return d->ErrorStr;
 }

+ 5 - 4
Libs/CommandLineModules/Core/ctkCmdLineModuleXmlValidator.h

@@ -24,7 +24,9 @@
 
 #include <ctkCommandLineModulesCoreExport.h>
 
-#include <QString>
+#include <QScopedPointer>
+
+class ctkCmdLineModuleXmlValidatorPrivate;
 
 class QIODevice;
 
@@ -37,6 +39,7 @@ class CTK_CMDLINEMODULECORE_EXPORT ctkCmdLineModuleXmlValidator
 public:
 
   ctkCmdLineModuleXmlValidator(QIODevice* input = 0);
+  ~ctkCmdLineModuleXmlValidator();
 
   void setInput(QIODevice* input);
   QIODevice* input() const;
@@ -50,10 +53,8 @@ public:
 
 private:
 
-  QIODevice* Input;
-  QIODevice* InputSchema;
+  QScopedPointer<ctkCmdLineModuleXmlValidatorPrivate> d;
 
-  QString ErrorStr;
 };
 
 #endif // CTKCMDLINEMODULEXMLVALIDATOR_H

+ 111 - 89
Libs/CommandLineModules/Core/ctkCmdLineModuleXslTransform.cpp

@@ -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;
 }

+ 2 - 17
Libs/CommandLineModules/Core/ctkCmdLineModuleXslTransform.h

@@ -25,12 +25,9 @@
 // CTK includes
 #include "ctkCommandLineModulesCoreExport.h"
 #include "ctkCmdLineModuleXmlValidator.h"
-class ctkCmdLineModuleXmlMsgHandler;
+class ctkCmdLineModuleXslTransformPrivate;
 
 // Qt includes
-#include <QList>
-#include <QString>
-#include <QXmlQuery>
 class QIODevice;
 
 /**
@@ -116,20 +113,8 @@ public:
 
 private:
 
-  bool validateOutput();
+  QScopedPointer<ctkCmdLineModuleXslTransformPrivate> d;
 
-  bool Validate;
-  bool Format;
-
-  QIODevice* OutputSchema;
-  QIODevice* Transformation;
-  QIODevice* Output;
-
-  QXmlQuery XslTransform;
-  QList<QIODevice*> ExtraTransformations;
-  ctkCmdLineModuleXmlMsgHandler* MsgHandler;
-
-  QString ErrorStr;
 };