]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/input_output/FGXMLParse.cpp
Sync. w. JSBSim
[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   string local_work_string;
99
100   while (!working_string.empty()) {
101     // clear leading newlines and spaces
102     string::size_type pos = working_string.find_first_not_of( " \n");
103     if (pos > 0)
104       working_string.erase(0, pos);
105
106     // remove spaces (only) from end of string
107     pos = working_string.find_last_not_of( " ");
108     if (pos != string::npos)
109       working_string.erase( ++pos);
110
111     if (!working_string.empty()) {
112       pos = working_string.find("\n");
113       if (pos != string::npos) local_work_string = working_string.substr(0,pos);
114       else local_work_string = working_string;
115       current_element->AddData(local_work_string);
116       working_string.erase(0, pos);
117     }
118   }
119
120   current_element = current_element->GetParent();
121 }
122
123 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124
125 void FGXMLParse::data (const char * s, int length)
126 {
127   working_string += string(s, length);
128 }
129
130 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131
132 void FGXMLParse::pi (const char * target, const char * data)
133 {
134 }
135
136 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137
138 void FGXMLParse::warning (const char * message, int line, int column)
139 {
140   cerr << "Warning: " << message << " line: " << line << " column: " << column << endl;
141 }
142
143 } // end namespace JSBSim