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.30 2010/09/04 14:15:15 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["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["RAD/SEC"]["DEG/SEC"] = 360.0/(2.0*3.1415926);
100 convert["DEG/SEC"]["RAD/SEC"] = 1.0/convert["RAD/SEC"]["DEG/SEC"];
102 convert["LBS/FT"]["N/M"] = 14.5939;
103 convert["N/M"]["LBS/FT"] = 1.0/convert["LBS/FT"]["N/M"];
105 convert["LBS/FT/SEC"]["N/M/SEC"] = 14.5939;
106 convert["N/M/SEC"]["LBS/FT/SEC"] = 1.0/convert["LBS/FT/SEC"]["N/M/SEC"];
107 // Damping force (Square Law)
108 convert["LBS/FT2/SEC2"]["N/M2/SEC2"] = 47.880259;
109 convert["N/M2/SEC2"]["LBS/FT2/SEC2"] = 1.0/convert["LBS/FT2/SEC2"]["N/M2/SEC2"];
111 convert["WATTS"]["HP"] = 0.001341022;
112 convert["HP"]["WATTS"] = 1.0/convert["WATTS"]["HP"];
114 convert["N"]["LBS"] = 0.22482;
115 convert["LBS"]["N"] = 1.0/convert["N"]["LBS"];
117 convert["KTS"]["FT/SEC"] = 1.68781;
118 convert["FT/SEC"]["KTS"] = 1.0/convert["KTS"]["FT/SEC"];
119 convert["M/S"]["FT/S"] = 3.2808399;
120 convert["M/SEC"]["FT/SEC"] = 3.2808399;
121 convert["FT/S"]["M/S"] = 1.0/convert["M/S"]["FT/S"];
122 convert["M/SEC"]["FT/SEC"] = 3.2808399;
123 convert["FT/SEC"]["M/SEC"] = 1.0/convert["M/SEC"]["FT/SEC"];
125 convert["FT*LBS"]["N*M"] = 1.35581795;
126 convert["N*M"]["FT*LBS"] = 1/convert["FT*LBS"]["N*M"];
128 convert["M4*SEC/KG"]["FT4*SEC/SLUG"] = convert["M"]["FT"]*convert["M"]["FT"]*
129 convert["M"]["FT"]*convert["M"]["FT"]/convert["KG"]["SLUG"];
130 convert["FT4*SEC/SLUG"]["M4*SEC/KG"] =
131 1.0/convert["M4*SEC/KG"]["FT4*SEC/SLUG"];
133 convert["INHG"]["PSF"] = 70.7180803;
134 convert["PSF"]["INHG"] = 1.0/convert["INHG"]["PSF"];
135 convert["ATM"]["INHG"] = 29.9246899;
136 convert["INHG"]["ATM"] = 1.0/convert["ATM"]["INHG"];
137 convert["PSI"]["INHG"] = 2.03625437;
138 convert["INHG"]["PSI"] = 1.0/convert["PSI"]["INHG"];
139 convert["INHG"]["PA"] = 3386.0; // inches Mercury to pascals
140 convert["PA"]["INHG"] = 1.0/convert["INHG"]["PA"];
141 convert["LBS/FT2"]["N/M2"] = 14.5939/convert["FT"]["M"];
142 convert["N/M2"]["LBS/FT2"] = 1.0/convert["LBS/FT2"]["N/M2"];
143 convert["LBS/FT2"]["PA"] = convert["LBS/FT2"]["N/M2"];
144 convert["PA"]["LBS/FT2"] = 1.0/convert["LBS/FT2"]["PA"];
146 convert["KG/MIN"]["LBS/MIN"] = convert["KG"]["LBS"];
148 convert["LBS/HP*HR"]["KG/KW*HR"] = 0.6083;
149 convert["KG/KW*HR"]["LBS/HP*HR"] = 1.0/convert["LBS/HP*HR"]["KG/KW*HR"];
151 convert["KG/L"]["LBS/GAL"] = 8.3454045;
152 convert["LBS/GAL"]["KG/L"] = 1.0/convert["KG/L"]["LBS/GAL"];
155 convert["M"]["M"] = 1.00;
156 convert["FT"]["FT"] = 1.00;
157 convert["IN"]["IN"] = 1.00;
159 convert["M2"]["M2"] = 1.00;
160 convert["FT2"]["FT2"] = 1.00;
162 convert["IN3"]["IN3"] = 1.00;
163 convert["CC"]["CC"] = 1.0;
164 convert["M3"]["M3"] = 1.0;
165 convert["FT3"]["FT3"] = 1.0;
166 convert["LTR"]["LTR"] = 1.0;
168 convert["KG"]["KG"] = 1.00;
169 convert["LBS"]["LBS"] = 1.00;
170 // Moments of Inertia
171 convert["KG*M2"]["KG*M2"] = 1.00;
172 convert["SLUG*FT2"]["SLUG*FT2"] = 1.00;
174 convert["DEG"]["DEG"] = 1.00;
175 convert["RAD"]["RAD"] = 1.00;
177 convert["DEG/SEC"]["DEG/SEC"] = 1.00;
178 convert["RAD/SEC"]["RAD/SEC"] = 1.00;
180 convert["LBS/FT"]["LBS/FT"] = 1.00;
181 convert["N/M"]["N/M"] = 1.00;
183 convert["LBS/FT/SEC"]["LBS/FT/SEC"] = 1.00;
184 convert["N/M/SEC"]["N/M/SEC"] = 1.00;
185 // Damping force (Square law)
186 convert["LBS/FT2/SEC2"]["LBS/FT2/SEC2"] = 1.00;
187 convert["N/M2/SEC2"]["N/M2/SEC2"] = 1.00;
189 convert["HP"]["HP"] = 1.00;
190 convert["WATTS"]["WATTS"] = 1.00;
192 convert["N"]["N"] = 1.00;
194 convert["FT/SEC"]["FT/SEC"] = 1.00;
195 convert["KTS"]["KTS"] = 1.00;
196 convert["M/S"]["M/S"] = 1.0;
197 convert["M/SEC"]["M/SEC"] = 1.0;
199 convert["FT*LBS"]["FT*LBS"] = 1.00;
200 convert["N*M"]["N*M"] = 1.00;
202 convert["M4*SEC/KG"]["M4*SEC/KG"] = 1.0;
203 convert["FT4*SEC/SLUG"]["FT4*SEC/SLUG"] = 1.0;
205 convert["PSI"]["PSI"] = 1.00;
206 convert["PSF"]["PSF"] = 1.00;
207 convert["INHG"]["INHG"] = 1.00;
208 convert["ATM"]["ATM"] = 1.0;
209 convert["PA"]["PA"] = 1.0;
210 convert["N/M2"]["N/M2"] = 1.00;
211 convert["LBS/FT2"]["LBS/FT2"] = 1.00;
213 convert["LBS/SEC"]["LBS/SEC"] = 1.00;
214 convert["KG/MIN"]["KG/MIN"] = 1.0;
215 convert["LBS/MIN"]["LBS/MIN"] = 1.0;
217 convert["LBS/HP*HR"]["LBS/HP*HR"] = 1.0;
218 convert["KG/KW*HR"]["KG/KW*HR"] = 1.0;
220 convert["KG/L"]["KG/L"] = 1.0;
221 convert["LBS/GAL"]["LBS/GAL"] = 1.0;
225 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 Element::~Element(void)
229 for (unsigned int i=0; i<children.size(); i++) delete children[i];
232 attribute_key.clear();
235 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237 string Element::GetAttributeValue(const string& attr)
240 for (unsigned int i=0; i<attribute_key.size(); i++) {
241 if (attribute_key[i] == attr) select = i;
243 if (select < 0) return string("");
244 else return attributes[attr];
247 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 double Element::GetAttributeValueAsNumber(const string& attr)
251 string attribute = GetAttributeValue(attr);
253 if (attribute.empty()) return HUGE_VAL;
254 else return (atof(attribute.c_str()));
257 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 Element* Element::GetElement(unsigned int el)
261 if (children.size() > el) {
271 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 Element* Element::GetNextElement(void)
275 if (children.size() > element_index+1) {
277 return children[element_index];
284 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 string Element::GetDataLine(unsigned int i)
288 if (data_lines.size() > 0) return data_lines[i];
289 else return string("");
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 double Element::GetDataAsNumber(void)
296 if (data_lines.size() == 1) {
297 return atof(data_lines[0].c_str());
298 } else if (data_lines.size() == 0) {
301 cerr << "Attempting to get single data value from multiple lines in element " << name << endl;
306 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308 unsigned int Element::GetNumElements(const string& element_name)
310 unsigned int number_of_elements=0;
311 Element* el=FindElement(element_name);
313 number_of_elements++;
314 el=FindNextElement(element_name);
316 return number_of_elements;
319 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
321 Element* Element::FindElement(const string& el)
323 if (el.empty() && children.size() >= 1) {
327 for (unsigned int i=0; i<children.size(); i++) {
328 if (el == children[i]->GetName()) {
337 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
339 Element* Element::FindNextElement(const string& el)
342 if (element_index < children.size()) {
343 return children[element_index++];
349 for (unsigned int i=element_index; i<children.size(); i++) {
350 if (el == children[i]->GetName()) {
359 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361 double Element::FindElementValueAsNumber(const string& el)
363 Element* element = FindElement(el);
365 return element->GetDataAsNumber();
367 cerr << "Attempting to get single data value from multiple lines" << endl;
372 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374 string Element::FindElementValue(const string& el)
376 Element* element = FindElement(el);
378 return element->GetDataLine();
384 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
386 double Element::FindElementValueAsNumberConvertTo(const string& el, const string& target_units)
388 Element* element = FindElement(el);
391 cerr << "Attempting to get non-existent element " << el << endl;
395 string supplied_units = element->GetAttributeValue("unit");
397 if (!supplied_units.empty()) {
398 if (convert.find(supplied_units) == convert.end()) {
399 cerr << endl << "Supplied unit: \"" << supplied_units << "\" does not exist (typo?). Add new unit"
400 << " conversion in FGXMLElement.cpp." << endl;
403 if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) {
404 cerr << endl << "Supplied unit: \"" << supplied_units << "\" cannot be converted to "
405 << target_units << ". Add new unit conversion in FGXMLElement.cpp or fix typo" << endl;
410 double value = element->GetDataAsNumber();
411 if (!supplied_units.empty()) {
412 value *= convert[supplied_units][target_units];
418 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
420 double Element::FindElementValueAsNumberConvertFromTo( const string& el,
421 const string& supplied_units,
422 const string& target_units)
424 Element* element = FindElement(el);
427 cerr << "Attempting to get non-existent element " << el << endl;
431 if (!supplied_units.empty()) {
432 if (convert.find(supplied_units) == convert.end()) {
433 cerr << endl << "Supplied unit: \"" << supplied_units << "\" does not exist (typo?). Add new unit"
434 << " conversion in FGXMLElement.cpp." << endl;
437 if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) {
438 cerr << endl << "Supplied unit: \"" << supplied_units << "\" cannot be converted to "
439 << target_units << ". Add new unit conversion in FGXMLElement.cpp or fix typo" << endl;
444 double value = element->GetDataAsNumber();
445 if (!supplied_units.empty()) {
446 value *= convert[supplied_units][target_units];
452 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
454 FGColumnVector3 Element::FindElementTripletConvertTo( const string& target_units)
456 FGColumnVector3 triplet;
459 string supplied_units = GetAttributeValue("unit");
461 if (!supplied_units.empty()) {
462 if (convert.find(supplied_units) == convert.end()) {
463 cerr << endl << "Supplied unit: \"" << supplied_units << "\" does not exist (typo?). Add new unit"
464 << " conversion in FGXMLElement.cpp." << endl;
467 if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) {
468 cerr << endl << "Supplied unit: \"" << supplied_units << "\" cannot be converted to "
469 << target_units << ". Add new unit conversion in FGXMLElement.cpp or fix typo" << endl;
474 item = FindElement("x");
475 if (!item) item = FindElement("roll");
477 value = item->GetDataAsNumber();
478 if (!supplied_units.empty()) value *= convert[supplied_units][target_units];
481 cerr << "Could not find an X triplet item for this column vector." << endl;
485 item = FindElement("y");
486 if (!item) item = FindElement("pitch");
488 value = item->GetDataAsNumber();
489 if (!supplied_units.empty()) value *= convert[supplied_units][target_units];
492 cerr << "Could not find a Y triplet item for this column vector." << endl;
496 item = FindElement("z");
497 if (!item) item = FindElement("yaw");
499 value = item->GetDataAsNumber();
500 if (!supplied_units.empty()) value *= convert[supplied_units][target_units];
503 cerr << "Could not find a Z triplet item for this column vector." << endl;
510 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
512 void Element::Print(unsigned int level)
514 unsigned int i, spaces;
517 for (spaces=0; spaces<=level; spaces++) cout << " "; // format output
518 cout << "Element Name: " << name;
519 for (i=0; i<attributes.size(); i++) {
520 cout << " " << attribute_key[i] << " = " << attributes[attribute_key[i]];
523 for (i=0; i<data_lines.size(); i++) {
524 for (spaces=0; spaces<=level; spaces++) cout << " "; // format output
525 cout << data_lines[i] << endl;
527 for (i=0; i<children.size(); i++) {
528 children[i]->Print(level);
532 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
534 void Element::AddAttribute(const string& name, const string& value)
536 attribute_key.push_back(name);
537 attributes[name] = value;
540 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
542 void Element::AddData(string d)
544 string::size_type string_start = d.find_first_not_of(" \t");
545 if (string_start != string::npos && string_start > 0) {
546 d.erase(0,string_start);
548 data_lines.push_back(d);
551 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553 } // end namespace JSBSim