JSBSim Flight Dynamics Model  1.2.0 (05 Nov 2023)
An Open Source Flight Dynamics and Control Software Library in C++
Element Class Reference

Detailed Description

Definition at line 143 of file FGXMLElement.h.

+ Inheritance diagram for Element:
+ Collaboration diagram for Element:

Public Member Functions

 Element (const std::string &nm)
 Constructor. More...
 
 ~Element (void)
 Destructor.
 
void AddAttribute (const std::string &name, const std::string &value)
 Stores an attribute belonging to this element. More...
 
void AddChildElement (Element *el)
 Adds a child element to the list of children stored for this element. More...
 
void AddData (std::string d)
 Stores data belonging to this element. More...
 
void ChangeName (const std::string &_name)
 
double DisperseValue (Element *e, double val, const std::string &supplied_units="", const std::string &target_units="")
 
ElementFindElement (const std::string &el="")
 Searches for a specified element. More...
 
FGColumnVector3 FindElementTripletConvertTo (const std::string &target_units)
 Composes a 3-element column vector for the supplied location or orientation. More...
 
std::string FindElementValue (const std::string &el="")
 Searches for the named element and returns the string data belonging to it. More...
 
bool FindElementValueAsBoolean (const std::string &el="")
 Searches for the named element and returns the data belonging to it as a bool. More...
 
double FindElementValueAsNumber (const std::string &el="")
 Searches for the named element and returns the data belonging to it as a number. More...
 
double FindElementValueAsNumberConvertFromTo (const std::string &el, const std::string &supplied_units, const std::string &target_units)
 Searches for the named element and converts and returns the data belonging to it. More...
 
double FindElementValueAsNumberConvertTo (const std::string &el, const std::string &target_units)
 Searches for the named element and converts and returns the data belonging to it. More...
 
ElementFindNextElement (const std::string &el="")
 Searches for the next element as specified. More...
 
std::string GetAttributeValue (const std::string &key)
 Retrieves an attribute. More...
 
double GetAttributeValueAsNumber (const std::string &key)
 Retrieves an attribute value as a double precision real number. More...
 
double GetDataAsNumber (void)
 Converts the element data to a number. More...
 
std::string GetDataLine (unsigned int i=0)
 Gets a line of data belonging to an element. More...
 
ElementGetElement (unsigned int el=0)
 Returns a pointer to the element requested by index. More...
 
const std::string & GetFileName (void) const
 Returns the name of the file in which the element has been read. More...
 
int GetLineNumber (void) const
 Returns the line number at which the element has been defined. More...
 
const std::string & GetName (void) const
 Retrieves the element name. More...
 
ElementGetNextElement (void)
 Returns a pointer to the next element in the list. More...
 
unsigned int GetNumDataLines (void)
 Returns the number of lines of data stored.
 
unsigned int GetNumElements (const std::string &element_name)
 Returns the number of named child elements for this element.
 
unsigned int GetNumElements (void)
 Returns the number of child elements for this element.
 
ElementGetParent (void)
 Returns a pointer to the parent of an element. More...
 
bool HasAttribute (const std::string &key)
 Determines if an element has the supplied attribute. More...
 
void MergeAttributes (Element *el)
 Merges the attributes of the current element with another element. More...
 
void Print (unsigned int level=0)
 Prints the element. More...
 
std::string ReadFrom (void) const
 Return a string that contains a description of the location where the current XML element was read from. More...
 
bool SetAttributeValue (const std::string &key, const std::string &value)
 Modifies an attribute. More...
 
void SetFileName (const std::string &name)
 Set the name of the file in which the element has been read. More...
 
void SetLineNumber (int line)
 Set the line number at which the element has been read. More...
 
void SetParent (Element *p)
 This function sets the value of the parent class attribute to the supplied Element pointer. More...
 

Constructor & Destructor Documentation

◆ Element()

Element ( const std::string &  nm)

Constructor.

Parameters
nmthe name of this element (if given)

Definition at line 52 of file FGXMLElement.cpp.

53 {
54  name = nm;
55  parent = 0L;
56  element_index = 0;
57  line_number = -1;
58 
59  if (!converterIsInitialized) {
60  converterIsInitialized = true;
61  // convert ["from"]["to"] = factor, so: from * factor = to
62  // Length
63  convert["M"]["FT"] = 3.2808399;
64  convert["FT"]["M"] = 1.0/convert["M"]["FT"];
65  convert["CM"]["FT"] = 0.032808399;
66  convert["FT"]["CM"] = 1.0/convert["CM"]["FT"];
67  convert["KM"]["FT"] = 3280.8399;
68  convert["FT"]["KM"] = 1.0/convert["KM"]["FT"];
69  convert["FT"]["IN"] = 12.0;
70  convert["IN"]["FT"] = 1.0/convert["FT"]["IN"];
71  convert["IN"]["M"] = convert["IN"]["FT"] * convert["FT"]["M"];
72  convert["M"]["IN"] = convert["M"]["FT"] * convert["FT"]["IN"];
73  // Area
74  convert["M2"]["FT2"] = convert["M"]["FT"]*convert["M"]["FT"];
75  convert["FT2"]["M2"] = 1.0/convert["M2"]["FT2"];
76  convert["CM2"]["FT2"] = convert["CM"]["FT"]*convert["CM"]["FT"];
77  convert["FT2"]["CM2"] = 1.0/convert["CM2"]["FT2"];
78  convert["M2"]["IN2"] = convert["M"]["IN"]*convert["M"]["IN"];
79  convert["IN2"]["M2"] = 1.0/convert["M2"]["IN2"];
80  convert["FT2"]["IN2"] = 144.0;
81  convert["IN2"]["FT2"] = 1.0/convert["FT2"]["IN2"];
82  // Volume
83  convert["IN3"]["CC"] = 16.387064;
84  convert["CC"]["IN3"] = 1.0/convert["IN3"]["CC"];
85  convert["FT3"]["IN3"] = 1728.0;
86  convert["IN3"]["FT3"] = 1.0/convert["FT3"]["IN3"];
87  convert["M3"]["FT3"] = 35.3146667;
88  convert["FT3"]["M3"] = 1.0/convert["M3"]["FT3"];
89  convert["LTR"]["IN3"] = 61.0237441;
90  convert["IN3"]["LTR"] = 1.0/convert["LTR"]["IN3"];
91  convert["GAL"]["FT3"] = 0.133681;
92  convert["FT3"]["GAL"] = 1.0/convert["GAL"]["FT3"];
93  convert["IN3"]["GAL"] = convert["IN3"]["FT3"]*convert["FT3"]["GAL"];
94  convert["LTR"]["GAL"] = convert["LTR"]["IN3"]*convert["IN3"]["GAL"];
95  convert["M3"]["GAL"] = 1000.*convert["LTR"]["GAL"];
96  convert["CC"]["GAL"] = convert["CC"]["IN3"]*convert["IN3"]["GAL"];
97  // Mass & Weight
98  convert["LBS"]["KG"] = 0.45359237;
99  convert["KG"]["LBS"] = 1.0/convert["LBS"]["KG"];
100  convert["SLUG"]["KG"] = 14.59390;
101  convert["KG"]["SLUG"] = 1.0/convert["SLUG"]["KG"];
102  // Moments of Inertia
103  convert["SLUG*FT2"]["KG*M2"] = 1.35594;
104  convert["KG*M2"]["SLUG*FT2"] = 1.0/convert["SLUG*FT2"]["KG*M2"];
105  // Angles
106  convert["RAD"]["DEG"] = 180.0/M_PI;
107  convert["DEG"]["RAD"] = 1.0/convert["RAD"]["DEG"];
108  // Angular rates
109  convert["RAD/SEC"]["DEG/SEC"] = convert["RAD"]["DEG"];
110  convert["DEG/SEC"]["RAD/SEC"] = 1.0/convert["RAD/SEC"]["DEG/SEC"];
111  // Spring force
112  convert["LBS/FT"]["N/M"] = 14.5939;
113  convert["N/M"]["LBS/FT"] = 1.0/convert["LBS/FT"]["N/M"];
114  // Damping force
115  convert["LBS/FT/SEC"]["N/M/SEC"] = 14.5939;
116  convert["N/M/SEC"]["LBS/FT/SEC"] = 1.0/convert["LBS/FT/SEC"]["N/M/SEC"];
117  // Damping force (Square Law)
118  convert["LBS/FT2/SEC2"]["N/M2/SEC2"] = 47.880259;
119  convert["N/M2/SEC2"]["LBS/FT2/SEC2"] = 1.0/convert["LBS/FT2/SEC2"]["N/M2/SEC2"];
120  // Power
121  convert["WATTS"]["HP"] = 0.001341022;
122  convert["HP"]["WATTS"] = 1.0/convert["WATTS"]["HP"];
123  // Force
124  convert["N"]["LBS"] = 0.22482;
125  convert["LBS"]["N"] = 1.0/convert["N"]["LBS"];
126  // Velocity
127  convert["KTS"]["FT/SEC"] = 1.6878098571;
128  convert["FT/SEC"]["KTS"] = 1.0/convert["KTS"]["FT/SEC"];
129  convert["M/S"]["FT/S"] = 3.2808399;
130  convert["M/S"]["KTS"] = convert["M/S"]["FT/S"]/convert["KTS"]["FT/SEC"];
131  convert["M/SEC"]["FT/SEC"] = 3.2808399;
132  convert["FT/S"]["M/S"] = 1.0/convert["M/S"]["FT/S"];
133  convert["M/SEC"]["FT/SEC"] = 3.2808399;
134  convert["FT/SEC"]["M/SEC"] = 1.0/convert["M/SEC"]["FT/SEC"];
135  convert["KM/SEC"]["FT/SEC"] = 3280.8399;
136  convert["FT/SEC"]["KM/SEC"] = 1.0/convert["KM/SEC"]["FT/SEC"];
137  // Torque
138  convert["FT*LBS"]["N*M"] = 1.35581795;
139  convert["N*M"]["FT*LBS"] = 1/convert["FT*LBS"]["N*M"];
140  // Valve
141  convert["M4*SEC/KG"]["FT4*SEC/SLUG"] = convert["M"]["FT"]*convert["M"]["FT"]*
142  convert["M"]["FT"]*convert["M"]["FT"]/convert["KG"]["SLUG"];
143  convert["FT4*SEC/SLUG"]["M4*SEC/KG"] =
144  1.0/convert["M4*SEC/KG"]["FT4*SEC/SLUG"];
145  // Pressure
146  convert["INHG"]["PSF"] = 70.7180803;
147  convert["PSF"]["INHG"] = 1.0/convert["INHG"]["PSF"];
148  convert["ATM"]["INHG"] = 29.9246899;
149  convert["INHG"]["ATM"] = 1.0/convert["ATM"]["INHG"];
150  convert["PSI"]["INHG"] = 2.03625437;
151  convert["INHG"]["PSI"] = 1.0/convert["PSI"]["INHG"];
152  convert["INHG"]["PA"] = 3386.0; // inches Mercury to pascals
153  convert["PA"]["INHG"] = 1.0/convert["INHG"]["PA"];
154  convert["LBS/FT2"]["N/M2"] = 14.5939/convert["FT"]["M"];
155  convert["N/M2"]["LBS/FT2"] = 1.0/convert["LBS/FT2"]["N/M2"];
156  convert["LBS/FT2"]["PA"] = convert["LBS/FT2"]["N/M2"];
157  convert["PA"]["LBS/FT2"] = 1.0/convert["LBS/FT2"]["PA"];
158  // Mass flow
159  convert["KG/MIN"]["LBS/MIN"] = convert["KG"]["LBS"];
160  convert["KG/SEC"]["LBS/SEC"] = convert["KG"]["LBS"];
161  convert ["N/SEC"]["LBS/SEC"] = 0.224808943;
162  convert ["LBS/SEC"]["N/SEC"] = 1.0/convert ["N/SEC"]["LBS/SEC"];
163  // Fuel Consumption
164  convert["LBS/HP*HR"]["KG/KW*HR"] = 0.6083;
165  convert["KG/KW*HR"]["LBS/HP*HR"] = 1.0/convert["LBS/HP*HR"]["KG/KW*HR"];
166  // Density
167  convert["KG/L"]["LBS/GAL"] = 8.3454045;
168  convert["LBS/GAL"]["KG/L"] = 1.0/convert["KG/L"]["LBS/GAL"];
169  // Gravitational
170  convert["FT3/SEC2"]["M3/SEC2"] = convert["FT3"]["M3"];
171  convert["M3/SEC2"]["FT3/SEC2"] = convert["M3"]["FT3"];
172 
173  // Length
174  convert["M"]["M"] = 1.00;
175  convert["KM"]["KM"] = 1.00;
176  convert["FT"]["FT"] = 1.00;
177  convert["IN"]["IN"] = 1.00;
178  // Area
179  convert["M2"]["M2"] = 1.00;
180  convert["FT2"]["FT2"] = 1.00;
181  // Volume
182  convert["IN3"]["IN3"] = 1.00;
183  convert["CC"]["CC"] = 1.0;
184  convert["M3"]["M3"] = 1.0;
185  convert["FT3"]["FT3"] = 1.0;
186  convert["LTR"]["LTR"] = 1.0;
187  convert["GAL"]["GAL"] = 1.0;
188  // Mass & Weight
189  convert["KG"]["KG"] = 1.00;
190  convert["LBS"]["LBS"] = 1.00;
191  // Moments of Inertia
192  convert["KG*M2"]["KG*M2"] = 1.00;
193  convert["SLUG*FT2"]["SLUG*FT2"] = 1.00;
194  // Angles
195  convert["DEG"]["DEG"] = 1.00;
196  convert["RAD"]["RAD"] = 1.00;
197  // Angular rates
198  convert["DEG/SEC"]["DEG/SEC"] = 1.00;
199  convert["RAD/SEC"]["RAD/SEC"] = 1.00;
200  // Spring force
201  convert["LBS/FT"]["LBS/FT"] = 1.00;
202  convert["N/M"]["N/M"] = 1.00;
203  // Damping force
204  convert["LBS/FT/SEC"]["LBS/FT/SEC"] = 1.00;
205  convert["N/M/SEC"]["N/M/SEC"] = 1.00;
206  // Damping force (Square law)
207  convert["LBS/FT2/SEC2"]["LBS/FT2/SEC2"] = 1.00;
208  convert["N/M2/SEC2"]["N/M2/SEC2"] = 1.00;
209  // Power
210  convert["HP"]["HP"] = 1.00;
211  convert["WATTS"]["WATTS"] = 1.00;
212  // Force
213  convert["N"]["N"] = 1.00;
214  // Velocity
215  convert["FT/SEC"]["FT/SEC"] = 1.00;
216  convert["KTS"]["KTS"] = 1.00;
217  convert["M/S"]["M/S"] = 1.0;
218  convert["M/SEC"]["M/SEC"] = 1.0;
219  convert["KM/SEC"]["KM/SEC"] = 1.0;
220  // Torque
221  convert["FT*LBS"]["FT*LBS"] = 1.00;
222  convert["N*M"]["N*M"] = 1.00;
223  // Valve
224  convert["M4*SEC/KG"]["M4*SEC/KG"] = 1.0;
225  convert["FT4*SEC/SLUG"]["FT4*SEC/SLUG"] = 1.0;
226  // Pressure
227  convert["PSI"]["PSI"] = 1.00;
228  convert["PSF"]["PSF"] = 1.00;
229  convert["INHG"]["INHG"] = 1.00;
230  convert["ATM"]["ATM"] = 1.0;
231  convert["PA"]["PA"] = 1.0;
232  convert["N/M2"]["N/M2"] = 1.00;
233  convert["LBS/FT2"]["LBS/FT2"] = 1.00;
234  // Mass flow
235  convert["LBS/SEC"]["LBS/SEC"] = 1.00;
236  convert["KG/MIN"]["KG/MIN"] = 1.0;
237  convert["LBS/MIN"]["LBS/MIN"] = 1.0;
238  convert["N/SEC"]["N/SEC"] = 1.0;
239  // Fuel Consumption
240  convert["LBS/HP*HR"]["LBS/HP*HR"] = 1.0;
241  convert["KG/KW*HR"]["KG/KW*HR"] = 1.0;
242  // Density
243  convert["KG/L"]["KG/L"] = 1.0;
244  convert["LBS/GAL"]["LBS/GAL"] = 1.0;
245  // Gravitational
246  convert["FT3/SEC2"]["FT3/SEC2"] = 1.0;
247  convert["M3/SEC2"]["M3/SEC2"] = 1.0;
248  // Electrical
249  convert["VOLTS"]["VOLTS"] = 1.0;
250  convert["OHMS"]["OHMS"] = 1.0;
251  convert["AMPERES"]["AMPERES"] = 1.0;
252  }
253 }

Member Function Documentation

◆ AddAttribute()

void AddAttribute ( const std::string &  name,
const std::string &  value 
)

Stores an attribute belonging to this element.

Parameters
nameThe string name of the attribute.
valueThe string value of the attribute.

Definition at line 725 of file FGXMLElement.cpp.

726 {
727  attributes[name] = value;
728 }
+ Here is the caller graph for this function:

◆ AddChildElement()

void AddChildElement ( Element el)
inline

Adds a child element to the list of children stored for this element.

Parameters
elChild element to add.

Definition at line 345 of file FGXMLElement.h.

345 {children.push_back(el);}
+ Here is the caller graph for this function:

◆ AddData()

void AddData ( std::string  d)

Stores data belonging to this element.

Parameters
dthe data to store.

Definition at line 732 of file FGXMLElement.cpp.

733 {
734  string::size_type string_start = d.find_first_not_of(" \t");
735  if (string_start != string::npos && string_start > 0) {
736  d.erase(0,string_start);
737  }
738  data_lines.push_back(d);
739 }

◆ FindElement()

Element * FindElement ( const std::string &  el = "")

Searches for a specified element.

Finds the first element that matches the supplied string, or simply the first element if no search string is supplied. This function call resets the internal element counter to the first element.

Parameters
elthe search string (empty string by default).
Returns
a pointer to the first element that matches the supplied search string.

Definition at line 394 of file FGXMLElement.cpp.

395 {
396  if (el.empty() && children.size() >= 1) {
397  element_index = 1;
398  return children[0];
399  }
400  for (unsigned int i=0; i<children.size(); i++) {
401  if (el == children[i]->GetName()) {
402  element_index = i+1;
403  return children[i];
404  }
405  }
406  element_index = 0;
407  return 0L;
408 }
const std::string & GetName(void) const
Retrieves the element name.
Definition: FGXMLElement.h:179
+ Here is the caller graph for this function:

◆ FindElementTripletConvertTo()

FGColumnVector3 FindElementTripletConvertTo ( const std::string &  target_units)

Composes a 3-element column vector for the supplied location or orientation.

This function processes a LOCATION or ORIENTATION construct, returning a filled-out 3-element column vector containing the X, Y, Z or ROLL, PITCH, YAW elements found in the supplied element. If one of the mentioned components is not found, that component is set to zero and a warning message is printed. All three elements should be supplied.

Parameters
target_unitsthe string representing the native units used by JSBSim to which the value returned will be converted.
Returns
a column vector object built from the LOCATION or ORIENT components.

Definition at line 594 of file FGXMLElement.cpp.

595 {
596  FGColumnVector3 triplet;
597  Element* item;
598  double value=0.0;
599  string supplied_units = GetAttributeValue("unit");
600 
601  if (!supplied_units.empty()) {
602  if (convert.find(supplied_units) == convert.end()) {
603  std::stringstream s;
604  s << ReadFrom() << "Supplied unit: \"" << supplied_units
605  << "\" does not exist (typo?).";
606  cerr << s.str() << endl;
607  throw invalid_argument(s.str());
608  }
609  if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) {
610  std::stringstream s;
611  s << ReadFrom() << "Supplied unit: \"" << supplied_units
612  << "\" cannot be converted to " << target_units;
613  cerr << s.str() << endl;
614  throw invalid_argument(s.str());
615  }
616  }
617 
618  item = FindElement("x");
619  if (!item) item = FindElement("roll");
620  if (item) {
621  value = item->GetDataAsNumber();
622  if (!supplied_units.empty()) value *= convert[supplied_units][target_units];
623  triplet(1) = DisperseValue(item, value, supplied_units, target_units);
624  } else {
625  triplet(1) = 0.0;
626  }
627 
628 
629  item = FindElement("y");
630  if (!item) item = FindElement("pitch");
631  if (item) {
632  value = item->GetDataAsNumber();
633  if (!supplied_units.empty()) value *= convert[supplied_units][target_units];
634  triplet(2) = DisperseValue(item, value, supplied_units, target_units);
635  } else {
636  triplet(2) = 0.0;
637  }
638 
639  item = FindElement("z");
640  if (!item) item = FindElement("yaw");
641  if (item) {
642  value = item->GetDataAsNumber();
643  if (!supplied_units.empty()) value *= convert[supplied_units][target_units];
644  triplet(3) = DisperseValue(item, value, supplied_units, target_units);
645  } else {
646  triplet(3) = 0.0;
647  }
648 
649  return triplet;
650 }
Element * FindElement(const std::string &el="")
Searches for a specified element.
Element(const std::string &nm)
Constructor.
std::string GetAttributeValue(const std::string &key)
Retrieves an attribute.
std::string ReadFrom(void) const
Return a string that contains a description of the location where the current XML element was read fr...
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ FindElementValue()

string FindElementValue ( const std::string &  el = "")

Searches for the named element and returns the string data belonging to it.

This function allows the data belonging to a named element to be returned as a string. If no element is found, the empty string is returned. If no argument is supplied, the data string for the first element is returned.

Parameters
elthe name of the element being searched for (the empty string by default)
Returns
the data value for the named element as a string, or the empty string if the element cannot be found.

Definition at line 473 of file FGXMLElement.cpp.

474 {
475  Element* element = FindElement(el);
476  if (element) {
477  return element->GetDataLine();
478  } else {
479  return "";
480  }
481 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ FindElementValueAsBoolean()

bool FindElementValueAsBoolean ( const std::string &  el = "")

Searches for the named element and returns the data belonging to it as a bool.

This function allows the data belonging to a named element to be returned as a bool. If no element is found, false is returned. If no argument is supplied, the data for the first element is returned.

Parameters
elthe name of the element being searched for (the empty string by default)
Returns
the data value for the named element as a bool, or false if the data is missing. Zero will be false, while any other number will be true.

Definition at line 451 of file FGXMLElement.cpp.

452 {
453  Element* element = FindElement(el);
454  if (element) {
455  // check value as an ordinary number
456  double value = element->GetDataAsNumber();
457 
458  // now check how it should return data
459  if (value == 0) {
460  return false;
461  } else {
462  return true;
463  }
464  } else {
465  cerr << ReadFrom() << "Attempting to get non-existent element " << el << " ;returning false"
466  << endl;
467  return false;
468  }
469 }
+ Here is the call graph for this function:

◆ FindElementValueAsNumber()

double FindElementValueAsNumber ( const std::string &  el = "")

Searches for the named element and returns the data belonging to it as a number.

This function allows the data belonging to a named element to be returned as a double. If no element is found, HUGE_VAL is returned. If no argument is supplied, the data for the first element is returned.

Parameters
elthe name of the element being searched for (the empty string by default)
Returns
the data value for the named element as a double, or HUGE_VAL if the data is missing.

Definition at line 434 of file FGXMLElement.cpp.

435 {
436  Element* element = FindElement(el);
437  if (element) {
438  double value = element->GetDataAsNumber();
439  value = DisperseValue(element, value);
440  return value;
441  } else {
442  std::stringstream s;
443  s << ReadFrom() << "Attempting to get non-existent element " << el;
444  cerr << s.str() << endl;
445  throw length_error(s.str());
446  }
447 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ FindElementValueAsNumberConvertFromTo()

double FindElementValueAsNumberConvertFromTo ( const std::string &  el,
const std::string &  supplied_units,
const std::string &  target_units 
)

Searches for the named element and converts and returns the data belonging to it.

This function allows the data belonging to a named element to be returned as a double. If no element is found, HUGE_VAL is returned. If no argument is supplied, the data for the first element is returned. Additionally, this function converts the value from the units specified in the supplied_units parameter to the units specified in the target_units parameter. JSBSim itself, as specified by the target_units parameter. The currently allowable unit conversions are seen in the source file FGXMLElement.cpp. Also, see above in the main documentation for this class.

Parameters
elthe name of the element being searched for (the empty string by default)
supplied_unitsthe string representing the units of the value as supplied by the config file.
target_unitsthe string representing the native units used by JSBSim to which the value returned will be converted.
Returns
the unit-converted data value for the named element as a double, or HUGE_VAL if the data is missing.

Definition at line 552 of file FGXMLElement.cpp.

555 {
556  Element* element = FindElement(el);
557 
558  if (!element) {
559  std::stringstream s;
560  s << ReadFrom() << "Attempting to get non-existent element " << el;
561  cerr << s.str() << endl;
562  throw length_error(s.str());
563  }
564 
565  if (!supplied_units.empty()) {
566  if (convert.find(supplied_units) == convert.end()) {
567  std::stringstream s;
568  s << element->ReadFrom() << "Supplied unit: \"" << supplied_units
569  << "\" does not exist (typo?).";
570  cerr << s.str() << endl;
571  throw invalid_argument(s.str());
572  }
573  if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) {
574  std::stringstream s;
575  s << element->ReadFrom() << "Supplied unit: \"" << supplied_units
576  << "\" cannot be converted to " << target_units;
577  cerr << s.str() << endl;
578  throw invalid_argument(s.str());
579  }
580  }
581 
582  double value = element->GetDataAsNumber();
583  if (!supplied_units.empty()) {
584  value *= convert[supplied_units][target_units];
585  }
586 
587  value = DisperseValue(element, value, supplied_units, target_units);
588 
589  return value;
590 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ FindElementValueAsNumberConvertTo()

double FindElementValueAsNumberConvertTo ( const std::string &  el,
const std::string &  target_units 
)

Searches for the named element and converts and returns the data belonging to it.

This function allows the data belonging to a named element to be returned as a double. If no element is found, HUGE_VAL is returned. If no argument is supplied, the data for the first element is returned. Additionally, this function converts the value from the units specified in the config file (via the UNITS="" attribute in the element definition) to the native units used by JSBSim itself, as specified by the target_units parameter. The currently allowable unit conversions are seen in the source file FGXMLElement.cpp. Also, see above in the main documentation for this class.

Parameters
elthe name of the element being searched for (the empty string by default)
target_unitsthe string representing the native units used by JSBSim to which the value returned will be converted.
Returns
the unit-converted data value for the named element as a double, or HUGE_VAL if the data is missing.

Definition at line 485 of file FGXMLElement.cpp.

486 {
487  Element* element = FindElement(el);
488 
489  if (!element) {
490  std::stringstream s;
491  s << ReadFrom() << "Attempting to get non-existent element " << el;
492  cerr << s.str() << endl;
493  throw length_error(s.str());
494  }
495 
496  string supplied_units = element->GetAttributeValue("unit");
497 
498  if (!supplied_units.empty()) {
499  if (convert.find(supplied_units) == convert.end()) {
500  std::stringstream s;
501  s << element->ReadFrom() << "Supplied unit: \"" << supplied_units
502  << "\" does not exist (typo?).";
503  cerr << s.str() << endl;
504  throw invalid_argument(s.str());
505  }
506  if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) {
507  std::stringstream s;
508  s << element->ReadFrom() << "Supplied unit: \"" << supplied_units
509  << "\" cannot be converted to " << target_units;
510  cerr << s.str() << endl;
511  throw invalid_argument(s.str());
512  }
513  }
514 
515  double value = element->GetDataAsNumber();
516 
517  // Sanity check for angular values
518  if ((supplied_units == "RAD") && (fabs(value) > 2 * M_PI)) {
519  cerr << element->ReadFrom() << element->GetName() << " value "
520  << value << " RAD is outside the range [ -2*M_PI RAD ; +2*M_PI RAD ]"
521  << endl;
522  }
523  if ((supplied_units == "DEG") && (fabs(value) > 360.0)) {
524  cerr << element->ReadFrom() << element->GetName() << " value "
525  << value << " DEG is outside the range [ -360 DEG ; +360 DEG ]"
526  << endl;
527  }
528 
529 
530  if (!supplied_units.empty()) {
531  value *= convert[supplied_units][target_units];
532  }
533 
534  if ((target_units == "RAD") && (fabs(value) > 2 * M_PI)) {
535  cerr << element->ReadFrom() << element->GetName() << " value "
536  << value << " RAD is outside the range [ -2*M_PI RAD ; +2*M_PI RAD ]"
537  << endl;
538  }
539  if ((target_units == "DEG") && (fabs(value) > 360.0)) {
540  cerr << element->ReadFrom() << element->GetName() << " value "
541  << value << " DEG is outside the range [ -360 DEG ; +360 DEG ]"
542  << endl;
543  }
544 
545  value = DisperseValue(element, value, supplied_units, target_units);
546 
547  return value;
548 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ FindNextElement()

Element * FindNextElement ( const std::string &  el = "")

Searches for the next element as specified.

This function would be called after FindElement() is first called (in order to reset the internal counter). If no argument is supplied (or the empty string) a pointer to the very next element is returned. Otherwise, the next occurence of the named element is returned. If the end of the list is reached, 0 is returned.

Parameters
elthe name of the next element to find.
Returns
the pointer to the found element, or 0 if no appropriate element us found.

Definition at line 412 of file FGXMLElement.cpp.

413 {
414  if (el.empty()) {
415  if (element_index < children.size()) {
416  return children[element_index++];
417  } else {
418  element_index = 0;
419  return 0L;
420  }
421  }
422  for (unsigned int i=element_index; i<children.size(); i++) {
423  if (el == children[i]->GetName()) {
424  element_index = i+1;
425  return children[i];
426  }
427  }
428  element_index = 0;
429  return 0L;
430 }
+ Here is the caller graph for this function:

◆ GetAttributeValue()

string GetAttributeValue ( const std::string &  key)

Retrieves an attribute.

Parameters
keyspecifies the attribute key to retrieve the value of.
Returns
the key value (as a string), or the empty string if no such attribute exists.

Definition at line 265 of file FGXMLElement.cpp.

266 {
267  if (HasAttribute(attr)) return attributes[attr];
268  else return ("");
269 }
bool HasAttribute(const std::string &key)
Determines if an element has the supplied attribute.
Definition: FGXMLElement.h:155
+ Here is the caller graph for this function:

◆ GetAttributeValueAsNumber()

double GetAttributeValueAsNumber ( const std::string &  key)

Retrieves an attribute value as a double precision real number.

Parameters
keyspecifies the attribute key to retrieve the value of.
Returns
the key value (as a number), or the HUGE_VAL if no such attribute exists.

Definition at line 284 of file FGXMLElement.cpp.

285 {
286  string attribute = GetAttributeValue(attr);
287 
288  if (attribute.empty()) {
289  std::stringstream s;
290  s << ReadFrom() << "Expecting numeric attribute value, but got no data";
291  cerr << s.str() << endl;
292  throw length_error(s.str());
293  }
294  else {
295  double number=0;
296  if (is_number(trim(attribute)))
297  number = atof_locale_c(attribute);
298  else {
299  std::stringstream s;
300  s << ReadFrom() << "Expecting numeric attribute value, but got: " << attribute;
301  cerr << s.str() << endl;
302  throw BaseException(s.str());
303  }
304 
305  return (number);
306  }
307 }
+ Here is the caller graph for this function:

◆ GetDataAsNumber()

double GetDataAsNumber ( void  )

Converts the element data to a number.

This function attempts to convert the first (and presumably only) line of data "owned" by the element into a real number. If there is not exactly one line of data owned by the element, then HUGE_VAL is returned.

Returns
the numeric value of the data owned by the element.

Definition at line 346 of file FGXMLElement.cpp.

347 {
348  if (data_lines.size() == 1) {
349  double number=0;
350  if (is_number(trim(data_lines[0])))
351  number = atof_locale_c(data_lines[0]);
352  else {
353  std::stringstream s;
354  s << ReadFrom() << "Expected numeric value, but got: " << data_lines[0];
355  cerr << s.str() << endl;
356  throw BaseException(s.str());
357  }
358 
359  return number;
360  } else if (data_lines.empty()) {
361  std::stringstream s;
362  s << ReadFrom() << "Expected numeric value, but got no data";
363  cerr << s.str() << endl;
364  throw length_error(s.str());
365  } else {
366  cerr << ReadFrom() << "Attempting to get single data value in element "
367  << "<" << name << ">" << endl
368  << " from multiple lines:" << endl;
369  for(unsigned int i=0; i<data_lines.size(); ++i)
370  cerr << data_lines[i] << endl;
371  std::stringstream s;
372  s << ReadFrom() << "Attempting to get single data value in element "
373  << "<" << name << ">"
374  << " from multiple lines (" << data_lines.size() << ").";
375  throw length_error(s.str());
376  }
377 }
+ Here is the caller graph for this function:

◆ GetDataLine()

string GetDataLine ( unsigned int  i = 0)

Gets a line of data belonging to an element.

Parameters
ithe index of the data line to return (0 by default).
Returns
a string representing the data line requested, or the empty string if none exists.

Definition at line 338 of file FGXMLElement.cpp.

339 {
340  if (!data_lines.empty()) return data_lines[i];
341  else return string("");
342 }
+ Here is the caller graph for this function:

◆ GetElement()

Element * GetElement ( unsigned int  el = 0)

Returns a pointer to the element requested by index.

This function also resets an internal counter to the index, so that subsequent calls to GetNextElement() will return the following elements sequentially, until the last element is reached. At that point, GetNextElement() will return NULL.

Parameters
elthe index of the requested element (0 by default)
Returns
a pointer to the Element, or 0 if no valid element exists.

Definition at line 311 of file FGXMLElement.cpp.

312 {
313  if (children.size() > el) {
314  element_index = el;
315  return children[el];
316  }
317  else {
318  element_index = 0;
319  return 0L;
320  }
321 }
+ Here is the caller graph for this function:

◆ GetFileName()

const std::string& GetFileName ( void  ) const
inline

Returns the name of the file in which the element has been read.

Returns
the file name

Definition at line 235 of file FGXMLElement.h.

235 { return file_name; }
+ Here is the caller graph for this function:

◆ GetLineNumber()

int GetLineNumber ( void  ) const
inline

Returns the line number at which the element has been defined.

Returns
the line number

Definition at line 230 of file FGXMLElement.h.

230 { return line_number; }
+ Here is the caller graph for this function:

◆ GetName()

const std::string& GetName ( void  ) const
inline

Retrieves the element name.

Returns
the element name, or the empty string if no name has been set.

Definition at line 179 of file FGXMLElement.h.

179 {return name;}
+ Here is the caller graph for this function:

◆ GetNextElement()

Element * GetNextElement ( void  )

Returns a pointer to the next element in the list.

The function GetElement() must be called first to be sure that this function will return the correct element. The call to GetElement() resets the internal counter to zero. Subsequent calls to GetNextElement() return a pointer to subsequent elements in the list. When the final element is reached, 0 is returned.

Returns
a pointer to the next Element in the sequence, or 0 if no valid Element is present.

Definition at line 325 of file FGXMLElement.cpp.

326 {
327  if (children.size() > element_index+1) {
328  element_index++;
329  return children[element_index];
330  } else {
331  element_index = 0;
332  return 0L;
333  }
334 }
+ Here is the caller graph for this function:

◆ GetParent()

Element* GetParent ( void  )
inline

Returns a pointer to the parent of an element.

Returns
a pointer to the parent Element, or 0 if this is the top level Element.

Definition at line 225 of file FGXMLElement.h.

225 {return parent;}
+ Here is the caller graph for this function:

◆ HasAttribute()

bool HasAttribute ( const std::string &  key)
inline

Determines if an element has the supplied attribute.

Parameters
keyspecifies the attribute key to retrieve the value of.
Returns
true or false.

Definition at line 155 of file FGXMLElement.h.

155 {return attributes.find(key) != attributes.end();}
+ Here is the caller graph for this function:

◆ MergeAttributes()

void MergeAttributes ( Element el)

Merges the attributes of the current element with another element.

The attributes from the current element override the element that is passed as a parameter. In other words if the two elements have an attribute with the same name, the attribute from the current element is kept and the corresponding attribute of the other element is ignored.

Parameters
elelement with which the current element will merge its attributes.

Definition at line 756 of file FGXMLElement.cpp.

757 {
758  map<string, string>::iterator it;
759 
760  for (it=el->attributes.begin(); it != el->attributes.end(); ++it) {
761  if (attributes.find(it->first) == attributes.end())
762  attributes[it->first] = it->second;
763  else {
764  if (FGJSBBase::debug_lvl > 0 && (attributes[it->first] != it->second))
765  cout << el->ReadFrom() << " Attribute '" << it->first << "' is overridden in file "
766  << GetFileName() << ": line " << GetLineNumber() << endl
767  << " The value '" << attributes[it->first] << "' will be used instead of '"
768  << it->second << "'." << endl;
769  }
770  }
771 }
int GetLineNumber(void) const
Returns the line number at which the element has been defined.
Definition: FGXMLElement.h:230
const std::string & GetFileName(void) const
Returns the name of the file in which the element has been read.
Definition: FGXMLElement.h:235
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Print()

void Print ( unsigned int  level = 0)

Prints the element.

Prints this element and calls the Print routine for child elements.

Parameters
dThe tab level. A level corresponds to a single space.

Definition at line 701 of file FGXMLElement.cpp.

702 {
703  unsigned int i, spaces;
704 
705  level+=2;
706  for (spaces=0; spaces<=level; spaces++) cout << " "; // format output
707  cout << "Element Name: " << name;
708 
709  map<string, string>::iterator it;
710  for (it = attributes.begin(); it != attributes.end(); ++it)
711  cout << " " << it->first << " = " << it->second;
712 
713  cout << endl;
714  for (i=0; i<data_lines.size(); i++) {
715  for (spaces=0; spaces<=level; spaces++) cout << " "; // format output
716  cout << data_lines[i] << endl;
717  }
718  for (i=0; i<children.size(); i++) {
719  children[i]->Print(level);
720  }
721 }

◆ ReadFrom()

string ReadFrom ( void  ) const

Return a string that contains a description of the location where the current XML element was read from.

Returns
a string describing the file name and line number where the element was read.

Definition at line 743 of file FGXMLElement.cpp.

744 {
745  ostringstream message;
746 
747  message << endl
748  << "In file " << GetFileName() << ": line " << GetLineNumber()
749  << endl;
750 
751  return message.str();
752 }
+ Here is the caller graph for this function:

◆ SetAttributeValue()

bool SetAttributeValue ( const std::string &  key,
const std::string &  value 
)

Modifies an attribute.

Parameters
keyspecifies the attribute key to modify the value of.
valuenew key value (as a string).
Returns
false if it did not find any attribute with the requested key, true otherwise.

Definition at line 273 of file FGXMLElement.cpp.

274 {
275  bool ret = HasAttribute(key);
276  if (ret)
277  attributes[key] = value;
278 
279  return ret;
280 }

◆ SetFileName()

void SetFileName ( const std::string &  name)
inline

Set the name of the file in which the element has been read.

Parameters
namefile name

Definition at line 369 of file FGXMLElement.h.

369 { file_name = name; }

◆ SetLineNumber()

void SetLineNumber ( int  line)
inline

Set the line number at which the element has been read.

Parameters
lineline number.

Definition at line 364 of file FGXMLElement.h.

364 { line_number = line; }

◆ SetParent()

void SetParent ( Element p)
inline

This function sets the value of the parent class attribute to the supplied Element pointer.

Parameters
ppointer to the parent Element.

Definition at line 341 of file FGXMLElement.h.

341 {parent = p;}
+ Here is the caller graph for this function:

The documentation for this class was generated from the following files: