1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 ------------- Copyright (C) 2004 Jon S. Berndt (jsb@hal-pc.org) -------------
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU Lesser General Public License as published by the Free Software
11 Foundation; either version 2 of the License, or (at your option) any later
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19 You should have received a copy of the GNU Lesser General Public License along with
20 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21 Place - Suite 330, Boston, MA 02111-1307, USA.
23 Further information about the GNU Lesser General Public License can also be found on
24 the world wide web at http://www.gnu.org.
26 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
28 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
33 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
48 #include <math/FGColumnVector3.h>
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54 #define ID_XMLELEMENT "$Id$"
56 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
62 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
66 /** Encapsulates an XML element.
67 This class handles the creation, storage, and manipulation of XML elements.
68 This class also can convert supplied values as follows:
70 convert ["from"]["to"] = factor, so: from * factor = to
71 - convert["M"]["FT"] = 3.2808399;
72 - convert["FT"]["M"] = 1.0/convert["M"]["FT"];
73 - convert["M2"]["FT2"] = convert["M"]["FT"]*convert["M"]["FT"];
74 - convert["FT2"]["M2"] = 1.0/convert["M2"]["FT2"];
75 - convert["FT"]["IN"] = 12.0;
76 - convert["IN"]["FT"] = 1.0/convert["FT"]["IN"];
77 - convert["LBS"]["KG"] = 0.45359237;
78 - convert["KG"]["LBS"] = 1.0/convert["LBS"]["KG"];
79 - convert["SLUG*FT2"]["KG*M2"] = 1.35594;
80 - convert["KG*M2"]["SLUG*FT2"] = 1.0/convert["SLUG*FT2"]["KG*M2"];
81 - convert["RAD"]["DEG"] = 360.0/(2.0*3.1415926);
82 - convert["DEG"]["RAD"] = 1.0/convert["RAD"]["DEG"];
83 - convert["LBS/FT"]["N/M"] = 14.5939;
84 - convert["LBS/FT/SEC"]["N/M/SEC"] = 14.5939;
85 - convert["N/M"]["LBS/FT"] = 1.0/convert["LBS/FT"]["N/M"];
86 - convert["N/M/SEC"]["LBS/FT/SEC"] = 1.0/convert["LBS/FT/SEC"]["N/M/SEC"];
87 - convert["WATTS"]["HP"] = 0.001341022;
88 - convert["HP"]["WATTS"] = 1.0/convert["WATTS"]["HP"];
89 - convert["N"]["LBS"] = 0.22482;
90 - convert["LBS"]["N"] = 1.0/convert["N"]["LBS"];
91 - convert["KTS"]["FT/SEC"] = ktstofps;
92 - convert["KG/MIN"]["LBS/MIN"] = convert["KG"]["LBS"];
94 - convert["M"]["M"] = 1.00;
95 - convert["FT"]["FT"] = 1.00;
96 - convert["IN"]["IN"] = 1.00;
97 - convert["DEG"]["DEG"] = 1.00;
98 - convert["RAD"]["RAD"] = 1.00;
99 - convert["M2"]["M2"] = 1.00;
100 - convert["FT2"]["FT2"] = 1.00;
101 - convert["KG*M2"]["KG*M2"] = 1.00;
102 - convert["SLUG*FT2"]["SLUG*FT2"] = 1.00;
103 - convert["KG"]["KG"] = 1.00;
104 - convert["LBS"]["LBS"] = 1.00;
105 - convert["LBS/FT"]["LBS/FT"] = 1.00;
106 - convert["N/M"]["N/M"] = 1.00;
107 - convert["LBS/FT/SEC"]["LBS/FT/SEC"] = 1.00;
108 - convert["N/M/SEC"]["N/M/SEC"] = 1.00;
109 - convert["PSI"]["PSI"] = 1.00;
110 - convert["INHG"]["INHG"] = 1.00;
111 - convert["HP"]["HP"] = 1.00;
112 - convert["N"]["N"] = 1.00;
113 - convert["WATTS"]["WATTS"] = 1.00;
114 - convert["KTS"]["KTS"] = 1.0;
115 - convert["FT/SEC"]["FT/SEC"] = 1.0;
116 - convert["KG/MIN"]["KG/MIN"] = 1.0;
117 - convert["LBS/MIN"]["LBS/MIN"] = 1.0;
122 - M2 = meters squared
134 @author Jon S. Berndt
138 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
145 @param nm the name of this element (if given)
151 /** Retrieves an attribute.
152 @param key specifies the attribute key to retrieve the value of.
153 @return the key value (as a string), or the empty string if no such
155 string GetAttributeValue(string key);
157 /** Retrieves an attribute value as a double precision real number.
158 @param key specifies the attribute key to retrieve the value of.
159 @return the key value (as a number), or the HUGE_VAL if no such
161 double GetAttributeValueAsNumber(string key);
163 /** Retrieves the element name.
164 @return the element name, or the empty string if no name has been set.*/
165 string GetName(void) {return name;}
167 /** Gets a line of data belonging to an element.
168 @param i the index of the data line to return (0 by default).
169 @return a string representing the data line requested, or the empty string
171 string GetDataLine(unsigned int i=0);
173 /// Returns the number of lines of data stored
174 unsigned int GetNumDataLines(void) {return (unsigned int)data_lines.size();}
176 /// Returns the number of child elements for this element.
177 unsigned int GetNumElements(void) {return (unsigned int)children.size();}
179 /// Returns the number of named child elements for this element.
180 unsigned int GetNumElements(string);
182 /** Converts the element data to a number.
183 This function attempts to convert the first (and presumably only) line of
184 data "owned" by the element into a real number. If there is not exactly one
185 line of data owned by the element, then HUGE_VAL is returned.
186 @return the numeric value of the data owned by the element.*/
187 double GetDataAsNumber(void);
189 /** Returns a pointer to the element requested by index.
190 This function also resets an internal counter to the index, so that
191 subsequent calls to GetNextElement() will return the following
192 elements sequentially, until the last element is reached. At that point,
193 GetNextElement() will return NULL.
194 @param el the index of the requested element (0 by default)
195 @return a pointer to the Element, or 0 if no valid element exists. */
196 Element* GetElement(unsigned int el=0);
198 /** Returns a pointer to the next element in the list.
199 The function GetElement() must be called first to be sure that this
200 function will return the correct element. The call to GetElement() resets
201 the internal counter to zero. Subsequent calls to GetNextElement() return
202 a pointer to subsequent elements in the list. When the final element is
203 reached, 0 is returned.
204 @return a pointer to the next Element in the sequence, or 0 if no valid
205 Element is present. */
206 Element* GetNextElement(void);
208 /** Returns a pointer to the parent of an element.
209 @return a pointer to the parent Element, or 0 if this is the top level Element. */
210 Element* GetParent(void) {return parent;}
212 /** Searches for a specified element.
213 Finds the first element that matches the supplied string, or simply the first
214 element if no search string is supplied. This function call resets the internal
215 element counter to the first element.
216 @param el the search string (empty string by default).
217 @return a pointer to the first element that matches the supplied search string. */
218 Element* FindElement(string el="");
220 /** Searches for the next element as specified.
221 This function would be called after FindElement() is first called (in order to
222 reset the internal counter). If no argument is supplied (or the empty string)
223 a pointer to the very next element is returned. Otherwise, the next occurence
224 of the named element is returned. If the end of the list is reached, 0 is
226 @param el the name of the next element to find.
227 @return the pointer to the found element, or 0 if no appropriate element us
229 Element* FindNextElement(string el="");
231 /** Searches for the named element and returns the string data belonging to it.
232 This function allows the data belonging to a named element to be returned
233 as a string. If no element is found, the empty string is returned. If no
234 argument is supplied, the data string for the first element is returned.
235 @param el the name of the element being searched for (the empty string by
237 @return the data value for the named element as a string, or the empty
238 string if the element cannot be found. */
239 string FindElementValue(string el="");
241 /** Searches for the named element and returns the data belonging to it as a number.
242 This function allows the data belonging to a named element to be returned
243 as a double. If no element is found, HUGE_VAL is returned. If no
244 argument is supplied, the data for the first element is returned.
245 @param el the name of the element being searched for (the empty string by
247 @return the data value for the named element as a double, or HUGE_VAL if the
249 double FindElementValueAsNumber(string el="");
251 /** Searches for the named element and converts and returns the data belonging to it.
252 This function allows the data belonging to a named element to be returned
253 as a double. If no element is found, HUGE_VAL is returned. If no
254 argument is supplied, the data for the first element is returned. Additionally,
255 this function converts the value from the units specified in the config file (via
256 the UNITS="" attribute in the element definition) to the native units used by
257 JSBSim itself, as specified by the target_units parameter. The currently
258 allowable unit conversions are seen in the source file FGXMLElement.cpp.
259 Also, see above in the main documentation for this class.
260 @param el the name of the element being searched for (the empty string by
262 @param target_units the string representing the native units used by JSBSim
263 to which the value returned will be converted.
264 @return the unit-converted data value for the named element as a double,
265 or HUGE_VAL if the data is missing. */
266 double FindElementValueAsNumberConvertTo(string el, string target_units);
268 /** Searches for the named element and converts and returns the data belonging to it.
269 This function allows the data belonging to a named element to be returned
270 as a double. If no element is found, HUGE_VAL is returned. If no
271 argument is supplied, the data for the first element is returned. Additionally,
272 this function converts the value from the units specified in the supplied_units
273 parameter to the units specified in the target_units parameter. JSBSim itself,
274 as specified by the target_units parameter. The currently allowable unit
275 conversions are seen in the source file FGXMLElement.cpp. Also, see above
276 in the main documentation for this class.
277 @param el the name of the element being searched for (the empty string by
279 @param supplied_units the string representing the units of the value as
280 supplied by the config file.
281 @param target_units the string representing the native units used by JSBSim
282 to which the value returned will be converted.
283 @return the unit-converted data value for the named element as a double,
284 or HUGE_VAL if the data is missing. */
285 double FindElementValueAsNumberConvertFromTo( string el,
286 string supplied_units,
287 string target_units);
289 /** Composes a 3-element column vector for the supplied location or orientation.
290 This function processes a LOCATION or ORIENTATION construct, returning a
291 filled-out 3-element column vector containing the X, Y, Z or ROLL, PITCH,
292 YAW elements found in the supplied element. If one of the mentioned components
293 is not found, that component is set to zero and a warning message is printed.
294 All three elements should be supplied.
295 @param target_units the string representing the native units used by JSBSim
296 to which the value returned will be converted.
297 @return a column vector object built from the LOCATION or ORIENT components. */
298 FGColumnVector3 FindElementTripletConvertTo( string target_units);
300 /** This function sets the value of the parent class attribute to the supplied
302 @param p pointer to the parent Element. */
303 void SetParent(Element* p) {parent = p;}
305 /** Adds a child element to the list of children stored for this element.
306 * @param el Child element to add. */
307 void AddChildElement(Element* el) {children.push_back(el);}
309 /** Stores an attribute belonging to this element.
310 * @param name The string name of the attribute.
311 * @param value The string value of the attribute. */
312 void AddAttribute(string name, string value);
314 /** Stores data belonging to this element.
315 * @param d the data to store. */
316 void AddData(string d);
318 /** Prints the element.
319 * Prints this element and calls the Print routine for child elements.
320 * @param d The tab level. A level corresponds to a single space. */
321 void Print(unsigned int level=0);
325 map <string, string> attributes;
326 vector <string> data_lines;
327 vector <Element*> children;
328 vector <string> attribute_key;
330 unsigned int element_index;
331 typedef map <string, map <string, double> > tMapConvert;
335 } // namespace JSBSim
337 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%