kodi
NptXml.h
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 const int NPT_ERROR_XML_NO_ROOT = NPT_ERROR_BASE_XML - 2;
49 const int NPT_ERROR_XML_MULTIPLE_ROOTS = NPT_ERROR_BASE_XML - 3;
50 
51 #define NPT_XML_ANY_NAMESPACE "*"
52 #define NPT_XML_NO_NAMESPACE NULL
53 
54 /*----------------------------------------------------------------------
55 | forward declarations
56 +---------------------------------------------------------------------*/
57 class NPT_XmlProcessor;
58 
59 /*----------------------------------------------------------------------
60 | NPT_XmlAttribute
61 +---------------------------------------------------------------------*/
63 {
64  public:
65  // methods
66  NPT_XmlAttribute(const char* name, const char* value);
67  NPT_XmlAttribute(const char* prefix, const char* name, const char* value) :
68  m_Prefix(prefix), m_Name(name), m_Value(value) {}
69  const NPT_String& GetPrefix() const { return m_Prefix; }
70  const NPT_String& GetName() const { return m_Name; }
71  const NPT_String& GetValue() const { return m_Value; }
72  void SetValue(const char* value) { m_Value = value; }
73 
74  private:
75  // members
76  NPT_String m_Prefix;
77  NPT_String m_Name;
78  NPT_String m_Value;
79 
80  NPT_XmlAttribute(const NPT_XmlAttribute& attribute) :
81  m_Prefix(attribute.m_Prefix),
82  m_Name(attribute.m_Name),
83  m_Value(attribute.m_Value) {}
84  NPT_XmlAttribute& operator=(const NPT_XmlAttribute& a);
85 
86  // friends
87  friend class NPT_XmlAttributeFinder;
89 };
90 
91 /*----------------------------------------------------------------------
92 | NPT_XmlNamespaceMap
93 +---------------------------------------------------------------------*/
95 {
96 public:
97  // destructor
99 
100  // methods
101  NPT_Result SetNamespaceUri(const char* prefix, const char* uri);
102  const NPT_String* GetNamespaceUri(const char* prefix);
103  const NPT_String* GetNamespacePrefix(const char* uri);
104 
105 private:
106  // types
107  class Entry {
108  public:
109  // constructor
110  Entry(const char* prefix, const char* uri) :
111  m_Prefix(prefix), m_Uri(uri) {}
112 
113  // members
114  NPT_String m_Prefix;
115  NPT_String m_Uri;
116  };
117 
118  // members
119  NPT_List<Entry*> m_Entries;
120 
121  // friends
122  friend class NPT_XmlWriter;
123  friend class NPT_XmlNodeWriter;
124  friend class NPT_XmlNodeCanonicalWriter;
125 };
126 
127 /*----------------------------------------------------------------------
128 | NPT_XmlNode
129 +---------------------------------------------------------------------*/
130 class NPT_XmlElementNode;
131 class NPT_XmlTextNode;
133 {
134  public:
135  // types
136  typedef enum {
137  DOCUMENT,
138  ELEMENT,
139  TEXT
140  } Type;
141 
142  // methods
143  NPT_XmlNode(Type type) : m_Type(type), m_Parent(NULL) {}
144  virtual ~NPT_XmlNode() {}
145  Type GetType() const { return m_Type; }
146  NPT_XmlNode* GetParent() const { return m_Parent; }
147 
148  // type casting
149  virtual NPT_XmlElementNode* AsElementNode() { return NULL; }
150  virtual const NPT_XmlElementNode* AsElementNode() const { return NULL; }
151  virtual NPT_XmlTextNode* AsTextNode() { return NULL; }
152  virtual const NPT_XmlTextNode* AsTextNode() const { return NULL; }
153 
154  protected:
155  // methods
156  virtual void SetParent(NPT_XmlNode* parent) { m_Parent = parent; }
157 
158  // members
159  Type m_Type;
160  NPT_XmlNode* m_Parent;
161 
162  // friends
163  friend class NPT_XmlNodeFinder;
164  friend class NPT_XmlSerializer;
165  friend class NPT_XmlWriter;
166  friend class NPT_XmlElementNode; // to allow access to SetParent()
167 };
168 
169 /*----------------------------------------------------------------------
170 | NPT_XmlElementNode
171 +---------------------------------------------------------------------*/
173 {
174  public:
175  // methods
176  NPT_XmlElementNode(const char* tag);
177  NPT_XmlElementNode(const char* prefix, const char* tag);
178  ~NPT_XmlElementNode() override;
179  NPT_List<NPT_XmlNode*>& GetChildren() { return m_Children; }
180  const NPT_List<NPT_XmlNode*>&
181  GetChildren() const { return m_Children; }
182  NPT_XmlElementNode* GetChild(const char* tag,
183  const char* namespc = NPT_XML_NO_NAMESPACE,
184  NPT_Ordinal n=0) const;
185  NPT_Result AddChild(NPT_XmlNode* child);
186  NPT_Result SetAttribute(const char* prefix,
187  const char* name,
188  const char* value);
189  NPT_Result SetAttribute(const char* name,
190  const char* value);
191  NPT_Result AddText(const char* text);
193  GetAttributes() { return m_Attributes; }
195  GetAttributes() const { return m_Attributes; }
196  const NPT_String* GetAttribute(const char* name,
197  const char* namespc = NPT_XML_NO_NAMESPACE) const;
198  const NPT_String& GetPrefix() const { return m_Prefix; }
199  const NPT_String& GetTag() const { return m_Tag; }
200  const NPT_String* GetText(NPT_Ordinal n=0) const;
201 
202  // bring all the namespace definitions used in this element of one of its descendants
203  // into the namespace map of this element so that it may be serialized as a
204  // standalone element without any prefixes with undefined namespace uris
205  NPT_Result MakeStandalone();
206 
207  // namespace methods
208  const NPT_String* GetNamespace() const;
209  NPT_Result SetNamespaceUri(const char* prefix, const char* uri);
210  const NPT_String* GetNamespaceUri(const char* prefix) const;
211  const NPT_String* GetNamespacePrefix(const char* uri) const;
212 
213  // type casting
214  NPT_XmlElementNode* AsElementNode() override { return this; }
215  const NPT_XmlElementNode* AsElementNode() const override { return this; }
216 
217 protected:
218  // methods
219  void SetParent(NPT_XmlNode* parent) override;
220  void SetNamespaceParent(NPT_XmlElementNode* parent);
221  void RelinkNamespaceMaps();
222 
223  NPT_Result AddAttribute(const char* name, const char* value);
224 
225  // members
226  NPT_String m_Prefix;
227  NPT_String m_Tag;
228  NPT_List<NPT_XmlNode*> m_Children;
229  NPT_List<NPT_XmlAttribute*> m_Attributes;
230  NPT_XmlNamespaceMap* m_NamespaceMap;
231  NPT_XmlElementNode* m_NamespaceParent;
232 
233  // friends
234  friend class NPT_XmlTagFinder;
235  friend class NPT_XmlSerializer;
236  friend class NPT_XmlWriter;
237  friend class NPT_XmlNodeWriter;
238  friend class NPT_XmlNodeCanonicalWriter;
239  friend class NPT_XmlParser;
240  friend class NPT_XmlProcessor;
241  friend class NPT_XmlNamespaceCollapser;
242 };
243 
244 /*----------------------------------------------------------------------
245 | NPT_XmlTextNode
246 +---------------------------------------------------------------------*/
248 {
249  public:
250  // types
251  typedef enum {
252  CHARACTER_DATA,
253  IGNORABLE_WHITESPACE,
254  CDATA_SECTION,
255  ENTITY_REFERENCE,
256  COMMENT
257  } TokenType;
258 
259  // constructor
260  NPT_XmlTextNode(TokenType token_type, const char* text);
261 
262  // methods
263  const NPT_String& GetString() const { return m_Text; }
264  TokenType GetTokenType() const { return m_TokenType; }
265 
266  // type casting
267  NPT_XmlTextNode* AsTextNode() override { return this; }
268  const NPT_XmlTextNode* AsTextNode() const override { return this; }
269 
270  private:
271  // members
272  TokenType m_TokenType;
273  NPT_String m_Text;
274 };
275 
276 /*----------------------------------------------------------------------
277 | NPT_XmlParser
278 +---------------------------------------------------------------------*/
280 {
281  public:
282  // methods
283  NPT_XmlParser(bool keep_whitespace = true);
284  virtual ~NPT_XmlParser();
285  virtual NPT_Result Parse(const char* xml,
286  NPT_XmlNode*& tree,
287  bool incremental=false);
288  virtual NPT_Result Parse(const char* xml,
289  NPT_Size size,
290  NPT_XmlNode*& tree,
291  bool incremental=false);
292  virtual NPT_Result Parse(NPT_InputStream& stream,
293  NPT_XmlNode*& tree,
294  bool incremental=false);
295  virtual NPT_Result Parse(NPT_InputStream& stream,
296  NPT_Size& size,
297  NPT_XmlNode*& tree,
298  bool incremental=false);
299 
300  protected:
301  // NPT_XmlHandler methods
302  NPT_Result OnStartElement(const char* name);
303  NPT_Result OnElementAttribute(const char* name, const char* value);
304  NPT_Result OnEndElement(const char* name);
305  NPT_Result OnCharacterData(const char* data, NPT_Size size);
306  void RemoveIgnorableWhitespace();
307 
308  // members
309  NPT_XmlProcessor* m_Processor;
310  NPT_XmlElementNode* m_Root;
311  NPT_XmlElementNode* m_CurrentElement;
312  bool m_KeepWhitespace;
313 
314 private:
315  void Reset();
316 
317  // friends
318  friend class NPT_XmlProcessor;
319 };
320 
321 /*----------------------------------------------------------------------
322 | NPT_XmlSerializer
323 +---------------------------------------------------------------------*/
325 {
326 public:
327  // methods
329  NPT_Cardinal indentation = 0,
330  bool shrink_empty_elements = true,
331  bool add_xml_decl = false);
332  virtual ~NPT_XmlSerializer();
333  virtual NPT_Result StartDocument();
334  virtual NPT_Result EndDocument();
335  virtual NPT_Result StartElement(const char* prefix, const char* name);
336  virtual NPT_Result EndElement(const char* prefix, const char* name);
337  virtual NPT_Result Attribute(const char* prefix, const char* name, const char* value);
338  virtual NPT_Result Text(const char* text);
339  virtual NPT_Result CdataSection(const char* data);
340  virtual NPT_Result Comment(const char* comment);
341 
342 protected:
343  // methods
344  void EscapeChar(unsigned char c, char* text);
345  NPT_Result ProcessPending();
346  NPT_Result OutputEscapedString(const char* text, bool attribute);
347  void OutputIndentation(bool start);
348 
349  // members
350  NPT_OutputStream* m_Output;
351  bool m_ElementPending;
352  NPT_Cardinal m_Depth;
353  NPT_Cardinal m_Indentation;
354  NPT_String m_IndentationPrefix;
355  bool m_ElementHasText;
356  bool m_ShrinkEmptyElements;
357  bool m_AddXmlDecl;
358 };
359 
360 /*----------------------------------------------------------------------
361 | NPT_XmlWriter
362 +---------------------------------------------------------------------*/
364 {
365 public:
366  // constructor
367  explicit NPT_XmlWriter(NPT_Cardinal indentation = 0) : m_Indentation(indentation) {}
368 
369  // methods
370  NPT_Result Serialize(NPT_XmlNode& node,
371  NPT_OutputStream& stream,
372  bool add_xml_decl = false);
373 
374 private:
375  // members
376  NPT_Cardinal m_Indentation;
377 };
378 
379 /*----------------------------------------------------------------------
380 | NPT_XmlCanonicalizer
381 +---------------------------------------------------------------------*/
383 {
384 public:
385  // methods
386  NPT_Result Serialize(NPT_XmlNode& node,
387  NPT_OutputStream& stream,
388  bool add_xml_decl = false);
389 };
390 
391 #endif // _NPT_XML_H_
Definition: NptXml.h:172
Definition: NptXml.cpp:116
Definition: NptXml.cpp:2062
Definition: NptStreams.h:60
Definition: NptXml.cpp:2016
Definition: NptXml.cpp:186
Definition: NptXml.cpp:1096
Definition: NptXml.h:247
Definition: NptXml.h:324
Definition: NptStreams.h:92
Definition: NptXml.h:363
Definition: NptXml.h:132
Definition: NptXml.h:279
Definition: NptXml.cpp:69
Definition: NptXml.h:382
Definition: NptXml.h:62
Definition: NptXml.cpp:134
Definition: NptXml.h:94
Definition: NptStrings.h:57
Definition: NptList.h:54