PageRenderTime 36ms CodeModel.GetById 1ms app.highlight 32ms RepoModel.GetById 0ms app.codeStats 1ms

/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
  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
 27#ifndef LLXUIPARSER_H
 28#define LLXUIPARSER_H
 29
 30#include "llinitparam.h"
 31#include "llregistry.h"
 32#include "llpointer.h"
 33
 34#include <boost/function.hpp>
 35#include <iosfwd>
 36#include <stack>
 37#include <set>
 38
 39
 40
 41class LLView;
 42
 43
 44typedef LLPointer<class LLXMLNode> LLXMLNodePtr;
 45
 46
 47// lookup widget type by name
 48class LLWidgetTypeRegistry
 49:	public LLRegistrySingleton<std::string, const std::type_info*, LLWidgetTypeRegistry>
 50{};
 51
 52
 53// global static instance for registering all widget types
 54typedef boost::function<LLView* (LLXMLNodePtr node, LLView *parent, LLXMLNodePtr output_node)> LLWidgetCreatorFunc;
 55
 56typedef LLRegistry<std::string, LLWidgetCreatorFunc> widget_registry_t;
 57
 58class LLChildRegistryRegistry
 59: public LLRegistrySingleton<const std::type_info*, widget_registry_t, LLChildRegistryRegistry>
 60{};
 61
 62
 63
 64class LLXSDWriter : public LLInitParam::Parser
 65{
 66	LOG_CLASS(LLXSDWriter);
 67public:
 68	void writeXSD(const std::string& name, LLXMLNodePtr node, const LLInitParam::BaseBlock& block, const std::string& xml_namespace);
 69
 70	/*virtual*/ std::string getCurrentElementName() { return LLStringUtil::null; }
 71
 72	LLXSDWriter();
 73
 74protected:
 75	void writeAttribute(const std::string& type, const Parser::name_stack_t&, S32 min_count, S32 max_count, const std::vector<std::string>* possible_values);
 76	void addAttributeToSchema(LLXMLNodePtr nodep, const std::string& attribute_name, const std::string& type, bool mandatory, const std::vector<std::string>* possible_values);
 77	LLXMLNodePtr mAttributeNode;
 78	LLXMLNodePtr mElementNode;
 79	LLXMLNodePtr mSchemaNode;
 80
 81	typedef std::set<std::string> string_set_t;
 82	typedef std::map<LLXMLNodePtr, string_set_t> attributes_map_t;
 83	attributes_map_t	mAttributesWritten;
 84};
 85
 86
 87
 88// NOTE: DOES NOT WORK YET
 89// should support child widgets for XUI
 90class LLXUIXSDWriter : public LLXSDWriter
 91{
 92public:
 93	void writeXSD(const std::string& name, const std::string& path, const LLInitParam::BaseBlock& block);
 94};
 95
 96
 97class LLXUIParserImpl;
 98
 99class LLXUIParser : public LLInitParam::Parser
100{
101LOG_CLASS(LLXUIParser);
102
103public:
104	LLXUIParser();
105	typedef LLInitParam::Parser::name_stack_t name_stack_t;
106
107	/*virtual*/ std::string getCurrentElementName();
108	/*virtual*/ void parserWarning(const std::string& message);
109	/*virtual*/ void parserError(const std::string& message);
110
111	void readXUI(LLXMLNodePtr node, LLInitParam::BaseBlock& block, const std::string& filename = LLStringUtil::null, bool silent=false);
112	void writeXUI(LLXMLNodePtr node, const LLInitParam::BaseBlock& block, const LLInitParam::BaseBlock* diff_block = NULL);
113
114private:
115	bool readXUIImpl(LLXMLNodePtr node, LLInitParam::BaseBlock& block);
116	bool readAttributes(LLXMLNodePtr nodep, LLInitParam::BaseBlock& block);
117
118	//reader helper functions
119	static bool readFlag(Parser& parser, void* val_ptr);
120	static bool readBoolValue(Parser& parser, void* val_ptr);
121	static bool readStringValue(Parser& parser, void* val_ptr);
122	static bool readU8Value(Parser& parser, void* val_ptr);
123	static bool readS8Value(Parser& parser, void* val_ptr);
124	static bool readU16Value(Parser& parser, void* val_ptr);
125	static bool readS16Value(Parser& parser, void* val_ptr);
126	static bool readU32Value(Parser& parser, void* val_ptr);
127	static bool readS32Value(Parser& parser, void* val_ptr);
128	static bool readF32Value(Parser& parser, void* val_ptr);
129	static bool readF64Value(Parser& parser, void* val_ptr);
130	static bool readColor4Value(Parser& parser, void* val_ptr);
131	static bool readUIColorValue(Parser& parser, void* val_ptr);
132	static bool readUUIDValue(Parser& parser, void* val_ptr);
133	static bool readSDValue(Parser& parser, void* val_ptr);
134
135	//writer helper functions
136	static bool writeFlag(Parser& parser, const void* val_ptr, name_stack_t&);
137	static bool writeBoolValue(Parser& parser, const void* val_ptr, name_stack_t&);
138	static bool writeStringValue(Parser& parser, const void* val_ptr, name_stack_t&);
139	static bool writeU8Value(Parser& parser, const void* val_ptr, name_stack_t&);
140	static bool writeS8Value(Parser& parser, const void* val_ptr, name_stack_t&);
141	static bool writeU16Value(Parser& parser, const void* val_ptr, name_stack_t&);
142	static bool writeS16Value(Parser& parser, const void* val_ptr, name_stack_t&);
143	static bool writeU32Value(Parser& parser, const void* val_ptr, name_stack_t&);
144	static bool writeS32Value(Parser& parser, const void* val_ptr, name_stack_t&);
145	static bool writeF32Value(Parser& parser, const void* val_ptr, name_stack_t&);
146	static bool writeF64Value(Parser& parser, const void* val_ptr, name_stack_t&);
147	static bool writeColor4Value(Parser& parser, const void* val_ptr, name_stack_t&);
148	static bool writeUIColorValue(Parser& parser, const void* val_ptr, name_stack_t&);
149	static bool writeUUIDValue(Parser& parser, const void* val_ptr, name_stack_t&);
150	static bool writeSDValue(Parser& parser, const void* val_ptr, name_stack_t&);
151
152	LLXMLNodePtr getNode(name_stack_t& stack);
153
154private:
155	Parser::name_stack_t			mNameStack;
156	LLXMLNodePtr					mCurReadNode;
157	// Root of the widget XML sub-tree, for example, "line_editor"
158	LLXMLNodePtr					mWriteRootNode;
159	
160	typedef std::map<std::string, LLXMLNodePtr>	out_nodes_t;
161	out_nodes_t						mOutNodes;
162	LLXMLNodePtr					mLastWrittenChild;
163	S32								mCurReadDepth;
164	std::string						mCurFileName;
165	std::string						mRootNodeName;
166};
167
168// LLSimpleXUIParser is a streamlined SAX-based XUI parser that does not support localization 
169// or parsing of a tree of independent param blocks, such as child widgets.
170// Use this for reading non-localized files that only need a single param block as a result.
171//
172// NOTE: In order to support nested block parsing, we need callbacks for start element that
173// push new blocks contexts on the mScope stack.
174// NOTE: To support localization without building a DOM, we need to enforce consistent 
175// ordering of child elements from base file to localized diff file.  Then we can use a pair
176// of coroutines to perform matching of xml nodes during parsing.  Not sure if the overhead
177// of coroutines would offset the gain from SAX parsing
178class LLSimpleXUIParserImpl;
179
180class LLSimpleXUIParser : public LLInitParam::Parser
181{
182LOG_CLASS(LLSimpleXUIParser);
183public:
184	typedef LLInitParam::Parser::name_stack_t name_stack_t;
185	typedef LLInitParam::BaseBlock* (*element_start_callback_t)(LLSimpleXUIParser&, const char* block_name);
186
187	LLSimpleXUIParser(element_start_callback_t element_cb = NULL);
188	virtual ~LLSimpleXUIParser();
189
190	/*virtual*/ std::string getCurrentElementName();
191	/*virtual*/ void parserWarning(const std::string& message);
192	/*virtual*/ void parserError(const std::string& message);
193
194	bool readXUI(const std::string& filename, LLInitParam::BaseBlock& block, bool silent=false);
195
196
197private:
198	//reader helper functions
199	static bool readFlag(Parser&, void* val_ptr);
200	static bool readBoolValue(Parser&, void* val_ptr);
201	static bool readStringValue(Parser&, void* val_ptr);
202	static bool readU8Value(Parser&, void* val_ptr);
203	static bool readS8Value(Parser&, void* val_ptr);
204	static bool readU16Value(Parser&, void* val_ptr);
205	static bool readS16Value(Parser&, void* val_ptr);
206	static bool readU32Value(Parser&, void* val_ptr);
207	static bool readS32Value(Parser&, void* val_ptr);
208	static bool readF32Value(Parser&, void* val_ptr);
209	static bool readF64Value(Parser&, void* val_ptr);
210	static bool readColor4Value(Parser&, void* val_ptr);
211	static bool readUIColorValue(Parser&, void* val_ptr);
212	static bool readUUIDValue(Parser&, void* val_ptr);
213	static bool readSDValue(Parser&, void* val_ptr);
214
215private:
216	static void startElementHandler(void *userData, const char *name, const char **atts);
217	static void endElementHandler(void *userData, const char *name);
218	static void characterDataHandler(void *userData, const char *s, int len);
219
220	void startElement(const char *name, const char **atts);
221	void endElement(const char *name);
222	void characterData(const char *s, int len);
223	bool readAttributes(const char **atts);
224	bool processText();
225
226	Parser::name_stack_t			mNameStack;
227	struct XML_ParserStruct*		mParser;
228	LLXMLNodePtr					mLastWrittenChild;
229	S32								mCurReadDepth;
230	std::string						mCurFileName;
231	std::string						mTextContents;
232	const char*						mCurAttributeValueBegin;
233	std::vector<S32>				mTokenSizeStack;
234	std::vector<std::string>		mScope;
235	std::vector<bool>				mEmptyLeafNode;
236	element_start_callback_t		mElementCB;
237
238	std::vector<std::pair<LLInitParam::BaseBlock*, S32> > mOutputStack;
239};
240
241
242#endif //LLXUIPARSER_H