ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
vtkCommandOptionsXMLParser.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ParaView
4  Module: vtkCommandOptionsXMLParser.cxx
5 
6  Copyright (c) Kitware, Inc.
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
16 #include "vtkCommandOptions.h"
17 #include "vtkObjectFactory.h"
18 #include <map>
19 
20 //----------------------------------------------------------------------------
22 {
23  enum Type
24  {
27  CHAR_TYPE
28  };
29  void* Variable;
32 };
33 
34 //----------------------------------------------------------------------------
35 //****************************************************************************
36 class vtkCommandOptionsXMLParserInternal
37 {
38 public:
39  vtkCommandOptionsXMLParserInternal() { this->ProcessType = 0; }
40 
41  void AddArgument(
42  const char* arg, vtkCommandOptionsXMLParserArgumentStructure::Type type, void* var, int ptype);
43  int SetArgument(const char* arg, const char* value);
44  int GetArgumentProcessType(const char* arg)
45  {
46  if (this->ArgumentToVariableMap.count(arg) == 0)
47  {
48  return 0;
49  }
50  return this->ArgumentToVariableMap[arg].ProcessType;
51  }
52  std::map<std::string, vtkCommandOptionsXMLParserArgumentStructure> ArgumentToVariableMap;
53  int ProcessType;
54 };
55 
56 //----------------------------------------------------------------------------
58 {
59  this->Internals->ProcessType = ptype;
60 }
61 
62 //----------------------------------------------------------------------------
64 {
65  if (!ptype)
66  {
68  return;
69  }
70 }
71 
72 //----------------------------------------------------------------------------
73 int vtkCommandOptionsXMLParserInternal::SetArgument(const char* arg, const char* value)
74 {
75  if (this->ArgumentToVariableMap.count(arg))
76  {
77  vtkCommandOptionsXMLParserArgumentStructure tmp = this->ArgumentToVariableMap[arg];
78  if (!(tmp.ProcessType & this->ProcessType || tmp.ProcessType == vtkCommandOptions::EVERYBODY ||
79  this->ProcessType == vtkCommandOptions::EVERYBODY))
80  {
81  // Silently skip argument in xml because the process type does not match
82  return 1;
83  }
84  switch (tmp.VariableType)
85  {
87  {
88  int* variable = (int*)tmp.Variable;
89  *variable = 1;
90  }
91  break;
93  {
94  if (!value)
95  {
96  vtkGenericWarningMacro("Bad XML Format missing Value for Name=\"" << arg << "\"");
97  return 0;
98  }
99  int* variable = (int*)tmp.Variable;
100  *variable = atoi(value);
101  }
102  break;
104  {
105  if (!value)
106  {
107  vtkGenericWarningMacro("Bad XML Format missing Value for Name=\"" << arg << "\"");
108  return 0;
109  }
110  char** variable = static_cast<char**>(tmp.Variable);
111  if (*variable)
112  {
113  delete[] * variable;
114  *variable = 0;
115  }
116  *variable = strcpy(new char[strlen(value) + 1], value);
117  }
118  break;
119  }
120  }
121  else
122  {
123  vtkGenericWarningMacro("Bad XML Format Unknown Option " << arg);
124  return 0;
125  }
126  return 1;
127 }
128 
129 //----------------------------------------------------------------------------
130 void vtkCommandOptionsXMLParserInternal::AddArgument(
131  const char* arg, vtkCommandOptionsXMLParserArgumentStructure::Type type, void* var, int ptype)
132 {
133  if (strlen(arg) < 3)
134  {
135  vtkGenericWarningMacro("AddArgument must take arguments of the form --foo. "
136  "Argument not added: "
137  << arg);
138  return;
139  }
141  vardata.VariableType = type;
142  vardata.Variable = var;
143  vardata.ProcessType = ptype;
144  this->ArgumentToVariableMap[std::string(arg + 2)] = vardata;
145 }
146 
147 //****************************************************************************
148 //----------------------------------------------------------------------------
149 
150 //----------------------------------------------------------------------------
152 
153 //----------------------------------------------------------------------------
155 {
156  this->InPVXTag = 0;
157  this->PVOptions = 0;
158  this->Internals = new vtkCommandOptionsXMLParserInternal;
159 }
160 
161 //----------------------------------------------------------------------------
163 {
164  delete this->Internals;
165 }
166 
167 //----------------------------------------------------------------------------
168 void vtkCommandOptionsXMLParser::StartElement(const char* name, const char** atts)
169 {
170  if (strcmp(name, "pvx") == 0)
171  {
172  this->InPVXTag = 1;
173  return;
174  }
175  if (!this->InPVXTag)
176  {
177  vtkErrorMacro("Bad XML Element found not in <pvx></pvx> tag: " << name);
178  return;
179  }
180  if (strcmp(name, "Option") == 0)
181  {
182  // check to see if the Named Option Name=option
183  // is valid for this type of process. Each argument
184  // has a number of processes that it is valid for.
185  if (atts && atts[0] && atts[1])
186  {
187  if (strcmp(atts[0], "Name") == 0)
188  {
189  int type = this->Internals->GetArgumentProcessType(atts[1]);
190  if (!(type & this->PVOptions->GetProcessType() || type == vtkCommandOptions::EVERYBODY))
191  {
192  return;
193  }
194  }
195  }
196  this->HandleOption(atts);
197  return;
198  }
199  if (strcmp(name, "Process") == 0)
200  {
201  this->HandleProcessType(atts);
202  return;
203  }
204  this->PVOptions->ParseExtraXMLTag(name, atts);
205 }
206 
207 //----------------------------------------------------------------------------
209 {
210  if (atts == nullptr || atts[0] == nullptr || strcmp(atts[0], "Type") != 0)
211  {
212  vtkErrorMacro(
213  "Bad XML Format 0 attributes found in Process Type, expected Process Type=\"..\" ");
214  return;
215  }
216  if (atts[1] == nullptr)
217  {
218  vtkErrorMacro("Bad XML Format 1 attributes found in Process Process Type=\"..\" ");
219  return;
220  }
221  this->SetProcessType(atts[1]);
222 }
223 
224 //----------------------------------------------------------------------------
226 {
227  // atts should be { "Name", "somename", "Value", "somevalue" }
228  // The Value is optional as it may be a boolean option
229  const char* nameTag = atts[0];
230  const char* name = 0;
231  // make sure there is a Name=
232  if (!nameTag || (strcmp(nameTag, "Name") != 0))
233  {
234  vtkErrorMacro(
235  "Bad XML Format 0 attributes found in Option, expected Name=\"..\" [Value=\"...\"]");
236  return;
237  }
238  // Set name to be the next attribute
239  name = atts[1];
240  // make sure Name=something
241  if (!name)
242  {
243  vtkErrorMacro("Bad XML Format, Name has no name.");
244  return;
245  }
246 
247  // Now look for Value tag
248  const char* valueTag = atts[2];
249  const char* value = 0;
250  // if there is a value tag and it is "Value"
251  if (valueTag && (strcmp(valueTag, "Value") != 0))
252  {
253  vtkErrorMacro("Bad XML Format missing value tag");
254  return;
255  }
256  else if (valueTag)
257  {
258  if (atts[3])
259  {
260  value = atts[3];
261  }
262  else
263  {
264  vtkErrorMacro("Bad XML Format missing value tag present but no value");
265  return;
266  }
267  }
268 
269  this->Internals->SetArgument(name, value);
270 }
271 
272 //----------------------------------------------------------------------------
274 {
275  if (strcmp(name, "pvx") == 0)
276  {
277  this->InPVXTag = 0;
278  return;
279  }
280  if (strcmp(name, "Process") == 0)
281  {
282  this->Internals->ProcessType = 0;
283  return;
284  }
285 }
286 
287 //----------------------------------------------------------------------------
288 void vtkCommandOptionsXMLParser::AddBooleanArgument(const char* longarg, int* var, int type)
289 {
290  this->Internals->AddArgument(
292 }
293 
294 //----------------------------------------------------------------------------
295 void vtkCommandOptionsXMLParser::AddArgument(const char* longarg, int* var, int type)
296 {
297  this->Internals->AddArgument(
299 }
300 
301 //----------------------------------------------------------------------------
302 void vtkCommandOptionsXMLParser::AddArgument(const char* longarg, char** var, int type)
303 {
304  this->Internals->AddArgument(
306 }
307 
308 //----------------------------------------------------------------------------
309 void vtkCommandOptionsXMLParser::PrintSelf(ostream& os, vtkIndent indent)
310 {
311  this->Superclass::PrintSelf(os, indent);
312 }
std::string name
char type
virtual void SetProcessType(const char *ptype)
void EndElement(const char *name) override
void AddArgument(const char *longarg, int *var, int type=0)
void StartElement(const char *name, const char **atts) override
void AddBooleanArgument(const char *longarg, int *var, int type=0)
void HandleProcessType(const char **atts)
void PrintSelf(ostream &os, vtkIndent indent) override
virtual int ParseExtraXMLTag(const char *, const char **)
vtkStandardNewMacro(vtkCommandOptionsXMLParser)