| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | File: FGXMLElement.h |
| 4 | Author: Jon S. Berndt |
| 5 | Date started: 9/28/04 |
| 6 | |
| 7 | ------------- Copyright (C) 2004 Jon S. Berndt ([email protected]) ------------- |
| 8 | |
| 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 |
| 12 | version. |
| 13 | |
| 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 |
| 17 | details. |
| 18 | |
| 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. |
| 22 | |
| 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. |
| 25 | |
| 26 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 27 | SENTRY |
| 28 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 29 | |
| 30 | #ifndef XMLELEMENT_H |
| 31 | #define XMLELEMENT_H |
| 32 | |
| 33 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 34 | INCLUDES |
| 35 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 36 | |
| 37 | #include <string> |
| 38 | #include <map> |
| 39 | #include <vector> |
| 40 | |
| 41 | #include "simgear/structure/SGSharedPtr.hxx" |
| 42 | #include "math/FGColumnVector3.h" |
| 43 | |
| 44 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 45 | FORWARD DECLARATIONS |
| 46 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 47 | |
| 48 | namespace JSBSim { |
| 49 | |
| 50 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 51 | CLASS DOCUMENTATION |
| 52 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 53 | |
| 54 | /** Encapsulates an XML element. |
| 55 | This class handles the creation, storage, and manipulation of XML elements. |
| 56 | This class also can convert supplied values as follows: |
| 57 | |
| 58 | convert ["from"]["to"] = factor, so: from * factor = to |
| 59 | - convert["M"]["FT"] = 3.2808399; |
| 60 | - convert["FT"]["M"] = 1.0/convert["M"]["FT"]; |
| 61 | - convert["M2"]["FT2"] = convert["M"]["FT"]*convert["M"]["FT"]; |
| 62 | - convert["FT2"]["M2"] = 1.0/convert["M2"]["FT2"]; |
| 63 | - convert["FT"]["IN"] = 12.0; |
| 64 | - convert["IN"]["FT"] = 1.0/convert["FT"]["IN"]; |
| 65 | - convert["LBS"]["KG"] = 0.45359237; |
| 66 | - convert["KG"]["LBS"] = 1.0/convert["LBS"]["KG"]; |
| 67 | - convert["SLUG*FT2"]["KG*M2"] = 1.35594; |
| 68 | - convert["KG*M2"]["SLUG*FT2"] = 1.0/convert["SLUG*FT2"]["KG*M2"]; |
| 69 | - convert["RAD"]["DEG"] = 360.0/(2.0*3.1415926); |
| 70 | - convert["DEG"]["RAD"] = 1.0/convert["RAD"]["DEG"]; |
| 71 | - convert["LBS/FT"]["N/M"] = 14.5939; |
| 72 | - convert["LBS/FT/SEC"]["N/M/SEC"] = 14.5939; |
| 73 | - convert["N/M"]["LBS/FT"] = 1.0/convert["LBS/FT"]["N/M"]; |
| 74 | - convert["N/M/SEC"]["LBS/FT/SEC"] = 1.0/convert["LBS/FT/SEC"]["N/M/SEC"]; |
| 75 | - convert["WATTS"]["HP"] = 0.001341022; |
| 76 | - convert["HP"]["WATTS"] = 1.0/convert["WATTS"]["HP"]; |
| 77 | - convert["N"]["LBS"] = 0.22482; |
| 78 | - convert["LBS"]["N"] = 1.0/convert["N"]["LBS"]; |
| 79 | - convert["KTS"]["FT/SEC"] = ktstofps; |
| 80 | - convert["KG/MIN"]["LBS/MIN"] = convert["KG"]["LBS"]; |
| 81 | - convert["LBS/HP*HR"]["KG/KW*HR"] = 0.6083; |
| 82 | - convert["KG/KW*HR"]["LBS/HP*HR"] = 1/convert["LBS/HP*HR"]["KG/KW*HR"]; |
| 83 | - convert["KG/L"]["LBS/GAL"] = 8.3454045; |
| 84 | |
| 85 | - convert["M"]["M"] = 1.00; |
| 86 | - convert["FT"]["FT"] = 1.00; |
| 87 | - convert["IN"]["IN"] = 1.00; |
| 88 | - convert["DEG"]["DEG"] = 1.00; |
| 89 | - convert["RAD"]["RAD"] = 1.00; |
| 90 | - convert["M2"]["M2"] = 1.00; |
| 91 | - convert["FT2"]["FT2"] = 1.00; |
| 92 | - convert["KG*M2"]["KG*M2"] = 1.00; |
| 93 | - convert["SLUG*FT2"]["SLUG*FT2"] = 1.00; |
| 94 | - convert["KG"]["KG"] = 1.00; |
| 95 | - convert["LBS"]["LBS"] = 1.00; |
| 96 | - convert["LBS/FT"]["LBS/FT"] = 1.00; |
| 97 | - convert["N/M"]["N/M"] = 1.00; |
| 98 | - convert["LBS/FT/SEC"]["LBS/FT/SEC"] = 1.00; |
| 99 | - convert["N/M/SEC"]["N/M/SEC"] = 1.00; |
| 100 | - convert["PSI"]["PSI"] = 1.00; |
| 101 | - convert["INHG"]["INHG"] = 1.00; |
| 102 | - convert["HP"]["HP"] = 1.00; |
| 103 | - convert["N"]["N"] = 1.00; |
| 104 | - convert["WATTS"]["WATTS"] = 1.00; |
| 105 | - convert["KTS"]["KTS"] = 1.0; |
| 106 | - convert["FT/SEC"]["FT/SEC"] = 1.0; |
| 107 | - convert["KG/MIN"]["KG/MIN"] = 1.0; |
| 108 | - convert["LBS/MIN"]["LBS/MIN"] = 1.0; |
| 109 | - convert["LBS/HP*HR"]["LBS/HP*HR"] = 1.0; |
| 110 | - convert["KG/KW*HR"]["KG/KW*HR"] = 1.0; |
| 111 | - convert["KG/L"]["KG/L"] = 1.0; |
| 112 | - convert["LBS/GAL"]["LBS/GAL"] = 1.0; |
| 113 | |
| 114 | Where: |
| 115 | - N = newtons |
| 116 | - M = meters |
| 117 | - M2 = meters squared |
| 118 | - KG = kilograms |
| 119 | - LBS = pounds force |
| 120 | - FT = feet |
| 121 | - FT2 = feet squared |
| 122 | - SEC = seconds |
| 123 | - MIN = minutes |
| 124 | - SLUG = slug |
| 125 | - DEG = degrees |
| 126 | - RAD = radians |
| 127 | - WATTS = watts |
| 128 | - HP = horsepower |
| 129 | - HR = hour |
| 130 | - L = liter |
| 131 | - GAL = gallon (U.S. liquid) |
| 132 | |
| 133 | @author Jon S. Berndt |
| 134 | */ |
| 135 | |
| 136 | class Element; |
| 137 | typedef SGSharedPtr<Element> Element_ptr; |
| 138 | |
| 139 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 140 | CLASS DECLARATION |
| 141 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 142 | |
| 143 | class JSBSIM_API Element : public SGReferenced { |
| 144 | public: |
| 145 | /** Constructor |
| 146 | @param nm the name of this element (if given) |
| 147 | */ |
| 148 | Element(const std::string& nm); |
| 149 | /// Destructor |
| 150 | ~Element(void); |
| 151 | |
| 152 | /** Determines if an element has the supplied attribute. |
| 153 | @param key specifies the attribute key to retrieve the value of. |
| 154 | @return true or false. */ |
| 155 | bool HasAttribute(const std::string& key) {return attributes.find(key) != attributes.end();} |
| 156 | |
| 157 | /** Retrieves an attribute. |
| 158 | @param key specifies the attribute key to retrieve the value of. |
| 159 | @return the key value (as a string), or the empty string if no such |
| 160 | attribute exists. */ |
| 161 | std::string GetAttributeValue(const std::string& key); |
| 162 | |
| 163 | /** Modifies an attribute. |
| 164 | @param key specifies the attribute key to modify the value of. |
| 165 | @param value new key value (as a string). |
| 166 | @return false if it did not find any attribute with the requested key, |
| 167 | true otherwise. |
| 168 | */ |
| 169 | bool SetAttributeValue(const std::string& key, const std::string& value); |
| 170 | |
| 171 | /** Retrieves an attribute value as a double precision real number. |
| 172 | @param key specifies the attribute key to retrieve the value of. |
| 173 | @return the key value (as a number), or the HUGE_VAL if no such |
| 174 | attribute exists. */ |
| 175 | double GetAttributeValueAsNumber(const std::string& key); |
| 176 | |
| 177 | /** Retrieves the element name. |
| 178 | @return the element name, or the empty string if no name has been set.*/ |
| 179 | const std::string& GetName(void) const {return name;} |
| 180 | void ChangeName(const std::string& _name) { name = _name; } |
| 181 | |
| 182 | /** Gets a line of data belonging to an element. |
| 183 | @param i the index of the data line to return (0 by default). |
| 184 | @return a string representing the data line requested, or the empty string |
| 185 | if none exists.*/ |
| 186 | std::string GetDataLine(unsigned int i=0); |
| 187 | |
| 188 | /// Returns the number of lines of data stored |
| 189 | unsigned int GetNumDataLines(void) {return (unsigned int)data_lines.size();} |
| 190 | |
| 191 | /// Returns the number of child elements for this element. |
| 192 | unsigned int GetNumElements(void) {return (unsigned int)children.size();} |
| 193 | |
| 194 | /// Returns the number of named child elements for this element. |
| 195 | unsigned int GetNumElements(const std::string& element_name); |
| 196 | |
| 197 | /** Converts the element data to a number. |
| 198 | This function attempts to convert the first (and presumably only) line of |
| 199 | data "owned" by the element into a real number. If there is not exactly one |
| 200 | line of data owned by the element, then HUGE_VAL is returned. |
| 201 | @return the numeric value of the data owned by the element.*/ |
| 202 | double GetDataAsNumber(void); |
| 203 | |
| 204 | /** Returns a pointer to the element requested by index. |
| 205 | This function also resets an internal counter to the index, so that |
| 206 | subsequent calls to GetNextElement() will return the following |
| 207 | elements sequentially, until the last element is reached. At that point, |
| 208 | GetNextElement() will return NULL. |
| 209 | @param el the index of the requested element (0 by default) |
| 210 | @return a pointer to the Element, or 0 if no valid element exists. */ |
| 211 | Element* GetElement(unsigned int el=0); |
| 212 | |
| 213 | /** Returns a pointer to the next element in the list. |
| 214 | The function GetElement() must be called first to be sure that this |
| 215 | function will return the correct element. The call to GetElement() resets |
| 216 | the internal counter to zero. Subsequent calls to GetNextElement() return |
| 217 | a pointer to subsequent elements in the list. When the final element is |
| 218 | reached, 0 is returned. |
| 219 | @return a pointer to the next Element in the sequence, or 0 if no valid |
| 220 | Element is present. */ |
| 221 | Element* GetNextElement(void); |
| 222 | |
| 223 | /** Returns a pointer to the parent of an element. |
| 224 | @return a pointer to the parent Element, or 0 if this is the top level Element. */ |
| 225 | Element* GetParent(void) {return parent;} |
| 226 | |
| 227 | /** Returns the line number at which the element has been defined. |
| 228 | @return the line number |
| 229 | */ |
| 230 | int GetLineNumber(void) const { return line_number; } |
| 231 | |
| 232 | /** Returns the name of the file in which the element has been read. |
| 233 | @return the file name |
| 234 | */ |
| 235 | const std::string& GetFileName(void) const { return file_name; } |
| 236 | |
| 237 | /** Searches for a specified element. |
| 238 | Finds the first element that matches the supplied string, or simply the first |
| 239 | element if no search string is supplied. This function call resets the internal |
| 240 | element counter to the first element. |
| 241 | @param el the search string (empty string by default). |
| 242 | @return a pointer to the first element that matches the supplied search string. */ |
| 243 | Element* FindElement(const std::string& el=""); |
| 244 | |
| 245 | /** Searches for the next element as specified. |
| 246 | This function would be called after FindElement() is first called (in order to |
| 247 | reset the internal counter). If no argument is supplied (or the empty string) |
| 248 | a pointer to the very next element is returned. Otherwise, the next occurence |
| 249 | of the named element is returned. If the end of the list is reached, 0 is |
| 250 | returned. |
| 251 | @param el the name of the next element to find. |
| 252 | @return the pointer to the found element, or 0 if no appropriate element us |
| 253 | found.*/ |
| 254 | Element* FindNextElement(const std::string& el=""); |
| 255 | |
| 256 | /** Searches for the named element and returns the string data belonging to it. |
| 257 | This function allows the data belonging to a named element to be returned |
| 258 | as a string. If no element is found, the empty string is returned. If no |
| 259 | argument is supplied, the data string for the first element is returned. |
| 260 | @param el the name of the element being searched for (the empty string by |
| 261 | default) |
| 262 | @return the data value for the named element as a string, or the empty |
| 263 | string if the element cannot be found. */ |
| 264 | std::string FindElementValue(const std::string& el=""); |
| 265 | |
| 266 | /** Searches for the named element and returns the data belonging to it as a number. |
| 267 | This function allows the data belonging to a named element to be returned |
| 268 | as a double. If no element is found, HUGE_VAL is returned. If no |
| 269 | argument is supplied, the data for the first element is returned. |
| 270 | @param el the name of the element being searched for (the empty string by |
| 271 | default) |
| 272 | @return the data value for the named element as a double, or HUGE_VAL if the |
| 273 | data is missing. */ |
| 274 | double FindElementValueAsNumber(const std::string& el=""); |
| 275 | |
| 276 | /** Searches for the named element and returns the data belonging to it as a bool. |
| 277 | This function allows the data belonging to a named element to be returned |
| 278 | as a bool. If no element is found, false is returned. If no |
| 279 | argument is supplied, the data for the first element is returned. |
| 280 | @param el the name of the element being searched for (the empty string by |
| 281 | default) |
| 282 | @return the data value for the named element as a bool, or false if the |
| 283 | data is missing. Zero will be false, while any other number will be true. */ |
| 284 | bool FindElementValueAsBoolean(const std::string& el=""); |
| 285 | |
| 286 | /** Searches for the named element and converts and returns the data belonging to it. |
| 287 | This function allows the data belonging to a named element to be returned |
| 288 | as a double. If no element is found, HUGE_VAL is returned. If no |
| 289 | argument is supplied, the data for the first element is returned. Additionally, |
| 290 | this function converts the value from the units specified in the config file (via |
| 291 | the UNITS="" attribute in the element definition) to the native units used by |
| 292 | JSBSim itself, as specified by the target_units parameter. The currently |
| 293 | allowable unit conversions are seen in the source file FGXMLElement.cpp. |
| 294 | Also, see above in the main documentation for this class. |
| 295 | @param el the name of the element being searched for (the empty string by |
| 296 | default) |
| 297 | @param target_units the string representing the native units used by JSBSim |
| 298 | to which the value returned will be converted. |
| 299 | @return the unit-converted data value for the named element as a double, |
| 300 | or HUGE_VAL if the data is missing. */ |
| 301 | double FindElementValueAsNumberConvertTo(const std::string& el, const std::string& target_units); |
| 302 | |
| 303 | /** Searches for the named element and converts and returns the data belonging to it. |
| 304 | This function allows the data belonging to a named element to be returned |
| 305 | as a double. If no element is found, HUGE_VAL is returned. If no |
| 306 | argument is supplied, the data for the first element is returned. Additionally, |
| 307 | this function converts the value from the units specified in the supplied_units |
| 308 | parameter to the units specified in the target_units parameter. JSBSim itself, |
| 309 | as specified by the target_units parameter. The currently allowable unit |
| 310 | conversions are seen in the source file FGXMLElement.cpp. Also, see above |
| 311 | in the main documentation for this class. |
| 312 | @param el the name of the element being searched for (the empty string by |
| 313 | default) |
| 314 | @param supplied_units the string representing the units of the value as |
| 315 | supplied by the config file. |
| 316 | @param target_units the string representing the native units used by JSBSim |
| 317 | to which the value returned will be converted. |
| 318 | @return the unit-converted data value for the named element as a double, |
| 319 | or HUGE_VAL if the data is missing. */ |
| 320 | double FindElementValueAsNumberConvertFromTo( const std::string& el, |
| 321 | const std::string& supplied_units, |
| 322 | const std::string& target_units); |
| 323 | |
| 324 | /** Composes a 3-element column vector for the supplied location or orientation. |
| 325 | This function processes a LOCATION or ORIENTATION construct, returning a |
| 326 | filled-out 3-element column vector containing the X, Y, Z or ROLL, PITCH, |
| 327 | YAW elements found in the supplied element. If one of the mentioned components |
| 328 | is not found, that component is set to zero and a warning message is printed. |
| 329 | All three elements should be supplied. |
| 330 | @param target_units the string representing the native units used by JSBSim |
| 331 | to which the value returned will be converted. |
| 332 | @return a column vector object built from the LOCATION or ORIENT components. */ |
| 333 | FGColumnVector3 FindElementTripletConvertTo( const std::string& target_units); |
| 334 | |
| 335 | double DisperseValue(Element *e, double val, const std::string& supplied_units="", |
| 336 | const std::string& target_units=""); |
| 337 | |
| 338 | /** This function sets the value of the parent class attribute to the supplied |
| 339 | Element pointer. |
| 340 | @param p pointer to the parent Element. */ |
| 341 | void SetParent(Element* p) {parent = p;} |
| 342 | |
| 343 | /** Adds a child element to the list of children stored for this element. |
| 344 | * @param el Child element to add. */ |
| 345 | void AddChildElement(Element* el) {children.push_back(el);} |
| 346 | |
| 347 | /** Stores an attribute belonging to this element. |
| 348 | * @param name The string name of the attribute. |
| 349 | * @param value The string value of the attribute. */ |
| 350 | void AddAttribute(const std::string& name, const std::string& value); |
| 351 | |
| 352 | /** Stores data belonging to this element. |
| 353 | * @param d the data to store. */ |
| 354 | void AddData(std::string d); |
| 355 | |
| 356 | /** Prints the element. |
| 357 | * Prints this element and calls the Print routine for child elements. |
| 358 | * @param d The tab level. A level corresponds to a single space. */ |
| 359 | void Print(unsigned int level=0); |
| 360 | |
| 361 | /** Set the line number at which the element has been read. |
| 362 | * @param line line number. |
| 363 | */ |
| 364 | void SetLineNumber(int line) { line_number = line; } |
| 365 | |
| 366 | /** Set the name of the file in which the element has been read. |
| 367 | * @param name file name |
| 368 | */ |
| 369 | void SetFileName(const std::string& name) { file_name = name; } |
| 370 | |
| 371 | /** Return a string that contains a description of the location where the |
| 372 | * current XML element was read from. |
| 373 | * @return a string describing the file name and line number where the |
| 374 | * element was read. |
| 375 | */ |
| 376 | std::string ReadFrom(void) const; |
| 377 | |
| 378 | /** Merges the attributes of the current element with another element. The |
| 379 | * attributes from the current element override the element that is passed |
| 380 | * as a parameter. In other words if the two elements have an attribute with |
| 381 | * the same name, the attribute from the current element is kept and the |
| 382 | * corresponding attribute of the other element is ignored. |
| 383 | * @param el element with which the current element will merge its attributes. |
| 384 | */ |
| 385 | void MergeAttributes(Element* el); |
| 386 | |
| 387 | private: |
| 388 | std::string name; |
| 389 | std::map <std::string, std::string> attributes; |
| 390 | std::vector <std::string> data_lines; |
| 391 | std::vector <Element_ptr> children; |
| 392 | Element *parent; |
| 393 | unsigned int element_index; |
| 394 | std::string file_name; |
| 395 | int line_number; |
| 396 | typedef std::map <std::string, std::map <std::string, double> > tMapConvert; |
| 397 | static tMapConvert convert; |
| 398 | static bool converterIsInitialized; |
| 399 | }; |
| 400 | |
| 401 | } // namespace JSBSim |
| 402 | |
| 403 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 404 | |
| 405 | #endif |