FlightGear next
FGPropertyReader.cpp
Go to the documentation of this file.
1/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Module: FGPropertyReader.cpp
4 Author: Bertrand Coconnier
5 Date started: 12/30/13
6 Purpose: Read and manage properties from XML data
7
8 ------------- Copyright (C) 2013 Bertrand Coconnier -------------
9
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU Lesser General Public License as published by the Free Software
12 Foundation; either version 2 of the License, or (at your option) any later
13 version.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 details.
19
20 You should have received a copy of the GNU Lesser General Public License along with
21 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22 Place - Suite 330, Boston, MA 02111-1307, USA.
23
24 Further information about the GNU Lesser General Public License can also be found on
25 the world wide web at http://www.gnu.org.
26
27FUNCTIONAL DESCRIPTION
28--------------------------------------------------------------------------------
29This class reads and manages properties defined in XML data
30
31HISTORY
32--------------------------------------------------------------------------------
3312/30/13 BC Created
34
35%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36INCLUDES
37%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38
39#include "FGPropertyReader.h"
40#include "FGPropertyManager.h"
41#include "FGXMLElement.h"
42#include "FGJSBBase.h"
43
44using namespace std;
45
46namespace JSBSim {
47
48/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49CLASS IMPLEMENTATION
50%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51
52//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53
55{
56 for (auto v: interface_prop_initial_value) {
57 SGPropertyNode* node = v.first;
58 if (!node->getAttribute(SGPropertyNode::PRESERVE))
59 node->setDoubleValue(v.second);
60 }
61
62 return true;
63}
64
65//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66
68{
69 Element *property_element = el->FindElement("property");
70 if (property_element && FGJSBBase::debug_lvl > 0) {
71 cout << endl << " ";
72 if (override)
73 cout << "Overriding";
74 else
75 cout << "Declared";
76 cout << " properties" << endl << endl;
77 }
78
79 while (property_element) {
80 SGPropertyNode* node = nullptr;
81 double value=0.0;
82 if ( ! property_element->GetAttributeValue("value").empty())
83 value = property_element->GetAttributeValueAsNumber("value");
84
85 string interface_property_string = property_element->GetDataLine();
86 if (PM->HasNode(interface_property_string)) {
87 if (override) {
88 node = PM->GetNode(interface_property_string);
89
90 if (FGJSBBase::debug_lvl > 0) {
91 if (interface_prop_initial_value.find(node) == interface_prop_initial_value.end()) {
92 cout << property_element->ReadFrom()
93 << " The following property will be overridden but it has not been" << endl
94 << " defined in the current model '" << el->GetName() << "'" << endl;
95 }
96
97 cout << " " << "Overriding value for property " << interface_property_string << endl
98 << " (old value: " << node->getDoubleValue() << " new value: " << value << ")"
99 << endl << endl;
100 }
101
102 node->setDoubleValue(value);
103 }
104 else {
105 cerr << property_element->ReadFrom()
106 << " Property " << interface_property_string
107 << " is already defined." << endl;
108 property_element = el->FindNextElement("property");
109 continue;
110 }
111 } else {
112 node = PM->GetNode(interface_property_string, true);
113 if (node) {
114 node->setDoubleValue(value);
115
116 if (FGJSBBase::debug_lvl > 0)
117 cout << " " << interface_property_string << " (initial value: "
118 << value << ")" << endl << endl;
119 }
120 else {
121 cerr << "Could not create property " << interface_property_string
122 << endl;
123 property_element = el->FindNextElement("property");
124 continue;
125 }
126 }
127 interface_prop_initial_value[node] = value;
128 if (property_element->GetAttributeValue("persistent") == string("true"))
129 node->setAttribute(SGPropertyNode::PRESERVE, true);
130
131 property_element = el->FindNextElement("property");
132 }
133
134 // End of interface property loading logic
135}
136}
const std::string & GetName(void) const
Retrieves the element name.
std::string ReadFrom(void) const
Return a string that contains a description of the location where the current XML element was read fr...
double GetAttributeValueAsNumber(const std::string &key)
Retrieves an attribute value as a double precision real number.
std::string GetDataLine(unsigned int i=0)
Gets a line of data belonging to an element.
std::string GetAttributeValue(const std::string &key)
Retrieves an attribute.
Element * FindElement(const std::string &el="")
Searches for a specified element.
Element * FindNextElement(const std::string &el="")
Searches for the next element as specified.
static short debug_lvl
Definition FGJSBBase.h:190
FGPropertyNode * GetNode(void) const
bool HasNode(const std::string &path) const
void Load(Element *el, FGPropertyManager *PropertyManager, bool override)