Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cXmlDocument.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cXmlDocument_H
7 #define _INC_cXmlDocument_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cXml.h"
13 #include "GrayCore/include/CList.h"
17 
18 namespace GrayLib
19 {
20  class cXmlContainer;
21  class cXmlReader;
22  struct IXmlVisitor; // OnVisit
23 
24  // forward declare XMLNODE_TYPE types.
25 #define cXmlNodeTypeDEF(a,b,c) class cXml##a;
26 #include "cXmlNodeTypes.tbl"
27 #undef cXmlNodeTypeDEF
28 
29  class GRAYLIB_LINK cXmlNode : public cListNodeT<cXmlNode>, public cXml, protected cNonCopyable
30  {
54 
55  protected:
58  void* m_pUserData;
59 
60  public:
62 
63  protected:
65  cXmlNode(XMLNODE_TYPE eNodeType, cXmlString sValue = "");
66 
69  virtual void SetCopy(const cXmlNode& n);
70 
71  public:
72  virtual ~cXmlNode();
73 
75  {
77  return m_eNodeType;
78  }
79  bool isType(XMLNODE_TYPE eType) const
80  {
81  return m_eNodeType == eType;
82  }
83 
84  void put_UserData(void* pUserData)
85  {
87  m_pUserData = pUserData;
88  }
89  void* get_UserData() const
90  {
92  return m_pUserData;
93  }
94 
96  virtual void SetClear();
97 
98  void put_Value(cXmlString sValue)
99  {
101  m_sValue = sValue;
102  }
103  const cXmlString& get_Value() const
104  {
106  return m_sValue;
107  }
108 
117  HRESULT QueryValueInt(int* piValue) const;
118 
120  HRESULT QueryValueDouble(double* pdValue) const;
121 
122  int get_ValueInt() const;
123  double get_ValueDouble() const;
124 
125  void put_ValueInt(int iValue);
126  void put_ValueDouble(double dValue);
127 
130  cXmlDocument* get_Document() const;
131 
133  cXmlNode* GetPreviousSibling(const char* pszValue) const;
134 
136  cXmlNode* GetNextSiblingX(const char* pszValue) const;
137 
141  cXmlElement* get_NextSiblingElement() const;
142 
146  cXmlElement* GetNextSiblingElement(const char* pszValue) const;
147 
149  {
151  return nullptr;
152  }
153 
154 #if 1
156 #define cXmlNodeTypeIMPL(a,b) \
157  virtual cXml##a* To##a() { return b; } \
158  virtual const cXml##a* To##a() const { return b; }
159 
160 #define cXmlNodeTypeDEF(a,b,c) cXmlNodeTypeIMPL(a,nullptr)
161 #include "cXmlNodeTypes.tbl"
162 #undef cXmlNodeTypeDEF
163 
164 #else
165 #define cXmlNodeTypeIMPL(a,b)
166 #endif
167 
170  virtual cXmlNode* Clone() const = 0;
171 
172  virtual HRESULT SetParseNode(cXmlReader& rReader) = 0;
173  HRESULT SetParse(const char* pszData, CXML_ENCODING_TYPE eEncoding = CXML_ENCODING_UNKNOWN);
174 
183  virtual HRESULT Print(cStreamOutput& rOut, int nDepth = 0) const = 0;
184  static HRESULT PrintDepth(cStreamOutput& rOut, int nDepth);
185 
206  //
207  virtual bool OnVisit(IXmlVisitor* pVisitor) const = 0;
208 
226 
228  {
229  return m_Pos.get_Line1();
230  }
232  {
233  return m_Pos.get_Column1();
234  }
235  };
236 
238  {
241 
242  public:
243  cXmlComment() : cXmlNode(cXmlNode::XMLNODE_Comment)
244  {
246  }
247  cXmlComment(cXmlString sValue) : cXmlNode(cXmlNode::XMLNODE_Comment, sValue)
248  {
250  }
251  cXmlComment(const cXmlComment&);
252 
253  void operator=(const cXmlComment& rCopy)
254  {
255  SetClear();
256  SetCopy(rCopy);
257  }
258 
259  virtual ~cXmlComment()
260  {}
261 
263  virtual cXmlNode* Clone() const;
264 
265  // Attribute parsing starts: at the ! of the !--
266  // returns: next char past '>'
267  virtual HRESULT SetParseNode(cXmlReader& rReader);
268 
269  // Write this Comment to a cStreamOutput stream.
270  virtual HRESULT Print(cStreamOutput& rOut, int nDepth = 0) const;
271 
273  virtual bool OnVisit(IXmlVisitor* visitor) const;
274 
275  cXmlNodeTypeIMPL(Comment, this);
276  };
277 
279  {
285 
286  friend class cXmlElement;
287  typedef cXmlNode SUPER_t;
288 
289  private:
290  bool m_bCDATA;
291 
292  protected:
293  virtual void SetCopy(const cXmlText& n);
294  virtual void SetClear()
295  {
296  m_bCDATA = false;
297  SUPER_t::SetClear();
298  }
299 
300  // [internal use]
301  bool isBlank() const;
302 
303  public:
304  cXmlText(cXmlString sValue = "", bool bCDATA = false)
305  : cXmlNode(cXmlNode::XMLNODE_Text, sValue)
306  , m_bCDATA(bCDATA)
307  {
311  }
312  virtual ~cXmlText()
313  {}
314 
315  cXmlText(const cXmlText& rCopy)
316  : cXmlNode(cXmlNode::XMLNODE_Text)
317  {
318  SetCopy(rCopy);
319  }
320 
321  void operator=(const cXmlText& rCopy)
322  {
323  SetClear();
324  SetCopy(rCopy);
325  }
326 
327  bool isCDATA() const
328  {
330  return m_bCDATA;
331  }
332  void put_CDATA(bool bCDATA)
333  {
335  m_bCDATA = bCDATA;
336  }
337 
339  virtual cXmlNode* Clone() const;
340 
341  virtual HRESULT SetParseNode(cXmlReader& rReader);
342 
344  virtual HRESULT Print(cStreamOutput& rOut, int nDepth = 0) const;
345 
347  virtual bool OnVisit(IXmlVisitor* content) const;
348 
349  cXmlNodeTypeIMPL(Text, this);
350  };
351 
353  {
368 
369  typedef cXmlNode SUPER_t;
370 
371  public:
372  static const char k_pszVersion[];
373  static const char k_pszEncoding[];
374  static const char k_pszStandalone[];
375 
376  private:
377  cXmlString m_sEncoding;
378  cXmlString m_sStandalone;
379 
380  protected:
381  virtual void SetCopy(const cXmlDeclaration& n);
382  virtual void SetClear()
383  {
384  m_sEncoding = "";
385  m_sStandalone = "";
386  SUPER_t::SetClear();
387  }
388 
389  public:
391  cXmlDeclaration(cXmlString sVersion = "1.0", cXmlString sEncoding = "", cXmlString sStandalone = "");
392 
393  cXmlDeclaration(const cXmlDeclaration& rCopy);
394  void operator=(const cXmlDeclaration& rCopy)
395  {
396  SetClear();
397  SetCopy(rCopy);
398  }
399 
401  {}
402 
403  const cXmlString& get_Version() const
404  {
406  return m_sValue;
407  }
408  const cXmlString& get_Encoding() const
409  {
411  return m_sEncoding;
412  }
413  const cXmlString& get_Standalone() const
414  {
416  return m_sStandalone;
417  }
418 
419  CXML_ENCODING_TYPE get_EncodingEnum() const;
420 
422  virtual cXmlNode* Clone() const;
423 
424  virtual HRESULT SetParseNode(cXmlReader& rReader);
425 
427  cXmlString get_PrintStr() const;
428  virtual HRESULT Print(cStreamOutput& rOut, int nDepth = 0) const
429  {
430  UNREFERENCED_PARAMETER(nDepth);
431  return rOut.WriteString(get_PrintStr());
432  }
433 
435  virtual bool OnVisit(IXmlVisitor* visitor) const;
436 
437  cXmlNodeTypeIMPL(Declaration, this);
438  };
439 
441  {
448 
449  public:
450  cXmlUnknown(cXmlString sValue = "") : cXmlNode(cXmlNode::XMLNODE_Unknown, sValue)
451  {}
452  virtual ~cXmlUnknown()
453  {}
454 
455  cXmlUnknown(const cXmlUnknown& rCopy) : cXmlNode(cXmlNode::XMLNODE_Unknown)
456  {
457  SetCopy(rCopy);
458  }
459  void operator=(const cXmlUnknown& rCopy)
460  {
461  SetClear();
462  SetCopy(rCopy);
463  }
464 
466  virtual cXmlNode* Clone() const;
467 
468  virtual HRESULT SetParseNode(cXmlReader& rReader);
469 
471  virtual HRESULT Print(cStreamOutput& rOut, int nDepth = 0) const;
472 
474  virtual bool OnVisit(IXmlVisitor* content) const;
475 
476  cXmlNodeTypeIMPL(Unknown, this);
477  };
478 
479  class GRAYLIB_LINK cXmlContainer : public cListT<cXmlNode>, public cXml
480  {
483 
484  public:
485  virtual ~cXmlContainer()
486  {
487  DisposeAll();
488  }
489 
491  virtual cXmlNode* ToNode() = 0;
492 
493  cXmlNode* GetChildFirst(const char* value) const;
494  cXmlNode* GetChildLast(const char* value) const;
495 
511 
513  cXmlNode* IterateChildren(const char* value, const cXmlNode* previous) const;
514 
524  void SetCopyChildren(const cXmlContainer& cont);
525 
528  cXmlNode* InsertChildEnd(const cXmlNode& addThis);
529 
532  cXmlNode* InsertChildAfter(cXmlNode* afterThis, const cXmlNode& addThis);
533 
536  cXmlNode* InsertChildBefore(cXmlNode* beforeThis, const cXmlNode& addThis);
537 
539  bool RemoveChild(cXmlNode* removeThis);
540 
542  cXmlElement* get_FirstChildElement() const;
543 
545  cXmlElement* GetFirstChildElement(const char* pszValue) const;
546  };
547 
549  {
554 
555  typedef cXmlNode SUPER_t;
556 
557  public:
559 
560  protected:
561  virtual void SetCopy(const cXmlElement& n);
562  virtual void SetClear();
563 
564  public:
565  cXmlElement(cXmlString sValue);
566  cXmlElement(const cXmlElement& rCopy);
567  virtual ~cXmlElement();
568 
569  void operator=(const cXmlElement& rCopy)
570  {
571  SetClear();
572  SetCopy(rCopy);
573  }
574 
575  // Convenience function for easy access to the text inside an element. Although easy
576  // and concise, GetText() is limited compared to getting the cXmlText child
577  // and accessing it directly.
578  //
579  // If the first child of 'this' is a cXmlText, the GetText()
580  // returns the character string of the Text node, else null is returned.
581  //
582  // This is a convenient method for getting the text of simple contained text:
583  //@verbatim
584  //<foo>This is text</foo>
585  // const char* str = fooElement->GetText();
586  //@endverbatim
587  //
588  //'str' will be a pointer to "This is text".
589  //
590  // Note that this function can be misleading. If the element foo was created from
591  // this XML:
592  //@verbatim
593  //<foo><b>This is text</b></foo>
594  //@endverbatim
595  //
596  // then the value of str would be null. The first child node isn't a text node, it is
597  // another element. From this XML:
598  //@verbatim
599  //<foo>This is <b>text</b></foo>
600  //@endverbatim
601  // GetText() will return "This is ".
602  //
603  // WARNING: GetText() accesses a child node - don't become confused with the
604  // similarly named cXmlHandle::Text() and cXmlNode::ToText() which are
605  // safe type casts on the referenced node.
606  //
607  const char* get_Text() const;
608 
610  virtual cXmlNode* Clone() const;
611 
614  virtual HRESULT SetParseNode(cXmlReader& rReader);
615 
617  virtual HRESULT Print(cStreamOutput& rOut, int nDepth = 0) const;
618 
620  virtual bool OnVisit(IXmlVisitor* visitor) const;
621 
623  {
624  return this;
625  }
626  virtual cXmlNode* ToNode()
627  {
629  return this;
630  }
631  cXmlNodeTypeIMPL(Element, this);
632  };
633 
635  {
641 
642  typedef cXmlNode SUPER_t;
643 
644  private:
645  bool m_bUseMicrosoftBOM;
646 
647  private:
648  virtual void SetCopy(const cXmlDocument& n);
649 
650  public:
652  cXmlDocument();
653  cXmlDocument(const cXmlDocument& rCopy);
654 
655  virtual ~cXmlDocument()
656  {}
657 
658  void operator=(const cXmlDocument& rCopy)
659  {
660  SetClear();
661  SetCopy(rCopy);
662  }
663 
668  HRESULT LoadFile(cStreamInput& stmIn);
670  HRESULT LoadFile(const FILECHAR_t* pszFilename);
671 
673  HRESULT SaveFile(cStreamOutput& rOut) const;
675  HRESULT SaveFile(const FILECHAR_t* pszFilename) const;
676 
677  virtual void SetClear();
679  {
680  return this;
681  }
682  virtual cXmlNode* ToNode()
683  {
685  return this;
686  }
687 
689  virtual cXmlNode* Clone() const;
690 
694  virtual HRESULT SetParseNode(cXmlReader& rReader);
695 
697  virtual HRESULT Print(cStreamOutput& rOut, int nDepth = 0) const;
698 
700  virtual bool OnVisit(IXmlVisitor* content) const;
701 
702  cXmlNodeTypeIMPL(Document, this);
703  };
704 
705 #undef cXmlNodeTypeIMPL
706 
707 };
708 
709 #endif // _INC_cXml_H
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define UNREFERENCED_PARAMETER(P)
< _WIN32 type thing. get rid of stupid warning.
Definition: SysTypes.h:299
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define cXmlNodeTypeIMPL(a, b)
dynamic_cast like to a more defined XMLNODE_TYPE. Will return null if not of the requested type like ...
Definition: cXmlDocument.h:156
Definition: cXmlDocument.h:238
cXmlComment(cXmlString sValue)
Definition: cXmlDocument.h:247
cXmlComment()
Definition: cXmlDocument.h:243
void operator=(const cXmlComment &rCopy)
Definition: cXmlDocument.h:253
virtual ~cXmlComment()
Definition: cXmlDocument.h:259
Definition: cXmlDocument.h:480
virtual cXmlNode * ToNode()=0
cast cXmlContainer is a cXmlDocument or cXmlElement
HRESULT LinkChildEnd(cXmlNode *addThis)
virtual ~cXmlContainer()
Definition: cXmlDocument.h:485
Definition: cXmlDocument.h:353
virtual HRESULT Print(cStreamOutput &rOut, int nDepth=0) const
Definition: cXmlDocument.h:428
virtual ~cXmlDeclaration()
Definition: cXmlDocument.h:400
void operator=(const cXmlDeclaration &rCopy)
Definition: cXmlDocument.h:394
const cXmlString & get_Standalone() const
Definition: cXmlDocument.h:413
virtual void SetClear()
Delete all the children of this node. Does not affect 'this'.
Definition: cXmlDocument.h:382
const cXmlString & get_Encoding() const
Definition: cXmlDocument.h:408
const cXmlString & get_Version() const
Definition: cXmlDocument.h:403
Definition: cXmlDocument.h:635
virtual cXmlNode * ToNode()
cast cXmlContainer is a cXmlDocument or cXmlElement
Definition: cXmlDocument.h:682
virtual ~cXmlDocument()
Definition: cXmlDocument.h:655
void operator=(const cXmlDocument &rCopy)
Definition: cXmlDocument.h:658
virtual cXmlContainer * ToContainer()
Definition: cXmlDocument.h:678
Definition: cXmlDocument.h:549
cXmlAttributeSet m_Attributes
Definition: cXmlDocument.h:558
virtual cXmlNode * ToNode()
cast cXmlContainer is a cXmlDocument or cXmlElement
Definition: cXmlDocument.h:626
void operator=(const cXmlElement &rCopy)
Definition: cXmlDocument.h:569
virtual cXmlContainer * ToContainer()
Definition: cXmlDocument.h:622
Definition: cXmlDocument.h:30
bool isType(XMLNODE_TYPE eType) const
Definition: cXmlDocument.h:79
void * get_UserData() const
Definition: cXmlDocument.h:89
void put_UserData(void *pUserData)
Definition: cXmlDocument.h:84
virtual cXmlContainer * ToContainer()
Definition: cXmlDocument.h:148
cXmlString m_sValue
parent usage dependent m_eNodeType.
Definition: cXmlDocument.h:57
const cXmlString & get_Value() const
Definition: cXmlDocument.h:103
virtual bool OnVisit(IXmlVisitor *pVisitor) const =0
virtual cXmlNode * Clone() const =0
void * m_pUserData
Field containing a generic pointer to arbitrary user data.
Definition: cXmlDocument.h:58
ITERATE_t get_Row() const
Definition: cXmlDocument.h:227
void put_Value(cXmlString sValue)
Definition: cXmlDocument.h:98
virtual HRESULT Print(cStreamOutput &rOut, int nDepth=0) const =0
StrLen_t get_Column() const
Definition: cXmlDocument.h:231
XMLNODE_TYPE get_Type() const
Definition: cXmlDocument.h:74
virtual HRESULT SetParseNode(cXmlReader &rReader)=0
cTextPos m_Pos
Where (line,col) in the parsed XML document is this node? m_PosNode.
Definition: cXmlDocument.h:61
const XMLNODE_TYPE m_eNodeType
Definition: cXmlDocument.h:56
Definition: cXmlReader.h:69
Definition: cXmlDocument.h:279
bool isCDATA() const
Definition: cXmlDocument.h:327
virtual void SetClear()
Delete all the children of this node. Does not affect 'this'.
Definition: cXmlDocument.h:294
cXmlText(const cXmlText &rCopy)
Definition: cXmlDocument.h:315
void operator=(const cXmlText &rCopy)
Definition: cXmlDocument.h:321
cXmlText(cXmlString sValue="", bool bCDATA=false)
Definition: cXmlDocument.h:304
void put_CDATA(bool bCDATA)
Definition: cXmlDocument.h:332
virtual ~cXmlText()
Definition: cXmlDocument.h:312
Definition: cXmlDocument.h:441
void operator=(const cXmlUnknown &rCopy)
Definition: cXmlDocument.h:459
cXmlUnknown(const cXmlUnknown &rCopy)
Definition: cXmlDocument.h:455
cXmlUnknown(cXmlString sValue="")
Definition: cXmlDocument.h:450
virtual ~cXmlUnknown()
Definition: cXmlDocument.h:452
Definition: cIniMap.h:56
Definition: cList.h:205
Definition: cList.h:224
Definition: cNonCopyable.h:17
Definition: cStream.h:306
Definition: cStream.h:126
virtual HRESULT WriteString(const char *pszStr)
Definition: cStream.h:212
Definition: cTextPos.h:18
StrLen_t get_Column1() const noexcept
< Get 1 based column.
Definition: cTextPos.h:73
ITERATE_t get_Line1() const noexcept
< Get 1 based line.
Definition: cTextPos.h:69
Definition: cMesh.h:22
class __DECL_IMPORT cXmlReader
Definition: cXmlReader.h:19
CXML_ENCODING_TYPE
Definition: cXml.h:24
@ CXML_ENCODING_UNKNOWN
Definition: cXml.h:27
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
int ITERATE_t
like size_t but signed
Definition: Index.h:28
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22
Definition: cXmlVisitor.h:22
Definition: cXml.h:34
XMLNODE_TYPE
Definition: cXml.h:39