Initial revision
[lsystem3d.git] / lsystem3d / src / xmlstructure.cpp
CommitLineData
e5247a3b 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 <string>
23
24#include <libxml++/document.h>
25#include <libxml++/nodes/element.h>
26#include <libxml++/nodes/node.h>
27#include <libxml++/parsers/domparser.h>
28
29#include <glibmm/stringutils.h>
30
31#include "xmlstructure.h"
32
33using namespace std;
34
35
36
37/**
38 * Constructor
39 */
40XMLStructure::XMLStructure()
41{
42 _rootElement = NULL;
43 _activeElement = NULL;
44 _activeDocument = NULL;
45 _activeChildInList = NULL;
46}
47
48
49
50/**
51 * Destructor
52 */
53XMLStructure::~XMLStructure()
54{
55 delete _activeDocument;
56}
57
58
59
60/**
61 * Create new document with root node
62 * @param rootName name of root
63 */
64void XMLStructure::createDocumentWithRoot(string rootName)
65{
66 delete _activeDocument;
67 _activeDocument = new xmlpp::Document;
68
69 _rootElement = _activeDocument->create_root_node(rootName);
70}
71
72
73
74/**
75 * Add new child to root
76 * @param name name of child
77 */
78void XMLStructure::addChildToRoot(string name)
79{
80 _activeElement = _rootElement->add_child(name);
81}
82
83
84
85/**
86 * Add a string as content of current child
87 * @param text the text
88 */
89void XMLStructure::addString(string text)
90{
91 _activeElement->add_child_text(text);
92}
93
94
95
96/**
97 * Add a number as content of current child
98 * @param value the number
99 */
100void XMLStructure::addNumber(double value)
101{
102 // convert double -> string
103 string numberAsString = Glib::Ascii::dtostr(value);
104
105 addString(numberAsString);
106}
107
108
109
110/**
111 * Add attribute to current child
112 * @param name name of attribute
113 * @param value value of attribute
114 */
115void XMLStructure::addAttribute(string name, string value)
116{
117 _activeElement->set_attribute(name, value);
118}
119
120
121
122/**
123 * Save document to file
124 * @param path path to file
125 */
126void XMLStructure::saveToDisk(string path)
127{
128 _activeDocument->write_to_file_formatted(path);
129}
130
131
132
133/**
134 * Load document from file
135 * @param path path to file
136 */
137void XMLStructure::loadFromDisk(string path)
138{
139 // TODO: validate?
140 //_parser.set_validate(true);
141
142 _parser.parse_file(path);
143
144 _rootElement = _parser.get_document()->get_root_node();
145}
146
147
148
149/**
150 * Find all children with the specified name
151 * @param name name of children
152 * @return true if found, else false
153 */
154bool XMLStructure::findChild(string name)
155{
156 _activeList = _rootElement->get_children(name);
157 _activeChildInList = _activeList.begin();
158
159 return !_activeList.empty();
160}
161
162
163
164/**
165 * Find next child in current search result list
166 * @return true if found, else false
167 */
168bool XMLStructure::findNextChild()
169{
170 _activeChildInList++;
171
172 bool endOfList = false;
173
174 // last child?
175 if (_activeChildInList == _activeList.end())
176 {
177 _activeChildInList--;
178 endOfList = true;
179 }
180
181 return !endOfList;
182}
183
184
185
186/**
187 * Get content of current child as a string
188 * @return the string
189 */
190string XMLStructure::getString()
191{
192 // the child
193 xmlpp::Node *child = (*_activeChildInList)->get_children().front();
194
195 // its content
196 string content = dynamic_cast<xmlpp::ContentNode*>(child)->get_content();
197
198 return content;
199}
200
201
202
203/**
204 * Get content of current child as a number
205 * @return the number
206 */
207double XMLStructure::getNumber()
208{
209 // convert string -> double
210 double number = Glib::Ascii::strtod(getString());
211
212 return number;
213}
214
215
216
217/**
218 * Get attribute value of current child
219 * @param name name of attribute
220 * @return the value
221 */
222string XMLStructure::getAttribute(string name)
223{
224 // the child
225 xmlpp::Element *child = dynamic_cast<xmlpp::Element*>(*_activeChildInList);
226
227 // its attribute value
228 string attributeValue = child->get_attribute(name)->get_value();
229
230 return attributeValue;
231}