VAPOR3 3.9.4
XmlNode.h
Go to the documentation of this file.
1//
2// $Id$
3//
4
5#ifndef _XmlNode_h_
6#define _XmlNode_h_
7
8#include <iostream>
9#include <map>
10#include <vector>
11#include <string>
12#include <stack>
13#include <vapor/MyBase.h>
14#ifdef WIN32
15 #pragma warning(disable : 4251)
16#endif
17
18namespace VAPoR {
19
20//
50public:
61 XmlNode(const string &tag, const map<string, string> &attrs, size_t numChildrenHint = 0);
62 XmlNode(const string &tag, size_t numChildrenHint = 0);
64
65 virtual XmlNode *Construct(const string &tag, const map<string, string> &attrs, size_t numChildrenHint = 0) { return (new XmlNode(tag, attrs, numChildrenHint)); }
66
74 XmlNode(const XmlNode &node);
75
76 virtual XmlNode *Clone() { return new XmlNode(*this); };
77
84 //
85 virtual ~XmlNode();
86
90 //
91 string &Tag() { return (_tag); }
92
93 string GetTag() const { return (_tag); }
94
95 void SetTag(string tag) { _tag = tag; }
96
100 //
101 map<string, string> &Attrs() { return (_attrmap); }
102
103 // These methods set or get XML character data, possibly formatting
104 // the data in the process. The paramter 'tag' identifies the XML
105 // element tag associated with the character data. The
106 // parameter, 'values', contains the character data itself. The Long and
107 // Double versions of these methods convert a between character streams
108 // and vectors of longs or doubles as appropriate.
109 //
110
121 //
122 virtual void SetElementLong(const vector<string> &tags, const vector<long> &values);
123 virtual void SetElementLong(const vector<string> &tags, long value)
124 {
125 vector<long> values(1, value);
126 XmlNode::SetElementLong(tags, values);
127 }
128
139 //
140 virtual void SetElementLong(const string &tag, const vector<long> &values);
141 virtual void SetElementLong(const string &tag, long value)
142 {
143 vector<long> values(1, value);
144 XmlNode::SetElementLong(tag, values);
145 }
146
157 virtual const vector<long> &GetElementLong(const string &tag) const;
158
164 virtual bool HasElementLong(const string &tag) const;
165
176 //
177 virtual void SetElementDouble(const string &tag, const vector<double> &values);
178 virtual void SetElementDouble(const string &tag, double value)
179 {
180 vector<double> values(1, value);
181 XmlNode::SetElementDouble(tag, values);
182 }
183
194 //
195 virtual void SetElementDouble(const vector<string> &tags, const vector<double> &values);
196 virtual void SetElementDouble(const vector<string> &tags, double value)
197 {
198 vector<double> values(1, value);
199 XmlNode::SetElementDouble(tags, values);
200 }
201
212 virtual const vector<double> &GetElementDouble(const string &tag) const;
213
219 virtual bool HasElementDouble(const string &tag) const;
220
232 //
233 virtual void SetElementString(const string &tag, const string &values);
234
245 virtual const string &GetElementString(const string &tag) const;
246
260 //
261 virtual void SetElementStringVec(const string &tag, const vector<string> &values);
275 //
276 virtual void SetElementStringVec(const vector<string> &tagpath, const vector<string> &values);
277
291 virtual void GetElementStringVec(const string &tag, vector<string> &vec) const;
292
298 virtual bool HasElementString(const string &tag) const;
299
305 //
306
315 //
316 XmlNode *AddChild(const XmlNode *child);
317 XmlNode *AddChild(const XmlNode &child);
318
319 virtual int GetNumChildren() const { return (int)(_children.size()); };
320
333 //
334 virtual XmlNode *NewChild(const string &tag, const map<string, string> &attrs, size_t numChildrenHint = 0);
335
336 virtual XmlNode *NewChild(const string &tag, size_t numChildrenHint = 0)
337 {
338 map<string, string> attrs;
339 return (NewChild(tag, attrs, numChildrenHint));
340 }
341
351 virtual int DeleteChild(size_t index);
352 virtual int DeleteChild(const string &tag);
353
357 //
358 virtual void DeleteAll();
359
360#ifdef VAPOR3_0_0_ALPHA
369
370 virtual int ReplaceChild(XmlNode *childNode, XmlNode *newChild);
371#endif
372
383 //
384 virtual XmlNode *GetChild(size_t index) const;
385
391 virtual bool HasChild(size_t index) const;
392
400 //
401 virtual XmlNode *GetChild(const string &tag) const;
402
408 virtual bool HasChild(const string &tag) const;
409
420 virtual XmlNode *GetNode(std::vector<string> path, bool absolute) const;
421
431 //
432 virtual XmlNode *GetNode(string path) const;
433
440 //
441 virtual XmlNode *GetParent() const { return (_parent); }
442
453 //
454 void SetParent(XmlNode *parent);
455
457 //
458 friend ostream &operator<<(ostream &s, const XmlNode &node);
459
461 //
463
465 //
466 bool operator==(const XmlNode &rhs) const;
467 bool operator!=(const XmlNode &rhs) const { return (!(*this == rhs)); };
468
473 //
474 virtual bool IsRoot() const { return (GetParent() == NULL); }
475
481 //
482 virtual std::vector<string> GetPathVec() const;
483
488 //
489 virtual string GetPath() const;
490
496 virtual XmlNode *GetRoot() const;
497
498 static const std::vector<XmlNode *> &GetAllocatedNodes() { return (_allocatedNodes); }
499
500 // Following is a substitute for exporting the "<<" operator in windows.
501 // I don't know how to export an operator<< !
502 static ostream &streamOut(ostream &os, const XmlNode &node);
503
504 static bool IsValidXMLElement(const string &s, int *badIdx=nullptr);
505 static string SanitizeXMLTag(string s);
506
507private:
508 static vector<long> _emptyLongVec; // empty elements
509 static vector<double> _emptyDoubleVec;
510 static vector<string> _emptyStringVec;
511 static string _emptyString;
512
513 static std::vector<XmlNode *> _allocatedNodes;
514
515 map<string, vector<long>> _longmap; // node's long data
516 map<string, vector<double>> _doublemap; // node's double data
517 map<string, string> _stringmap; // node's string data
518 map<string, string> _attrmap; // node's attributes
519
520 vector<XmlNode *> _children; // node's children
521 string _tag; // node's tag name
522
523 size_t _asciiLimit; // length limit beyond which element data are encoded
524 XmlNode *_parent; // Node's parent
525};
526// ostream& VAPoR::operator<< (ostream& os, const XmlNode& node);
527
529public:
532
533 int LoadFromFile(XmlNode *root, string path);
534 int LoadFromFile(XmlNode *root, istream &in);
535
536private:
537 enum type { UNKNOWN, PARENT, LONG_DATA, DOUBLE_DATA, STRING_DATA };
538
539 XmlNode * _root;
540 type _nodeType;
541 std::stack<XmlNode *> _nodeStack;
542 string _stringData;
543
544 void _startElementHandler(string tag, map<string, string> &myattrs);
545 void _endElementHandler(string tag);
546 void _charDataHandler(string s);
547
548 bool _isParentElement(string tag, map<string, string> myattrs) const;
549 bool _isDataElement(string tag, map<string, string> myattrs, type &dtype) const;
550
551 friend void _StartElementHandler(void *userData, const char *tag, const char **attrs);
552
553 friend void _EndElementHandler(void *userData, const char *tag);
554
555 friend void _CharDataHandler(void *userData, const char *s, int len);
556};
557
558}; // namespace VAPoR
559
560#endif // _XmlNode_h_
An Xml tree.
Definition: XmlNode.h:49
virtual XmlNode * GetChild(size_t index) const
virtual void SetElementLong(const vector< string > &tags, long value)
Definition: XmlNode.h:123
virtual void SetElementDouble(const vector< string > &tags, double value)
Definition: XmlNode.h:196
virtual void SetElementDouble(const string &tag, const vector< double > &values)
XmlNode & operator=(const XmlNode &rhs)
Assignment operator.
virtual std::vector< string > GetPathVec() const
virtual void SetElementDouble(const string &tag, double value)
Definition: XmlNode.h:178
static bool IsValidXMLElement(const string &s, int *badIdx=nullptr)
virtual XmlNode * GetRoot() const
static const std::vector< XmlNode * > & GetAllocatedNodes()
Definition: XmlNode.h:498
virtual const vector< long > & GetElementLong(const string &tag) const
bool operator==(const XmlNode &rhs) const
Equivalence operator.
virtual void DeleteAll()
void SetTag(string tag)
Definition: XmlNode.h:95
virtual XmlNode * Construct(const string &tag, const map< string, string > &attrs, size_t numChildrenHint=0)
Definition: XmlNode.h:65
virtual bool HasChild(const string &tag) const
virtual const string & GetElementString(const string &tag) const
virtual bool HasElementDouble(const string &tag) const
virtual XmlNode * Clone()
Definition: XmlNode.h:76
virtual int DeleteChild(size_t index)
XmlNode(const string &tag, const map< string, string > &attrs, size_t numChildrenHint=0)
static ostream & streamOut(ostream &os, const XmlNode &node)
virtual XmlNode * GetNode(std::vector< string > path, bool absolute) const
string & Tag()
Definition: XmlNode.h:91
XmlNode(const string &tag, size_t numChildrenHint=0)
XmlNode * AddChild(const XmlNode *child)
virtual bool HasChild(size_t index) const
virtual XmlNode * NewChild(const string &tag, const map< string, string > &attrs, size_t numChildrenHint=0)
virtual void SetElementLong(const string &tag, const vector< long > &values)
virtual ~XmlNode()
virtual XmlNode * GetParent() const
Definition: XmlNode.h:441
virtual XmlNode * NewChild(const string &tag, size_t numChildrenHint=0)
Definition: XmlNode.h:336
virtual void GetElementStringVec(const string &tag, vector< string > &vec) const
virtual bool HasElementString(const string &tag) const
virtual void SetElementLong(const string &tag, long value)
Definition: XmlNode.h:141
static string SanitizeXMLTag(string s)
virtual XmlNode * GetChild(const string &tag) const
map< string, string > & Attrs()
Definition: XmlNode.h:101
string GetTag() const
Definition: XmlNode.h:93
XmlNode(const XmlNode &node)
void SetParent(XmlNode *parent)
virtual void SetElementLong(const vector< string > &tags, const vector< long > &values)
virtual const vector< double > & GetElementDouble(const string &tag) const
virtual bool IsRoot() const
Definition: XmlNode.h:474
virtual bool HasElementLong(const string &tag) const
XmlNode * AddChild(const XmlNode &child)
bool operator!=(const XmlNode &rhs) const
Definition: XmlNode.h:467
virtual int GetNumChildren() const
Definition: XmlNode.h:319
virtual string GetPath() const
friend ostream & operator<<(ostream &s, const XmlNode &node)
Write the XML tree, rooted at this node, to a file in XML format.
virtual void SetElementStringVec(const string &tag, const vector< string > &values)
virtual void SetElementStringVec(const vector< string > &tagpath, const vector< string > &values)
virtual int DeleteChild(const string &tag)
virtual void SetElementDouble(const vector< string > &tags, const vector< double > &values)
virtual XmlNode * GetNode(string path) const
virtual void SetElementString(const string &tag, const string &values)
int LoadFromFile(XmlNode *root, istream &in)
friend void _EndElementHandler(void *userData, const char *tag)
int LoadFromFile(XmlNode *root, string path)
friend void _StartElementHandler(void *userData, const char *tag, const char **attrs)
friend void _CharDataHandler(void *userData, const char *s, int len)
Wasp base class.
Definition: MyBase.h:67
#define PARAMS_API
Definition: common.h:77