]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/input_output/FGXMLParse.cpp
Upgrade to JSBSim 1.0-prerelease
[flightgear.git] / src / FDM / JSBSim / input_output / 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 (jsb@hal-pc.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 "FGXMLParse.h"
33 #include <cstdlib>
34
35 namespace JSBSim {
36
37 static const char *IdSrc = "$Id$";
38 static const char *IdHdr = ID_XMLPARSE;
39
40 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 CLASS IMPLEMENTATION
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
43
44 #include "FGXMLParse.h"
45
46 using namespace std;
47
48 FGXMLParse::FGXMLParse(void)
49 {
50   first_element_read = false;
51   current_element = document = 0L;
52 }
53
54 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55
56 FGXMLParse::~FGXMLParse(void)
57 {
58   delete document;
59 }
60
61 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62
63 void FGXMLParse::startXML(void)
64 {
65 }
66
67 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68
69 void FGXMLParse::reset(void)
70 {
71   delete document;
72   first_element_read = false;
73   current_element = document = 0L;
74 }
75
76 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77
78 void FGXMLParse::endXML(void)
79 {
80   // At this point, document should equal current_element ?
81 }
82
83 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84
85 void FGXMLParse::startElement (const char * name, const XMLAttributes &atts)
86 {
87   string Name(name);
88   Element *temp_element;
89
90   working_string.erase();
91
92   if (!first_element_read) {
93     document = new Element(Name);
94     current_element = document;
95     first_element_read = true;
96   } else {
97     temp_element = new Element(Name);
98     temp_element->SetParent(current_element);
99     current_element->AddChildElement(temp_element);
100     current_element = temp_element;
101   }
102
103   if (current_element == 0L) {
104     cerr << "No current element read (no top-level element in XML file?)" << endl;
105     exit (-1);
106   }
107
108   for (int i=0; i<atts.size();i++) {
109     current_element->AddAttribute(atts.getName(i), atts.getValue(i));
110   }
111 }
112
113 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114
115 void FGXMLParse::endElement (const char * name)
116 {
117   string local_work_string;
118
119   while (!working_string.empty()) {
120     // clear leading newlines and spaces
121     string::size_type pos = working_string.find_first_not_of( " \n");
122     if (pos > 0)
123       working_string.erase(0, pos);
124
125     // remove spaces (only) from end of string
126     pos = working_string.find_last_not_of( " ");
127     if (pos != string::npos)
128       working_string.erase( ++pos);
129
130     if (!working_string.empty()) {
131       pos = working_string.find("\n");
132       if (pos != string::npos) local_work_string = working_string.substr(0,pos);
133       else local_work_string = working_string;
134       current_element->AddData(local_work_string);
135       working_string.erase(0, pos);
136     }
137   }
138
139   current_element = current_element->GetParent();
140 }
141
142 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143
144 void FGXMLParse::data (const char * s, int length)
145 {
146   working_string += string(s, length);
147 }
148
149 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150
151 void FGXMLParse::pi (const char * target, const char * data)
152 {
153 }
154
155 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156
157 void FGXMLParse::warning (const char * message, int line, int column)
158 {
159   cerr << "Warning: " << message << " line: " << line << " column: " << column << endl;
160 }
161
162 } // end namespace JSBSim