llxmltree.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * @file llxmltree.h
  3. * @author Aaron Yonas, Richard Nelson
  4. * @brief LLXmlTree class definition
  5. *
  6. * $LicenseInfo:firstyear=2001&license=viewergpl$
  7. *
  8. * Copyright (c) 2001-2009, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #ifndef LL_LLXMLTREE_H
  34. #define LL_LLXMLTREE_H
  35. #include <map>
  36. #include <vector>
  37. #include "llstring.h"
  38. #include "llstringtable.h"
  39. #include "llxmlparser.h"
  40. class LLColor4;
  41. class LLColor4U;
  42. class LLQuaternion;
  43. class LLUUID;
  44. class LLVector3;
  45. class LLVector3d;
  46. class LLXmlTreeNode;
  47. class LLXmlTreeParser;
  48. class LLXmlTree
  49. {
  50. friend class LLXmlTreeNode;
  51. protected:
  52. LOG_CLASS(LLXmlTree);
  53. public:
  54. LLXmlTree();
  55. virtual ~LLXmlTree();
  56. void cleanup();
  57. virtual bool parseFile(const std::string& path, bool keep_contents = true);
  58. LL_INLINE LLXmlTreeNode* getRoot() { return mRoot; }
  59. void dump();
  60. void dumpNode(LLXmlTreeNode* node, const std::string& prefix);
  61. LL_INLINE static LLStdStringHandle addAttributeString(const std::string& name)
  62. {
  63. return sAttributeKeys.addString(name);
  64. }
  65. public:
  66. // global
  67. static LLStdStringTable sAttributeKeys;
  68. protected:
  69. LLXmlTreeNode* mRoot;
  70. // local
  71. LLStdStringTable mNodeNames;
  72. };
  73. class LLXmlTreeNode
  74. {
  75. friend class LLXmlTree;
  76. friend class LLXmlTreeParser;
  77. protected:
  78. LOG_CLASS(LLXmlTreeNode);
  79. // Protected since nodes are only created and destroyed by friend classes
  80. // and other LLXmlTreeNodes
  81. LLXmlTreeNode(const char* name, LLXmlTreeNode* parent, LLXmlTree* tree);
  82. public:
  83. virtual ~LLXmlTreeNode();
  84. LL_INLINE const std::string& getName() { return mName; }
  85. LL_INLINE bool hasName(const std::string& name) { return mName == name; }
  86. bool hasAttribute(const std::string& name);
  87. // Fast versions using handles
  88. bool getFastAttributeBool(LLStdStringHandle cannonical_name, bool& value);
  89. bool getFastAttributeU8(LLStdStringHandle cannonical_name, U8& value);
  90. bool getFastAttributeS8(LLStdStringHandle cannonical_name, S8& value);
  91. bool getFastAttributeU16(LLStdStringHandle cannonical_name, U16& value);
  92. bool getFastAttributeS16(LLStdStringHandle cannonical_name, S16& value);
  93. bool getFastAttributeU32(LLStdStringHandle cannonical_name, U32& value);
  94. bool getFastAttributeS32(LLStdStringHandle cannonical_name, S32& value);
  95. bool getFastAttributeF32(LLStdStringHandle cannonical_name, F32& value);
  96. bool getFastAttributeF64(LLStdStringHandle cannonical_name, F64& value);
  97. bool getFastAttributeColor(LLStdStringHandle cannonical_name,
  98. LLColor4& value);
  99. bool getFastAttributeColor4(LLStdStringHandle cannonical_name,
  100. LLColor4& value);
  101. bool getFastAttributeColor4U(LLStdStringHandle cannonical_name,
  102. LLColor4U& value);
  103. bool getFastAttributeVector3(LLStdStringHandle cannonical_name,
  104. LLVector3& value);
  105. bool getFastAttributeVector3d(LLStdStringHandle cannonical_name,
  106. LLVector3d& value);
  107. bool getFastAttributeQuat(LLStdStringHandle cannonical_name,
  108. LLQuaternion& value);
  109. bool getFastAttributeUUID(LLStdStringHandle cannonical_name,
  110. LLUUID& value);
  111. bool getFastAttributeString(LLStdStringHandle cannonical_name,
  112. std::string& value);
  113. // Normal versions find 'name' in LLXmlTree::sAttributeKeys then call fast
  114. // versions
  115. virtual bool getAttributeBool(const char* name, bool& value);
  116. virtual bool getAttributeU8(const char* name, U8& value);
  117. virtual bool getAttributeS8(const char* name, S8& value);
  118. virtual bool getAttributeU16(const char* name, U16& value);
  119. virtual bool getAttributeS16(const char* name, S16& value);
  120. virtual bool getAttributeU32(const char* name, U32& value);
  121. virtual bool getAttributeS32(const char* name, S32& value);
  122. virtual bool getAttributeF32(const char* name, F32& value);
  123. virtual bool getAttributeF64(const char* name, F64& value);
  124. virtual bool getAttributeColor(const char* name, LLColor4& value);
  125. virtual bool getAttributeColor4(const char* name, LLColor4& value);
  126. virtual bool getAttributeColor4U(const char* name, LLColor4U& value);
  127. virtual bool getAttributeVector3(const char* name, LLVector3& value);
  128. virtual bool getAttributeVector3d(const char* name, LLVector3d& value);
  129. virtual bool getAttributeQuat(const char* name, LLQuaternion& value);
  130. virtual bool getAttributeUUID(const char* name, LLUUID& value);
  131. virtual bool getAttributeString(const char* name, std::string& value);
  132. LL_INLINE const std::string& getContents() { return mContents; }
  133. std::string getTextContents();
  134. LL_INLINE LLXmlTreeNode* getParent() { return mParent; }
  135. LLXmlTreeNode* getFirstChild();
  136. LLXmlTreeNode* getNextChild();
  137. LL_INLINE S32 getChildCount() { return (S32)mChildren.size(); }
  138. // returns first child with name, NULL if none
  139. LLXmlTreeNode* getChildByName(const std::string& name);
  140. // returns next child with name, NULL if none
  141. LLXmlTreeNode* getNextNamedChild();
  142. protected:
  143. LL_INLINE const std::string* getAttribute(LLStdStringHandle name)
  144. {
  145. attribute_map_t::iterator iter = mAttributes.find(name);
  146. return iter == mAttributes.end() ? NULL : iter->second;
  147. }
  148. private:
  149. void addAttribute(const char* name, const std::string& value);
  150. void appendContents(const std::string& str);
  151. void addChild(LLXmlTreeNode* child);
  152. void dump(const std::string& prefix);
  153. protected:
  154. typedef std::map<LLStdStringHandle, const std::string*> attribute_map_t;
  155. attribute_map_t mAttributes;
  156. private:
  157. std::string mName;
  158. std::string mContents;
  159. typedef std::vector<class LLXmlTreeNode*> child_vec_t;
  160. child_vec_t mChildren;
  161. child_vec_t::iterator mChildrenIter;
  162. typedef std::multimap<LLStdStringHandle, LLXmlTreeNode*> child_map_t;
  163. child_map_t mChildMap; // For fast name lookups
  164. child_map_t::iterator mChildMapIter;
  165. child_map_t::iterator mChildMapEndIter;
  166. LLXmlTreeNode* mParent;
  167. LLXmlTree* mTree;
  168. };
  169. class LLXmlTreeParser : public LLXmlParser
  170. {
  171. protected:
  172. LOG_CLASS(LLXmlTreeParser);
  173. public:
  174. LLXmlTreeParser(LLXmlTree* tree);
  175. bool parseFile(const std::string& path, LLXmlTreeNode** root,
  176. bool keep_contents);
  177. protected:
  178. const std::string& tabs();
  179. void startElement(const char* name, const char** attributes) override;
  180. void endElement(const char* name) override;
  181. void characterData(const char* s, int len) override;
  182. void processingInstruction(const char* target, const char* data) override;
  183. void comment(const char* data) override;
  184. void startCdataSection() override;
  185. void endCdataSection() override;
  186. void defaultData(const char* s, int len) override;
  187. void unparsedEntityDecl(const char* entity_name, const char* base,
  188. const char* system_id, const char* public_id,
  189. const char* notation_name) override;
  190. // Template method pattern
  191. LLXmlTreeNode* createXmlTreeNode(const char* name, LLXmlTreeNode* parent);
  192. protected:
  193. LLXmlTree* mTree;
  194. LLXmlTreeNode* mRoot;
  195. LLXmlTreeNode* mCurrent;
  196. bool mDump; // Dump parse tree to llinfos as it is read.
  197. bool mKeepContents;
  198. };
  199. #endif // LL_LLXMLTREE_H