ctkModuleDescriptionValidator.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 "ctkModuleDescriptionValidator.h"
  16. #include <QFile>
  17. #include <QBuffer>
  18. #include <QXmlStreamReader>
  19. #include <QXmlSchema>
  20. #include <QXmlSchemaValidator>
  21. #include <QXmlQuery>
  22. #include <QAbstractMessageHandler>
  23. #include <QDebug>
  24. class _MessageHandler : public QAbstractMessageHandler
  25. {
  26. public:
  27. QString statusMessage() const { return m_description; }
  28. int line() const { return m_sourceLocation.line(); }
  29. int column() const { return m_sourceLocation.column(); }
  30. protected:
  31. virtual void handleMessage(QtMsgType type, const QString& description,
  32. const QUrl& identifier, const QSourceLocation& sourceLocation)
  33. {
  34. Q_UNUSED(identifier)
  35. m_messageType = type;
  36. m_sourceLocation = sourceLocation;
  37. QXmlStreamReader reader(description);
  38. m_description.clear();
  39. m_description.reserve(description.size());
  40. while(!reader.atEnd())
  41. {
  42. reader.readNext();
  43. switch(reader.tokenType())
  44. {
  45. case QXmlStreamReader::Characters:
  46. {
  47. m_description.append(reader.text().toString());
  48. continue;
  49. }
  50. case QXmlStreamReader::StartElement:
  51. /* Fallthrough, */
  52. case QXmlStreamReader::EndElement:
  53. /* Fallthrough, */
  54. case QXmlStreamReader::StartDocument:
  55. /* Fallthrough, */
  56. case QXmlStreamReader::EndDocument:
  57. continue;
  58. default:
  59. Q_ASSERT_X(false, Q_FUNC_INFO,
  60. "Unexpected node.");
  61. }
  62. }
  63. }
  64. private:
  65. QtMsgType m_messageType;
  66. QString m_description;
  67. QSourceLocation m_sourceLocation;
  68. };
  69. ctkModuleDescriptionValidator::ctkModuleDescriptionValidator(QIODevice *input)
  70. : _input(input), _inputSchema(0), _outputSchema(0), _transformation(0)
  71. {
  72. }
  73. void ctkModuleDescriptionValidator::setInput(QIODevice *input)
  74. {
  75. _input = input;
  76. }
  77. QString ctkModuleDescriptionValidator::output()
  78. {
  79. return _output;
  80. }
  81. void ctkModuleDescriptionValidator::setInputSchema(QIODevice *input)
  82. {
  83. _inputSchema = input;
  84. }
  85. void ctkModuleDescriptionValidator::setOutputSchema(QIODevice *output)
  86. {
  87. _outputSchema = output;
  88. }
  89. void ctkModuleDescriptionValidator::setXSLTransformation(QIODevice *transformation)
  90. {
  91. _transformation = transformation;
  92. }
  93. bool ctkModuleDescriptionValidator::validate()
  94. {
  95. return validateXMLInput() && validateXSLTOutput();
  96. }
  97. bool ctkModuleDescriptionValidator::validateXMLInput()
  98. {
  99. _errorStr.clear();
  100. if (!_input)
  101. {
  102. _errorStr = "No input set for validation.";
  103. return false;
  104. }
  105. QIODevice* inputSchema = _inputSchema;
  106. QScopedPointer<QIODevice> defaultInputSchema(new QFile(":/ctkModuleDescription.xsd"));
  107. if (!inputSchema)
  108. {
  109. inputSchema = defaultInputSchema.data();
  110. inputSchema->open(QIODevice::ReadOnly);
  111. }
  112. _MessageHandler errorHandler;
  113. QXmlSchema schema;
  114. schema.setMessageHandler(&errorHandler);
  115. if (!schema.load(inputSchema))
  116. {
  117. QString msg("Invalid input schema at line %1, column %2: %3");
  118. _errorStr = msg.arg(errorHandler.line()).arg(errorHandler.column()).arg(errorHandler.statusMessage());
  119. return false;
  120. }
  121. QXmlSchemaValidator validator(schema);
  122. if (!validator.validate(_input))
  123. {
  124. QString msg("Error validating CLI XML description, at line %1, column %2: %3");
  125. _errorStr = msg.arg(errorHandler.line()).arg(errorHandler.column())
  126. .arg(errorHandler.statusMessage());
  127. return false;
  128. }
  129. return true;
  130. }
  131. bool ctkModuleDescriptionValidator::validateXSLTOutput()
  132. {
  133. _errorStr.clear();
  134. _output.clear();
  135. if (!_input)
  136. {
  137. _errorStr = "No input set for deriving an output.";
  138. return false;
  139. }
  140. else if (!(_input->openMode() & QIODevice::ReadOnly))
  141. {
  142. _input->open(QIODevice::ReadOnly);
  143. }
  144. _input->reset();
  145. _MessageHandler msgHandler;
  146. QXmlQuery xslTransform(QXmlQuery::XSLT20);
  147. xslTransform.setMessageHandler(&msgHandler);
  148. if (!xslTransform.setFocus(_input))
  149. {
  150. QString msg("Error transforming CLI XML description: %1");
  151. _errorStr = msg.arg(msgHandler.statusMessage());
  152. return false;
  153. }
  154. QIODevice* transformation = _transformation;
  155. QScopedPointer<QIODevice> defaultTransform(new QFile(":/ctkQtModuleDescription.xsl"));
  156. if (!transformation)
  157. {
  158. transformation = defaultTransform.data();
  159. transformation->open(QIODevice::ReadOnly);
  160. }
  161. xslTransform.setQuery(transformation);
  162. if (!xslTransform.evaluateTo(&_output))
  163. {
  164. _output.clear();
  165. QString msg("Error transforming CLI XML description, at line %1, column %2: %3");
  166. _errorStr = msg.arg(msgHandler.line()).arg(msgHandler.column())
  167. .arg(msgHandler.statusMessage());
  168. return false;
  169. }
  170. QIODevice* outputSchema = _outputSchema;
  171. // If there is no custom schema for validating the output, we just return.
  172. // The QtDesigner.xsd schema below (which should be the default) exhausts
  173. // the memory during validation.
  174. if (!outputSchema) return true;
  175. QScopedPointer<QIODevice> defaultOutputSchema(new QFile(":/QtDesigner.xsd"));
  176. if (!outputSchema)
  177. {
  178. outputSchema = defaultOutputSchema.data();
  179. outputSchema->open(QIODevice::ReadOnly);
  180. }
  181. outputSchema->reset();
  182. QXmlSchema schema;
  183. schema.setMessageHandler(&msgHandler);
  184. if (!schema.load(outputSchema))
  185. {
  186. QString msg("Invalid output schema at line %1, column %2: %3");
  187. _errorStr = msg.arg(msgHandler.line()).arg(msgHandler.column()).arg(msgHandler.statusMessage());
  188. return false;
  189. }
  190. QXmlSchemaValidator validator(schema);
  191. validator.setMessageHandler(&msgHandler);
  192. QByteArray outputData;
  193. outputData.append(_output);
  194. if (!validator.validate(outputData))
  195. {
  196. QString msg("Error validating transformed CLI XML description, at line %1, column %2: %3");
  197. _errorStr = msg.arg(msgHandler.line()).arg(msgHandler.column())
  198. .arg(msgHandler.statusMessage());
  199. return false;
  200. }
  201. return true;
  202. }
  203. bool ctkModuleDescriptionValidator::error() const
  204. {
  205. return !_errorStr.isEmpty();
  206. }
  207. QString ctkModuleDescriptionValidator::errorString() const
  208. {
  209. return _errorStr;
  210. }