comparison deps/Platinum/ThirdParty/Neptune/Source/Core/NptXml.h @ 0:3425707ddbf6

Initial import (hopefully this mercurial stuff works...)
author fraserofthenight
date Mon, 06 Jul 2009 08:06:28 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3425707ddbf6
1 /*****************************************************************
2 |
3 | Neptune - Xml Support
4 |
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
6 | All rights reserved.
7 |
8 | Redistribution and use in source and binary forms, with or without
9 | modification, are permitted provided that the following conditions are met:
10 | * Redistributions of source code must retain the above copyright
11 | notice, this list of conditions and the following disclaimer.
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 | * Neither the name of Axiomatic Systems nor the
16 | names of its contributors may be used to endorse or promote products
17 | derived from this software without specific prior written permission.
18 |
19 | THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY
20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 |
30 ****************************************************************/
31
32 #ifndef _NPT_XML_H_
33 #define _NPT_XML_H_
34
35 /*----------------------------------------------------------------------
36 | includes
37 +---------------------------------------------------------------------*/
38 #include "NptTypes.h"
39 #include "NptList.h"
40 #include "NptStrings.h"
41 #include "NptStreams.h"
42
43 /*----------------------------------------------------------------------
44 | constants
45 +---------------------------------------------------------------------*/
46 const int NPT_ERROR_XML_INVALID_NESTING = NPT_ERROR_BASE_XML - 0;
47 const int NPT_ERROR_XML_TAG_MISMATCH = NPT_ERROR_BASE_XML - 1;
48
49 #define NPT_XML_ANY_NAMESPACE "*"
50 #define NPT_XML_NO_NAMESPACE NULL
51
52 /*----------------------------------------------------------------------
53 | forward declarations
54 +---------------------------------------------------------------------*/
55 class NPT_XmlProcessor;
56
57 /*----------------------------------------------------------------------
58 | NPT_XmlAttribute
59 +---------------------------------------------------------------------*/
60 class NPT_XmlAttribute
61 {
62 public:
63 // methods
64 NPT_XmlAttribute(const char* name, const char* value);
65 NPT_XmlAttribute(const char* prefix, const char* name, const char* value) :
66 m_Prefix(prefix), m_Name(name), m_Value(value) {}
67 const NPT_String& GetPrefix() const { return m_Prefix; }
68 const NPT_String& GetName() const { return m_Name; }
69 const NPT_String& GetValue() const { return m_Value; }
70 void SetValue(const char* value) { m_Value = value; }
71
72 private:
73 // members
74 NPT_String m_Prefix;
75 NPT_String m_Name;
76 NPT_String m_Value;
77
78 NPT_XmlAttribute(const NPT_XmlAttribute& attribute) :
79 m_Prefix(attribute.m_Prefix),
80 m_Name(attribute.m_Name),
81 m_Value(attribute.m_Value) {}
82 NPT_XmlAttribute& operator=(const NPT_XmlAttribute& a);
83
84 // friends
85 friend class NPT_XmlAttributeFinder;
86 friend class NPT_XmlAttributeFinderWithPrefix;
87 };
88
89 /*----------------------------------------------------------------------
90 | NPT_XmlNamespaceMap
91 +---------------------------------------------------------------------*/
92 class NPT_XmlNamespaceMap
93 {
94 public:
95 // destructor
96 ~NPT_XmlNamespaceMap();
97
98 // methods
99 NPT_Result SetNamespaceUri(const char* prefix, const char* uri);
100 const NPT_String* GetNamespaceUri(const char* prefix);
101 const NPT_String* GetNamespacePrefix(const char* uri);
102
103 private:
104 // types
105 class Entry {
106 public:
107 // constructor
108 Entry(const char* prefix, const char* uri) :
109 m_Prefix(prefix), m_Uri(uri) {}
110
111 // members
112 NPT_String m_Prefix;
113 NPT_String m_Uri;
114 };
115
116 // members
117 NPT_List<Entry*> m_Entries;
118
119 // friends
120 friend class NPT_XmlWriter;
121 friend class NPT_XmlNodeWriter;
122 friend class NPT_XmlNodeCanonicalWriter;
123 };
124
125 /*----------------------------------------------------------------------
126 | NPT_XmlNode
127 +---------------------------------------------------------------------*/
128 class NPT_XmlElementNode;
129 class NPT_XmlTextNode;
130 class NPT_XmlNode
131 {
132 public:
133 // types
134 typedef enum {
135 DOCUMENT,
136 ELEMENT,
137 TEXT
138 } Type;
139
140 // methods
141 NPT_XmlNode(Type type) : m_Type(type), m_Parent(NULL) {}
142 virtual ~NPT_XmlNode() {}
143 Type GetType() const { return m_Type; }
144 NPT_XmlNode* GetParent() const { return m_Parent; }
145
146 // type casting
147 virtual NPT_XmlElementNode* AsElementNode() { return NULL; }
148 virtual const NPT_XmlElementNode* AsElementNode() const { return NULL; }
149 virtual NPT_XmlTextNode* AsTextNode() { return NULL; }
150 virtual const NPT_XmlTextNode* AsTextNode() const { return NULL; }
151
152 protected:
153 // methods
154 virtual void SetParent(NPT_XmlNode* parent) { m_Parent = parent; }
155
156 // members
157 Type m_Type;
158 NPT_XmlNode* m_Parent;
159
160 // friends
161 friend class NPT_XmlNodeFinder;
162 friend class NPT_XmlSerializer;
163 friend class NPT_XmlWriter;
164 friend class NPT_XmlElementNode; // to allow access to SetParent()
165 };
166
167 /*----------------------------------------------------------------------
168 | NPT_XmlElementNode
169 +---------------------------------------------------------------------*/
170 class NPT_XmlElementNode : public NPT_XmlNode
171 {
172 public:
173 // methods
174 NPT_XmlElementNode(const char* tag);
175 NPT_XmlElementNode(const char* prefix, const char* tag);
176 virtual ~NPT_XmlElementNode();
177 NPT_List<NPT_XmlNode*>& GetChildren() { return m_Children; }
178 const NPT_List<NPT_XmlNode*>&
179 GetChildren() const { return m_Children; }
180 NPT_XmlElementNode* GetChild(const char* tag,
181 const char* namespc = NPT_XML_NO_NAMESPACE,
182 NPT_Ordinal n=0) const;
183 NPT_Result AddChild(NPT_XmlNode* child);
184 NPT_Result SetAttribute(const char* prefix,
185 const char* name,
186 const char* value);
187 NPT_Result SetAttribute(const char* name,
188 const char* value);
189 NPT_Result AddText(const char* text);
190 NPT_List<NPT_XmlAttribute*>&
191 GetAttributes() { return m_Attributes; }
192 const NPT_List<NPT_XmlAttribute*>&
193 GetAttributes() const { return m_Attributes; }
194 const NPT_String* GetAttribute(const char* name,
195 const char* namespc = NPT_XML_NO_NAMESPACE) const;
196 const NPT_String& GetPrefix() const { return m_Prefix; }
197 const NPT_String& GetTag() const { return m_Tag; }
198 const NPT_String* GetText(NPT_Ordinal n=0) const;
199
200 // bring all the namespace definitions used in this element of one of its descendants
201 // into the namespace map of this element so that it may be serialized as a
202 // standalone element without any prefixes with undefined namespace uris
203 NPT_Result MakeStandalone();
204
205 // namespace methods
206 const NPT_String* GetNamespace() const;
207 NPT_Result SetNamespaceUri(const char* prefix, const char* uri);
208 const NPT_String* GetNamespaceUri(const char* prefix) const;
209 const NPT_String* GetNamespacePrefix(const char* uri) const;
210
211 // type casting
212 NPT_XmlElementNode* AsElementNode() { return this; }
213 const NPT_XmlElementNode* AsElementNode() const { return this; }
214
215 protected:
216 // methods
217 void SetParent(NPT_XmlNode* parent);
218 void SetNamespaceParent(NPT_XmlElementNode* parent);
219 void RelinkNamespaceMaps();
220
221 NPT_Result AddAttribute(const char* name, const char* value);
222
223 // members
224 NPT_String m_Prefix;
225 NPT_String m_Tag;
226 NPT_List<NPT_XmlNode*> m_Children;
227 NPT_List<NPT_XmlAttribute*> m_Attributes;
228 NPT_XmlNamespaceMap* m_NamespaceMap;
229 NPT_XmlElementNode* m_NamespaceParent;
230
231 // friends
232 friend class NPT_XmlTagFinder;
233 friend class NPT_XmlSerializer;
234 friend class NPT_XmlWriter;
235 friend class NPT_XmlNodeWriter;
236 friend class NPT_XmlNodeCanonicalWriter;
237 friend class NPT_XmlParser;
238 friend class NPT_XmlProcessor;
239 friend class NPT_XmlNamespaceCollapser;
240 };
241
242 /*----------------------------------------------------------------------
243 | NPT_XmlTextNode
244 +---------------------------------------------------------------------*/
245 class NPT_XmlTextNode : public NPT_XmlNode
246 {
247 public:
248 // types
249 typedef enum {
250 CHARACTER_DATA,
251 IGNORABLE_WHITESPACE,
252 CDATA_SECTION,
253 ENTITY_REFERENCE,
254 COMMENT
255 } TokenType;
256
257 // constructor
258 NPT_XmlTextNode(TokenType token_type, const char* text);
259
260 // methods
261 const NPT_String& GetString() const { return m_Text; }
262
263 // type casting
264 NPT_XmlTextNode* AsTextNode() { return this; }
265 const NPT_XmlTextNode* AsTextNode() const { return this; }
266
267 private:
268 // members
269 TokenType m_TokenType;
270 NPT_String m_Text;
271 };
272
273 /*----------------------------------------------------------------------
274 | NPT_XmlParser
275 +---------------------------------------------------------------------*/
276 class NPT_XmlParser
277 {
278 public:
279 // methods
280 NPT_XmlParser(bool keep_whitespace = true);
281 virtual ~NPT_XmlParser();
282 virtual NPT_Result Parse(const char* xml,
283 NPT_XmlNode*& tree,
284 bool incremental=false);
285 virtual NPT_Result Parse(const char* xml,
286 NPT_Size size,
287 NPT_XmlNode*& tree,
288 bool incremental=false);
289 virtual NPT_Result Parse(NPT_InputStream& stream,
290 NPT_XmlNode*& tree,
291 bool incremental=false);
292 virtual NPT_Result Parse(NPT_InputStream& stream,
293 NPT_Size& size,
294 NPT_XmlNode*& tree,
295 bool incremental=false);
296
297 protected:
298 // NPT_XmlHandler methods
299 NPT_Result OnStartElement(const char* name);
300 NPT_Result OnElementAttribute(const char* name, const char* value);
301 NPT_Result OnEndElement(const char* name);
302 NPT_Result OnCharacterData(const char* data, unsigned long size);
303 void RemoveIgnorableWhitespace();
304
305 // members
306 NPT_XmlProcessor* m_Processor;
307 NPT_XmlElementNode* m_Tree;
308 NPT_XmlElementNode* m_CurrentElement;
309 bool m_KeepWhitespace;
310
311 // friends
312 friend class NPT_XmlProcessor;
313 };
314
315 /*----------------------------------------------------------------------
316 | NPT_XmlSerializer
317 +---------------------------------------------------------------------*/
318 class NPT_XmlSerializer
319 {
320 public:
321 // methods
322 NPT_XmlSerializer(NPT_OutputStream* output,
323 NPT_Cardinal indentation = 0,
324 bool shrink_empty_elements = true);
325 virtual ~NPT_XmlSerializer();
326 virtual NPT_Result StartDocument();
327 virtual NPT_Result EndDocument();
328 virtual NPT_Result StartElement(const char* prefix, const char* name);
329 virtual NPT_Result EndElement(const char* prefix, const char* name);
330 virtual NPT_Result Attribute(const char* prefix, const char* name, const char* value);
331 virtual NPT_Result Text(const char* text);
332 virtual NPT_Result CdataSection(const char* data);
333 virtual NPT_Result Comment(const char* comment);
334
335 protected:
336 // methods
337 void EscapeChar(unsigned char c, char* text);
338 NPT_Result ProcessPending();
339 NPT_Result OutputEscapedString(const char* text, bool attribute);
340 void OutputIndentation(bool start);
341
342 // members
343 NPT_OutputStream* m_Output;
344 bool m_ElementPending;
345 NPT_Cardinal m_Depth;
346 NPT_Cardinal m_Indentation;
347 NPT_String m_IndentationPrefix;
348 bool m_ElementHasText;
349 bool m_ShrinkEmptyElements;
350 };
351
352 /*----------------------------------------------------------------------
353 | NPT_XmlWriter
354 +---------------------------------------------------------------------*/
355 class NPT_XmlWriter
356 {
357 public:
358 // constructor
359 explicit NPT_XmlWriter(NPT_Cardinal indentation = 0) : m_Indentation(indentation) {}
360
361 // methods
362 NPT_Result Serialize(NPT_XmlNode& node, NPT_OutputStream& stream);
363
364 private:
365 // members
366 NPT_Cardinal m_Indentation;
367 };
368
369 /*----------------------------------------------------------------------
370 | NPT_XmlCanonicalizer
371 +---------------------------------------------------------------------*/
372 class NPT_XmlCanonicalizer
373 {
374 public:
375 // methods
376 NPT_Result Serialize(NPT_XmlNode& node, NPT_OutputStream& stream);
377 };
378
379 #endif // _NPT_XML_H_