Removed glibmm dependency.
[lsystem3d.git] / src / xmlstructure.cpp
1 // Copyright (C) 2006 Erik Dahlberg
2 //
3 // This file is part of LSystem3D.
4 //
5 // LSystem3D is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // LSystem3D is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with LSystem3D; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
20
21
22 #include <cstdlib>
23
24 #include <sstream>
25 #include <string>
26
27 #include <libxml++/attribute.h>
28 #include <libxml++/document.h>
29 #include <libxml++/nodes/element.h>
30 #include <libxml++/nodes/node.h>
31 #include <libxml++/parsers/domparser.h>
32
33 #include "xmlstructure.h"
34
35 using namespace std;
36
37
38
39 /**
40 * Constructor
41 */
42 XMLStructure::XMLStructure()
43 {
44 _rootElement = NULL;
45 _activeElement = NULL;
46 _activeDocument = NULL;
47 _activeChildInList = (xmlpp::Node::NodeList::iterator)NULL;
48 }
49
50
51
52 /**
53 * Destructor
54 */
55 XMLStructure::~XMLStructure()
56 {
57 delete _activeDocument;
58 }
59
60
61
62 /**
63 * Create new document with root node
64 * @param rootName name of root
65 */
66 void XMLStructure::createDocumentWithRoot(string rootName)
67 {
68 delete _activeDocument;
69 _activeDocument = new xmlpp::Document;
70
71 _rootElement = _activeDocument->create_root_node(rootName);
72 }
73
74
75
76 /**
77 * Add new child to root
78 * @param name name of child
79 */
80 void XMLStructure::addChildToRoot(string name)
81 {
82 _activeElement = _rootElement->add_child(name);
83 }
84
85
86
87 /**
88 * Add a string as content of current child
89 * @param text the text
90 */
91 void XMLStructure::addString(string text)
92 {
93 _activeElement->add_child_text(text);
94 }
95
96
97
98 /**
99 * Add a number as content of current child
100 * @param value the number
101 */
102 void XMLStructure::addNumber(double value)
103 {
104 addString(doubleToString(value));
105 }
106
107
108
109 /**
110 * Add attribute to current child
111 * @param name name of attribute
112 * @param value value of attribute
113 */
114 void XMLStructure::addAttribute(string name, string value)
115 {
116 _activeElement->set_attribute(name, value);
117 }
118
119
120
121 /**
122 * Save document to file
123 * @param path path to file
124 */
125 void XMLStructure::saveToDisk(string path)
126 {
127 _activeDocument->write_to_file_formatted(path);
128 }
129
130
131
132 /**
133 * Load document from file
134 * @param path path to file
135 */
136 void XMLStructure::loadFromDisk(string path)
137 {
138 // TODO: validate?
139 //_parser.set_validate(true);
140
141 _parser.parse_file(path);
142
143 _rootElement = _parser.get_document()->get_root_node();
144 }
145
146
147
148 /**
149 * Find all children with the specified name
150 * @param name name of children
151 * @return true if found, else false
152 */
153 bool XMLStructure::findChild(string name)
154 {
155 _activeList = _rootElement->get_children(name);
156 _activeChildInList = _activeList.begin();
157
158 return !_activeList.empty();
159 }
160
161
162
163 /**
164 * Find next child in current search result list
165 * @return true if found, else false
166 */
167 bool XMLStructure::findNextChild()
168 {
169 _activeChildInList++;
170
171 bool endOfList = false;
172
173 // last child?
174 if (_activeChildInList == _activeList.end())
175 {
176 _activeChildInList--;
177 endOfList = true;
178 }
179
180 return !endOfList;
181 }
182
183
184
185 /**
186 * Get content of current child as a string
187 * @return the string
188 */
189 string XMLStructure::getString()
190 {
191 // the child
192 xmlpp::Node *child = (*_activeChildInList)->get_children().front();
193
194 // its content
195 string content = dynamic_cast<xmlpp::ContentNode*>(child)->get_content();
196
197 return content;
198 }
199
200
201
202 /**
203 * Get content of current child as a number
204 * @return the number
205 */
206 double XMLStructure::getNumber()
207 {
208 double number = strtod(getString().c_str(), NULL);
209
210 return number;
211 }
212
213
214
215 /**
216 * Get attribute value of current child
217 * @param name name of attribute
218 * @return the value
219 */
220 string XMLStructure::getAttribute(string name)
221 {
222 // the child
223 xmlpp::Element *child = dynamic_cast<xmlpp::Element*>(*_activeChildInList);
224
225 // its attribute value
226 string attributeValue;
227 xmlpp::Attribute *attribute = child->get_attribute(name);
228
229 if (attribute)
230 {
231 attributeValue = attribute->get_value();
232 }
233
234 return attributeValue;
235 }
236
237
238
239 /**
240 * Convert double to string
241 * @param doubleValue the double value
242 * @return the corresponding string
243 */
244 string XMLStructure::doubleToString(double doubleValue)
245 {
246 ostringstream doubleAsString;
247 doubleAsString << doubleValue;
248
249 return doubleAsString.str();
250 }