ctkCmdLineModuleXmlValidator.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. ctkCmdLineModuleXmlValidator::ctkCmdLineModuleXmlValidator(QIODevice *input)
  23. : Input(input), InputSchema(0)
  24. {
  25. }
  26. void ctkCmdLineModuleXmlValidator::setInput(QIODevice *input)
  27. {
  28. Input = input;
  29. }
  30. QIODevice* ctkCmdLineModuleXmlValidator::input() const
  31. {
  32. return Input;
  33. }
  34. void ctkCmdLineModuleXmlValidator::setInputSchema(QIODevice *input)
  35. {
  36. InputSchema = input;
  37. }
  38. bool ctkCmdLineModuleXmlValidator::validateInput()
  39. {
  40. ErrorStr.clear();
  41. if (!Input)
  42. {
  43. ErrorStr = "No input set for validation.";
  44. return false;
  45. }
  46. QIODevice* inputSchema = InputSchema;
  47. QScopedPointer<QIODevice> defaultInputSchema(new QFile(":/ctkCmdLineModule.xsd"));
  48. if (!inputSchema)
  49. {
  50. inputSchema = defaultInputSchema.data();
  51. inputSchema->open(QIODevice::ReadOnly);
  52. }
  53. ctkCmdLineModuleXmlMsgHandler errorHandler;
  54. QXmlSchema schema;
  55. schema.setMessageHandler(&errorHandler);
  56. if (!schema.load(inputSchema))
  57. {
  58. QString msg("Invalid input schema at line %1, column %2: %3");
  59. ErrorStr = msg.arg(errorHandler.line()).arg(errorHandler.column()).arg(errorHandler.statusMessage());
  60. return false;
  61. }
  62. QXmlSchemaValidator validator(schema);
  63. if (!validator.validate(Input))
  64. {
  65. QString msg("Error validating CLI XML description, at line %1, column %2: %3");
  66. ErrorStr = msg.arg(errorHandler.line()).arg(errorHandler.column())
  67. .arg(errorHandler.statusMessage());
  68. return false;
  69. }
  70. return true;
  71. }
  72. bool ctkCmdLineModuleXmlValidator::error() const
  73. {
  74. return !ErrorStr.isEmpty();
  75. }
  76. QString ctkCmdLineModuleXmlValidator::errorString() const
  77. {
  78. return ErrorStr;
  79. }