JSBSim Flight Dynamics Model  1.0 (02 March 2017)
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 Software
13  Foundation; either version 2 of the License, or (at your option) any later
14  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 with
22  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23  Place - Suite 330, Boston, MA 02111-1307, USA.
24 
25  Further information about the GNU Lesser General Public License can also be found on
26  the world wide web at http://www.gnu.org.
27 
28 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
29 INCLUDES
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
31 
32 #include <string>
33 #include <iostream>
34 #include <cstdlib>
35 
36 #include "FGJSBBase.h"
37 #include "FGXMLParse.h"
38 #include "FGXMLElement.h"
39 #include "input_output/string_utilities.h"
40 
41 using namespace std;
42 
43 namespace JSBSim {
44 
45 IDENT(IdSrc,"$Id: FGXMLParse.cpp,v 1.16 2014/06/09 11:52:06 bcoconni Exp $");
46 IDENT(IdHdr,ID_XMLPARSE);
47 
48 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 CLASS IMPLEMENTATION
50 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51 
52 using namespace std;
53 
54 FGXMLParse::FGXMLParse(void)
55 {
56  first_element_read = false;
57  current_element = document = 0L;
58 }
59 
60 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 
62 void FGXMLParse::startXML(void)
63 {
64 }
65 
66 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 
68 void FGXMLParse::reset(void)
69 {
70  first_element_read = false;
71  current_element = document = 0L;
72 }
73 
74 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 
76 void FGXMLParse::endXML(void)
77 {
78  // At this point, document should equal current_element ?
79 }
80 
81 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 
83 void FGXMLParse::startElement (const char * name, const XMLAttributes &atts)
84 {
85  string Name(name);
86  Element *temp_element;
87 
88  working_string.erase();
89 
90  if (!first_element_read) {
91  document = new Element(Name);
92  current_element = document;
93  first_element_read = true;
94  } else {
95  temp_element = new Element(Name);
96  temp_element->SetParent(current_element);
97  current_element->AddChildElement(temp_element);
98  current_element = temp_element;
99  }
100 
101  if (current_element == 0L) {
102  cerr << "In file " << getPath() << ": line " << getLine() << endl
103  << "No current element read (running out of memory?)" << endl;
104  exit (-1);
105  }
106 
107  current_element->SetLineNumber(getLine());
108  current_element->SetFileName(getPath());
109 
110  for (int i=0; i<atts.size();i++) {
111  current_element->AddAttribute(atts.getName(i), atts.getValue(i));
112  }
113 }
114 
115 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 
117 void FGXMLParse::endElement (const char * name)
118 {
119  if (!working_string.empty()) {
120  vector <string> work_strings = split(working_string, '\n');
121  for (unsigned int i=0; i<work_strings.size(); i++) current_element->AddData(work_strings[i]);
122  }
123 
124  current_element = current_element->GetParent();
125 }
126 
127 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
128 
129 void FGXMLParse::data (const char * s, int length)
130 {
131  working_string += string(s, length);
132 }
133 
134 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 
136 void FGXMLParse::pi (const char * target, const char * data)
137 {
138 }
139 
140 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 
142 void FGXMLParse::warning (const char * message, int line, int column)
143 {
144  cerr << "Warning: " << message << " line: " << line << " column: " << column << endl;
145 }
146 
147 } // end namespace JSBSim
STL namespace.