ATLAS Offline Software
TBXMLWriterToolBase.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
6 #include "TBXMLWriterToolBase.h"
7 
8 #include <iostream>
9 #include <string>
10 #include <vector>
11 
13 // XML writer implementation //
15 
16 const std::string TBXMLWriterToolBase::m_doubleQuote = "\042";
17 
18 // dictionary
19 void
20 TBXMLWriterToolBase::openDictionary(std::ostream& outStream,
21  const std::string& dictName,
22  const std::vector<std::string>&
23  listOfElements)
24 {
25  // clean up
26  m_elementList.clear();
27  m_dictionary = dictName;
28 
29  // write header
30  outStream << "<?XML version=" << m_doubleQuote
31  << "1" << m_doubleQuote << "?>" << std::endl;
32 
33  // write begin of dictionary
34  outStream << "<!DOCTYPE " << dictName
35  << " [" << std::endl;
36 
37  // add doctype as element
38  this->addDictElement(outStream,dictName,listOfElements);
39 }
40 
41 // template <typename DATA>
42 void
43 TBXMLWriterToolBase::openDictionary(std::ostream& outStream,
44  const std::string& dictName)
45 {
46  std::vector<std::string> emptyList;
47  this->openDictionary(outStream,dictName, emptyList);
48 }
49 
50 // template <typename DATA>
51 void
52 TBXMLWriterToolBase::closeDictionary(std::ostream& outStream)
53 {
54  outStream << "]>" << std::endl;
55 }
56 
57 //template <typename DATA>
58 void
59 TBXMLWriterToolBase::addDictElement(std::ostream& outStream,
60  const std::string& theElement,
61  const std::vector<std::string>&
62  listOfElements)
63 {
64  outStream << "<!ELEMENT " << theElement;
65  // add document as element
66  if ( listOfElements.size() == 0 )
67  {
68  outStream << ">" << std::endl;
69  }
70  else
71  {
72  // store elements
73  if ( listOfElements[0] == "ANY" ||
74  listOfElements[0] == "EMPTY" ||
75  listOfElements[0] == "ALL" )
76  {
77  outStream << " " << listOfElements[0] << " >" << std::endl;
78  }
79  else
80  {
81  outStream << " (" << listOfElements[0];
82  for ( unsigned int i=1; i<listOfElements.size(); i++ )
83  {
84  outStream << "," << listOfElements[i];
85  }
86  outStream << ") >" << std::endl;
87  }
88  }
89 }
90 
91 // template <typename DATA>
92 void
93 TBXMLWriterToolBase::addAttributes(std::ostream& outStream,
94  const std::string& theElement,
95  const std::vector<std::string>&
96  listOfAttr)
97 {
98  outStream << " <!ATTLIST " << theElement << std::endl;
99  for ( unsigned int i=0; i<listOfAttr.size(); i++ )
100  {
101  outStream << " " << listOfAttr[i] << std::endl;
102  }
103  outStream << ">" << std::endl;
104 }
105 
106 // template <typename DATA>
107 void
108 TBXMLWriterToolBase::openElement(std::ostream& outStream,
109  const std::string& theElement,
110  const std::vector<std::string>&
111  listOfAttr,
112  const std::vector<std::string>&
113  listOfValues)
114 {
115  m_elementList.push_back(theElement);
116  outStream << "<" << theElement;
117  if ( listOfAttr.size() > 0 && listOfValues.size() == listOfAttr.size() )
118  {
119  for ( unsigned int i=0; i<listOfAttr.size(); i++ )
120  {
121  outStream << " " << listOfAttr[i] << "="
122  << m_doubleQuote
123  << listOfValues[i]
124  << m_doubleQuote;
125  }
126  }
127  outStream << ">" << std::endl;
128 }
129 
130 // template <typename DATA>
131 void
132 TBXMLWriterToolBase::closeElement(std::ostream& outStream)
133 {
134  unsigned int thisSize = m_elementList.size() - 1;
135  outStream << "</" << m_elementList[thisSize] << ">" << std::endl;
136  m_elementList.resize(thisSize);
137 }
138 
139 // template <typename DATA>
140 void
141 TBXMLWriterToolBase::closeElement(std::ostream& outStream,
142  const std::string& theElement)
143 {
145  find( m_elementList.begin(), m_elementList.end(), theElement);
146  if ( inVec != m_elementList.end() )
147  {
148  outStream << "</" << (*inVec) << ">" << std::endl;
149  m_elementList.erase(inVec);
150  }
151  else
152  {
153  outStream << "<!-- XML Structural Problem! \n\n"
154  << " cannot find an element " << theElement << "!\n\n"
155  << "-->" << std::endl;
156  }
157 }
158 
159 // template <typename DATA>
160 void
162 {
163  for ( int i=m_elementList.size()-1; i>=0; i-- )
164  {
165  outStream << "</" << m_elementList[i] << ">" << std::endl;
166  }
167  m_elementList.clear();
168 }
169 
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
TBXMLWriterToolBase::openElement
virtual void openElement(std::ostream &outStream, const std::string &theElement, const std::vector< std::string > &listOfAttr, const std::vector< std::string > &listOfValues)
Definition: TBXMLWriterToolBase.cxx:108
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
TBXMLWriterToolBase::addDictElement
virtual void addDictElement(std::ostream &outStream, const std::string &theElement, const std::vector< std::string > &listOfElements)
Definition: TBXMLWriterToolBase.cxx:59
TBXMLWriterToolBase::openDictionary
virtual void openDictionary(std::ostream &outStream, const std::string &dictName, const std::vector< std::string > &listOfElements)
Definition: TBXMLWriterToolBase.cxx:20
TBXMLWriterToolBase::addAttributes
virtual void addAttributes(std::ostream &outStream, const std::string &theElement, const std::vector< std::string > &listOfAttributes)
Definition: TBXMLWriterToolBase.cxx:93
TBXMLWriterToolBase::closeAllElements
virtual void closeAllElements(std::ostream &outStream)
Definition: TBXMLWriterToolBase.cxx:161
TBXMLWriterToolBase::closeElement
virtual void closeElement(std::ostream &outStream, const std::string &theElement)
Definition: TBXMLWriterToolBase.cxx:141
TBXMLWriterToolBase::m_elementList
std::vector< std::string > m_elementList
Definition: TBXMLWriterToolBase.h:106
TBXMLWriterToolBase.h
lumiFormat.i
int i
Definition: lumiFormat.py:92
TBXMLWriterToolBase::m_dictionary
std::string m_dictionary
Definition: TBXMLWriterToolBase.h:107
TBXMLWriterToolBase::m_doubleQuote
static const std::string m_doubleQuote
Definition: TBXMLWriterToolBase.h:109
TBXMLWriterToolBase::closeDictionary
virtual void closeDictionary(std::ostream &outStream)
Definition: TBXMLWriterToolBase.cxx:52