Initial revision
[lsystem3d.git] / lsystem3d / src / xmlstructure.h
diff --git a/lsystem3d/src/xmlstructure.h b/lsystem3d/src/xmlstructure.h
new file mode 100644 (file)
index 0000000..6f3f658
--- /dev/null
@@ -0,0 +1,151 @@
+// Copyright (C) 2006 Erik Dahlberg
+// 
+// This file is part of LSystem3d.
+// 
+// LSystem3D is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+// 
+// LSystem3D is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with LSystem3D; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+
+
+
+#ifndef XMLSTRUCTURE_H
+#define XMLSTRUCTURE_H
+
+#include <string>
+
+#include <libxml++/document.h>
+#include <libxml++/nodes/element.h>
+#include <libxml++/nodes/node.h>
+#include <libxml++/parsers/domparser.h>
+
+using namespace std;
+
+
+
+/**
+ * Basic logic for building and parsing xml-structures
+ */
+class XMLStructure
+{
+public:
+    /**
+     * Constructor
+     */
+    XMLStructure();
+
+    /**
+     * Destructor
+     */
+    ~XMLStructure();
+    
+protected:
+    
+    // Saver
+    // -----
+    
+    /**
+     * Create new document with root node
+     * @param rootName name of root
+     */
+    void createDocumentWithRoot(string rootName);
+    
+    /**
+     * Add new child to root
+     * @param name name of child
+     */
+    void addChildToRoot(string name);
+    
+    /**
+     * Add a string as content of current child
+     * @param text the text
+     */
+    void addString(string text);
+    
+    /**
+     * Add a number as content of current child
+     * @param value the number
+     */
+    void addNumber(double value);
+    
+    /**
+     * Add attribute to current child
+     * @param name name of attribute
+     * @param value value of attribute
+     */
+    void addAttribute(string name, string value);
+    
+    /**
+     * Save document to file
+     * @param path path to file
+     */
+    void saveToDisk(string path);
+    
+    
+    // Loader
+    // ------
+    
+    /**
+     * Load document from file
+     * @param path path to file
+     */
+    void loadFromDisk(string path);
+    
+    /**
+     * Find all children with the specified name
+     * @param name name of children
+     * @return true if found, else false
+     */
+    bool findChild(string name);
+    
+    /**
+     * Find next child in current search result list
+     * @return true if found, else false
+     */
+    bool findNextChild();
+    
+    /**
+     * Get content of current child as a string
+     * @return the string
+     */
+    string getString();
+    
+    /**
+     * Get content of current child as a number
+     * @return the number
+     */
+    double getNumber();
+    
+    /**
+     * Get attribute value of current child
+     * @param name name of attribute
+     * @return the value
+     */
+    string getAttribute(string name);    
+    
+private:
+    
+    // Saver
+    xmlpp::Element *_rootElement;
+    xmlpp::Element *_activeElement;
+    xmlpp::Document *_activeDocument;
+    
+    // Loader
+    xmlpp::DomParser _parser;
+    xmlpp::Node::NodeList _activeList;
+    xmlpp::Node::NodeList::iterator _activeChildInList;
+};
+
+
+
+#endif