1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 Date started: 08/20/2004
6 Purpose: Config file read-in class and XML parser
9 ------------- Copyright (C) 2001 Jon S. Berndt (jsb@hal-pc.org) -------------
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
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
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.
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.
28 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
32 #include "FGXMLParse.h"
37 static const char *IdSrc = "$Id$";
38 static const char *IdHdr = ID_XMLPARSE;
40 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
44 #include "FGXMLParse.h"
48 FGXMLParse::FGXMLParse(void)
50 first_element_read = false;
51 current_element = document = 0L;
54 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 FGXMLParse::~FGXMLParse(void)
61 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 void FGXMLParse::startXML(void)
67 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 void FGXMLParse::reset(void)
72 first_element_read = false;
73 current_element = document = 0L;
76 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 void FGXMLParse::endXML(void)
80 // At this point, document should equal current_element ?
83 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 void FGXMLParse::startElement (const char * name, const XMLAttributes &atts)
88 Element *temp_element;
90 working_string.erase();
92 if (!first_element_read) {
93 document = new Element(Name);
94 current_element = document;
95 first_element_read = true;
97 temp_element = new Element(Name);
98 temp_element->SetParent(current_element);
99 current_element->AddChildElement(temp_element);
100 current_element = temp_element;
103 if (current_element == 0L) {
104 cerr << "No current element read (no top-level element in XML file?)" << endl;
108 for (int i=0; i<atts.size();i++) {
109 current_element->AddAttribute(atts.getName(i), atts.getValue(i));
113 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 void FGXMLParse::endElement (const char * name)
117 string local_work_string;
119 while (!working_string.empty()) {
120 // clear leading newlines and spaces
121 string::size_type pos = working_string.find_first_not_of( " \n");
123 working_string.erase(0, pos);
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);
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);
139 current_element = current_element->GetParent();
142 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 void FGXMLParse::data (const char * s, int length)
146 working_string += string(s, length);
149 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 void FGXMLParse::pi (const char * target, const char * data)
155 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 void FGXMLParse::warning (const char * message, int line, int column)
159 cerr << "Warning: " << message << " line: " << line << " column: " << column << endl;
162 } // end namespace JSBSim