Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
XMLNode.cxx
1/*
2 * XMLNode.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 "XMLNode.h"
11
12#include <TDOMParser.h>
13#include <TXMLAttr.h> // for TXMLAttr
14#include <TXMLDocument.h> // for TXMLDocument
15#include <TXMLNode.h>
16#include <iostream>
17
18namespace Hal {
19 XMLNode::XMLNode(TString name, TString value) : TNamed(name, value) {
20 fChildren.SetOwner(kTRUE);
21 fAttrib.SetOwner(kTRUE);
22 }
23
24 XMLNode::XMLNode(const XMLNode& other) : XMLNode(other.GetName(), other.GetValue()) {
25 for (int i = 0; i < other.fChildren.GetEntries(); i++) {
26 fChildren.Add(new XMLNode(*other.GetChild(i)));
27 }
28 for (int i = 0; i < other.fAttrib.GetEntries(); i++) {
29 fAttrib.Add(new XMLAttrib(*other.GetAttrib(i)));
30 }
31 }
32
34 if (&other == this) return *this;
35 SetName(other.GetName());
36 SetValue(other.GetValue());
37 fChildren.Clear();
38 fAttrib.Clear();
39 for (int i = 0; i < other.fChildren.GetEntries(); i++) {
40 fChildren.Add(new XMLNode(*other.GetChild(i)));
41 }
42 for (int i = 0; i < other.fAttrib.GetEntries(); i++) {
43 fAttrib.Add(new XMLAttrib(*other.GetAttrib(i)));
44 }
45 return *this;
46 }
47
48 void XMLNode::Copy(TXMLNode* node) {
49 fChildren.Clear();
50 fAttrib.Clear();
51 SetName(node->GetNodeName());
52 SetTitle(node->GetText());
53 if (node->HasChildren()) {
54 TXMLNode* child = node->GetChildren();
55 do {
56 if (child == nullptr) break;
57 TString name = child->GetNodeName();
58 if (name != "text") { // skip "text" nodes
59 XMLNode* tempnode = new XMLNode();
60 tempnode->Copy(child);
61 fChildren.Add(tempnode);
62 }
63 if (child->HasNextNode()) child = child->GetNextNode();
64 } while (child->HasNextNode());
65 }
66 if (node->HasAttributes()) {
67 TList* atr_list = node->GetAttributes();
68 for (int i = 0; i < atr_list->GetEntries(); i++) {
69 TXMLAttr* atrib = (TXMLAttr*) atr_list->At(i);
70 fAttrib.Add(new XMLAttrib(atrib->GetName(), atrib->GetValue()));
71 }
72 }
73 }
74
76 TString new_atr = attrib->GetName();
77 if (GetAttrib(new_atr) != nullptr) {
78 std::cout << "XMLNode::AddAttrib Can't have two attributes with the same name!" << std::endl;
79 return;
80 }
81 fAttrib.AddLast(attrib);
82 }
83
84 Int_t XMLNode::GetNChildren(TString name) const {
85 Int_t counter = 0;
86 for (int i = 0; i < GetNChildren(); i++) {
87 TString name_temp = GetChild(i)->GetName();
88 if (name_temp == name) { counter++; }
89 }
90 return counter;
91 }
92
93 XMLNode* XMLNode::GetChild(TString name, Int_t count) const {
94 Int_t control_index = 0;
95 for (int i = 0; i < fChildren.GetEntries(); i++) {
96 XMLNode* node = GetChild(i);
97 TString temp = node->GetName();
98 if (temp == name) { control_index++; }
99 if (control_index > count) return node;
100 }
101 return nullptr;
102 }
103
104 XMLAttrib* XMLNode::GetAttrib(TString name) const { return static_cast<XMLAttrib*>(fAttrib.FindObject(name)); }
105
106 XMLNode* XMLNode::GetChild(Int_t index) const { return static_cast<XMLNode*>(fChildren.At(index)); }
107
108 XMLAttrib* XMLNode::GetAttrib(Int_t index) const { return static_cast<XMLAttrib*>(fAttrib.At(index)); }
109
110 XMLNode::~XMLNode() {}
111
112 //---------- HalXMLFile ------------------------------------------------------------------------------------
113
114 XMLFile::XMLFile(TString name, TString mode) : fName(name) {
115 if (mode == "read" || mode == "READ") {
116 fOverwrite = kFALSE;
117 TDOMParser Parser;
118 Parser.SetValidate(kFALSE);
119 Parser.ParseFile(name);
120 TXMLNode* MainNode = Parser.GetXMLDocument()->GetRootNode();
121 fRootNode.reset(new XMLNode());
122 fRootNode->Copy(MainNode);
123 } else {
124 fOverwrite = kTRUE;
125 }
126 }
127
128 void XMLFile::CreateRootNode(TString name) { fRootNode.reset(new XMLNode(name)); }
129
130 void XMLFile::SetRootNode(XMLNode* node) { fRootNode.reset(node); }
131
133 if (fOverwrite) {
134 if (!fRootNode) {
135 std::cout << "HalXMLFile::Close() No root node!" << std::endl;
136 return;
137 }
138 TXMLEngine engine;
139 XMLNodePointer_t mainnode = engine.NewChild(0, 0, fRootNode->GetName());
140 ExportNode(mainnode, engine, *fRootNode.get());
141 XMLDocPointer_t xmldoc = engine.NewDoc();
142 engine.DocSetRootElement(xmldoc, mainnode);
143 engine.SaveDoc(xmldoc, fName);
144 engine.FreeDoc(xmldoc);
145 }
146 }
147
148 void XMLFile::ExportNode(XMLNodePointer_t& nodePointer, TXMLEngine& engine, const XMLNode& node) const {
149 for (int i = 0; i < node.GetNChildren(); i++) {
150 XMLNodePointer_t child = engine.NewChild(nodePointer, 0, node.GetChild(i)->GetName(), node.GetChild(i)->GetValue());
151 for (int j = 0; j < node.GetChild(i)->GetNAttributes(); j++) {
152 engine.NewAttr(child, 0, node.GetChild(i)->GetAttrib(j)->GetName(), node.GetChild(i)->GetAttrib(j)->GetValue());
153 }
154 ExportNode(child, engine, *node.GetChild(i));
155 }
156 }
157
159 if (fRootNode && fOverwrite) Close();
160 }
161} // namespace Hal
TString GetValue() const
Definition XMLNode.h:45
void CreateRootNode(TString name)
Definition XMLNode.cxx:128
void Close()
Definition XMLNode.cxx:132
XMLFile(TString name="", TString mode="read")
Definition XMLNode.cxx:114
virtual ~XMLFile()
Definition XMLNode.cxx:158
void SetRootNode(XMLNode *node)
Definition XMLNode.cxx:130
void Copy(TXMLNode *node)
Definition XMLNode.cxx:48
Int_t GetNChildren() const
Definition XMLNode.h:104
void SetValue(TString value)
Definition XMLNode.h:88
XMLNode * GetChild(TString name, Int_t count=0) const
Definition XMLNode.cxx:93
XMLAttrib * GetAttrib(TString name) const
Definition XMLNode.cxx:104
TString GetValue() const
Definition XMLNode.h:120
XMLNode(const XMLNode &other)
Definition XMLNode.cxx:24
XMLNode & operator=(const XMLNode &other)
Definition XMLNode.cxx:33
void AddAttrib(XMLAttrib *attrib)
Definition XMLNode.cxx:75