ctkCmdLineModuleParameterParsers_p.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. #ifndef CTKCMDLINEMODULEPARAMETERPARSERS_P_H
  16. #define CTKCMDLINEMODULEPARAMETERPARSERS_P_H
  17. #include <QXmlStreamReader>
  18. #include "ctkCmdLineModuleParameter.h"
  19. #include "ctkCmdLineModuleParameter_p.h"
  20. namespace {
  21. static bool parseBooleanAttribute(const QStringRef& attrValue)
  22. {
  23. if (attrValue.compare("true", Qt::CaseInsensitive) == 0 ||
  24. attrValue.compare("1") == 0)
  25. {
  26. return true;
  27. }
  28. return false;
  29. }
  30. }
  31. struct ctkCmdLineModuleParameterParser
  32. {
  33. public:
  34. virtual ~ctkCmdLineModuleParameterParser() {}
  35. virtual ctkCmdLineModuleParameter parse(QXmlStreamReader &xmlReader)
  36. {
  37. ctkCmdLineModuleParameter moduleParam = this->createModuleParameter();
  38. this->handleAttributes(moduleParam.d.data(), xmlReader);
  39. while(xmlReader.readNextStartElement())
  40. {
  41. this->handleSubElement(moduleParam.d.data(), xmlReader);
  42. }
  43. return moduleParam;
  44. }
  45. protected:
  46. virtual ctkCmdLineModuleParameter createModuleParameter()
  47. {
  48. return ctkCmdLineModuleParameter();
  49. }
  50. virtual void handleAttributes(ctkCmdLineModuleParameterPrivate* moduleParamPrivate, QXmlStreamReader& xmlReader)
  51. {
  52. // handle common attributes
  53. moduleParamPrivate->Hidden = parseBooleanAttribute(xmlReader.attributes().value("hidden"));
  54. }
  55. virtual bool handleSubElement(ctkCmdLineModuleParameterPrivate* moduleParamPrivate, QXmlStreamReader& xmlReader)
  56. {
  57. // handle common sub-elements
  58. QStringRef name = xmlReader.name();
  59. if (name.compare("name", Qt::CaseInsensitive) == 0)
  60. {
  61. moduleParamPrivate->Name = xmlReader.readElementText().trimmed();
  62. }
  63. else if (name.compare("description", Qt::CaseInsensitive) == 0)
  64. {
  65. moduleParamPrivate->Description = xmlReader.readElementText().trimmed();
  66. }
  67. else if (name.compare("label", Qt::CaseInsensitive) == 0)
  68. {
  69. moduleParamPrivate->Label = xmlReader.readElementText().trimmed();
  70. }
  71. else if (name.compare("default", Qt::CaseInsensitive) == 0)
  72. {
  73. moduleParamPrivate->Default = xmlReader.readElementText().trimmed();
  74. }
  75. else if (name.compare("flag", Qt::CaseInsensitive) == 0)
  76. {
  77. QString flag = xmlReader.readElementText().trimmed();
  78. if (flag.startsWith('-')) flag = flag.remove(0, 1);
  79. moduleParamPrivate->Flag = flag;
  80. moduleParamPrivate->FlagAliasesAsString = xmlReader.attributes().value("alias").toString();
  81. moduleParamPrivate->DeprecatedFlagAliasesAsString = xmlReader.attributes().value("deprecatedalias").toString();
  82. }
  83. else if (name.compare("longflag", Qt::CaseInsensitive) == 0)
  84. {
  85. QString longFlag = xmlReader.readElementText().trimmed();
  86. if (longFlag.startsWith('-')) longFlag = longFlag.remove(0, 1);
  87. moduleParamPrivate->LongFlag = longFlag;
  88. moduleParamPrivate->LongFlagAliasesAsString = xmlReader.attributes().value("alias").toString();
  89. moduleParamPrivate->DeprecatedLongFlagAliasesAsString = xmlReader.attributes().value("deprecatedalias").toString();
  90. }
  91. else if (name.compare("index", Qt::CaseInsensitive) == 0)
  92. {
  93. moduleParamPrivate->Index = xmlReader.readElementText().toInt();
  94. }
  95. else if (name.compare("channel", Qt::CaseInsensitive) == 0)
  96. {
  97. moduleParamPrivate->Channel = xmlReader.readElementText().trimmed();
  98. }
  99. else
  100. {
  101. xmlReader.skipCurrentElement();
  102. return false;
  103. }
  104. return true;
  105. }
  106. bool handleConstraintsElement(ctkCmdLineModuleParameterPrivate* moduleParamPrivate, QXmlStreamReader& xmlReader)
  107. {
  108. moduleParamPrivate->Constraints = true;
  109. while(xmlReader.readNextStartElement())
  110. {
  111. QStringRef constraintElem = xmlReader.name();
  112. if (constraintElem.compare("minimum", Qt::CaseInsensitive) == 0)
  113. {
  114. moduleParamPrivate->Minimum = xmlReader.readElementText().trimmed();
  115. }
  116. else if (constraintElem.compare("maximum", Qt::CaseInsensitive) == 0)
  117. {
  118. moduleParamPrivate->Maximum = xmlReader.readElementText().trimmed();
  119. }
  120. else if (constraintElem.compare("step", Qt::CaseInsensitive) == 0)
  121. {
  122. moduleParamPrivate->Step = xmlReader.readElementText().trimmed();
  123. }
  124. else
  125. {
  126. xmlReader.skipCurrentElement();
  127. return false;
  128. }
  129. }
  130. return true;
  131. }
  132. };
  133. class ctkCmdLineModuleMultipleParameterParser : public ctkCmdLineModuleParameterParser
  134. {
  135. protected:
  136. void handleAttributes(ctkCmdLineModuleParameterPrivate* moduleParamPrivate, QXmlStreamReader& xmlReader)
  137. {
  138. ctkCmdLineModuleParameterParser::handleAttributes(moduleParamPrivate, xmlReader);
  139. moduleParamPrivate->Hidden = parseBooleanAttribute(xmlReader.attributes().value("multiple"));
  140. }
  141. };
  142. class ctkCmdLineModuleScalarVectorParameterParser : public ctkCmdLineModuleParameterParser
  143. {
  144. protected:
  145. bool handleSubElement(ctkCmdLineModuleParameterPrivate* moduleParamPrivate, QXmlStreamReader& xmlReader)
  146. {
  147. QStringRef name = xmlReader.name();
  148. if (name.compare("constraints", Qt::CaseInsensitive) == 0)
  149. {
  150. return handleConstraintsElement(moduleParamPrivate, xmlReader);
  151. }
  152. else
  153. {
  154. return ctkCmdLineModuleParameterParser::handleSubElement(moduleParamPrivate, xmlReader);
  155. }
  156. }
  157. };
  158. class ctkCmdLineModuleScalarParameterParser : public ctkCmdLineModuleMultipleParameterParser
  159. {
  160. protected:
  161. bool handleSubElement(ctkCmdLineModuleParameterPrivate* moduleParamPrivate, QXmlStreamReader& xmlReader)
  162. {
  163. QStringRef name = xmlReader.name();
  164. if (name.compare("constraints", Qt::CaseInsensitive) == 0)
  165. {
  166. return handleConstraintsElement(moduleParamPrivate, xmlReader);
  167. }
  168. else
  169. {
  170. return ctkCmdLineModuleMultipleParameterParser::handleSubElement(moduleParamPrivate, xmlReader);
  171. }
  172. }
  173. };
  174. class ctkCmdLineModuleEnumerationParameterParser : public ctkCmdLineModuleParameterParser
  175. {
  176. protected:
  177. bool handleSubElement(ctkCmdLineModuleParameterPrivate* moduleParamPrivate, QXmlStreamReader& xmlReader)
  178. {
  179. QStringRef name = xmlReader.name();
  180. if (name.compare("element", Qt::CaseInsensitive) == 0)
  181. {
  182. moduleParamPrivate->Elements.push_back(xmlReader.readElementText().trimmed());
  183. return true;
  184. }
  185. else
  186. {
  187. return ctkCmdLineModuleParameterParser::handleSubElement(moduleParamPrivate, xmlReader);
  188. }
  189. }
  190. };
  191. class ctkCmdLineModulePointParameterParser : public ctkCmdLineModuleMultipleParameterParser
  192. {
  193. protected:
  194. void handleAttributes(ctkCmdLineModuleParameterPrivate* moduleParamPrivate, QXmlStreamReader& xmlReader)
  195. {
  196. ctkCmdLineModuleMultipleParameterParser::handleAttributes(moduleParamPrivate, xmlReader);
  197. moduleParamPrivate->CoordinateSystem = xmlReader.attributes().value("coordinateSystem").toString().trimmed();
  198. }
  199. };
  200. class ctkCmdLineModuleFileParameterParser : public ctkCmdLineModuleMultipleParameterParser
  201. {
  202. protected:
  203. void handleAttributes(ctkCmdLineModuleParameterPrivate* moduleParamPrivate, QXmlStreamReader& xmlReader)
  204. {
  205. ctkCmdLineModuleMultipleParameterParser::handleAttributes(moduleParamPrivate, xmlReader);
  206. moduleParamPrivate->FileExtensionsAsString =xmlReader.attributes().value("fileExtensions").toString().trimmed();
  207. }
  208. };
  209. class ctkCmdLineModuleGeometryParameterParser : public ctkCmdLineModuleMultipleParameterParser
  210. {
  211. protected:
  212. void handleAttributes(ctkCmdLineModuleParameterPrivate* moduleParamPrivate, QXmlStreamReader& xmlReader)
  213. {
  214. ctkCmdLineModuleMultipleParameterParser::handleAttributes(moduleParamPrivate, xmlReader);
  215. moduleParamPrivate->setFileExtensionsAsString(xmlReader.attributes().value("fileExtensions").toString().trimmed());
  216. moduleParamPrivate->Type = xmlReader.attributes().value("type").toString().trimmed();
  217. }
  218. };
  219. class ctkCmdLineModuleImageParameterParser : public ctkCmdLineModuleMultipleParameterParser
  220. {
  221. protected:
  222. void handleAttributes(ctkCmdLineModuleParameterPrivate* moduleParamPrivate, QXmlStreamReader& xmlReader)
  223. {
  224. ctkCmdLineModuleMultipleParameterParser::handleAttributes(moduleParamPrivate, xmlReader);
  225. moduleParamPrivate->setFileExtensionsAsString(xmlReader.attributes().value("fileExtensions").toString().trimmed());
  226. moduleParamPrivate->Type = xmlReader.attributes().value("type").toString().trimmed();
  227. }
  228. };
  229. #endif // CTKCMDLINEMODULEPARAMETERPARSERS_P_H