ctkCmdLineModuleXslTransform.cpp 4.4 KB

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