ctkCmdLineModuleParameterParsers_p.h 9.1 KB

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