FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairXMLNode.cxx
Go to the documentation of this file.
1 /*
2  * FairXMLNode.cxx
3  *
4  * Created on: 01-10-2013
5  * Author: Daniel Wielanek
6  * E-mail: daniel.wielanek@gmail.com
7  * Warsaw University of Technology, Faculty of Physics
8  */
9 
10 #include "FairXMLNode.h"
11 
12 #include <TDOMParser.h>
13 #include <TXMLAttr.h> // for TXMLAttr
14 #include <TXMLDocument.h> // for TXMLDocument
15 #include <TXMLNode.h>
16 #include <fairlogger/Logger.h>
17 #include <iostream>
18 
19 FairXMLNode::FairXMLNode(TString name, TString value)
20  : TNamed(name, value)
21 {
22  fChildren.SetOwner(kTRUE);
23  fAttrib.SetOwner(kTRUE);
24 }
25 
27  : FairXMLNode(other.GetName(), other.GetValue())
28 {
29  for (int i = 0; i < other.fChildren.GetEntries(); i++) {
30  fChildren.Add(new FairXMLNode(*other.GetChild(i)));
31  }
32  for (int i = 0; i < other.fAttrib.GetEntries(); i++) {
33  fAttrib.Add(new FairXMLAttrib(*other.GetAttrib(i)));
34  }
35 }
36 
38 {
39  if (&other == this)
40  return *this;
41  SetName(other.GetName());
42  SetValue(other.GetValue());
43  fChildren.Clear();
44  fAttrib.Clear();
45  for (int i = 0; i < other.fChildren.GetEntries(); i++) {
46  fChildren.Add(new FairXMLNode(*other.GetChild(i)));
47  }
48  for (int i = 0; i < other.fAttrib.GetEntries(); i++) {
49  fAttrib.Add(new FairXMLAttrib(*other.GetAttrib(i)));
50  }
51  return *this;
52 }
53 
54 void FairXMLNode::Copy(TXMLNode* node)
55 {
56  fChildren.Clear();
57  fAttrib.Clear();
58  SetName(node->GetNodeName());
59  SetTitle(node->GetText());
60  if (node->HasChildren()) {
61  TXMLNode* child = node->GetChildren();
62  do {
63  if (child == nullptr)
64  break;
65  TString name = child->GetNodeName();
66  if (name != "text") { // skip "text" nodes
67  FairXMLNode* tempnode = new FairXMLNode();
68  tempnode->Copy(child);
69  fChildren.Add(tempnode);
70  }
71  if (child->HasNextNode())
72  child = child->GetNextNode();
73  } while (child->HasNextNode());
74  }
75  if (node->HasAttributes()) {
76  TList* atr_list = node->GetAttributes();
77  for (int i = 0; i < atr_list->GetEntries(); i++) {
78  TXMLAttr* atrib = (TXMLAttr*)atr_list->At(i);
79  fAttrib.Add(new FairXMLAttrib(atrib->GetName(), atrib->GetValue()));
80  }
81  }
82 }
83 
85 {
86  TString new_atr = attrib->GetName();
87  if (GetAttrib(new_atr) != nullptr) {
88  LOG(error) << "FairXMLNode::AddAttrib Can't have two attributes with the same name!";
89  return;
90  }
91  fAttrib.AddLast(attrib);
92 }
93 
94 Int_t FairXMLNode::GetNChildren(TString name) const
95 {
96  Int_t counter = 0;
97  for (int i = 0; i < GetNChildren(); i++) {
98  TString name_temp = GetChild(i)->GetName();
99  if (name_temp == name) {
100  counter++;
101  }
102  }
103  return counter;
104 }
105 
106 FairXMLNode* FairXMLNode::GetChild(TString name, Int_t count) const
107 {
108  Int_t control_index = 0;
109  for (int i = 0; i < fChildren.GetEntries(); i++) {
110  FairXMLNode* node = GetChild(i);
111  TString temp = node->GetName();
112  if (temp == name) {
113  control_index++;
114  }
115  if (control_index > count)
116  return node;
117  }
118  return nullptr;
119 }
120 
122 {
123  return static_cast<FairXMLAttrib*>(fAttrib.FindObject(name));
124 }
125 
126 FairXMLNode* FairXMLNode::GetChild(Int_t index) const { return static_cast<FairXMLNode*>(fChildren.At(index)); }
127 
128 FairXMLAttrib* FairXMLNode::GetAttrib(Int_t index) const { return static_cast<FairXMLAttrib*>(fAttrib.At(index)); }
129 
131 
132 //---------- FairXMLFile ------------------------------------------------------------------------------------
133 
134 FairXMLFile::FairXMLFile(TString name, TString mode)
135  : fName(name)
136 {
137  if (mode == "read" || mode == "READ") {
138  fOverwrite = kFALSE;
139  TDOMParser Parser;
140  Parser.SetValidate(kFALSE);
141  Parser.ParseFile(name);
142  TXMLNode* MainNode = Parser.GetXMLDocument()->GetRootNode();
143  fRootNode.reset(new FairXMLNode());
144  fRootNode->Copy(MainNode);
145  } else {
146  fOverwrite = kTRUE;
147  }
148 }
149 
150 void FairXMLFile::CreateRootNode(TString name) { fRootNode.reset(new FairXMLNode(name)); }
151 
152 void FairXMLFile::SetRootNode(FairXMLNode* node) { fRootNode.reset(node); }
153 
155 {
156  if (fOverwrite) {
157  if (!fRootNode) {
158  LOG(error) << "FairXMLFile::Close() No root node!";
159  return;
160  }
161  TXMLEngine engine;
162  XMLNodePointer_t mainnode = engine.NewChild(0, 0, fRootNode->GetName());
163  ExportNode(mainnode, engine, *fRootNode.get());
164  XMLDocPointer_t xmldoc = engine.NewDoc();
165  engine.DocSetRootElement(xmldoc, mainnode);
166  engine.SaveDoc(xmldoc, fName);
167  engine.FreeDoc(xmldoc);
168  fRootNode.reset(nullptr);
169  }
170 }
171 
172 void FairXMLFile::ExportNode(XMLNodePointer_t& nodePointer, TXMLEngine& engine, const FairXMLNode& node) const
173 {
174  for (int i = 0; i < node.GetNChildren(); i++) {
175  XMLNodePointer_t child =
176  engine.NewChild(nodePointer, 0, node.GetChild(i)->GetName(), node.GetChild(i)->GetValue());
177  for (int j = 0; j < node.GetChild(i)->GetNAttributes(); j++) {
178  engine.NewAttr(
179  child, 0, node.GetChild(i)->GetAttrib(j)->GetName(), node.GetChild(i)->GetAttrib(j)->GetValue());
180  }
181  ExportNode(child, engine, *node.GetChild(i));
182  }
183 }
184 
186 {
187  if (fRootNode && fOverwrite)
188  Close();
189 }
Int_t GetNChildren() const
Definition: FairXMLNode.h:108
Int_t GetNAttributes() const
Definition: FairXMLNode.h:113
TString GetValue() const
Definition: FairXMLNode.h:48
FairXMLFile(TString name="", TString mode="read")
FairXMLNode(const FairXMLNode &other)
Definition: FairXMLNode.cxx:26
FairXMLAttrib * GetAttrib(TString name) const
void Copy(TXMLNode *node)
Definition: FairXMLNode.cxx:54
TString GetValue() const
Definition: FairXMLNode.h:124
FairXMLNode & operator=(const FairXMLNode &other)
Definition: FairXMLNode.cxx:37
void SetRootNode(FairXMLNode *node)
FairXMLNode * GetChild(TString name, Int_t count=0) const
void SetValue(TString value)
Definition: FairXMLNode.h:92
virtual ~FairXMLNode()
void CreateRootNode(TString name)
virtual ~FairXMLFile()
void AddAttrib(FairXMLAttrib *attrib)
Definition: FairXMLNode.cxx:84