FlightGear next
FGXMLParse.cpp
Go to the documentation of this file.
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%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
29INCLUDES
30%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
31
32#include "FGXMLParse.h"
34
35using namespace std;
36
37namespace JSBSim {
38
39/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40CLASS IMPLEMENTATION
41%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42
44{
45 current_element = document = nullptr;
46 working_string.erase();
47}
48
49//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50
51void 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
62void 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
94void FGXMLParse::endElement (const char * name)
95{
96 dumpDataLines();
97 current_element = current_element->GetParent();
98}
99
100//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101
102void FGXMLParse::data (const char * s, int length)
103{
104 working_string += string(s, length);
105}
106
107//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108
109void 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
#define i(x)
void SetParent(Element *p)
This function sets the value of the parent class attribute to the supplied Element pointer.
void AddData(std::string d)
Stores data belonging to this element.
void data(const char *s, int length) override
void endElement(const char *name) override
void startElement(const char *name, const XMLAttributes &atts) override
void warning(const char *message, int line, int column) override
const char * name
std::vector< std::string > split(std::string str, char d)