VR-Forces 4.2 Class Documentation
vrfXmlUtils.h
Go to the documentation of this file.
1 /*********************************************************************
2 ** Copyright (c) 2013 MaK Technologies, Inc.
3 ** All rights reserved.
4 *********************************************************************/
5 
6 #pragma once
7 
10 #include "mtl/env.h"
11 #include "mtl/envXMLWriter.h"
13 #include "vrfobjparam/vrfParamDb.h"
16 #include "vrfutil/rwRegData.h"
18 #include "extMtl/extDtcRead.h"
19 #include "vrfExtProtocol/objType.h"
20 #include "vrfExtProtocol/rangedObjType.h"
22 #include "vrfutil/nLenIntVec.h"
25 #include "vrfutil/subObjList.h"
26 #include "vrfutil/subObjEntry.h"
27 #include "vrfutil/formTbl.h"
28 #include "vrfutil/formTblEntry.h"
29 #include "vrfutil/soilList.h"
30 #include "vrfutil/soilFactors.h"
31 
32 #include "vlutil/vlStringUtil.h"
33 #include "vlutil/vlFilename.h"
34 
35 #include <boost/function.hpp>
36 #include <boost/lexical_cast.hpp>
37 
38 #include <iostream>
39 #include "vrfutil/rdrWritr.h"
41 
43 {
44 public:
45  typedef std::list<DtEnvValueSP> DtEnvValueSPList;
46 
50 
53  static void getObjectElements(DtEnvironmentSP env, DtEnvValueSPList& objectNodes);
54 
56  static DtEnvValueSP findObjectElementByMatchType(DtEnvironmentSP env, const DtObjectType& objType);
57  static DtEnvValueSP findObjectElementByObjectType(DtEnvironmentSP env, const DtObjectType& objType);
58 
62  static DtString getAttribute(DtEnvironmentSP env, const DtString& name,
63  DtEnvValueSP root);
64 
66  static bool hasAttribute(DtEnvironmentSP env, const DtString& name,
67  DtEnvValueSP root);
68 
69  static bool FindAndRemove(DtEnvironmentSP env, const DtString& nodeType, const DtString& paramName, DtEnvValueSP root);
70 
72  static void remove(DtEnvironmentSP, DtEnvValueSP);
73 
75  static DtString getPlatform(DtEnvironmentSP env, DtEnvValueSP root)
76  {
77  return getAttribute(env, vrfXml::Platform, root);
78  }
79 
80  static DtString GetObjectValue(DtEnvValueSP root)
81  {
82  return XmlBindingCharToMtl(root->value());
83  }
84 
85  static DtEnvValueSP AddElement(DtEnvironmentSP env, const DtString& type,
86  DtEnvValueSP root, const DtString& paramName = DtString::nullString())
87  {
88  FindAndRemove(env, type, paramName, root);
89  DtEnvValueSP newElement = env->addObject(type, type, root);
90  if (paramName != DtString::nullString())
91  {
92  env->addAttribute(vrfXml::ParamName, paramName, newElement);
93  }
94  return newElement;
95  }
96 
98  static DtEnvValueSP addbooleanElement(DtEnvironmentSP env, DtEnvValueSP root, const DtString& name, bool value)
99  {
100  DtEnvValueSP newElement = AddElement(env, vrfXml::Bool, root, name);
101  newElement->setValue(value ? "True" : "False");
102  return newElement;
103  }
104 
106  static DtEnvValueSP addRealElement(DtEnvironmentSP env, DtEnvValueSP root, const DtString& name, double value)
107  {
108  DtEnvValueSP newElement = AddElement(env, vrfXml::Real, root, name);
109  newElement->setValue(DtString(value));
110  return newElement;
111  }
112 
113  static DtEnvValueSP addBooleanAttribute(DtEnvironmentSP env, DtEnvValueSP root, const DtString& name, bool value)
114  {
115  return env->addAttribute(name, value ? "True" : "False", root);
116  }
117 
119  static DtEnvValueSP addStringElement(DtEnvironmentSP env, DtEnvValueSP root, const DtString& name, const DtString& value)
120  {
121  DtEnvValueSP newElement = AddElement(env, vrfXml::String, root, name);
122  newElement->setValue(MtlBindingCharToXml(value));
123  return newElement;
124  }
125 
126  static DtEnvValueSP addStringAttribute(DtEnvironmentSP env, DtEnvValueSP root, const DtString& name, const DtString& value)
127  {
128  DtEnvValueSP attr = env->getAttribute(name, root);
129  if (attr.get())
130  {
131  attr->setValue(MtlBindingCharToXml(value));
132  return attr;
133  }
134 
135  return attr = env->addAttribute(name, MtlBindingCharToXml(value), root);
136  }
137 
138  static void setObjectType(DtEnvironmentSP env, DtEnvValueSP root, const DtObjectType& t)
139  {
140  addStringAttribute(env, root, vrfXml::ObjectType, t.string());
141  }
142 
143  static void setMatchType(DtEnvironmentSP env, DtEnvValueSP root, const DtObjectType& t)
144  {
145  addStringAttribute(env, root, vrfXml::MatchType, t.string());
146  }
147 
148  static void setPlatform(DtEnvironmentSP env, DtEnvValueSP root, const DtString& t)
149  {
150  addStringAttribute(env, root, vrfXml::Platform, t);
151  }
152 
153  static DtObjectType StringToObjectType(const std::string& str)
154  {
155  if (str.length() == 0)
156  {
157  return DtObjectType::nullType();
158  }
159 
160  char* p = (char*)str.c_str();
161  DtObjectType objectType;
162 
163  objectType[7] = ((int)strtol(p, &p, 0));
164  if (*p) p++;
165  objectType[0] = ((int)strtol(p, &p, 0));
166  if (*p) p++;
167  objectType[1] = ((int)strtol(p, &p, 0));
168  if (*p) p++;
169  objectType[2] = ((int)strtol(p, &p, 0));
170  if (*p) p++;
171  objectType[3] = ((int)strtol(p, &p, 0));
172  if (*p) p++;
173  objectType[4] = ((int)strtol(p, &p, 0));
174  if (*p) p++;
175  objectType[5] = ((int)strtol(p, &p, 0));
176  if (*p) p++;
177  objectType[6] = ((int)strtol(p, &p, 0));
178 
179  return objectType;
180  }
181 
183  static DtObjectType getObjectTypeAttr(DtEnvironmentSP env, const DtString& attributeName, DtEnvValueSP root)
184  {
185  DtString objType = getAttribute(env, attributeName, root);
186  return StringToObjectType(objType.c_str());
187  }
188 
189  static DtObjectType getObjectType(DtEnvironmentSP env, DtEnvValueSP root,
190  bool fallbackOnMatchType = true)
191  {
192  DtObjectType objectType = getObjectTypeAttr(env, vrfXml::ObjectType, root);
193  if (objectType == DtObjectType::nullType() && fallbackOnMatchType)
194  {
195  return getMatchType(env, root, false);
196  }
197 
198  return objectType;
199  }
200 
201  static DtObjectType getMatchType(DtEnvironmentSP env, DtEnvValueSP root,
202  bool fallbackOnObjectType = true)
203  {
204  DtObjectType matchType = getObjectTypeAttr(env, vrfXml::MatchType, root);
205  if (matchType == DtObjectType::nullType() && fallbackOnObjectType)
206  {
207  return getObjectType(env, root, false);
208  }
209 
210  return matchType;
211  }
212 
213  static DtRangedObjectType getRangedObjectType(DtEnvironmentSP env,
214  const DtString& attributeName, DtEnvValueSP root);
215 
216  static double getDoubleAttribute(DtEnvironmentSP env, DtString attributeName, DtEnvValueSP root)
217  {
218  return boost::lexical_cast<double>(getAttribute(env, attributeName, root));
219  }
220 
221  static int getIntegerFromHex(DtEnvironmentSP env, const DtString& attributeName, DtEnvValueSP root)
222  {
223  char* p = 0;
224  return strtoul(getAttribute(env, attributeName, root), &p, 0);
225  }
226 
227  static int getInt(DtEnvironmentSP env, const DtString& attributeName, DtEnvValueSP root)
228  {
229  std::string attr = getAttribute(env, attributeName, root).c_str();
230  std::stringstream stringStream(attr);
231  int asInt;
232  stringStream >> asInt;
233  return asInt;
234  }
235 
236  //Convert a string to a bool, allowing 0/1 and "True"/"False" case insensitive
237  static bool ToBool(const DtString& str)
238  {
239  std::string stdStr = str.c_str();
240  std::transform(stdStr.begin(), stdStr.end(), stdStr.begin(), ::tolower);
241  std::istringstream is(stdStr);
242  bool b;
243  is >> std::boolalpha >> b;
244  return b;
245  }
246 
247  static bool getBoolAttribute(DtEnvironmentSP env, const DtString& attributeName, DtEnvValueSP root)
248  {
249  return ToBool(getAttribute(env, attributeName, root));
250  }
251 
254  static void ExtractBindings(DtVariableBindingsList& list,
255  const DtFilename& searchPath, DtEnvironmentSP env, DtEnvValueSP node);
256 
258  static DtReaderWriter* CreateRwObject(const DtString& rwType)
259  {
260  return DtReaderWriter::factory()->createItem(rwType, DtString::nullString(), NULL);
261  }
262 
263  static void getOrientation(DtEnvironmentSP env,
264  const DtEnvValueSP parentNode, DtTaitBryan& orientation)
265  {
266  orientation.setPsi(getDoubleAttribute(env, vrfXml::Yaw, parentNode));
267  orientation.setTheta(getDoubleAttribute(env, vrfXml::Pitch, parentNode));
268  orientation.setPhi(getDoubleAttribute(env, vrfXml::Roll, parentNode));
269  }
270 
271  static void getXYZAttrs(DtEnvironmentSP env, const DtEnvValueSP root,
272  double& x, double& y, double& z)
273  {
274  //attributes will either be named "x", "y", "z" or "right", "forward", "up"
275  if (hasAttribute(env, vrfXml::BodyForward, root))
276  {
277  x = getDoubleAttribute(env, vrfXml::BodyForward, root);
278  y = getDoubleAttribute(env, vrfXml::BodyRight, root);
279  z = getDoubleAttribute(env, vrfXml::BodyDown, root);
280  }
281  else
282  {
283  x = getDoubleAttribute(env, vrfXml::VectorX, root);
284  y = getDoubleAttribute(env, vrfXml::VectorY, root);
285  z = getDoubleAttribute(env, vrfXml::VectorZ, root);
286  }
287  }
288 
289  //Finds children by types in DtBindingTypes. Creates and initializes the rwObjects.
290  //bindingType will be used to specify which type of entries are in the list. If unspecified,
291  //all binding types will be checked.
292  static void PopulateList(
293  boost::function<void (DtReaderWriter*)> addFunction,
294  DtEnvironmentSP env, DtEnvValueSP root, const DtFilename& searchPath,
295  const DtVariableBindingsList& bindings,
296  const vrfXml::TypeList& bindingTypes = vrfXml::theBindingTypes.types);
297 
300  static void CreateVariableBinding(const DtString& bindingType,
301  const DtFilename& searchPath, const DtVariableBindingsList& systemBindings,
302  DtRwVariableBindings& newBindings,
303  DtEnvironmentSP env, DtEnvValueSP binding);
304 
306  static void AddSystems(DtComponentSystemDescriptorList& list,
307  const DtVariableBindingsList& variableBindings, DtEnvironmentSP env,
308  DtEnvValueSP* root, const DtFilename& searchPath);
309 
311  static void AddSystem(DtComponentSystemDescriptorList& list,
312  DtEnvironmentSP env, const DtVariableBindingsList& variableBindings,
313  DtEnvValueSP root, const DtFilename& searchPath,
314  DtEnvironmentSP parentEnt = DtEnvironmentSP(), DtString systemName = "");
315 
316  static DtEnvironmentSP CreateVrfXmlEnvironment();
317 
319  static DtEnvironmentSP CreateVrfXmlEnvironment(
320  const DtString& dataSourceFilename, bool showConsoleOutput = true);
321 
322  static DtEnvValueSP GetElementByType(DtEnvironmentSP env,
323  const DtString& type, DtEnvValueSP root);
324 
325  static DtEnvValueSPList GetElementsByType(DtEnvironmentSP env, const DtString& type,
326  DtEnvValueSP root);
327 
331 
332  static DtString XmlBindingCharToMtl(const DtString& str)
333  {
334  return DtReplaceAllSubstrings(str, vrfXml::XmlVariableChar,
336  }
337 
338  static DtString MtlBindingCharToXml(const DtString& str)
339  {
340  return DtReplaceAllSubstrings(str, vrfXml::MtlVariableChar,
342  }
344 
345  static DtString NLengthIntListToString(const DtNLengthIntegerVector& list);
346 
347  static DtString NLengthStringListToString(const DtRwNLengthStringVector& list);
348 
349  static void StringToNLengthIntList(const DtString& string, DtNLengthIntegerVector& list);
350 
351  static void StringToNLengthStringList(const DtString& string, DtRwNLengthStringVector& list);
352 
353  static DtEnvValueSP FindElementByParamName(DtEnvironmentSP env,
354  const DtString& nodeType, const DtString& attributeValue,
355  DtEnvValueSP envValue, bool traverseElements = true)
356  {
357  return FindElementByAttrValue(env, nodeType, vrfXml::ParamName,
358  attributeValue, envValue);
359  }
360 
361  static DtEnvValueSP FindElementByAttrValue(DtEnvironmentSP env,
362  const DtString& nodeType, const DtString& attributeName,
363  const DtString& attributeValue, DtEnvValueSP envValue,
364  bool traverseElements = true);
365 
366  typedef std::list<DtString> StringVector;
367  typedef std::vector<DtReaderWriterRegistryData*> RegDataVector;
368 
369  typedef std::map<DtString, DtString> GroupMappings;
370 
371  static DtReaderWriterRegistryData* DeepFindRw(const DtString& keyword,
372  DtReaderWriterRegistry& registry, StringVector& path);
373 
374  static bool IsGroupedElement(const DtString& rwName,
375  const DtXmlUtils::GroupMappings& groupedElements, DtString& groupName);
376 
377  static DtEnvValueSP FindGroupElement(DtEnvironmentSP env,
378  DtEnvValueSP parentNode, const DtString& groupName,
379  bool createIfMissing = true);
380 
381  static void FindAndRemoveGroupElement(DtEnvironmentSP env,
382  DtEnvValueSP parentNode, const DtString& groupName);
383 
384  static void IterateOverChildren(DtReaderWriterRegistry& oldRegistry,
385  DtReaderWriterRegistry& newRegistry, DtEnvironmentSP env,
386  DtEnvValueSP root, const GroupMappings& groupedElements, StringVector& path);
387 
388  static void ExtractVariableBindings(DtRwVariableBindings* variableBindings,
389  DtEnvironmentSP env, DtEnvValueSP parentNode);
390 
391  static void ExtractSystem(DtReaderWriter* systemObject, DtEnvironmentSP env,
392  DtEnvValueSP simObjectNode);
393 
394  static void ExtractSystems(DtReaderWriterRegistry& oldRegistry,
395  DtEnvironmentSP env, DtEnvValueSP simObjectNode);
396 
397  static void WriteXML(DtEnvironmentSP env, DtString filename)
398  {
399  DtEnvXMLWriter writer(vrfXml::SimObject);
400  writer.writeFile(*env, filename);
401  }
402 
403  static DtString removeQuotes(const DtString& str)
404  {
405  const char quote('"');
406  DtString output(str);
407  if (output[0] == quote && output[output.size() - 1] == quote)
408  {
409  output.snipFront(quote);
410  output.snipBack(quote);
411  }
412  return output;
413  }
414 
415  typedef std::list<DtString> NameList;
416 
417  static void WriteVector(DtEnvironmentSP env, DtEnvValueSP root,
418  const DtRwVector* vec, bool writeAsBodyPosition)
419  {
420  if (writeAsBodyPosition)
421  {
422  env->addAttribute(vrfXml::BodyRight, (*vec)[1], root);
423  env->addAttribute(vrfXml::BodyForward, (*vec)[0], root);
424  env->addAttribute(vrfXml::BodyDown, (*vec)[2], root);
425  }
426  else
427  {
428  env->addAttribute(vrfXml::VectorX, (*vec)[0], root);
429  env->addAttribute(vrfXml::VectorY, (*vec)[1], root);
430  env->addAttribute(vrfXml::VectorZ, (*vec)[2], root);
431  }
432  }
433 
434  static bool WriteAsBodyPosition(const DtReaderWriter* rwObject)
435  {
436  return vrfXml::RwTypeToXmlType(rwObject) == vrfXml::BodyPosition;
437  }
438 
439  static DtString IntToHexString(const int& number)
440  {
441  char buff[8];
442  sprintf(buff, "%X", number);
443  DtString app("0X");
444  app += buff;
445  return app;
446  }
447 };

Document ID: Generated on Sun Nov 24 19:49:21 EST 2013 from SVN revision 133924
Copyright © 2005-2013 VT MÄK. All Rights Reserved (www.mak.com)