ctkCommandLineModuleExplorerMain.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. // Qt includes
  16. #include <QApplication>
  17. #include <QDebug>
  18. #include <QFile>
  19. #include <QBuffer>
  20. #include <QWidget>
  21. #include <QProcess>
  22. #include <QDebug>
  23. #include <QXmlQuery>
  24. #include <QUiLoader>
  25. // CTK includes
  26. #include <ctkCommandLineParser.h>
  27. #include <ctkCmdLineModuleXmlValidator.h>
  28. #include "ctkCmdLineModuleExplorerMainWindow.h"
  29. int main(int argc, char** argv)
  30. {
  31. QApplication myApp(argc, argv);
  32. myApp.setOrganizationName("CommonTK");
  33. myApp.setApplicationName("CommandLineModuleExplorer");
  34. ctkCommandLineParser cmdLineParser;
  35. cmdLineParser.setArgumentPrefix("--", "-");
  36. cmdLineParser.setStrictModeEnabled(true);
  37. cmdLineParser.addArgument("module", "", QVariant::String, "Path to a CLI module (executable), and show the generated GUI.");
  38. cmdLineParser.addArgument("gui", "", QVariant::String, "Path to a CLI XML file, and show the generated GUI.");
  39. cmdLineParser.addArgument("validate-module", "", QVariant::String, "Path to a CLI module (executable), and validate the XML.");
  40. cmdLineParser.addArgument("validate-xml", "", QVariant::String, "Path to a CLI XML file, and validate the XML.");
  41. cmdLineParser.addArgument("string", "", QVariant::String, "An XML string to validate. Be careful to quote correctly." );
  42. cmdLineParser.addArgument("xml", "", QVariant::Bool, "Generate XML for this application");
  43. cmdLineParser.addArgument("verbose", "v", QVariant::Bool, "Be verbose.");
  44. cmdLineParser.addArgument("help", "h", QVariant::Bool, "Print this help text.");
  45. bool parseOkay = false;
  46. QHash<QString, QVariant> args = cmdLineParser.parseArguments(argc, argv, &parseOkay);
  47. QTextStream out(stdout, QIODevice::WriteOnly);
  48. if(!parseOkay)
  49. {
  50. out << "Error parsing command line arguments: " << cmdLineParser.errorString() << '\n';
  51. return EXIT_FAILURE;
  52. }
  53. if (args.contains("help"))
  54. {
  55. out << "Usage:\n" << cmdLineParser.helpText();
  56. out.flush();
  57. return EXIT_SUCCESS;
  58. }
  59. if (args.contains("validate-module"))
  60. {
  61. if (args.contains("validate-xml"))
  62. {
  63. out << "Ignoring \"validate-xml\" option.\n\n";
  64. }
  65. QString input = args["validate-module"].toString();
  66. if (!QFile::exists(input))
  67. {
  68. qCritical() << "Module does not exist:" << input;
  69. return EXIT_FAILURE;
  70. }
  71. QProcess process;
  72. process.setReadChannel(QProcess::StandardOutput);
  73. process.start(input, QStringList("--xml"));
  74. if (!process.waitForFinished() || process.exitStatus() == QProcess::CrashExit ||
  75. process.error() != QProcess::UnknownError)
  76. {
  77. qWarning() << "The executable at" << input << "could not be started:" << process.errorString();
  78. return EXIT_FAILURE;
  79. }
  80. process.waitForReadyRead();
  81. QByteArray xml = process.readAllStandardOutput();
  82. if (args.contains("verbose"))
  83. {
  84. qDebug() << xml;
  85. }
  86. // validate the outputted xml description
  87. QBuffer xmlInput(&xml);
  88. xmlInput.open(QIODevice::ReadOnly);
  89. ctkCmdLineModuleXmlValidator validator(&xmlInput);
  90. if (!validator.validateInput())
  91. {
  92. qCritical() << validator.errorString();
  93. return EXIT_FAILURE;
  94. }
  95. return EXIT_SUCCESS;
  96. }
  97. else if (args.contains("validate-xml"))
  98. {
  99. QFile input(args["validate-xml"].toString());
  100. if (!input.exists())
  101. {
  102. qCritical() << "XML description does not exist:" << input.fileName();
  103. return EXIT_FAILURE;
  104. }
  105. input.open(QIODevice::ReadOnly);
  106. ctkCmdLineModuleXmlValidator validator(&input);
  107. if (!validator.validateInput())
  108. {
  109. qCritical() << validator.errorString();
  110. return EXIT_FAILURE;
  111. }
  112. return EXIT_SUCCESS;
  113. }
  114. else if (args.contains("string"))
  115. {
  116. QByteArray byteArray;
  117. byteArray.append(args["string"].toString());
  118. QBuffer buffer(&byteArray);
  119. buffer.open(QIODevice::ReadOnly);
  120. ctkCmdLineModuleXmlValidator validator(&buffer);
  121. if (!validator.validateInput())
  122. {
  123. qCritical() << validator.errorString();
  124. return EXIT_FAILURE;
  125. }
  126. out << "Validated successfully";
  127. return EXIT_SUCCESS;
  128. }
  129. else if (args.contains("xml"))
  130. {
  131. out << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  132. out << "<executable>\n";
  133. out << " <category>Utilities</category>\n";
  134. out << " <title>ctkCommandLineModuleExplorer</title>\n";
  135. out << " <description><![CDATA[Used to check the validity of CLI XML descriptors.]]></description>\n";
  136. out << " <version>0.0.1</version>\n";
  137. out << " <documentation-url>http://www.commontk.org</documentation-url>\n";
  138. out << " <license>Apache 2</license>\n";
  139. out << " <contributor>Various</contributor>\n";
  140. out << " <acknowledgements><![CDATA[]]></acknowledgements>\n";
  141. out << " <parameters>\n";
  142. out << " <label>Input parameters</label>\n";
  143. out << " <description><![CDATA[Input/output parameters]]></description>\n";
  144. out << " <string>\n";
  145. out << " <name>inputText</name>\n";
  146. out << " <longflag>string</longflag>\n";
  147. out << " <description>Input text containing an XML string.</description>\n";
  148. out << " <label>Enter XML as a string:</label>\n";
  149. out << " <channel>input</channel>\n";
  150. out << " </string>\n";
  151. out << " </parameters>\n";
  152. out << "</executable>\n";
  153. out.flush();
  154. return EXIT_SUCCESS;
  155. }
  156. ctkCLModuleExplorerMainWindow mainWindow;
  157. if (args.contains("module"))
  158. {
  159. mainWindow.addModule(args["module"].toString());
  160. }
  161. if (args.contains("gui"))
  162. {
  163. mainWindow.addModule(QString("xmlchecker://") + args["gui"].toString());
  164. }
  165. mainWindow.show();
  166. return myApp.exec();
  167. }