ctkCmdLineModuleXslTransform.cpp 8.1 KB

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