ctkCmdLineModuleParameterParsers_p.h 8.9 KB

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