| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | Header: FGXMLParse.cpp |
| 4 | Author: Jon Berndt |
| 5 | Date started: 08/20/2004 |
| 6 | Purpose: Config file read-in class and XML parser |
| 7 | Called by: Various |
| 8 | |
| 9 | ------------- Copyright (C) 2001 Jon S. Berndt ([email protected]) ------------- |
| 10 | |
| 11 | This program is free software; you can redistribute it and/or modify it under |
| 12 | the terms of the GNU Lesser General Public License as published by the Free |
| 13 | Software Foundation; either version 2 of the License, or (at your option) any |
| 14 | later version. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 19 | details. |
| 20 | |
| 21 | You should have received a copy of the GNU Lesser General Public License along |
| 22 | with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 23 | Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 24 | |
| 25 | Further information about the GNU Lesser General Public License can also be |
| 26 | found on the world wide web at http://www.gnu.org. |
| 27 | |
| 28 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 29 | INCLUDES |
| 30 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 31 | |
| 32 | #include <iostream> |
| 33 | |
| 34 | #include "FGXMLParse.h" |
| 35 | #include "input_output/string_utilities.h" |
| 36 | |
| 37 | using namespace std; |
| 38 | |
| 39 | namespace JSBSim { |
| 40 | |
| 41 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 42 | CLASS IMPLEMENTATION |
| 43 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 44 | |
| 45 | void FGXMLParse::reset(void) |
| 46 | { |
| 47 | current_element = document = nullptr; |
| 48 | working_string.erase(); |
| 49 | } |
| 50 | |
| 51 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 52 | |
| 53 | void FGXMLParse::dumpDataLines(void) |
| 54 | { |
| 55 | if (!working_string.empty()) { |
| 56 | for (auto s: split(working_string, '\n')) |
| 57 | current_element->AddData(s); |
| 58 | } |
| 59 | working_string.erase(); |
| 60 | } |
| 61 | |
| 62 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 63 | |
| 64 | void FGXMLParse::startElement (const char * name, const XMLAttributes &atts) |
| 65 | { |
| 66 | if (!document) { |
| 67 | document = new Element(name); |
| 68 | current_element = document; |
| 69 | } else { |
| 70 | dumpDataLines(); |
| 71 | |
| 72 | Element* temp_element = new Element(name); |
| 73 | if (temp_element) { |
| 74 | temp_element->SetParent(current_element); |
| 75 | current_element->AddChildElement(temp_element); |
| 76 | } |
| 77 | current_element = temp_element; |
| 78 | } |
| 79 | |
| 80 | if (!current_element) { |
| 81 | cerr << "In file " << getPath() << ": line " << getLine() << endl |
| 82 | << "No current element read (running out of memory?)" << endl; |
| 83 | throw("Fatal error"); |
| 84 | } |
| 85 | |
| 86 | current_element->SetLineNumber(getLine()); |
| 87 | current_element->SetFileName(getPath()); |
| 88 | |
| 89 | for (int i=0; i<atts.size();i++) { |
| 90 | current_element->AddAttribute(atts.getName(i), atts.getValue(i)); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 95 | |
| 96 | void FGXMLParse::endElement (const char * name) |
| 97 | { |
| 98 | dumpDataLines(); |
| 99 | current_element = current_element->GetParent(); |
| 100 | } |
| 101 | |
| 102 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 103 | |
| 104 | void FGXMLParse::data (const char * s, int length) |
| 105 | { |
| 106 | working_string += string(s, length); |
| 107 | } |
| 108 | |
| 109 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 110 | |
| 111 | void FGXMLParse::warning (const char * message, int line, int column) |
| 112 | { |
| 113 | cerr << "Warning: " << message << " line: " << line << " column: " << column |
| 114 | << endl; |
| 115 | } |
| 116 | |
| 117 | } // end namespace JSBSim |