ctkCmdLineModuleXslTransform.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 <QXmlQuery>
  20. #include <QXmlSchema>
  21. #include <QXmlSchemaValidator>
  22. #include <QXmlFormatter>
  23. // CTK includes
  24. #include "ctkCmdLineModuleXslTransform.h"
  25. #include "ctkCmdLineModuleXmlMsgHandler_p.h"
  26. class ctkCmdLineModuleXslTransformPrivate
  27. {
  28. public:
  29. ctkCmdLineModuleXslTransformPrivate(QIODevice *output)
  30. : Validate(false)
  31. , Format(false)
  32. , OutputSchema(0)
  33. , Transformation(0)
  34. , Output(output)
  35. , XslTransform(QXmlQuery::XSLT20)
  36. {
  37. this->XslTransform.setMessageHandler(&this->MsgHandler);
  38. }
  39. bool validateOutput();
  40. bool Validate;
  41. bool Format;
  42. QIODevice* OutputSchema;
  43. QIODevice* Transformation;
  44. QIODevice* Output;
  45. QXmlQuery XslTransform;
  46. QList<QIODevice*> ExtraTransformations;
  47. ctkCmdLineModuleXmlMsgHandler MsgHandler;
  48. QString ErrorStr;
  49. };
  50. bool ctkCmdLineModuleXslTransformPrivate::validateOutput()
  51. {
  52. this->ErrorStr.clear();
  53. QIODevice* outputSchema = this->OutputSchema;
  54. // If there is no custom schema for validating the output, we just return.
  55. // The QtDesigner.xsd schema below (which should be the default) exhausts
  56. // the memory during validation.
  57. if (!outputSchema) return true;
  58. QScopedPointer<QIODevice> defaultOutputSchema(new QFile(":/QtDesigner.xsd"));
  59. if (!outputSchema)
  60. {
  61. outputSchema = defaultOutputSchema.data();
  62. outputSchema->open(QIODevice::ReadOnly);
  63. }
  64. outputSchema->reset();
  65. QXmlSchema schema;
  66. ctkCmdLineModuleXmlMsgHandler msgHandler;
  67. schema.setMessageHandler(&msgHandler);
  68. if (!schema.load(outputSchema))
  69. {
  70. QString msg("Invalid output schema at line %1, column %2: %3");
  71. ErrorStr = msg.arg(msgHandler.line()).arg(msgHandler.column()).arg(msgHandler.statusMessage());
  72. return false;
  73. }
  74. QXmlSchemaValidator validator(schema);
  75. validator.setMessageHandler(&msgHandler);
  76. if (!validator.validate(Output))
  77. {
  78. QString msg("Error validating transformed XML input, at line %1, column %2: %3");
  79. ErrorStr = msg.arg(msgHandler.line()).arg(msgHandler.column())
  80. .arg(msgHandler.statusMessage());
  81. return false;
  82. }
  83. return true;
  84. }
  85. ctkCmdLineModuleXslTransform::ctkCmdLineModuleXslTransform(QIODevice *input, QIODevice *output)
  86. : ctkCmdLineModuleXmlValidator(input)
  87. , d(new ctkCmdLineModuleXslTransformPrivate(output))
  88. {
  89. this->bindVariable("executableWidget", QVariant(QString("QWidget")));
  90. this->bindVariable("parametersWidget", QVariant(QString("ctkCollapsibleGroupBox")));
  91. this->bindVariable("booleanWidget", QVariant(QString("QCheckBox")));
  92. this->bindVariable("integerWidget", QVariant(QString("QSpinBox")));
  93. this->bindVariable("floatingWidget", QVariant(QString("QDoubleSpinBox")));
  94. this->bindVariable("vectorWidget", QVariant(QString("QLineEdit")));
  95. this->bindVariable("enumWidget", QVariant(QString("QComboBox")));
  96. this->bindVariable("imageWidget", QVariant(QString("ctkPathLineEdit")));
  97. this->bindVariable("directoryWidget", QVariant(QString("ctkPathLineEdit")));
  98. this->bindVariable("pointWidget", QVariant(QString("ctkCoordinatesWidget")));
  99. this->bindVariable("unsupportedWidget", QVariant(QString("QLabel")));
  100. }
  101. ctkCmdLineModuleXslTransform::~ctkCmdLineModuleXslTransform()
  102. {
  103. }
  104. void ctkCmdLineModuleXslTransform::setOutput(QIODevice* output)
  105. {
  106. d->Output = output;
  107. }
  108. QIODevice* ctkCmdLineModuleXslTransform::output() const
  109. {
  110. return d->Output;
  111. }
  112. void ctkCmdLineModuleXslTransform::setOutputSchema(QIODevice *output)
  113. {
  114. d->OutputSchema = output;
  115. }
  116. bool ctkCmdLineModuleXslTransform::formatXmlOutput() const
  117. {
  118. return d->Format;
  119. }
  120. void ctkCmdLineModuleXslTransform::setFormatXmlOutput(bool format)
  121. {
  122. d->Format = format;
  123. }
  124. bool ctkCmdLineModuleXslTransform::transform()
  125. {
  126. d->ErrorStr.clear();
  127. if (!d->Output)
  128. {
  129. d->ErrorStr = "No output device set";
  130. return false;
  131. }
  132. QIODevice* inputDevice = this->input();
  133. if (!inputDevice)
  134. {
  135. d->ErrorStr = "No input set for deriving an output.";
  136. return false;
  137. }
  138. else if (!(inputDevice->openMode() & QIODevice::ReadOnly))
  139. {
  140. inputDevice->open(QIODevice::ReadOnly);
  141. }
  142. inputDevice->reset();
  143. if (!d->XslTransform.setFocus(inputDevice))
  144. {
  145. QString msg("Error transforming XML input: %1");
  146. d->ErrorStr = msg.arg(d->MsgHandler.statusMessage());
  147. return false;
  148. }
  149. QIODevice* transformation = d->Transformation;
  150. QScopedPointer<QIODevice> defaultTransform(new QFile(":/ctkCmdLineModuleXmlToQtUi.xsl"));
  151. if (!transformation)
  152. {
  153. transformation = defaultTransform.data();
  154. transformation->open(QIODevice::ReadOnly);
  155. }
  156. QString query(transformation->readAll());
  157. QString extra;
  158. foreach(QIODevice* extraIODevice, d->ExtraTransformations)
  159. {
  160. extraIODevice->open(QIODevice::ReadOnly);
  161. extra += extraIODevice->readAll();
  162. }
  163. query.replace("<!-- EXTRA TRANSFORMATIONS -->", extra);
  164. #if 0
  165. qDebug() << query;
  166. #endif
  167. d->XslTransform.setQuery(query);
  168. bool closeOutput = false;
  169. if (!(d->Output->openMode() & QIODevice::WriteOnly))
  170. {
  171. d->Output->open(QIODevice::WriteOnly);
  172. closeOutput = true;
  173. }
  174. QScopedPointer<QXmlSerializer> xmlSerializer;
  175. if (d->Format)
  176. {
  177. xmlSerializer.reset(new QXmlFormatter(d->XslTransform, d->Output));
  178. }
  179. else
  180. {
  181. xmlSerializer.reset(new QXmlSerializer(d->XslTransform, d->Output));
  182. }
  183. if (!d->XslTransform.evaluateTo(xmlSerializer.data()))
  184. {
  185. QString msg("Error transforming XML input, at line %1, column %2: %3");
  186. d->ErrorStr = msg.arg(d->MsgHandler.line()).arg(d->MsgHandler.column())
  187. .arg(d->MsgHandler.statusMessage());
  188. return false;
  189. }
  190. #if 0
  191. qDebug() << d->Output;
  192. #endif
  193. if (closeOutput)
  194. {
  195. d->Output->close();
  196. }
  197. else
  198. {
  199. d->Output->reset();
  200. }
  201. if (d->Validate)
  202. {
  203. return d->validateOutput();
  204. }
  205. return true;
  206. }
  207. void ctkCmdLineModuleXslTransform::setXslTransformation(QIODevice *transformation)
  208. {
  209. d->Transformation = transformation;
  210. }
  211. void ctkCmdLineModuleXslTransform::bindVariable(const QString& name, const QVariant& value)
  212. {
  213. d->XslTransform.bindVariable(name, value);
  214. }
  215. void ctkCmdLineModuleXslTransform::setXslExtraTransformation(QIODevice* transformation)
  216. {
  217. QList<QIODevice*> transformations;
  218. transformations<<transformation;
  219. this->setXslExtraTransformations(transformations);
  220. }
  221. void ctkCmdLineModuleXslTransform::setXslExtraTransformations(const QList<QIODevice *>& transformations)
  222. {
  223. d->ExtraTransformations = transformations;
  224. }
  225. void ctkCmdLineModuleXslTransform::setValidateOutput(bool validate)
  226. {
  227. d->Validate = validate;
  228. }
  229. bool ctkCmdLineModuleXslTransform::error() const
  230. {
  231. return ctkCmdLineModuleXmlValidator::error() || !d->ErrorStr.isEmpty();
  232. }
  233. QString ctkCmdLineModuleXslTransform::errorString() const
  234. {
  235. QString errStr = this->ctkCmdLineModuleXmlValidator::errorString();
  236. if (errStr.isEmpty())
  237. {
  238. return d->ErrorStr;
  239. }
  240. return errStr;
  241. }