ctkCmdLineModuleXslTransform.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. // Qt includes
  16. #include <QBuffer>
  17. #include <QDebug>
  18. #include <QFile>
  19. #include <QXmlSchema>
  20. #include <QXmlSchemaValidator>
  21. // CTK includes
  22. #include "ctkCmdLineModuleXslTransform.h"
  23. #include "ctkCmdLineModuleXmlMsgHandler_p.h"
  24. ctkCmdLineModuleXslTransform::ctkCmdLineModuleXslTransform(QIODevice *input, QIODevice *output)
  25. : ctkCmdLineModuleXmlValidator(input)
  26. , Validate(false)
  27. , OutputSchema(0)
  28. , Transformation(0)
  29. , Output(output)
  30. , XslTransform(QXmlQuery::XSLT20)
  31. , MsgHandler(0)
  32. {
  33. this->MsgHandler = new ctkCmdLineModuleXmlMsgHandler();
  34. this->XslTransform.setMessageHandler(this->MsgHandler);
  35. this->bindVariable("executableWidget", QVariant(QString("QWidget")));
  36. this->bindVariable("parametersWidget", QVariant(QString("ctkCollapsibleGroupBox")));
  37. this->bindVariable("booleanWidget", QVariant(QString("QCheckBox")));
  38. this->bindVariable("integerWidget", QVariant(QString("QSpinBox")));
  39. this->bindVariable("floatingWidget", QVariant(QString("QDoubleSpinBox")));
  40. this->bindVariable("vectorWidget", QVariant(QString("QLineEdit")));
  41. this->bindVariable("enumWidget", QVariant(QString("QComboBox")));
  42. this->bindVariable("imageWidget", QVariant(QString("ctkPathLineEdit")));
  43. this->bindVariable("directoryWidget", QVariant(QString("ctkPathLineEdit")));
  44. this->bindVariable("pointWidget", QVariant(QString("ctkCoordinatesWidget")));
  45. this->bindVariable("unsupportedWidget", QVariant(QString("QLabel")));
  46. }
  47. ctkCmdLineModuleXslTransform::~ctkCmdLineModuleXslTransform()
  48. {
  49. delete this->MsgHandler;
  50. }
  51. void ctkCmdLineModuleXslTransform::setOutput(QIODevice* output)
  52. {
  53. this->Output = output;
  54. }
  55. QIODevice* ctkCmdLineModuleXslTransform::output() const
  56. {
  57. return this->Output;
  58. }
  59. void ctkCmdLineModuleXslTransform::setOutputSchema(QIODevice *output)
  60. {
  61. this->OutputSchema = output;
  62. }
  63. bool ctkCmdLineModuleXslTransform::transform()
  64. {
  65. this->ErrorStr.clear();
  66. if (!Output)
  67. {
  68. this->ErrorStr = "No output device set";
  69. return false;
  70. }
  71. QIODevice* inputDevice = this->input();
  72. if (!inputDevice)
  73. {
  74. this->ErrorStr = "No input set for deriving an output.";
  75. return false;
  76. }
  77. else if (!(inputDevice->openMode() & QIODevice::ReadOnly))
  78. {
  79. inputDevice->open(QIODevice::ReadOnly);
  80. }
  81. inputDevice->reset();
  82. if (!this->XslTransform.setFocus(inputDevice))
  83. {
  84. QString msg("Error transforming XML input: %1");
  85. this->ErrorStr = msg.arg(this->MsgHandler->statusMessage());
  86. return false;
  87. }
  88. QIODevice* transformation = this->Transformation;
  89. QScopedPointer<QIODevice> defaultTransform(new QFile(":/ctkCmdLineModuleXmlToQtUi.xsl"));
  90. if (!transformation)
  91. {
  92. transformation = defaultTransform.data();
  93. transformation->open(QIODevice::ReadOnly);
  94. }
  95. QString query(transformation->readAll());
  96. QString extra;
  97. foreach(QIODevice* extraIODevice, this->ExtraTransformations)
  98. {
  99. extraIODevice->open(QIODevice::ReadOnly);
  100. extra += extraIODevice->readAll();
  101. }
  102. query.replace("<!-- EXTRA TRANSFORMATIONS -->", extra);
  103. #if 0
  104. qDebug() << query;
  105. #endif
  106. this->XslTransform.setQuery(query);
  107. bool closeOutput = false;
  108. if (!(this->Output->openMode() & QIODevice::WriteOnly))
  109. {
  110. this->Output->open(QIODevice::WriteOnly);
  111. closeOutput = true;
  112. }
  113. if (!this->XslTransform.evaluateTo(this->Output))
  114. {
  115. QString msg("Error transforming XML input, at line %1, column %2: %3");
  116. this->ErrorStr = msg.arg(this->MsgHandler->line()).arg(this->MsgHandler->column())
  117. .arg(this->MsgHandler->statusMessage());
  118. return false;
  119. }
  120. #if 0
  121. qDebug() << this->Output;
  122. #endif
  123. if (closeOutput)
  124. {
  125. this->Output->close();
  126. }
  127. else
  128. {
  129. this->Output->reset();
  130. }
  131. if (this->Validate)
  132. {
  133. return this->validateOutput();
  134. }
  135. return true;
  136. }
  137. void ctkCmdLineModuleXslTransform::setXslTransformation(QIODevice *transformation)
  138. {
  139. this->Transformation = transformation;
  140. }
  141. void ctkCmdLineModuleXslTransform::bindVariable(const QString& name, const QVariant& value)
  142. {
  143. this->XslTransform.bindVariable(name, value);
  144. }
  145. void ctkCmdLineModuleXslTransform::setXslExtraTransformations(QList<QIODevice*> transformations)
  146. {
  147. this->ExtraTransformations = transformations;
  148. }
  149. void ctkCmdLineModuleXslTransform::setValidateOutput(bool validate)
  150. {
  151. this->Validate = validate;
  152. }
  153. bool ctkCmdLineModuleXslTransform::validateOutput()
  154. {
  155. this->ErrorStr.clear();
  156. QIODevice* outputSchema = this->OutputSchema;
  157. // If there is no custom schema for validating the output, we just return.
  158. // The QtDesigner.xsd schema below (which should be the default) exhausts
  159. // the memory during validation.
  160. if (!outputSchema) return true;
  161. QScopedPointer<QIODevice> defaultOutputSchema(new QFile(":/QtDesigner.xsd"));
  162. if (!outputSchema)
  163. {
  164. outputSchema = defaultOutputSchema.data();
  165. outputSchema->open(QIODevice::ReadOnly);
  166. }
  167. outputSchema->reset();
  168. QXmlSchema schema;
  169. ctkCmdLineModuleXmlMsgHandler msgHandler;
  170. schema.setMessageHandler(&msgHandler);
  171. if (!schema.load(outputSchema))
  172. {
  173. QString msg("Invalid output schema at line %1, column %2: %3");
  174. ErrorStr = msg.arg(msgHandler.line()).arg(msgHandler.column()).arg(msgHandler.statusMessage());
  175. return false;
  176. }
  177. QXmlSchemaValidator validator(schema);
  178. validator.setMessageHandler(&msgHandler);
  179. if (!validator.validate(Output))
  180. {
  181. QString msg("Error validating transformed XML input, at line %1, column %2: %3");
  182. ErrorStr = msg.arg(msgHandler.line()).arg(msgHandler.column())
  183. .arg(msgHandler.statusMessage());
  184. return false;
  185. }
  186. return true;
  187. }
  188. bool ctkCmdLineModuleXslTransform::error() const
  189. {
  190. return ctkCmdLineModuleXmlValidator::error() || !this->ErrorStr.isEmpty();
  191. }
  192. QString ctkCmdLineModuleXslTransform::errorString() const
  193. {
  194. QString errStr = this->ctkCmdLineModuleXmlValidator::errorString();
  195. if (errStr.isEmpty())
  196. {
  197. return this->ErrorStr;
  198. }
  199. return errStr;
  200. }