1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4 Date started: 09/28/2004
5 Purpose: XML element class
8 ------------- Copyright (C) 2001 Jon S. Berndt (jon@jsbsim.org) -------------
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU Lesser General Public License as published by the Free Software
12 Foundation; either version 2 of the License, or (at your option) any later
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
20 You should have received a copy of the GNU Lesser General Public License along with
21 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22 Place - Suite 330, Boston, MA 02111-1307, USA.
24 Further information about the GNU Lesser General Public License can also be found on
25 the world wide web at http://www.gnu.org.
27 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
31 #include "FGXMLElement.h"
39 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
45 static const char *IdSrc = "$Id: FGXMLElement.cpp,v 1.32 2011/02/13 00:42:45 jberndt Exp $";
46 static const char *IdHdr = ID_XMLELEMENT;
48 bool Element::converterIsInitialized = false;
49 map <string, map <string, double> > Element::convert;
51 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
55 Element::Element(const string& nm)
61 if (!converterIsInitialized) {
62 converterIsInitialized = true;
63 // convert ["from"]["to"] = factor, so: from * factor = to
65 convert["M"]["FT"] = 3.2808399;
66 convert["FT"]["M"] = 1.0/convert["M"]["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"];
74 convert["M2"]["FT2"] = convert["M"]["FT"]*convert["M"]["FT"];
75 convert["FT2"]["M2"] = 1.0/convert["M2"]["FT2"];
76 convert["M2"]["IN2"] = convert["M"]["IN"]*convert["M"]["IN"];
77 convert["IN2"]["M2"] = 1.0/convert["M2"]["IN2"];
78 convert["FT2"]["IN2"] = 144.0;
79 convert["IN2"]["FT2"] = 1.0/convert["FT2"]["IN2"];
81 convert["IN3"]["CC"] = 16.387064;
82 convert["CC"]["IN3"] = 1.0/convert["IN3"]["CC"];
83 convert["FT3"]["IN3"] = 1728.0;
84 convert["IN3"]["FT3"] = 1.0/convert["FT3"]["IN3"];
85 convert["M3"]["FT3"] = 35.3146667;
86 convert["FT3"]["M3"] = 1.0/convert["M3"]["FT3"];
87 convert["LTR"]["IN3"] = 61.0237441;
88 convert["IN3"]["LTR"] = 1.0/convert["LTR"]["IN3"];
90 convert["LBS"]["KG"] = 0.45359237;
91 convert["KG"]["LBS"] = 1.0/convert["LBS"]["KG"];
92 convert["SLUG"]["KG"] = 14.59390;
93 convert["KG"]["SLUG"] = 1.0/convert["SLUG"]["KG"];
95 convert["SLUG*FT2"]["KG*M2"] = 1.35594;
96 convert["KG*M2"]["SLUG*FT2"] = 1.0/convert["SLUG*FT2"]["KG*M2"];
98 convert["RAD"]["DEG"] = 180.0/M_PI;
99 convert["DEG"]["RAD"] = 1.0/convert["RAD"]["DEG"];
101 convert["RAD/SEC"]["DEG/SEC"] = convert["RAD"]["DEG"];
102 convert["DEG/SEC"]["RAD/SEC"] = 1.0/convert["RAD/SEC"]["DEG/SEC"];
104 convert["LBS/FT"]["N/M"] = 14.5939;
105 convert["N/M"]["LBS/FT"] = 1.0/convert["LBS/FT"]["N/M"];
107 convert["LBS/FT/SEC"]["N/M/SEC"] = 14.5939;
108 convert["N/M/SEC"]["LBS/FT/SEC"] = 1.0/convert["LBS/FT/SEC"]["N/M/SEC"];
109 // Damping force (Square Law)
110 convert["LBS/FT2/SEC2"]["N/M2/SEC2"] = 47.880259;
111 convert["N/M2/SEC2"]["LBS/FT2/SEC2"] = 1.0/convert["LBS/FT2/SEC2"]["N/M2/SEC2"];
113 convert["WATTS"]["HP"] = 0.001341022;
114 convert["HP"]["WATTS"] = 1.0/convert["WATTS"]["HP"];
116 convert["N"]["LBS"] = 0.22482;
117 convert["LBS"]["N"] = 1.0/convert["N"]["LBS"];
119 convert["KTS"]["FT/SEC"] = 1.68781;
120 convert["FT/SEC"]["KTS"] = 1.0/convert["KTS"]["FT/SEC"];
121 convert["M/S"]["FT/S"] = 3.2808399;
122 convert["M/SEC"]["FT/SEC"] = 3.2808399;
123 convert["FT/S"]["M/S"] = 1.0/convert["M/S"]["FT/S"];
124 convert["M/SEC"]["FT/SEC"] = 3.2808399;
125 convert["FT/SEC"]["M/SEC"] = 1.0/convert["M/SEC"]["FT/SEC"];
126 convert["KM/SEC"]["FT/SEC"] = 3280.8399;
127 convert["FT/SEC"]["KM/SEC"] = 1.0/convert["KM/SEC"]["FT/SEC"];
129 convert["FT*LBS"]["N*M"] = 1.35581795;
130 convert["N*M"]["FT*LBS"] = 1/convert["FT*LBS"]["N*M"];
132 convert["M4*SEC/KG"]["FT4*SEC/SLUG"] = convert["M"]["FT"]*convert["M"]["FT"]*
133 convert["M"]["FT"]*convert["M"]["FT"]/convert["KG"]["SLUG"];
134 convert["FT4*SEC/SLUG"]["M4*SEC/KG"] =
135 1.0/convert["M4*SEC/KG"]["FT4*SEC/SLUG"];
137 convert["INHG"]["PSF"] = 70.7180803;
138 convert["PSF"]["INHG"] = 1.0/convert["INHG"]["PSF"];
139 convert["ATM"]["INHG"] = 29.9246899;
140 convert["INHG"]["ATM"] = 1.0/convert["ATM"]["INHG"];
141 convert["PSI"]["INHG"] = 2.03625437;
142 convert["INHG"]["PSI"] = 1.0/convert["PSI"]["INHG"];
143 convert["INHG"]["PA"] = 3386.0; // inches Mercury to pascals
144 convert["PA"]["INHG"] = 1.0/convert["INHG"]["PA"];
145 convert["LBS/FT2"]["N/M2"] = 14.5939/convert["FT"]["M"];
146 convert["N/M2"]["LBS/FT2"] = 1.0/convert["LBS/FT2"]["N/M2"];
147 convert["LBS/FT2"]["PA"] = convert["LBS/FT2"]["N/M2"];
148 convert["PA"]["LBS/FT2"] = 1.0/convert["LBS/FT2"]["PA"];
150 convert["KG/MIN"]["LBS/MIN"] = convert["KG"]["LBS"];
152 convert["LBS/HP*HR"]["KG/KW*HR"] = 0.6083;
153 convert["KG/KW*HR"]["LBS/HP*HR"] = 1.0/convert["LBS/HP*HR"]["KG/KW*HR"];
155 convert["KG/L"]["LBS/GAL"] = 8.3454045;
156 convert["LBS/GAL"]["KG/L"] = 1.0/convert["KG/L"]["LBS/GAL"];
159 convert["M"]["M"] = 1.00;
160 convert["KM"]["KM"] = 1.00;
161 convert["FT"]["FT"] = 1.00;
162 convert["IN"]["IN"] = 1.00;
164 convert["M2"]["M2"] = 1.00;
165 convert["FT2"]["FT2"] = 1.00;
167 convert["IN3"]["IN3"] = 1.00;
168 convert["CC"]["CC"] = 1.0;
169 convert["M3"]["M3"] = 1.0;
170 convert["FT3"]["FT3"] = 1.0;
171 convert["LTR"]["LTR"] = 1.0;
173 convert["KG"]["KG"] = 1.00;
174 convert["LBS"]["LBS"] = 1.00;
175 // Moments of Inertia
176 convert["KG*M2"]["KG*M2"] = 1.00;
177 convert["SLUG*FT2"]["SLUG*FT2"] = 1.00;
179 convert["DEG"]["DEG"] = 1.00;
180 convert["RAD"]["RAD"] = 1.00;
182 convert["DEG/SEC"]["DEG/SEC"] = 1.00;
183 convert["RAD/SEC"]["RAD/SEC"] = 1.00;
185 convert["LBS/FT"]["LBS/FT"] = 1.00;
186 convert["N/M"]["N/M"] = 1.00;
188 convert["LBS/FT/SEC"]["LBS/FT/SEC"] = 1.00;
189 convert["N/M/SEC"]["N/M/SEC"] = 1.00;
190 // Damping force (Square law)
191 convert["LBS/FT2/SEC2"]["LBS/FT2/SEC2"] = 1.00;
192 convert["N/M2/SEC2"]["N/M2/SEC2"] = 1.00;
194 convert["HP"]["HP"] = 1.00;
195 convert["WATTS"]["WATTS"] = 1.00;
197 convert["N"]["N"] = 1.00;
199 convert["FT/SEC"]["FT/SEC"] = 1.00;
200 convert["KTS"]["KTS"] = 1.00;
201 convert["M/S"]["M/S"] = 1.0;
202 convert["M/SEC"]["M/SEC"] = 1.0;
203 convert["KM/SEC"]["KM/SEC"] = 1.0;
205 convert["FT*LBS"]["FT*LBS"] = 1.00;
206 convert["N*M"]["N*M"] = 1.00;
208 convert["M4*SEC/KG"]["M4*SEC/KG"] = 1.0;
209 convert["FT4*SEC/SLUG"]["FT4*SEC/SLUG"] = 1.0;
211 convert["PSI"]["PSI"] = 1.00;
212 convert["PSF"]["PSF"] = 1.00;
213 convert["INHG"]["INHG"] = 1.00;
214 convert["ATM"]["ATM"] = 1.0;
215 convert["PA"]["PA"] = 1.0;
216 convert["N/M2"]["N/M2"] = 1.00;
217 convert["LBS/FT2"]["LBS/FT2"] = 1.00;
219 convert["LBS/SEC"]["LBS/SEC"] = 1.00;
220 convert["KG/MIN"]["KG/MIN"] = 1.0;
221 convert["LBS/MIN"]["LBS/MIN"] = 1.0;
223 convert["LBS/HP*HR"]["LBS/HP*HR"] = 1.0;
224 convert["KG/KW*HR"]["KG/KW*HR"] = 1.0;
226 convert["KG/L"]["KG/L"] = 1.0;
227 convert["LBS/GAL"]["LBS/GAL"] = 1.0;
231 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233 Element::~Element(void)
235 for (unsigned int i=0; i<children.size(); i++) delete children[i];
238 attribute_key.clear();
241 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 string Element::GetAttributeValue(const string& attr)
246 for (unsigned int i=0; i<attribute_key.size(); i++) {
247 if (attribute_key[i] == attr) select = i;
249 if (select < 0) return string("");
250 else return attributes[attr];
253 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255 double Element::GetAttributeValueAsNumber(const string& attr)
257 string attribute = GetAttributeValue(attr);
259 if (attribute.empty()) return HUGE_VAL;
260 else return (atof(attribute.c_str()));
263 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265 Element* Element::GetElement(unsigned int el)
267 if (children.size() > el) {
277 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 Element* Element::GetNextElement(void)
281 if (children.size() > element_index+1) {
283 return children[element_index];
290 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292 string Element::GetDataLine(unsigned int i)
294 if (data_lines.size() > 0) return data_lines[i];
295 else return string("");
298 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300 double Element::GetDataAsNumber(void)
302 if (data_lines.size() == 1) {
303 return atof(data_lines[0].c_str());
304 } else if (data_lines.size() == 0) {
307 cerr << "Attempting to get single data value from multiple lines in element " << name << endl;
312 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314 unsigned int Element::GetNumElements(const string& element_name)
316 unsigned int number_of_elements=0;
317 Element* el=FindElement(element_name);
319 number_of_elements++;
320 el=FindNextElement(element_name);
322 return number_of_elements;
325 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 Element* Element::FindElement(const string& el)
329 if (el.empty() && children.size() >= 1) {
333 for (unsigned int i=0; i<children.size(); i++) {
334 if (el == children[i]->GetName()) {
343 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
345 Element* Element::FindNextElement(const string& el)
348 if (element_index < children.size()) {
349 return children[element_index++];
355 for (unsigned int i=element_index; i<children.size(); i++) {
356 if (el == children[i]->GetName()) {
365 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367 double Element::FindElementValueAsNumber(const string& el)
369 Element* element = FindElement(el);
371 return element->GetDataAsNumber();
373 cerr << "Attempting to get single data value from multiple lines" << endl;
378 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
380 string Element::FindElementValue(const string& el)
382 Element* element = FindElement(el);
384 return element->GetDataLine();
390 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
392 double Element::FindElementValueAsNumberConvertTo(const string& el, const string& target_units)
394 Element* element = FindElement(el);
397 cerr << "Attempting to get non-existent element " << el << endl;
401 string supplied_units = element->GetAttributeValue("unit");
403 if (!supplied_units.empty()) {
404 if (convert.find(supplied_units) == convert.end()) {
405 cerr << endl << "Supplied unit: \"" << supplied_units << "\" does not exist (typo?). Add new unit"
406 << " conversion in FGXMLElement.cpp." << endl;
409 if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) {
410 cerr << endl << "Supplied unit: \"" << supplied_units << "\" cannot be converted to "
411 << target_units << ". Add new unit conversion in FGXMLElement.cpp or fix typo" << endl;
416 double value = element->GetDataAsNumber();
417 if (!supplied_units.empty()) {
418 value *= convert[supplied_units][target_units];
424 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426 double Element::FindElementValueAsNumberConvertFromTo( const string& el,
427 const string& supplied_units,
428 const string& target_units)
430 Element* element = FindElement(el);
433 cerr << "Attempting to get non-existent element " << el << endl;
437 if (!supplied_units.empty()) {
438 if (convert.find(supplied_units) == convert.end()) {
439 cerr << endl << "Supplied unit: \"" << supplied_units << "\" does not exist (typo?). Add new unit"
440 << " conversion in FGXMLElement.cpp." << endl;
443 if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) {
444 cerr << endl << "Supplied unit: \"" << supplied_units << "\" cannot be converted to "
445 << target_units << ". Add new unit conversion in FGXMLElement.cpp or fix typo" << endl;
450 double value = element->GetDataAsNumber();
451 if (!supplied_units.empty()) {
452 value *= convert[supplied_units][target_units];
458 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
460 FGColumnVector3 Element::FindElementTripletConvertTo( const string& target_units)
462 FGColumnVector3 triplet;
465 string supplied_units = GetAttributeValue("unit");
467 if (!supplied_units.empty()) {
468 if (convert.find(supplied_units) == convert.end()) {
469 cerr << endl << "Supplied unit: \"" << supplied_units << "\" does not exist (typo?). Add new unit"
470 << " conversion in FGXMLElement.cpp." << endl;
473 if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) {
474 cerr << endl << "Supplied unit: \"" << supplied_units << "\" cannot be converted to "
475 << target_units << ". Add new unit conversion in FGXMLElement.cpp or fix typo" << endl;
480 item = FindElement("x");
481 if (!item) item = FindElement("roll");
483 value = item->GetDataAsNumber();
484 if (!supplied_units.empty()) value *= convert[supplied_units][target_units];
490 item = FindElement("y");
491 if (!item) item = FindElement("pitch");
493 value = item->GetDataAsNumber();
494 if (!supplied_units.empty()) value *= convert[supplied_units][target_units];
500 item = FindElement("z");
501 if (!item) item = FindElement("yaw");
503 value = item->GetDataAsNumber();
504 if (!supplied_units.empty()) value *= convert[supplied_units][target_units];
513 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
515 void Element::Print(unsigned int level)
517 unsigned int i, spaces;
520 for (spaces=0; spaces<=level; spaces++) cout << " "; // format output
521 cout << "Element Name: " << name;
522 for (i=0; i<attributes.size(); i++) {
523 cout << " " << attribute_key[i] << " = " << attributes[attribute_key[i]];
526 for (i=0; i<data_lines.size(); i++) {
527 for (spaces=0; spaces<=level; spaces++) cout << " "; // format output
528 cout << data_lines[i] << endl;
530 for (i=0; i<children.size(); i++) {
531 children[i]->Print(level);
535 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
537 void Element::AddAttribute(const string& name, const string& value)
539 attribute_key.push_back(name);
540 attributes[name] = value;
543 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
545 void Element::AddData(string d)
547 string::size_type string_start = d.find_first_not_of(" \t");
548 if (string_start != string::npos && string_start > 0) {
549 d.erase(0,string_start);
551 data_lines.push_back(d);
554 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
556 } // end namespace JSBSim