| 1 | // Copyright (C) 2006 Erik Dahlberg |
| 2 | // |
| 3 | // This file is part of LSystem3D. |
| 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 | #ifndef XMLSTRUCTURE_H |
| 23 | #define XMLSTRUCTURE_H |
| 24 | |
| 25 | #include <string> |
| 26 | |
| 27 | #include <libxml++/document.h> |
| 28 | #include <libxml++/nodes/element.h> |
| 29 | #include <libxml++/nodes/node.h> |
| 30 | #include <libxml++/parsers/domparser.h> |
| 31 | |
| 32 | using namespace std; |
| 33 | |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * Basic logic for building and parsing xml-structures |
| 38 | */ |
| 39 | class XMLStructure |
| 40 | { |
| 41 | public: |
| 42 | /** |
| 43 | * Constructor |
| 44 | */ |
| 45 | XMLStructure(); |
| 46 | |
| 47 | /** |
| 48 | * Destructor |
| 49 | */ |
| 50 | ~XMLStructure(); |
| 51 | |
| 52 | protected: |
| 53 | |
| 54 | // Saver |
| 55 | // ----- |
| 56 | |
| 57 | /** |
| 58 | * Create new document with root node |
| 59 | * @param rootName name of root |
| 60 | */ |
| 61 | void createDocumentWithRoot(string rootName); |
| 62 | |
| 63 | /** |
| 64 | * Add new child to root |
| 65 | * @param name name of child |
| 66 | */ |
| 67 | void addChildToRoot(string name); |
| 68 | |
| 69 | /** |
| 70 | * Add a string as content of current child |
| 71 | * @param text the text |
| 72 | */ |
| 73 | void addString(string text); |
| 74 | |
| 75 | /** |
| 76 | * Add a number as content of current child |
| 77 | * @param value the number |
| 78 | */ |
| 79 | void addNumber(double value); |
| 80 | |
| 81 | /** |
| 82 | * Add attribute to current child |
| 83 | * @param name name of attribute |
| 84 | * @param value value of attribute |
| 85 | */ |
| 86 | void addAttribute(string name, string value); |
| 87 | |
| 88 | /** |
| 89 | * Save document to file |
| 90 | * @param path path to file |
| 91 | */ |
| 92 | void saveToDisk(string path); |
| 93 | |
| 94 | |
| 95 | // Loader |
| 96 | // ------ |
| 97 | |
| 98 | /** |
| 99 | * Load document from file |
| 100 | * @param path path to file |
| 101 | */ |
| 102 | void loadFromDisk(string path); |
| 103 | |
| 104 | /** |
| 105 | * Find all children with the specified name |
| 106 | * @param name name of children |
| 107 | * @return true if found, else false |
| 108 | */ |
| 109 | bool findChild(string name); |
| 110 | |
| 111 | /** |
| 112 | * Find next child in current search result list |
| 113 | * @return true if found, else false |
| 114 | */ |
| 115 | bool findNextChild(); |
| 116 | |
| 117 | /** |
| 118 | * Get content of current child as a string |
| 119 | * @return the string |
| 120 | */ |
| 121 | string getString(); |
| 122 | |
| 123 | /** |
| 124 | * Get content of current child as a number |
| 125 | * @return the number |
| 126 | */ |
| 127 | double getNumber(); |
| 128 | |
| 129 | /** |
| 130 | * Get attribute value of current child |
| 131 | * @param name name of attribute |
| 132 | * @return the value |
| 133 | */ |
| 134 | string getAttribute(string name); |
| 135 | |
| 136 | private: |
| 137 | |
| 138 | // Saver |
| 139 | xmlpp::Element *_rootElement; |
| 140 | xmlpp::Element *_activeElement; |
| 141 | xmlpp::Document *_activeDocument; |
| 142 | |
| 143 | // Loader |
| 144 | xmlpp::DomParser _parser; |
| 145 | xmlpp::Node::NodeList _activeList; |
| 146 | xmlpp::Node::NodeList::iterator _activeChildInList; |
| 147 | }; |
| 148 | |
| 149 | |
| 150 | |
| 151 | #endif |