1 // Copyright (C) 2006 Erik Dahlberg
3 // This file is part of LSystem3d.
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.
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.
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
24 #include <libxml++/document.h>
25 #include <libxml++/nodes/element.h>
26 #include <libxml++/nodes/node.h>
27 #include <libxml++/parsers/domparser.h>
29 #include <glibmm/stringutils.h>
31 #include "xmlstructure.h"
40 XMLStructure::XMLStructure()
43 _activeElement
= NULL
;
44 _activeDocument
= NULL
;
45 _activeChildInList
= NULL
;
53 XMLStructure::~XMLStructure()
55 delete _activeDocument
;
61 * Create new document with root node
62 * @param rootName name of root
64 void XMLStructure::createDocumentWithRoot(string rootName
)
66 delete _activeDocument
;
67 _activeDocument
= new xmlpp::Document
;
69 _rootElement
= _activeDocument
->create_root_node(rootName
);
75 * Add new child to root
76 * @param name name of child
78 void XMLStructure::addChildToRoot(string name
)
80 _activeElement
= _rootElement
->add_child(name
);
86 * Add a string as content of current child
87 * @param text the text
89 void XMLStructure::addString(string text
)
91 _activeElement
->add_child_text(text
);
97 * Add a number as content of current child
98 * @param value the number
100 void XMLStructure::addNumber(double value
)
102 // convert double -> string
103 string numberAsString
= Glib::Ascii::dtostr(value
);
105 addString(numberAsString
);
111 * Add attribute to current child
112 * @param name name of attribute
113 * @param value value of attribute
115 void XMLStructure::addAttribute(string name
, string value
)
117 _activeElement
->set_attribute(name
, value
);
123 * Save document to file
124 * @param path path to file
126 void XMLStructure::saveToDisk(string path
)
128 _activeDocument
->write_to_file_formatted(path
);
134 * Load document from file
135 * @param path path to file
137 void XMLStructure::loadFromDisk(string path
)
140 //_parser.set_validate(true);
142 _parser
.parse_file(path
);
144 _rootElement
= _parser
.get_document()->get_root_node();
150 * Find all children with the specified name
151 * @param name name of children
152 * @return true if found, else false
154 bool XMLStructure::findChild(string name
)
156 _activeList
= _rootElement
->get_children(name
);
157 _activeChildInList
= _activeList
.begin();
159 return !_activeList
.empty();
165 * Find next child in current search result list
166 * @return true if found, else false
168 bool XMLStructure::findNextChild()
170 _activeChildInList
++;
172 bool endOfList
= false;
175 if (_activeChildInList
== _activeList
.end())
177 _activeChildInList
--;
187 * Get content of current child as a string
190 string
XMLStructure::getString()
193 xmlpp::Node
*child
= (*_activeChildInList
)->get_children().front();
196 string content
= dynamic_cast<xmlpp::ContentNode
*>(child
)->get_content();
204 * Get content of current child as a number
207 double XMLStructure::getNumber()
209 // convert string -> double
210 double number
= Glib::Ascii::strtod(getString());
218 * Get attribute value of current child
219 * @param name name of attribute
222 string
XMLStructure::getAttribute(string name
)
225 xmlpp::Element
*child
= dynamic_cast<xmlpp::Element
*>(*_activeChildInList
);
227 // its attribute value
228 string attributeValue
= child
->get_attribute(name
)->get_value();
230 return attributeValue
;