JSBSim Flight Dynamics Model  1.2.1 (08 Aug 2024)
An Open Source Flight Dynamics and Control Software Library in C++
FGXMLParse.cpp
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 (jon@jsbsim.org) -------------
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 "FGXMLParse.h"
33 #include "input_output/string_utilities.h"
34 
35 using namespace std;
36 
37 namespace JSBSim {
38 
39 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 CLASS IMPLEMENTATION
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42 
43 void FGXMLParse::reset(void)
44 {
45  current_element = document = nullptr;
46  working_string.erase();
47 }
48 
49 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 
51 void FGXMLParse::dumpDataLines(void)
52 {
53  if (!working_string.empty()) {
54  for (auto s: split(working_string, '\n'))
55  current_element->AddData(s);
56  }
57  working_string.erase();
58 }
59 
60 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 
62 void FGXMLParse::startElement (const char * name, const XMLAttributes &atts)
63 {
64  if (!document) {
65  document = new Element(name);
66  current_element = document;
67  } else {
68  dumpDataLines();
69 
70  Element* temp_element = new Element(name);
71  if (temp_element) {
72  temp_element->SetParent(current_element);
73  current_element->AddChildElement(temp_element);
74  }
75  current_element = temp_element;
76  }
77 
78  if (!current_element) {
79  cerr << "In file " << getPath() << ": line " << getLine() << endl
80  << "No current element read (running out of memory?)" << endl;
81  throw("Fatal error");
82  }
83 
84  current_element->SetLineNumber(getLine());
85  current_element->SetFileName(getPath());
86 
87  for (int i=0; i<atts.size();i++) {
88  current_element->AddAttribute(atts.getName(i), atts.getValue(i));
89  }
90 }
91 
92 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 
94 void FGXMLParse::endElement (const char * name)
95 {
96  dumpDataLines();
97  current_element = current_element->GetParent();
98 }
99 
100 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 
102 void FGXMLParse::data (const char * s, int length)
103 {
104  working_string += string(s, length);
105 }
106 
107 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 
109 void FGXMLParse::warning (const char * message, int line, int column)
110 {
111  cerr << "Warning: " << message << " line: " << line << " column: " << column
112  << endl;
113 }
114 
115 } // end namespace JSBSim