]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/input_output/FGXMLParse.cpp
sync. with JSBSim v. 2.0
[flightgear.git] / src / FDM / JSBSim / input_output / FGXMLParse.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Header:       FGXMLParse.h
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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10 INCLUDES
11 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
12
13 #include "FGXMLParse.h"
14 #include <stdlib.h>
15
16 namespace JSBSim {
17
18 static const char *IdSrc = "$Id$";
19 static const char *IdHdr = ID_XMLPARSE;
20
21 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
22 CLASS IMPLEMENTATION
23 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
24
25 #include "FGXMLParse.h"
26
27 using namespace std;
28
29 FGXMLParse::FGXMLParse(void)
30 {
31   first_element_read = false;
32   current_element = document = 0L;
33 }
34
35 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36
37 FGXMLParse::~FGXMLParse(void)
38 {
39   if (document) delete document;
40 }
41
42 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43
44 void FGXMLParse::startXML(void)
45 {
46 }
47
48 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49
50 void FGXMLParse::reset(void)
51 {
52   if (document) delete document;
53   first_element_read = false;
54   current_element = document = 0L;
55 }
56
57 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58
59 void FGXMLParse::endXML(void)
60 {
61   // At this point, document should equal current_element ?
62 }
63
64 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65
66 void FGXMLParse::startElement (const char * name, const XMLAttributes &atts)
67 {
68   string Name(name);
69   Element *temp_element;
70
71   working_string.erase();
72
73   if (!first_element_read) {
74     document = new Element(Name);
75     current_element = document;
76     first_element_read = true;
77   } else {
78     temp_element = new Element(Name);
79     temp_element->SetParent(current_element);
80     current_element->AddChildElement(temp_element);
81     current_element = temp_element;
82   }
83
84   if (current_element == 0L) {
85     cerr << "No current element read (no top-level element in XML file?)" << endl;
86     exit (-1);
87   }
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   int size, pos;
99   string local_work_string;
100
101   while (!working_string.empty()) {
102      // clear leading newlines and spaces
103     while (working_string[0] == '\n' || working_string[0] == ' ')
104       working_string.erase(0,1);
105
106     // remove spaces (only) from end of string
107     size = working_string.size();
108     while (working_string[size-1] == ' ')
109     {
110       working_string.erase(size-1,1);
111       size = working_string.size();
112     }
113
114     if (!working_string.empty()) {
115       pos = working_string.find("\n");
116       if (pos != string::npos) local_work_string = working_string.substr(0,pos);
117       else local_work_string = working_string;
118       current_element->AddData(local_work_string);
119       working_string.erase(0, pos);
120     }
121   }
122
123   current_element = current_element->GetParent();
124 }
125
126 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127
128 void FGXMLParse::data (const char * s, int length)
129 {
130   working_string += string(s, length);
131 }
132
133 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134
135 void FGXMLParse::pi (const char * target, const char * data)
136 {
137 }
138
139 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140
141 void FGXMLParse::warning (const char * message, int line, int column)
142 {
143   cerr << "Warning: " << message << " line: " << line << " column: " << column << endl;
144 }
145
146 } // end namespace JSBSim