XMLNode.h

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #if !defined(__INCLUDE_XMLNodeh_INCLUDE__)
00022 #define __INCLUDE_XMLNodeh_INCLUDE__
00023 
00024 #include <list>
00025 #include <lang/LangString.h>
00026 #include <common/Vector.h>
00027 #include <common/FixedVector.h>
00028 #include <common/FileLines.h>
00029 #include <common/NumberParser.h>
00030 
00031 class XMLNode
00032 {
00033 public:
00034         enum NodeType
00035         {
00036                 XMLNodeType,
00037                 XMLParameterType,
00038                 XMLCommentType,
00039                 XMLContentType
00040         };
00041 
00042         XMLNode(const char *name, const char *content = "", 
00043                 NodeType = XMLNode::XMLNodeType);
00044         XMLNode(const char *name, const std::string &content, 
00045                 NodeType = XMLNode::XMLNodeType);
00046         XMLNode(const char *name, const LangString &content, 
00047                 NodeType = XMLNode::XMLNodeType);
00048         XMLNode(const char *name, NumberParser &content,
00049                 NodeType = XMLNode::XMLNodeType);
00050         XMLNode(const char *name, float content, 
00051                 NodeType = XMLNode::XMLNodeType);
00052         XMLNode(const char *name, bool content, 
00053                 NodeType = XMLNode::XMLNodeType);
00054         XMLNode(const char *name, int content, 
00055                 NodeType = XMLNode::XMLNodeType);
00056         XMLNode(const char *name, Vector &content, 
00057                 NodeType = XMLNode::XMLNodeType);
00058         XMLNode(const char *name, unsigned int content, 
00059                 NodeType = XMLNode::XMLNodeType);
00060         XMLNode(const char *name, fixed content, 
00061                 NodeType = XMLNode::XMLNodeType);
00062         XMLNode(const char *name, FixedVector &content, 
00063                 NodeType = XMLNode::XMLNodeType);
00064 
00065         virtual ~XMLNode();
00066 
00067         bool writeToFile(const std::string &fileName);
00068 
00069         NodeType getType() { return type_; }
00070         const char *getName() { return name_.c_str(); }
00071         const char *getContent();
00072         const char *getSource() { return source_.c_str(); }
00073         const XMLNode *getParent() { return parent_; }
00074 
00075         std::list<XMLNode *> &getChildren() { return children_; }
00076         std::list<XMLNode *> &getParameters() { return parameters_; }
00077 
00078         bool getNamedParameter(const char *name, XMLNode *&node, 
00079                 bool failOnError = true, bool remove = true);
00080         bool getNamedParameter(const char *name, std::string &value,
00081                 bool failOnError = true, bool remove = true);
00082         bool getNamedParameter(const char *name, LangString &value,
00083                 bool failOnError = true, bool remove = true);
00084         bool getNamedChild(const char *name, XMLNode *&node, 
00085                 bool failOnError = true, bool remove = true);
00086         bool getNamedChild(const char *name, LangString &node, 
00087                 bool failOnError = true, bool remove = true);
00088         bool getNamedChild(const char *name, std::string &value,
00089                 bool failOnError = true, bool remove = true);
00090         bool getNamedChild(const char *name, bool &value,
00091                 bool failOnError = true, bool remove = true);
00092         bool getNamedChild(const char *name, NumberParser &value,
00093                 bool failOnError = true, bool remove = true);
00094         bool getNamedChild(const char *name, float &value,
00095                 bool failOnError = true, bool remove = true);
00096         bool getNamedChild(const char *name, int &value,
00097                 bool failOnError = true, bool remove = true);
00098         bool getNamedChild(const char *name, unsigned int &value,
00099                 bool failOnError = true, bool remove = true);
00100         bool getNamedChild(const char *name, fixed &value,
00101                 bool failOnError = true, bool remove = true);
00102         bool getNamedChild(const char *name, Vector &value,
00103                 bool failOnError = true, bool remove = true);
00104         bool getNamedChild(const char *name, FixedVector &value,
00105                 bool failOnError = true, bool remove = true);
00106 
00107         bool failChildren();
00108         bool failContent();
00109         void resurrectRemovedChildren();
00110 
00111         void setSource(const char *source);
00112         void setLine(int line, int col);
00113         void setUseContentNodes(bool useContentNodes) 
00114                 { useContentNodes_ = useContentNodes; }
00115 
00116         bool returnError(const std::string &error);
00117         void addChild(XMLNode *node); 
00118         void addParameter(XMLNode *node);
00119         void addContent(const char *data, int len);
00120 
00121         static void removeSpecialChars(const std::string &content,
00122                 std::string &result);
00123         static void addSpecialChars(const std::string &content, 
00124                 std::string &result);
00125         static const char *getSpacer(int space);
00126 
00127 protected:
00128         bool useContentNodes_;
00129         NodeType type_;
00130         XMLNode *parent_;
00131         std::list<XMLNode *> children_;
00132         std::list<XMLNode *> removedChildren_; // So they are tidied up as well
00133         std::list<XMLNode *> parameters_;
00134         std::list<XMLNode *> removedParameters_; // Tidied
00135         std::string name_;
00136         std::string content_;
00137         std::string source_;
00138         int line_, col_;
00139 
00140         void addNodeToFile(FileLines &lines, int spacing);
00141 
00142 };
00143 
00144 #endif

Generated on Mon Feb 16 15:14:52 2009 for Scorched3D by  doxygen 1.5.3