ctkCmdLineModuleXmlValidator.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "ctkCmdLineModuleXmlValidator.h"
  16. #include "ctkCmdLineModuleXmlMsgHandler_p.h"
  17. #include <QFile>
  18. #include <QBuffer>
  19. #include <QXmlSchema>
  20. #include <QXmlSchemaValidator>
  21. #include <QDebug>
  22. //----------------------------------------------------------------------------
  23. class ctkCmdLineModuleXmlValidatorPrivate
  24. {
  25. public:
  26. ctkCmdLineModuleXmlValidatorPrivate()
  27. : Input(NULL), InputSchema(NULL)
  28. {}
  29. QIODevice* Input;
  30. QIODevice* InputSchema;
  31. QString ErrorStr;
  32. };
  33. //----------------------------------------------------------------------------
  34. ctkCmdLineModuleXmlValidator::ctkCmdLineModuleXmlValidator(QIODevice *input)
  35. : d(new ctkCmdLineModuleXmlValidatorPrivate)
  36. {
  37. d->Input = input;
  38. }
  39. //----------------------------------------------------------------------------
  40. ctkCmdLineModuleXmlValidator::~ctkCmdLineModuleXmlValidator()
  41. {
  42. }
  43. //----------------------------------------------------------------------------
  44. void ctkCmdLineModuleXmlValidator::setInput(QIODevice *input)
  45. {
  46. d->Input = input;
  47. }
  48. //----------------------------------------------------------------------------
  49. QIODevice* ctkCmdLineModuleXmlValidator::input() const
  50. {
  51. return d->Input;
  52. }
  53. //----------------------------------------------------------------------------
  54. void ctkCmdLineModuleXmlValidator::setInputSchema(QIODevice *input)
  55. {
  56. d->InputSchema = input;
  57. }
  58. //----------------------------------------------------------------------------
  59. bool ctkCmdLineModuleXmlValidator::validateInput()
  60. {
  61. d->ErrorStr.clear();
  62. if (!d->Input)
  63. {
  64. d->ErrorStr = "No input set for validation.";
  65. return false;
  66. }
  67. QIODevice* inputSchema = d->InputSchema;
  68. QScopedPointer<QIODevice> defaultInputSchema(new QFile(":/ctkCmdLineModule.xsd"));
  69. if (!inputSchema)
  70. {
  71. inputSchema = defaultInputSchema.data();
  72. inputSchema->open(QIODevice::ReadOnly);
  73. }
  74. ctkCmdLineModuleXmlMsgHandler errorHandler;
  75. QXmlSchema schema;
  76. schema.setMessageHandler(&errorHandler);
  77. if (!schema.load(inputSchema))
  78. {
  79. QString msg("Invalid input schema at line %1, column %2: %3");
  80. d->ErrorStr = msg.arg(errorHandler.line()).arg(errorHandler.column()).arg(errorHandler.statusMessage());
  81. return false;
  82. }
  83. QXmlSchemaValidator validator(schema);
  84. if (!validator.validate(d->Input))
  85. {
  86. QString msg("Error validating CLI XML description, at line %1, column %2: %3");
  87. d->ErrorStr = msg.arg(errorHandler.line()).arg(errorHandler.column())
  88. .arg(errorHandler.statusMessage());
  89. return false;
  90. }
  91. return true;
  92. }
  93. //----------------------------------------------------------------------------
  94. bool ctkCmdLineModuleXmlValidator::error() const
  95. {
  96. return !d->ErrorStr.isEmpty();
  97. }
  98. //----------------------------------------------------------------------------
  99. QString ctkCmdLineModuleXmlValidator::errorString() const
  100. {
  101. return d->ErrorStr;
  102. }