ctkCmdLineModuleXmlProgressWatcher.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 "ctkCmdLineModuleXmlProgressWatcher.h"
  16. #include <QIODevice>
  17. #include <QProcess>
  18. #include <QXmlStreamReader>
  19. #include <QDebug>
  20. namespace {
  21. static QString FILTER_START = "filter-start";
  22. static QString FILTER_NAME = "filter-name";
  23. static QString FILTER_COMMENT = "filter-comment";
  24. static QString FILTER_PROGRESS = "filter-progress";
  25. static QString FILTER_END = "filter-end";
  26. }
  27. //----------------------------------------------------------------------------
  28. class ctkCmdLineModuleXmlProgressWatcherPrivate
  29. {
  30. public:
  31. ctkCmdLineModuleXmlProgressWatcherPrivate(QIODevice* input, ctkCmdLineModuleXmlProgressWatcher* qq)
  32. : input(input), process(NULL), readPos(0), q(qq), error(false), currentProgress(0)
  33. {
  34. // wrap the content in an artifical root element
  35. reader.addData("<module-root>");
  36. }
  37. ctkCmdLineModuleXmlProgressWatcherPrivate(QProcess* input, ctkCmdLineModuleXmlProgressWatcher* qq)
  38. : input(input), process(input), readPos(0), q(qq), error(false), currentProgress(0)
  39. {
  40. // wrap the content in an artifical root element
  41. reader.addData("<module-root>");
  42. }
  43. void _q_readyRead()
  44. {
  45. input->seek(readPos);
  46. reader.addData(input->readAll());
  47. readPos = input->pos();
  48. parseProgressXml();
  49. }
  50. void _q_readyReadError()
  51. {
  52. emit q->errorDataAvailable(process->readAllStandardError());
  53. }
  54. void parseProgressXml()
  55. {
  56. QXmlStreamReader::TokenType type = reader.readNext();
  57. QByteArray outputData;
  58. while(type != QXmlStreamReader::Invalid)
  59. {
  60. switch(type)
  61. {
  62. case QXmlStreamReader::NoToken: break;
  63. case QXmlStreamReader::Characters:
  64. {
  65. if (stack.empty())
  66. {
  67. QByteArray output(reader.text().toString().toAscii());
  68. // get rid of a possible newline after the last xml end tag
  69. if (output.startsWith('\n')) output = output.remove(0,1);
  70. outputData.append(output);
  71. break;
  72. }
  73. if (stack.size() == 2 && stack.front() == FILTER_START)
  74. {
  75. if (stack.back() == FILTER_NAME)
  76. {
  77. currentName = reader.text().toString().trimmed();
  78. }
  79. else if (stack.back() == FILTER_COMMENT)
  80. {
  81. currentComment = reader.text().toString().trimmed();
  82. }
  83. }
  84. else if (stack.size() == 1 && stack.back() == FILTER_PROGRESS)
  85. {
  86. currentProgress = reader.text().toString().toFloat();
  87. }
  88. break;
  89. }
  90. case QXmlStreamReader::StartElement:
  91. {
  92. QStringRef name = reader.name();
  93. QString parent;
  94. if (!stack.empty()) parent = stack.back();
  95. if (name.compare("module-root") != 0)
  96. {
  97. stack.push_back(name.toString());
  98. }
  99. if (name.compare(FILTER_START, Qt::CaseInsensitive) == 0 ||
  100. name.compare(FILTER_PROGRESS, Qt::CaseInsensitive) == 0 ||
  101. name.compare(FILTER_END, Qt::CaseInsensitive) == 0)
  102. {
  103. if (!parent.isEmpty())
  104. {
  105. unexpectedNestedElement(name.toString());
  106. break;
  107. }
  108. if (name.compare(FILTER_START, Qt::CaseInsensitive) == 0)
  109. {
  110. currentName.clear();
  111. currentComment.clear();
  112. currentProgress = 0;
  113. }
  114. }
  115. break;
  116. }
  117. case QXmlStreamReader::EndElement:
  118. {
  119. QStringRef name = reader.name();
  120. QString curr;
  121. QString parent;
  122. if (!stack.empty())
  123. {
  124. curr = stack.back();
  125. stack.pop_back();
  126. if (!stack.empty()) parent = stack.back();
  127. }
  128. if (parent.isEmpty())
  129. {
  130. if (!outputData.isEmpty())
  131. {
  132. emit q->outputDataAvailable(outputData);
  133. outputData.clear();
  134. }
  135. if (name.compare(FILTER_START, Qt::CaseInsensitive) == 0)
  136. {
  137. emit q->filterStarted(currentName, currentComment);
  138. }
  139. else if (name.compare(FILTER_PROGRESS, Qt::CaseInsensitive) == 0)
  140. {
  141. emit q->filterProgress(currentProgress);
  142. }
  143. else if (name.compare(FILTER_END, Qt::CaseInsensitive) == 0)
  144. {
  145. emit q->filterFinished(currentName);
  146. }
  147. }
  148. break;
  149. }
  150. default:
  151. break;
  152. }
  153. type = reader.readNext();
  154. }
  155. if (type == QXmlStreamReader::Invalid && reader.error() != QXmlStreamReader::PrematureEndOfDocumentError)
  156. {
  157. if (!error)
  158. {
  159. error = true;
  160. emit q->filterXmlError(QString("Error parsing XML at line %1, column %2: ")
  161. .arg(reader.lineNumber()).arg(reader.columnNumber()) + reader.errorString());
  162. }
  163. }
  164. }
  165. void unexpectedNestedElement(const QString& element)
  166. {
  167. if (!error)
  168. {
  169. error = true;
  170. emit q->filterXmlError(QString("\"%1\" must be a top-level element, found at line %2.")
  171. .arg(element).arg(reader.lineNumber()));
  172. }
  173. }
  174. QIODevice* input;
  175. QProcess* process;
  176. qint64 readPos;
  177. ctkCmdLineModuleXmlProgressWatcher* q;
  178. bool error;
  179. QXmlStreamReader reader;
  180. QList<QString> stack;
  181. QString currentName;
  182. QString currentComment;
  183. float currentProgress;
  184. };
  185. //----------------------------------------------------------------------------
  186. ctkCmdLineModuleXmlProgressWatcher::ctkCmdLineModuleXmlProgressWatcher(QIODevice* input)
  187. : d(new ctkCmdLineModuleXmlProgressWatcherPrivate(input, this))
  188. {
  189. if (d->input == NULL) return;
  190. if (!(d->input->openMode() & QIODevice::ReadOnly))
  191. {
  192. input->open(QIODevice::ReadOnly);
  193. }
  194. connect(d->input, SIGNAL(readyRead()), SLOT(_q_readyRead()));
  195. }
  196. //----------------------------------------------------------------------------
  197. ctkCmdLineModuleXmlProgressWatcher::ctkCmdLineModuleXmlProgressWatcher(QProcess* input)
  198. : d(new ctkCmdLineModuleXmlProgressWatcherPrivate(input, this))
  199. {
  200. if (d->input == NULL) return;
  201. connect(input, SIGNAL(readyReadStandardOutput()), SLOT(_q_readyRead()));
  202. connect(input, SIGNAL(readyReadStandardError()), SLOT(_q_readyReadError()));
  203. }
  204. //----------------------------------------------------------------------------
  205. ctkCmdLineModuleXmlProgressWatcher::~ctkCmdLineModuleXmlProgressWatcher()
  206. {
  207. }
  208. #include "moc_ctkCmdLineModuleXmlProgressWatcher.cxx"