BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
MpdXMLNode.cxx
Go to the documentation of this file.
1/*
2 * MpdXMLNode.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 "MpdXMLNode.h"
11
12#include "TDOMParser.h"
13#include "TXMLEngine.h"
14
15#include <iostream>
16
18 : TNamed(name, value)
19{
20 fChildren.SetOwner(kTRUE);
21 fAttrib.SetOwner(kTRUE);
22}
23
25 : MpdXMLNode(other.GetName(), other.GetValue())
26{
27 for (int i = 0; i < other.fChildren.GetEntries(); i++) {
28 fChildren.Add(new MpdXMLNode(*other.GetChild(i)));
29 }
30 for (int i = 0; i < other.fAttrib.GetEntries(); i++) {
31 fAttrib.Add(new MpdXMLAttrib(*other.GetAttrib(i)));
32 }
33}
34
36{
37 if (&other == this)
38 return *this;
39 SetName(other.GetName());
40 SetValue(other.GetValue());
41 fChildren.Clear();
42 fAttrib.Clear();
43 for (int i = 0; i < other.fChildren.GetEntries(); i++) {
44 fChildren.Add(new MpdXMLNode(*other.GetChild(i)));
45 }
46 for (int i = 0; i < other.fAttrib.GetEntries(); i++) {
47 fAttrib.Add(new MpdXMLAttrib(*other.GetAttrib(i)));
48 }
49 return *this;
50}
51
53{
54 fChildren.Clear();
55 fAttrib.Clear();
56 SetName(node->GetNodeName());
57 SetTitle(node->GetText());
58 if (node->HasChildren()) {
59 TXMLNode* child = node->GetChildren();
60 do {
61 if (child == NULL)
62 break;
63 TString name = child->GetNodeName();
64 if (name != "text") { // skip "text" nodes
65 MpdXMLNode* tempnode = new MpdXMLNode();
66 tempnode->Copy(child);
67 fChildren.Add(tempnode);
68 }
69 if (child->HasNextNode())
70 child = child->GetNextNode();
71 } while (child->HasNextNode());
72 }
73 if (node->HasAttributes()) {
74 TList* atr_list = node->GetAttributes();
75 for (int i = 0; i < atr_list->GetEntries(); i++) {
76 TXMLAttr* atrib = (TXMLAttr*)atr_list->At(i);
77 fAttrib.Add(new MpdXMLAttrib(atrib->GetName(), atrib->GetValue()));
78 }
79 }
80}
81
83{
84 TString new_atr = attrib->GetName();
85 if (GetAttrib(new_atr) != nullptr) {
86 std::cout << "MpdXMLNode::AddAttrib Can't have two attributes with the same name!" << std::endl;
87 return;
88 }
89 fAttrib.AddLast(attrib);
90}
91
93{
94 Int_t counter = 0;
95 for (int i = 0; i < GetNChildren(); i++) {
96 TString name_temp = GetChild(i)->GetName();
97 if (name_temp == name) {
98 counter++;
99 }
100 }
101 return counter;
102}
103
105{
106 Int_t control_index = 0;
107 for (int i = 0; i < fChildren.GetEntries(); i++) {
108 MpdXMLNode* node = GetChild(i);
109 TString temp = node->GetName();
110 if (temp == name) {
111 control_index++;
112 }
113 if (control_index > count)
114 return node;
115 }
116 return nullptr;
117}
118
120{
121 return static_cast<MpdXMLAttrib*>(fAttrib.FindObject(name));
122}
123
125{
126 return static_cast<MpdXMLNode*>(fChildren.At(index));
127}
128
130{
131 return static_cast<MpdXMLAttrib*>(fAttrib.At(index));
132}
133
135
136//---------- MpdXMLFile ------------------------------------------------------------------------------------
137
139 : fName(name)
140{
141 if (mode == "read" || mode == "READ") {
143 TDOMParser Parser;
144 Parser.SetValidate(kFALSE);
145 Parser.ParseFile(name);
146 TXMLNode* MainNode = Parser.GetXMLDocument()->GetRootNode();
147 fRootNode.reset(new MpdXMLNode());
148 fRootNode->Copy(MainNode);
149 } else {
151 }
152}
153
155{
156 fRootNode.reset(new MpdXMLNode(name));
157}
158
160{
161 fRootNode.reset(node);
162}
163
165{
166 if (fOverwrite) {
167 if (!fRootNode) {
168 std::cout << "MpdXMLFile::Close() No root node!" << std::endl;
169 return;
170 }
171 TXMLEngine engine;
172 XMLNodePointer_t mainnode = engine.NewChild(0, 0, fRootNode->GetName());
173 ExportNode(mainnode, engine, *fRootNode.get());
174 XMLDocPointer_t xmldoc = engine.NewDoc();
175 engine.DocSetRootElement(xmldoc, mainnode);
176 engine.SaveDoc(xmldoc, fName);
177 engine.FreeDoc(xmldoc);
178 fRootNode.reset(nullptr);
179 }
180}
181
182void MpdXMLFile::ExportNode(XMLNodePointer_t& nodePointer, TXMLEngine& engine, const MpdXMLNode& node) const
183{
184 for (int i = 0; i < node.GetNChildren(); i++) {
185 XMLNodePointer_t child =
186 engine.NewChild(nodePointer, 0, node.GetChild(i)->GetName(), node.GetChild(i)->GetValue());
187 for (int j = 0; j < node.GetChild(i)->GetNAttributes(); j++) {
188 engine.NewAttr(child, 0, node.GetChild(i)->GetAttrib(j)->GetName(),
189 node.GetChild(i)->GetAttrib(j)->GetValue());
190 }
191 ExportNode(child, engine, *node.GetChild(i));
192 }
193}
194
196{
197 if (fRootNode && fOverwrite)
198 Close();
199}
int i
Definition P4_F32vec4.h:22
int Int_t
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
void * XMLNodePointer_t
void * XMLDocPointer_t
class for representing node attributes
Definition MpdXMLNode.h:21
TString GetValue() const
Definition MpdXMLNode.h:43
void CreateRootNode(TString name)
create new root node
Bool_t fOverwrite
Definition MpdXMLNode.h:156
TString fName
Definition MpdXMLNode.h:155
MpdXMLFile(TString name="", TString mode="read")
std::unique_ptr< MpdXMLNode > fRootNode
Definition MpdXMLNode.h:154
void SetRootNode(MpdXMLNode *node)
set new root node, old node will be overwritten
void ExportNode(XMLNodePointer_t &nodePointer, TXMLEngine &engine, const MpdXMLNode &node) const
virtual ~MpdXMLFile()
destroy object (and save xml file if needed and Close was not called)
void Close()
close and write xml (if needed)
class for representing XML node
Definition MpdXMLNode.h:56
void AddAttrib(MpdXMLAttrib *attrib)
add attribute to this class
TString GetValue() const
Definition MpdXMLNode.h:119
void Copy(TXMLNode *node)
copy data for node to this
MpdXMLNode & operator=(const MpdXMLNode &other)
assignement operator
MpdXMLNode * GetChild(TString name, Int_t count=0) const
search for node with given name
MpdXMLAttrib * GetAttrib(TString name) const
Int_t GetNChildren() const
Definition MpdXMLNode.h:103
TList fChildren
Definition MpdXMLNode.h:57
virtual ~MpdXMLNode()
MpdXMLNode(const MpdXMLNode &other)
copy constructor
void SetValue(TString value)
Definition MpdXMLNode.h:87
TList fAttrib
Definition MpdXMLNode.h:58
virtual Int_t GetEntries() const
virtual TXMLDocument * GetXMLDocument() const
Int_t ParseFile(const char *filename) override
TObject * At(Int_t idx) const override
virtual void SetTitle(const char *title="")
const char * GetName() const override
virtual void SetName(const char *name)
const char * GetValue() const
const char * GetName() const override
TXMLNode * GetRootNode() const
XMLNodePointer_t NewChild(XMLNodePointer_t parent, XMLNsPointer_t ns, const char *name, const char *content=nullptr)
XMLAttrPointer_t NewAttr(XMLNodePointer_t xmlnode, XMLNsPointer_t, const char *name, const char *value)
void SaveDoc(XMLDocPointer_t xmldoc, const char *filename, Int_t layout=1)
void FreeDoc(XMLDocPointer_t xmldoc)
XMLDocPointer_t NewDoc(const char *version="1.0")
void DocSetRootElement(XMLDocPointer_t xmldoc, XMLNodePointer_t xmlnode)
TList * GetAttributes()
Bool_t HasChildren() const
const char * GetText() const
TXMLNode * GetNextNode()
TXMLNode * GetChildren()
const char * GetNodeName() const
Bool_t HasNextNode() const
Bool_t HasAttributes() const
void SetValidate(Bool_t val=kTRUE)