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
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>
33 #include "xmlstructure.h"
42 XMLStructure::XMLStructure()
45 _activeElement
= NULL
;
46 _activeDocument
= NULL
;
47 _activeChildInList
= (xmlpp::Node::NodeList::iterator
)NULL
;
55 XMLStructure::~XMLStructure()
57 delete _activeDocument
;
63 * Create new document with root node
64 * @param rootName name of root
66 void XMLStructure::createDocumentWithRoot(string rootName
)
68 delete _activeDocument
;
69 _activeDocument
= new xmlpp::Document
;
71 _rootElement
= _activeDocument
->create_root_node(rootName
);
77 * Add new child to root
78 * @param name name of child
80 void XMLStructure::addChildToRoot(string name
)
82 _activeElement
= _rootElement
->add_child(name
);
88 * Add a string as content of current child
89 * @param text the text
91 void XMLStructure::addString(string text
)
93 _activeElement
->add_child_text(text
);
99 * Add a number as content of current child
100 * @param value the number
102 void XMLStructure::addNumber(double value
)
104 addString(doubleToString(value
));
110 * Add attribute to current child
111 * @param name name of attribute
112 * @param value value of attribute
114 void XMLStructure::addAttribute(string name
, string value
)
116 _activeElement
->set_attribute(name
, value
);
122 * Save document to file
123 * @param path path to file
125 void XMLStructure::saveToDisk(string path
)
127 _activeDocument
->write_to_file_formatted(path
);
133 * Load document from file
134 * @param path path to file
136 void XMLStructure::loadFromDisk(string path
)
139 //_parser.set_validate(true);
141 _parser
.parse_file(path
);
143 _rootElement
= _parser
.get_document()->get_root_node();
149 * Find all children with the specified name
150 * @param name name of children
151 * @return true if found, else false
153 bool XMLStructure::findChild(string name
)
155 _activeList
= _rootElement
->get_children(name
);
156 _activeChildInList
= _activeList
.begin();
158 return !_activeList
.empty();
164 * Find next child in current search result list
165 * @return true if found, else false
167 bool XMLStructure::findNextChild()
169 _activeChildInList
++;
171 bool endOfList
= false;
174 if (_activeChildInList
== _activeList
.end())
176 _activeChildInList
--;
186 * Get content of current child as a string
189 string
XMLStructure::getString()
192 xmlpp::Node
*child
= (*_activeChildInList
)->get_children().front();
195 string content
= dynamic_cast<xmlpp::ContentNode
*>(child
)->get_content();
203 * Get content of current child as a number
206 double XMLStructure::getNumber()
208 double number
= strtod(getString().c_str(), NULL
);
216 * Get attribute value of current child
217 * @param name name of attribute
220 string
XMLStructure::getAttribute(string name
)
223 xmlpp::Element
*child
= dynamic_cast<xmlpp::Element
*>(*_activeChildInList
);
225 // its attribute value
226 string attributeValue
;
227 xmlpp::Attribute
*attribute
= child
->get_attribute(name
);
231 attributeValue
= attribute
->get_value();
234 return attributeValue
;
240 * Convert double to string
241 * @param doubleValue the double value
242 * @return the corresponding string
244 string
XMLStructure::doubleToString(double doubleValue
)
246 ostringstream doubleAsString
;
247 doubleAsString
<< doubleValue
;
249 return doubleAsString
.str();