ctkCmdLineModuleXslTransform.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #include "ctkCmdLineModuleXslTransform.h"
  16. #include "ctkCmdLineModuleXmlMsgHandler_p.h"
  17. #include <QFile>
  18. #include <QBuffer>
  19. #include <QXmlSchema>
  20. #include <QXmlSchemaValidator>
  21. #include <QXmlQuery>
  22. #include <QDebug>
  23. ctkCmdLineModuleXslTransform::ctkCmdLineModuleXslTransform(QIODevice *input, QIODevice *output)
  24. : ctkCmdLineModuleXmlValidator(input),
  25. Validate(false), OutputSchema(0), Transformation(0), Output(output)
  26. {
  27. }
  28. void ctkCmdLineModuleXslTransform::setOutput(QIODevice* output)
  29. {
  30. Output = output;
  31. }
  32. QIODevice* ctkCmdLineModuleXslTransform::output() const
  33. {
  34. return Output;
  35. }
  36. void ctkCmdLineModuleXslTransform::setOutputSchema(QIODevice *output)
  37. {
  38. OutputSchema = output;
  39. }
  40. bool ctkCmdLineModuleXslTransform::transform()
  41. {
  42. if (!Output)
  43. {
  44. ErrorStr = "No output device set";
  45. return false;
  46. }
  47. QIODevice* inputDevice = input();
  48. if (!inputDevice)
  49. {
  50. ErrorStr = "No input set for deriving an output.";
  51. return false;
  52. }
  53. else if (!(inputDevice->openMode() & QIODevice::ReadOnly))
  54. {
  55. inputDevice->open(QIODevice::ReadOnly);
  56. }
  57. inputDevice->reset();
  58. ctkCmdLineModuleXmlMsgHandler msgHandler;
  59. QXmlQuery xslTransform(QXmlQuery::XSLT20);
  60. xslTransform.setMessageHandler(&msgHandler);
  61. if (!xslTransform.setFocus(inputDevice))
  62. {
  63. QString msg("Error transforming XML input: %1");
  64. ErrorStr = msg.arg(msgHandler.statusMessage());
  65. return false;
  66. }
  67. QIODevice* transformation = Transformation;
  68. QScopedPointer<QIODevice> defaultTransform(new QFile(":/ctkCmdLineModuleXmlToQtUi.xsl"));
  69. if (!transformation)
  70. {
  71. transformation = defaultTransform.data();
  72. transformation->open(QIODevice::ReadOnly);
  73. }
  74. xslTransform.setQuery(transformation);
  75. bool closeOutput = false;
  76. if (!(Output->openMode() & QIODevice::WriteOnly))
  77. {
  78. Output->open(QIODevice::WriteOnly);
  79. closeOutput = true;
  80. }
  81. if (!xslTransform.evaluateTo(Output))
  82. {
  83. QString msg("Error transforming XML input, at line %1, column %2: %3");
  84. ErrorStr = msg.arg(msgHandler.line()).arg(msgHandler.column())
  85. .arg(msgHandler.statusMessage());
  86. return false;
  87. }
  88. if (closeOutput)
  89. {
  90. Output->close();
  91. }
  92. else
  93. {
  94. Output->reset();
  95. }
  96. if (Validate)
  97. {
  98. return validateOutput();
  99. }
  100. return true;
  101. }
  102. void ctkCmdLineModuleXslTransform::setXslTransformation(QIODevice *transformation)
  103. {
  104. Transformation = transformation;
  105. }
  106. void ctkCmdLineModuleXslTransform::setValidateOutput(bool validate)
  107. {
  108. Validate = validate;
  109. }
  110. bool ctkCmdLineModuleXslTransform::validateOutput()
  111. {
  112. ErrorStr.clear();
  113. QIODevice* outputSchema = OutputSchema;
  114. // If there is no custom schema for validating the output, we just return.
  115. // The QtDesigner.xsd schema below (which should be the default) exhausts
  116. // the memory during validation.
  117. if (!outputSchema) return true;
  118. QScopedPointer<QIODevice> defaultOutputSchema(new QFile(":/QtDesigner.xsd"));
  119. if (!outputSchema)
  120. {
  121. outputSchema = defaultOutputSchema.data();
  122. outputSchema->open(QIODevice::ReadOnly);
  123. }
  124. outputSchema->reset();
  125. QXmlSchema schema;
  126. ctkCmdLineModuleXmlMsgHandler msgHandler;
  127. schema.setMessageHandler(&msgHandler);
  128. if (!schema.load(outputSchema))
  129. {
  130. QString msg("Invalid output schema at line %1, column %2: %3");
  131. ErrorStr = msg.arg(msgHandler.line()).arg(msgHandler.column()).arg(msgHandler.statusMessage());
  132. return false;
  133. }
  134. QXmlSchemaValidator validator(schema);
  135. validator.setMessageHandler(&msgHandler);
  136. if (!validator.validate(Output))
  137. {
  138. QString msg("Error validating transformed XML input, at line %1, column %2: %3");
  139. ErrorStr = msg.arg(msgHandler.line()).arg(msgHandler.column())
  140. .arg(msgHandler.statusMessage());
  141. return false;
  142. }
  143. return true;
  144. }
  145. bool ctkCmdLineModuleXslTransform::error() const
  146. {
  147. return ctkCmdLineModuleXmlValidator::error() || !ErrorStr.isEmpty();
  148. }
  149. QString ctkCmdLineModuleXslTransform::errorString() const
  150. {
  151. QString errStr = ctkCmdLineModuleXmlValidator::errorString();
  152. if (errStr.isEmpty())
  153. {
  154. return ErrorStr;
  155. }
  156. return errStr;
  157. }