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++/attribute.h>
25 #include <libxml++/document.h>
26 #include <libxml++/nodes/element.h>
27 #include <libxml++/nodes/node.h>
28 #include <libxml++/parsers/domparser.h>
30 #include <glibmm/stringutils.h>
32 #include "xmlstructure.h"
41 XMLStructure::XMLStructure()
44 _activeElement
= NULL
;
45 _activeDocument
= NULL
;
46 _activeChildInList
= (xmlpp::Node::NodeList::iterator
)NULL
;
54 XMLStructure::~XMLStructure()
56 delete _activeDocument
;
62 * Create new document with root node
63 * @param rootName name of root
65 void XMLStructure::createDocumentWithRoot(string rootName
)
67 delete _activeDocument
;
68 _activeDocument
= new xmlpp::Document
;
70 _rootElement
= _activeDocument
->create_root_node(rootName
);
76 * Add new child to root
77 * @param name name of child
79 void XMLStructure::addChildToRoot(string name
)
81 _activeElement
= _rootElement
->add_child(name
);
87 * Add a string as content of current child
88 * @param text the text
90 void XMLStructure::addString(string text
)
92 _activeElement
->add_child_text(text
);
98 * Add a number as content of current child
99 * @param value the number
101 void XMLStructure::addNumber(double value
)
103 // convert double -> string
104 string numberAsString
= Glib::Ascii::dtostr(value
);
106 addString(numberAsString
);
112 * Add attribute to current child
113 * @param name name of attribute
114 * @param value value of attribute
116 void XMLStructure::addAttribute(string name
, string value
)
118 _activeElement
->set_attribute(name
, value
);
124 * Save document to file
125 * @param path path to file
127 void XMLStructure::saveToDisk(string path
)
129 _activeDocument
->write_to_file_formatted(path
);
135 * Load document from file
136 * @param path path to file
138 void XMLStructure::loadFromDisk(string path
)
141 //_parser.set_validate(true);
143 _parser
.parse_file(path
);
145 _rootElement
= _parser
.get_document()->get_root_node();
151 * Find all children with the specified name
152 * @param name name of children
153 * @return true if found, else false
155 bool XMLStructure::findChild(string name
)
157 _activeList
= _rootElement
->get_children(name
);
158 _activeChildInList
= _activeList
.begin();
160 return !_activeList
.empty();
166 * Find next child in current search result list
167 * @return true if found, else false
169 bool XMLStructure::findNextChild()
171 _activeChildInList
++;
173 bool endOfList
= false;
176 if (_activeChildInList
== _activeList
.end())
178 _activeChildInList
--;
188 * Get content of current child as a string
191 string
XMLStructure::getString()
194 xmlpp::Node
*child
= (*_activeChildInList
)->get_children().front();
197 string content
= dynamic_cast<xmlpp::ContentNode
*>(child
)->get_content();
205 * Get content of current child as a number
208 double XMLStructure::getNumber()
210 // convert string -> double
211 double number
= Glib::Ascii::strtod(getString());
219 * Get attribute value of current child
220 * @param name name of attribute
223 string
XMLStructure::getAttribute(string name
)
226 xmlpp::Element
*child
= dynamic_cast<xmlpp::Element
*>(*_activeChildInList
);
228 // its attribute value
229 string attributeValue
;
230 xmlpp::Attribute
*attribute
= child
->get_attribute(name
);
234 attributeValue
= attribute
->get_value();
237 return attributeValue
;