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