PageRenderTime 113ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llxuixml/llxuiparser.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 242 lines | 150 code | 49 blank | 43 comment | 0 complexity | 8fd7ae0e371480b6b1deaddd1982a591 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llxuiparser.h
  3. * @brief Utility functions for handling XUI structures in XML
  4. *
  5. * $LicenseInfo:firstyear=2003&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LLXUIPARSER_H
  27. #define LLXUIPARSER_H
  28. #include "llinitparam.h"
  29. #include "llregistry.h"
  30. #include "llpointer.h"
  31. #include <boost/function.hpp>
  32. #include <iosfwd>
  33. #include <stack>
  34. #include <set>
  35. class LLView;
  36. typedef LLPointer<class LLXMLNode> LLXMLNodePtr;
  37. // lookup widget type by name
  38. class LLWidgetTypeRegistry
  39. : public LLRegistrySingleton<std::string, const std::type_info*, LLWidgetTypeRegistry>
  40. {};
  41. // global static instance for registering all widget types
  42. typedef boost::function<LLView* (LLXMLNodePtr node, LLView *parent, LLXMLNodePtr output_node)> LLWidgetCreatorFunc;
  43. typedef LLRegistry<std::string, LLWidgetCreatorFunc> widget_registry_t;
  44. class LLChildRegistryRegistry
  45. : public LLRegistrySingleton<const std::type_info*, widget_registry_t, LLChildRegistryRegistry>
  46. {};
  47. class LLXSDWriter : public LLInitParam::Parser
  48. {
  49. LOG_CLASS(LLXSDWriter);
  50. public:
  51. void writeXSD(const std::string& name, LLXMLNodePtr node, const LLInitParam::BaseBlock& block, const std::string& xml_namespace);
  52. /*virtual*/ std::string getCurrentElementName() { return LLStringUtil::null; }
  53. LLXSDWriter();
  54. protected:
  55. void writeAttribute(const std::string& type, const Parser::name_stack_t&, S32 min_count, S32 max_count, const std::vector<std::string>* possible_values);
  56. void addAttributeToSchema(LLXMLNodePtr nodep, const std::string& attribute_name, const std::string& type, bool mandatory, const std::vector<std::string>* possible_values);
  57. LLXMLNodePtr mAttributeNode;
  58. LLXMLNodePtr mElementNode;
  59. LLXMLNodePtr mSchemaNode;
  60. typedef std::set<std::string> string_set_t;
  61. typedef std::map<LLXMLNodePtr, string_set_t> attributes_map_t;
  62. attributes_map_t mAttributesWritten;
  63. };
  64. // NOTE: DOES NOT WORK YET
  65. // should support child widgets for XUI
  66. class LLXUIXSDWriter : public LLXSDWriter
  67. {
  68. public:
  69. void writeXSD(const std::string& name, const std::string& path, const LLInitParam::BaseBlock& block);
  70. };
  71. class LLXUIParserImpl;
  72. class LLXUIParser : public LLInitParam::Parser
  73. {
  74. LOG_CLASS(LLXUIParser);
  75. public:
  76. LLXUIParser();
  77. typedef LLInitParam::Parser::name_stack_t name_stack_t;
  78. /*virtual*/ std::string getCurrentElementName();
  79. /*virtual*/ void parserWarning(const std::string& message);
  80. /*virtual*/ void parserError(const std::string& message);
  81. void readXUI(LLXMLNodePtr node, LLInitParam::BaseBlock& block, const std::string& filename = LLStringUtil::null, bool silent=false);
  82. void writeXUI(LLXMLNodePtr node, const LLInitParam::BaseBlock& block, const LLInitParam::BaseBlock* diff_block = NULL);
  83. private:
  84. bool readXUIImpl(LLXMLNodePtr node, LLInitParam::BaseBlock& block);
  85. bool readAttributes(LLXMLNodePtr nodep, LLInitParam::BaseBlock& block);
  86. //reader helper functions
  87. static bool readFlag(Parser& parser, void* val_ptr);
  88. static bool readBoolValue(Parser& parser, void* val_ptr);
  89. static bool readStringValue(Parser& parser, void* val_ptr);
  90. static bool readU8Value(Parser& parser, void* val_ptr);
  91. static bool readS8Value(Parser& parser, void* val_ptr);
  92. static bool readU16Value(Parser& parser, void* val_ptr);
  93. static bool readS16Value(Parser& parser, void* val_ptr);
  94. static bool readU32Value(Parser& parser, void* val_ptr);
  95. static bool readS32Value(Parser& parser, void* val_ptr);
  96. static bool readF32Value(Parser& parser, void* val_ptr);
  97. static bool readF64Value(Parser& parser, void* val_ptr);
  98. static bool readColor4Value(Parser& parser, void* val_ptr);
  99. static bool readUIColorValue(Parser& parser, void* val_ptr);
  100. static bool readUUIDValue(Parser& parser, void* val_ptr);
  101. static bool readSDValue(Parser& parser, void* val_ptr);
  102. //writer helper functions
  103. static bool writeFlag(Parser& parser, const void* val_ptr, name_stack_t&);
  104. static bool writeBoolValue(Parser& parser, const void* val_ptr, name_stack_t&);
  105. static bool writeStringValue(Parser& parser, const void* val_ptr, name_stack_t&);
  106. static bool writeU8Value(Parser& parser, const void* val_ptr, name_stack_t&);
  107. static bool writeS8Value(Parser& parser, const void* val_ptr, name_stack_t&);
  108. static bool writeU16Value(Parser& parser, const void* val_ptr, name_stack_t&);
  109. static bool writeS16Value(Parser& parser, const void* val_ptr, name_stack_t&);
  110. static bool writeU32Value(Parser& parser, const void* val_ptr, name_stack_t&);
  111. static bool writeS32Value(Parser& parser, const void* val_ptr, name_stack_t&);
  112. static bool writeF32Value(Parser& parser, const void* val_ptr, name_stack_t&);
  113. static bool writeF64Value(Parser& parser, const void* val_ptr, name_stack_t&);
  114. static bool writeColor4Value(Parser& parser, const void* val_ptr, name_stack_t&);
  115. static bool writeUIColorValue(Parser& parser, const void* val_ptr, name_stack_t&);
  116. static bool writeUUIDValue(Parser& parser, const void* val_ptr, name_stack_t&);
  117. static bool writeSDValue(Parser& parser, const void* val_ptr, name_stack_t&);
  118. LLXMLNodePtr getNode(name_stack_t& stack);
  119. private:
  120. Parser::name_stack_t mNameStack;
  121. LLXMLNodePtr mCurReadNode;
  122. // Root of the widget XML sub-tree, for example, "line_editor"
  123. LLXMLNodePtr mWriteRootNode;
  124. typedef std::map<std::string, LLXMLNodePtr> out_nodes_t;
  125. out_nodes_t mOutNodes;
  126. LLXMLNodePtr mLastWrittenChild;
  127. S32 mCurReadDepth;
  128. std::string mCurFileName;
  129. std::string mRootNodeName;
  130. };
  131. // LLSimpleXUIParser is a streamlined SAX-based XUI parser that does not support localization
  132. // or parsing of a tree of independent param blocks, such as child widgets.
  133. // Use this for reading non-localized files that only need a single param block as a result.
  134. //
  135. // NOTE: In order to support nested block parsing, we need callbacks for start element that
  136. // push new blocks contexts on the mScope stack.
  137. // NOTE: To support localization without building a DOM, we need to enforce consistent
  138. // ordering of child elements from base file to localized diff file. Then we can use a pair
  139. // of coroutines to perform matching of xml nodes during parsing. Not sure if the overhead
  140. // of coroutines would offset the gain from SAX parsing
  141. class LLSimpleXUIParserImpl;
  142. class LLSimpleXUIParser : public LLInitParam::Parser
  143. {
  144. LOG_CLASS(LLSimpleXUIParser);
  145. public:
  146. typedef LLInitParam::Parser::name_stack_t name_stack_t;
  147. typedef LLInitParam::BaseBlock* (*element_start_callback_t)(LLSimpleXUIParser&, const char* block_name);
  148. LLSimpleXUIParser(element_start_callback_t element_cb = NULL);
  149. virtual ~LLSimpleXUIParser();
  150. /*virtual*/ std::string getCurrentElementName();
  151. /*virtual*/ void parserWarning(const std::string& message);
  152. /*virtual*/ void parserError(const std::string& message);
  153. bool readXUI(const std::string& filename, LLInitParam::BaseBlock& block, bool silent=false);
  154. private:
  155. //reader helper functions
  156. static bool readFlag(Parser&, void* val_ptr);
  157. static bool readBoolValue(Parser&, void* val_ptr);
  158. static bool readStringValue(Parser&, void* val_ptr);
  159. static bool readU8Value(Parser&, void* val_ptr);
  160. static bool readS8Value(Parser&, void* val_ptr);
  161. static bool readU16Value(Parser&, void* val_ptr);
  162. static bool readS16Value(Parser&, void* val_ptr);
  163. static bool readU32Value(Parser&, void* val_ptr);
  164. static bool readS32Value(Parser&, void* val_ptr);
  165. static bool readF32Value(Parser&, void* val_ptr);
  166. static bool readF64Value(Parser&, void* val_ptr);
  167. static bool readColor4Value(Parser&, void* val_ptr);
  168. static bool readUIColorValue(Parser&, void* val_ptr);
  169. static bool readUUIDValue(Parser&, void* val_ptr);
  170. static bool readSDValue(Parser&, void* val_ptr);
  171. private:
  172. static void startElementHandler(void *userData, const char *name, const char **atts);
  173. static void endElementHandler(void *userData, const char *name);
  174. static void characterDataHandler(void *userData, const char *s, int len);
  175. void startElement(const char *name, const char **atts);
  176. void endElement(const char *name);
  177. void characterData(const char *s, int len);
  178. bool readAttributes(const char **atts);
  179. bool processText();
  180. Parser::name_stack_t mNameStack;
  181. struct XML_ParserStruct* mParser;
  182. LLXMLNodePtr mLastWrittenChild;
  183. S32 mCurReadDepth;
  184. std::string mCurFileName;
  185. std::string mTextContents;
  186. const char* mCurAttributeValueBegin;
  187. std::vector<S32> mTokenSizeStack;
  188. std::vector<std::string> mScope;
  189. std::vector<bool> mEmptyLeafNode;
  190. element_start_callback_t mElementCB;
  191. std::vector<std::pair<LLInitParam::BaseBlock*, S32> > mOutputStack;
  192. };
  193. #endif //LLXUIPARSER_H