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$";
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["FT"]["IN"] = 12.0;
68 convert["IN"]["FT"] = 1.0/convert["FT"]["IN"];
69 convert["IN"]["M"] = convert["IN"]["FT"] * convert["FT"]["M"];
70 convert["M"]["IN"] = convert["M"]["FT"] * convert["FT"]["IN"];
72 convert["M2"]["FT2"] = convert["M"]["FT"]*convert["M"]["FT"];
73 convert["FT2"]["M2"] = 1.0/convert["M2"]["FT2"];
74 convert["M2"]["IN2"] = convert["M"]["IN"]*convert["M"]["IN"];
75 convert["IN2"]["M2"] = 1.0/convert["M2"]["IN2"];
76 convert["FT2"]["IN2"] = 144.0;
77 convert["IN2"]["FT2"] = 1.0/convert["FT2"]["IN2"];
79 convert["IN3"]["CC"] = 16.387064;
80 convert["CC"]["IN3"] = 1.0/convert["IN3"]["CC"];
81 convert["FT3"]["IN3"] = 1728.0;
82 convert["IN3"]["FT3"] = 1.0/convert["FT3"]["IN3"];
83 convert["M3"]["FT3"] = 35.3146667;
84 convert["FT3"]["M3"] = 1.0/convert["M3"]["FT3"];
85 convert["LTR"]["IN3"] = 61.0237441;
86 convert["IN3"]["LTR"] = 1.0/convert["LTR"]["IN3"];
88 convert["LBS"]["KG"] = 0.45359237;
89 convert["KG"]["LBS"] = 1.0/convert["LBS"]["KG"];
90 convert["SLUG"]["KG"] = 14.59390;
91 convert["KG"]["SLUG"] = 1.0/convert["SLUG"]["KG"];
93 convert["SLUG*FT2"]["KG*M2"] = 1.35594;
94 convert["KG*M2"]["SLUG*FT2"] = 1.0/convert["SLUG*FT2"]["KG*M2"];
96 convert["RAD"]["DEG"] = 360.0/(2.0*3.1415926);
97 convert["DEG"]["RAD"] = 1.0/convert["RAD"]["DEG"];
99 convert["LBS/FT"]["N/M"] = 14.5939;
100 convert["N/M"]["LBS/FT"] = 1.0/convert["LBS/FT"]["N/M"];
102 convert["LBS/FT/SEC"]["N/M/SEC"] = 14.5939;
103 convert["N/M/SEC"]["LBS/FT/SEC"] = 1.0/convert["LBS/FT/SEC"]["N/M/SEC"];
104 // Damping force (Square Law)
105 convert["LBS/FT2/SEC2"]["N/M2/SEC2"] = 47.880259;
106 convert["N/M2/SEC2"]["LBS/FT2/SEC2"] = 1.0/convert["LBS/FT2/SEC2"]["N/M2/SEC2"];
108 convert["WATTS"]["HP"] = 0.001341022;
109 convert["HP"]["WATTS"] = 1.0/convert["WATTS"]["HP"];
111 convert["N"]["LBS"] = 0.22482;
112 convert["LBS"]["N"] = 1.0/convert["N"]["LBS"];
114 convert["KTS"]["FT/SEC"] = 1.68781;
115 convert["FT/SEC"]["KTS"] = 1.0/convert["KTS"]["FT/SEC"];
116 convert["M/S"]["FT/S"] = 3.2808399;
117 convert["FT/S"]["M/S"] = 1.0/convert["M/S"]["FT/S"];
119 convert["FT*LBS"]["N*M"] = 1.35581795;
120 convert["N*M"]["FT*LBS"] = 1/convert["FT*LBS"]["N*M"];
122 convert["M4*SEC/KG"]["FT4*SEC/SLUG"] = convert["M"]["FT"]*convert["M"]["FT"]*
123 convert["M"]["FT"]*convert["M"]["FT"]/convert["KG"]["SLUG"];
124 convert["FT4*SEC/SLUG"]["M4*SEC/KG"] =
125 1.0/convert["M4*SEC/KG"]["FT4*SEC/SLUG"];
127 convert["INHG"]["PSF"] = 70.7180803;
128 convert["PSF"]["INHG"] = 1.0/convert["INHG"]["PSF"];
129 convert["ATM"]["INHG"] = 29.9246899;
130 convert["INHG"]["ATM"] = 1.0/convert["ATM"]["INHG"];
131 convert["PSI"]["INHG"] = 2.03625437;
132 convert["INHG"]["PSI"] = 1.0/convert["PSI"]["INHG"];
133 convert["INHG"]["PA"] = 3386.0; // inches Mercury to pascals
134 convert["PA"]["INHG"] = 1.0/convert["INHG"]["PA"];
135 convert["LBS/FT2"]["N/M2"] = 14.5939/convert["FT"]["M"];
136 convert["N/M2"]["LBS/FT2"] = 1.0/convert["LBS/FT2"]["N/M2"];
137 convert["LBS/FT2"]["PA"] = convert["LBS/FT2"]["N/M2"];
138 convert["PA"]["LBS/FT2"] = 1.0/convert["LBS/FT2"]["PA"];
140 convert["KG/MIN"]["LBS/MIN"] = convert["KG"]["LBS"];
142 convert["LBS/HP*HR"]["KG/KW*HR"] = 0.6083;
143 convert["KG/KW*HR"]["LBS/HP*HR"] = 1.0/convert["LBS/HP*HR"]["KG/KW*HR"];
145 convert["KG/L"]["LBS/GAL"] = 8.3454045;
146 convert["LBS/GAL"]["KG/L"] = 1.0/convert["KG/L"]["LBS/GAL"];
149 convert["M"]["M"] = 1.00;
150 convert["FT"]["FT"] = 1.00;
151 convert["IN"]["IN"] = 1.00;
153 convert["M2"]["M2"] = 1.00;
154 convert["FT2"]["FT2"] = 1.00;
156 convert["IN3"]["IN3"] = 1.00;
157 convert["CC"]["CC"] = 1.0;
158 convert["M3"]["M3"] = 1.0;
159 convert["FT3"]["FT3"] = 1.0;
160 convert["LTR"]["LTR"] = 1.0;
162 convert["KG"]["KG"] = 1.00;
163 convert["LBS"]["LBS"] = 1.00;
164 // Moments of Inertia
165 convert["KG*M2"]["KG*M2"] = 1.00;
166 convert["SLUG*FT2"]["SLUG*FT2"] = 1.00;
168 convert["DEG"]["DEG"] = 1.00;
169 convert["RAD"]["RAD"] = 1.00;
171 convert["LBS/FT"]["LBS/FT"] = 1.00;
172 convert["N/M"]["N/M"] = 1.00;
174 convert["LBS/FT/SEC"]["LBS/FT/SEC"] = 1.00;
175 convert["N/M/SEC"]["N/M/SEC"] = 1.00;
176 // Damping force (Square law)
177 convert["LBS/FT2/SEC2"]["LBS/FT2/SEC2"] = 1.00;
178 convert["N/M2/SEC2"]["N/M2/SEC2"] = 1.00;
180 convert["HP"]["HP"] = 1.00;
181 convert["WATTS"]["WATTS"] = 1.00;
183 convert["N"]["N"] = 1.00;
185 convert["FT/SEC"]["FT/SEC"] = 1.00;
186 convert["KTS"]["KTS"] = 1.00;
187 convert["M/S"]["M/S"] = 1.0;
189 convert["FT*LBS"]["FT*LBS"] = 1.00;
190 convert["N*M"]["N*M"] = 1.00;
192 convert["M4*SEC/KG"]["M4*SEC/KG"] = 1.0;
193 convert["FT4*SEC/SLUG"]["FT4*SEC/SLUG"] = 1.0;
195 convert["PSI"]["PSI"] = 1.00;
196 convert["PSF"]["PSF"] = 1.00;
197 convert["INHG"]["INHG"] = 1.00;
198 convert["ATM"]["ATM"] = 1.0;
199 convert["PA"]["PA"] = 1.0;
200 convert["N/M2"]["N/M2"] = 1.00;
201 convert["LBS/FT2"]["LBS/FT2"] = 1.00;
203 convert["LBS/SEC"]["LBS/SEC"] = 1.00;
204 convert["KG/MIN"]["KG/MIN"] = 1.0;
205 convert["LBS/MIN"]["LBS/MIN"] = 1.0;
207 convert["LBS/HP*HR"]["LBS/HP*HR"] = 1.0;
208 convert["KG/KW*HR"]["KG/KW*HR"] = 1.0;
210 convert["KG/L"]["KG/L"] = 1.0;
211 convert["LBS/GAL"]["LBS/GAL"] = 1.0;
215 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 Element::~Element(void)
219 for (unsigned int i=0; i<children.size(); i++) delete children[i];
222 attribute_key.clear();
225 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 string Element::GetAttributeValue(const string& attr)
230 for (unsigned int i=0; i<attribute_key.size(); i++) {
231 if (attribute_key[i] == attr) select = i;
233 if (select < 0) return string("");
234 else return attributes[attr];
237 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239 double Element::GetAttributeValueAsNumber(const string& attr)
241 string attribute = GetAttributeValue(attr);
243 if (attribute.empty()) return HUGE_VAL;
244 else return (atof(attribute.c_str()));
247 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 Element* Element::GetElement(unsigned int el)
251 if (children.size() > el) {
261 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263 Element* Element::GetNextElement(void)
265 if (children.size() > element_index+1) {
267 return children[element_index];
274 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 string Element::GetDataLine(unsigned int i)
278 if (data_lines.size() > 0) return data_lines[i];
279 else return string("");
282 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
284 double Element::GetDataAsNumber(void)
286 if (data_lines.size() == 1) {
287 return atof(data_lines[0].c_str());
288 } else if (data_lines.size() == 0) {
291 cerr << "Attempting to get single data value from multiple lines in element " << name << endl;
296 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 unsigned int Element::GetNumElements(const string& element_name)
300 unsigned int number_of_elements=0;
301 Element* el=FindElement(element_name);
303 number_of_elements++;
304 el=FindNextElement(element_name);
306 return number_of_elements;
309 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311 Element* Element::FindElement(const string& el)
313 if (el.empty() && children.size() >= 1) {
317 for (unsigned int i=0; i<children.size(); i++) {
318 if (el == children[i]->GetName()) {
327 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329 Element* Element::FindNextElement(const string& el)
332 if (element_index < children.size()) {
333 return children[element_index++];
339 for (unsigned int i=element_index; i<children.size(); i++) {
340 if (el == children[i]->GetName()) {
349 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
351 double Element::FindElementValueAsNumber(const string& el)
353 Element* element = FindElement(el);
355 return element->GetDataAsNumber();
357 cerr << "Attempting to get single data value from multiple lines" << endl;
362 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
364 string Element::FindElementValue(const string& el)
366 Element* element = FindElement(el);
368 return element->GetDataLine();
374 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
376 double Element::FindElementValueAsNumberConvertTo(const string& el, const string& target_units)
378 Element* element = FindElement(el);
381 cerr << "Attempting to get non-existent element " << el << endl;
385 string supplied_units = element->GetAttributeValue("unit");
387 if (!supplied_units.empty()) {
388 if (convert.find(supplied_units) == convert.end()) {
389 cerr << endl << "Supplied unit: \"" << supplied_units << "\" does not exist (typo?). Add new unit"
390 << " conversion in FGXMLElement.cpp." << endl;
393 if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) {
394 cerr << endl << "Supplied unit: \"" << supplied_units << "\" cannot be converted to "
395 << target_units << ". Add new unit conversion in FGXMLElement.cpp or fix typo" << endl;
400 double value = element->GetDataAsNumber();
401 if (!supplied_units.empty()) {
402 value *= convert[supplied_units][target_units];
408 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
410 double Element::FindElementValueAsNumberConvertFromTo( const string& el,
411 const string& supplied_units,
412 const string& target_units)
414 Element* element = FindElement(el);
417 cerr << "Attempting to get non-existent element " << el << endl;
421 if (!supplied_units.empty()) {
422 if (convert.find(supplied_units) == convert.end()) {
423 cerr << endl << "Supplied unit: \"" << supplied_units << "\" does not exist (typo?). Add new unit"
424 << " conversion in FGXMLElement.cpp." << endl;
427 if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) {
428 cerr << endl << "Supplied unit: \"" << supplied_units << "\" cannot be converted to "
429 << target_units << ". Add new unit conversion in FGXMLElement.cpp or fix typo" << endl;
434 double value = element->GetDataAsNumber();
435 if (!supplied_units.empty()) {
436 value *= convert[supplied_units][target_units];
442 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
444 FGColumnVector3 Element::FindElementTripletConvertTo( const string& target_units)
446 FGColumnVector3 triplet;
449 string supplied_units = GetAttributeValue("unit");
451 if (!supplied_units.empty()) {
452 if (convert.find(supplied_units) == convert.end()) {
453 cerr << endl << "Supplied unit: \"" << supplied_units << "\" does not exist (typo?). Add new unit"
454 << " conversion in FGXMLElement.cpp." << endl;
457 if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) {
458 cerr << endl << "Supplied unit: \"" << supplied_units << "\" cannot be converted to "
459 << target_units << ". Add new unit conversion in FGXMLElement.cpp or fix typo" << endl;
464 item = FindElement("x");
465 if (!item) item = FindElement("roll");
467 value = item->GetDataAsNumber();
468 if (!supplied_units.empty()) value *= convert[supplied_units][target_units];
471 cerr << "Could not find an X triplet item for this column vector." << endl;
475 item = FindElement("y");
476 if (!item) item = FindElement("pitch");
478 value = item->GetDataAsNumber();
479 if (!supplied_units.empty()) value *= convert[supplied_units][target_units];
482 cerr << "Could not find a Y triplet item for this column vector." << endl;
486 item = FindElement("z");
487 if (!item) item = FindElement("yaw");
489 value = item->GetDataAsNumber();
490 if (!supplied_units.empty()) value *= convert[supplied_units][target_units];
493 cerr << "Could not find a Z triplet item for this column vector." << endl;
500 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
502 void Element::Print(unsigned int level)
504 unsigned int i, spaces;
507 for (spaces=0; spaces<=level; spaces++) cout << " "; // format output
508 cout << "Element Name: " << name;
509 for (i=0; i<attributes.size(); i++) {
510 cout << " " << attribute_key[i] << " = " << attributes[attribute_key[i]];
513 for (i=0; i<data_lines.size(); i++) {
514 for (spaces=0; spaces<=level; spaces++) cout << " "; // format output
515 cout << data_lines[i] << endl;
517 for (i=0; i<children.size(); i++) {
518 children[i]->Print(level);
522 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
524 void Element::AddAttribute(const string& name, const string& value)
526 attribute_key.push_back(name);
527 attributes[name] = value;
530 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
532 void Element::AddData(string d)
534 string::size_type string_start = d.find_first_not_of(" \t");
535 if (string_start != string::npos && string_start > 0) {
536 d.erase(0,string_start);
538 data_lines.push_back(d);
541 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
543 } // end namespace JSBSim