1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 Date started: 08/20/2004
6 Purpose: Config file read-in class and XML parser
9 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
13 #include "FGXMLParse.h"
18 static const char *IdSrc = "$Id$";
19 static const char *IdHdr = ID_XMLPARSE;
21 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
23 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
25 #include "FGXMLParse.h"
29 FGXMLParse::FGXMLParse(void)
31 first_element_read = false;
32 current_element = document = 0L;
35 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 FGXMLParse::~FGXMLParse(void)
39 if (document) delete document;
42 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44 void FGXMLParse::startXML(void)
48 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 void FGXMLParse::reset(void)
52 if (document) delete document;
53 first_element_read = false;
54 current_element = document = 0L;
57 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59 void FGXMLParse::endXML(void)
61 // At this point, document should equal current_element ?
64 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 void FGXMLParse::startElement (const char * name, const XMLAttributes &atts)
69 Element *temp_element;
71 working_string.erase();
73 if (!first_element_read) {
74 document = new Element(Name);
75 current_element = document;
76 first_element_read = true;
78 temp_element = new Element(Name);
79 temp_element->SetParent(current_element);
80 current_element->AddChildElement(temp_element);
81 current_element = temp_element;
84 if (current_element == 0L) {
85 cerr << "No current element read (no top-level element in XML file?)" << endl;
89 for (int i=0; i<atts.size();i++) {
90 current_element->AddAttribute(atts.getName(i), atts.getValue(i));
94 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 void FGXMLParse::endElement (const char * name)
98 string local_work_string;
100 while (!working_string.empty()) {
101 // clear leading newlines and spaces
102 string::size_type pos = working_string.find_first_not_of( " \n");
104 working_string.erase(0, pos);
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);
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);
120 current_element = current_element->GetParent();
123 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125 void FGXMLParse::data (const char * s, int length)
127 working_string += string(s, length);
130 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132 void FGXMLParse::pi (const char * target, const char * data)
136 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 void FGXMLParse::warning (const char * message, int line, int column)
140 cerr << "Warning: " << message << " line: " << line << " column: " << column << endl;
143 } // end namespace JSBSim