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