VAPoR  0.1
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 <vapor/MyBase.h>
13 #ifdef WIN32
14 #pragma warning(disable : 4251)
15 #endif
16 
17 namespace VAPoR {
18 
19 
20 //
48 public:
49  enum ErrCode_T {
50  ERR_DEF = 1, // default error
51  ERR_TNP // Tag not present
52  };
53 
54 
65  XmlNode(
66  const string &tag, const map<string, string> &attrs,
67  size_t numChildrenHint = 0
68  );
69 XmlNode(
70  const string &tag,
71  size_t numChildrenHint = 0
72  );
73  XmlNode() {_objInitialized = false;}
74  virtual XmlNode *Construct(
75  const string &tag, const map<string, string> &attrs,
76  size_t numChildrenHint = 0
77  ) {return(new XmlNode( tag, attrs, numChildrenHint)); }
78 
85  XmlNode(const XmlNode &node);
86 
87  virtual XmlNode *Clone() {return new XmlNode(*this); };
88 
89  virtual ~XmlNode();
90 
94  //
95  string &Tag() { return (_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  // Get methods return a negative result if the named tag does not exist.
111  // If the named tag does not exist, 'Get' methods will return a
112  // reference to a vector (or string) of zero length, *and* will
113  // register an error with the SetErrMsg() method.
114  //
115 
126  //
127  virtual int SetElementLong(
128  const vector<string> &tags, const vector<long> &values
129  );
140  //
141  virtual int SetElementLong(
142  const string &tag, const vector<long> &values
143  );
155  virtual const vector<long> &GetElementLong(const string &tag) const;
156 
157 
158 
164  virtual int HasElementLong(const string &tag) const;
165 
176  //
177  virtual int SetElementDouble(
178  const string &tag, const vector<double> &values
179  );
180 
191  //
192  virtual int SetElementDouble(
193  const vector<string> &tags, const vector<double> &values
194  );
206  virtual const vector<double> &GetElementDouble(const string &tag) const;
207 
208 
214  virtual int HasElementDouble(const string &tag) const;
215 
227  //
228  virtual int SetElementString(const string &tag, const string &values);
229 
241  virtual const string &GetElementString(const string &tag) const;
242 
256  //
257  virtual int SetElementStringVec(
258  const string &tag,const vector <string> &values
259  );
273  //
274 virtual int SetElementStringVec(
275  const vector<string> &tagpath, const vector <string> &values
276 );
277 
291  virtual void GetElementStringVec(const string &tag, vector <string> &vec) const;
292 
293 
299  virtual int HasElementString(const string &tag) const;
300 
306  //
307 
318  //
319  virtual void AddChild(
320  XmlNode* child
321  );
322 
323  virtual int GetNumChildren() const { return (int)(_children.size());};
324 
337  //
338  virtual XmlNode *NewChild(
339  const string &tag, const map <string, string> &attrs,
340  size_t numChildrenHint = 0
341  );
342 
352  virtual int DeleteChild(size_t index);
353  virtual int DeleteChild(const string &tag);
357  //
358  virtual void DeleteAll();
362  //
363  virtual void ClearChildren() {_children.clear();}
364 
373 
374  virtual int ReplaceChild(XmlNode* childNode, XmlNode* newChild);
375 
376 
387  //
388  virtual XmlNode *GetChild(size_t index) const;
389 
396  //
397  virtual XmlNode *GetParent() {return(_parent);}
398 
404  virtual int HasChild(size_t index);
405 
413  //
414  virtual XmlNode *GetChild(const string &tag) const;
415 
425  //
426  virtual bool &ErrOnMissing() {return (_errOnMissing); };
427 
433  virtual int HasChild(const string &tag);
434 
436  //
437  friend ostream& operator<<(ostream &s, const XmlNode& node);
438 
439  //Following is a substitute for exporting the "<<" operator in windows.
440  //I don't know how to export an operator<< !
441  static ostream& streamOut(ostream& os, const XmlNode& node);
442 
443  //help with strings that xml can't parse:
444  // replace all occurrences of 'input' with 'output'
445  //output should not occur in input.
446 
447  static string replaceAll(const string& sourceString, const char* input, const char* output);
448  static vector <long> _emptyLongVec; // empty elements
449  static vector <double> _emptyDoubleVec;
450  static vector <string> _emptyStringVec;
451  static string _emptyString;
452 
453 
454 protected:
455  map <string, vector<long> > _longmap; // node's long data
456  map <string, vector<double> > _doublemap; // node's double data
457  map <string, string> _stringmap; // node's string data
459 private:
460  static string replacement;
461  int _objInitialized; // has the obj successfully been initialized?
462 
463 
464  map <string, string> _attrmap; // node's attributes
465  vector <XmlNode *> _children; // node's children
466  string _tag; // node's tag name
467 
468  size_t _asciiLimit; // length limit beyond which element data are encoded
469  XmlNode *_parent; // Node's parent
470 
471 
472  // Recursively delete all chidren of the specified node. The node itself
473  // is not deleted.
474  //
475  void _deleteChildren(XmlNode *node);
476 
477 };
478 //ostream& VAPoR::operator<< (ostream& os, const XmlNode& node);
479 };
480 
481 #endif // _XmlNode_h_
map< string, string > & Attrs()
Definition: XmlNode.h:101
#define VDF_API
Definition: common.h:61
virtual XmlNode * GetParent()
Definition: XmlNode.h:397
map< string, vector< long > > _longmap
Definition: XmlNode.h:455
static vector< long > _emptyLongVec
Definition: XmlNode.h:448
An Xml tree.
Definition: XmlNode.h:47
bool _errOnMissing
Definition: XmlNode.h:458
map< string, string > _stringmap
Definition: XmlNode.h:457
static string _emptyString
Definition: XmlNode.h:451
virtual XmlNode * Clone()
Definition: XmlNode.h:87
static vector< string > _emptyStringVec
Definition: XmlNode.h:450
virtual int GetNumChildren() const
Definition: XmlNode.h:323
virtual bool & ErrOnMissing()
Definition: XmlNode.h:426
VetsUtil base class.
Definition: MyBase.h:68
string & Tag()
Definition: XmlNode.h:95
virtual void ClearChildren()
Definition: XmlNode.h:363
static vector< double > _emptyDoubleVec
Definition: XmlNode.h:449
virtual XmlNode * Construct(const string &tag, const map< string, string > &attrs, size_t numChildrenHint=0)
Definition: XmlNode.h:74
map< string, vector< double > > _doublemap
Definition: XmlNode.h:456