MpdXMLNode.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * FairXLMNode.h
  3. *
  4. * Created on: 13 wrz 2017
  5. * Author: Daniel Wielanek
  6. * E-mail: daniel.wielanek@gmail.com
  7. * Warsaw University of Technology, Faculty of Physics
  8. */
  9. #ifndef MPDXLMNODE_H
  10. #define MPDXLMNODE_H
  11. #include <TXMLNode.h>
  12. #include <TList.h>
  13. #include <TXMLAttr.h>
  14. #include <TXMLEngine.h>
  15. #include <TNamed.h>
  16. #include <memory>
  17. /**
  18. * class for representing node attributes
  19. */
  20. class MpdXMLAttrib: public TNamed{
  21. public :
  22. /**
  23. * default constructor
  24. */
  25. MpdXMLAttrib():TNamed(){}
  26. /**
  27. * default constructor
  28. * @param name name of attribute
  29. * @param value value of attribute
  30. */
  31. MpdXMLAttrib(TString name, TString value){
  32. SetName(name);
  33. SetTitle(value);}
  34. /**
  35. *
  36. * @return value of node
  37. */
  38. TString GetValue() const{return GetTitle();};
  39. /**
  40. *
  41. * @param val value of node to se
  42. */
  43. void SetValue(TString val){SetTitle(val);};
  44. virtual ~MpdXMLAttrib(){};
  45. ClassDef(MpdXMLAttrib,1)
  46. };
  47. /**
  48. * class for representing XML node
  49. */
  50. class MpdXMLNode : public TNamed{
  51. TList fChildren;
  52. TList fAttrib;
  53. public:
  54. /**
  55. * copy constructor
  56. * @param other
  57. */
  58. MpdXMLNode(const MpdXMLNode &other);
  59. /**
  60. * default constructor
  61. * @param name name of node
  62. * @param value value of node
  63. */
  64. MpdXMLNode(TString name="",TString value="");
  65. /**
  66. * assignement operator
  67. * @param node
  68. * @return
  69. */
  70. MpdXMLNode& operator=(const MpdXMLNode &other);
  71. /**
  72. * copy data for node to this
  73. * @param node
  74. */
  75. void Copy(TXMLNode *node);
  76. /**
  77. *
  78. * @param value new value
  79. */
  80. void SetValue(TString value){SetTitle(value);};
  81. /**
  82. * add child node to this node, node is now owned by
  83. * parent node
  84. * @param node node to add
  85. */
  86. void AddChild(MpdXMLNode *node){fChildren.AddLast(node);};
  87. /**
  88. * add attribute to this class
  89. * @param attrib
  90. */
  91. void AddAttrib(MpdXMLAttrib *attrib);
  92. /**
  93. *
  94. * @return number of childen nodes
  95. */
  96. Int_t GetNChildren() const {return fChildren.GetEntries();};
  97. /**
  98. *
  99. * @return number of attributes
  100. */
  101. Int_t GetNAttributes() const{return fAttrib.GetEntries();};
  102. /**
  103. * search for child with given name
  104. * @param name name of node
  105. * @return number of node with given name
  106. */
  107. Int_t GetNChildren(TString name) const;
  108. /**
  109. *
  110. * @return value of node
  111. */
  112. TString GetValue() const{return GetTitle();};
  113. /**
  114. *
  115. * @param name name of atribute
  116. * @return
  117. */
  118. MpdXMLAttrib *GetAttrib(TString name)const;
  119. /**
  120. *
  121. * @param index index of atrribute
  122. * @return
  123. */
  124. MpdXMLAttrib *GetAttrib(Int_t index)const;
  125. /**
  126. * search for node with given name
  127. * @param name name of node
  128. * @param count number of node (if more than one with given name exist)
  129. * @return node
  130. */
  131. MpdXMLNode *GetChild(TString name, Int_t count =0) const;
  132. /**
  133. *
  134. * @param index child number
  135. * @return child at given position
  136. */
  137. MpdXMLNode *GetChild(Int_t index) const;
  138. virtual ~MpdXMLNode();
  139. ClassDef(MpdXMLNode,1)
  140. };
  141. /**
  142. * class for opening XML files
  143. */
  144. class MpdXMLFile: public TObject{
  145. std::unique_ptr<MpdXMLNode> fRootNode;
  146. TString fName;
  147. Bool_t fOverwrite;
  148. void ExportNode(XMLNodePointer_t &nodePointer, TXMLEngine &engine,const MpdXMLNode &node) const;
  149. public:
  150. /**
  151. *
  152. * @param name name of xml file
  153. * @param mode if "READ" or "read" - only read file, otherwise create /overwrite file
  154. */
  155. MpdXMLFile(TString name="", TString mode="read");
  156. /**
  157. * create new root node
  158. * @param name name of new node
  159. */
  160. void CreateRootNode(TString name);
  161. /**
  162. * set new root node, old node will be overwritten
  163. * @param node root node
  164. */
  165. void SetRootNode(MpdXMLNode *node);
  166. /**
  167. *
  168. * @return root node
  169. */
  170. MpdXMLNode *GetRootNode()const{return fRootNode.get();};
  171. /**
  172. * close and write xml (if needed)
  173. */
  174. void Close();
  175. /**
  176. * destroy object (and save xml file if needed and Close was not called)
  177. */
  178. virtual ~MpdXMLFile();
  179. ClassDef(MpdXMLFile,1)
  180. };
  181. #endif /* MPDXLMNODE_H */