herwig
is hosted by
Hepforge
,
IPPP Durham
Herwig
7.3.0
Utilities
XML
Element.h
1
// -*- C++ -*-
2
//
3
// Element.hpp is a part of myXML
4
// Copyright (C) 2012-2019 Simon Platzer, The Herwig Collaboration
5
//
6
// myXML is licenced under version 3 of the GPL, see COPYING for details.
7
//
8
9
#ifndef MYXML_Element_hpp_included
10
#define MYXML_Element_hpp_included
11
12
#include <string>
13
#include <map>
14
#include <list>
15
#include <sstream>
16
#include <iomanip>
17
18
namespace
XML {
19
24
namespace
ElementTypes {
25
29
enum
EnumerateElementTypes
{
30
31
Unknown = -1,
33
Root
= 0,
35
EmptyElement
= 1,
37
Element
= 2,
39
ProcessingInstruction
= 3,
41
CharacterData
= 4,
43
ParsedCharacterData
= 5,
45
Comment
= 6
48
};
49
50
}
51
56
class
Element
{
57
58
public
:
59
63
Element
()
64
:
theType
(ElementTypes::Unknown) {}
65
69
explicit
Element
(
int
newType,
70
const
std::string& newNameOrContent =
""
)
71
:
theType
(newType),
theNameOrContent
(newNameOrContent) {}
72
73
77
Element
(
const
Element
& other);
78
82
Element
&
operator=
(
const
Element
& other);
83
88
friend
bool
operator==
(
const
XML::Element
&one,
const
XML::Element
&two);
89
friend
bool
operator!=(
const
XML::Element
&one,
const
XML::Element
&two);
90
96
friend
XML::Element
operator+
(
const
XML::Element
& one,
const
XML::Element
& two);
97
98
public
:
99
103
int
type
()
const
{
return
theType
; }
104
108
const
std::string&
name
()
const
;
109
110
public
:
111
115
const
std::string&
content
()
const
;
116
120
std::string&
content
();
121
125
template
<
class
T>
126
Element
&
operator<<
(
const
T& t) {
127
assertContent
();
128
std::ostringstream out;
129
out << std::setprecision(16) << t;
130
theNameOrContent
+= out.str();
131
return
*
this
;
132
}
133
134
public
:
135
139
bool
hasAttributes
()
const
{
140
return
141
type
() ==
ElementTypes::Element
||
142
type
() ==
ElementTypes::EmptyElement
;
143
}
144
148
bool
hasAttribute
(
const
std::string&)
const
;
149
153
const
std::map<std::string,std::string>&
attributes
()
const
;
154
158
const
std::string&
attribute
(
const
std::string&)
const
;
159
163
std::string&
attribute
(
const
std::string&);
164
168
struct
Attribute
{
169
173
std::string
name
;
174
178
std::string
value
;
179
183
template
<
class
T>
184
Attribute
(
const
std::string& newName,
185
const
T& newValue)
186
:
name
(newName) {
187
std::ostringstream valueOut;
188
valueOut << std::setprecision(16) << newValue;
189
value
= valueOut.str();
190
191
192
193
}
194
198
inline
bool
operator==
(
const
Attribute
& other) {
199
return
(
name
== other.name &&
200
value
== other.value);
201
}
202
inline
bool
operator!=(
const
Attribute
& other) {
203
return
!(*
this
== other);
204
}
205
};
206
210
Element
&
operator<<
(
const
Attribute
&);
211
215
template
<
class
T>
216
void
appendAttribute
(
const
std::string&
name
,
const
T& t) {
217
*
this
<<
Attribute
(
name
,t);
218
}
219
223
template
<
class
T>
224
void
getFromAttribute
(
const
std::string&
name
, T& t)
const
{
225
std::istringstream in(
attribute
(
name
));
226
in >> std::setprecision(16) >> t;
227
}
228
229
public
:
230
234
bool
hasChildren
()
const
{
235
return
236
type
() ==
ElementTypes::Root
||
237
type
() ==
ElementTypes::Element
;
238
}
239
243
const
std::list<Element>&
children
()
const
;
244
248
std::list<Element>&
children
();
249
253
Element
&
append
(
const
Element
&);
254
258
Element
&
prepend
(
const
Element
&);
259
263
Element
&
operator<<
(
const
Element
&);
264
268
void
insert
(std::list<Element>::iterator,
const
Element
&);
269
273
void
erase
(std::list<Element>::iterator);
274
278
std::list<Element>::const_iterator
279
findFirst
(
int
type
,
const
std::string&
name
)
const
;
280
284
std::pair<std::multimap<std::pair<int,std::string>,std::list<Element>::iterator>::const_iterator,
285
std::multimap<std::pair<int,std::string>,std::list<Element>::iterator>::const_iterator>
286
findAll
(
int
type
,
const
std::string&
name
)
const
;
287
288
private
:
289
293
int
theType
;
294
298
std::string
theNameOrContent
;
299
303
std::map<std::string,std::string>
theAttributes
;
304
308
std::list<Element>
theChildren
;
309
313
std::multimap<std::pair<int,std::string>,std::list<Element>::iterator>
theIndex
;
314
315
private
:
316
320
void
index
();
321
325
void
assertContent
()
const
;
326
330
void
assertNamed
()
const
;
331
335
void
assertAttributes
()
const
;
336
340
void
assertChildren
()
const
;
341
342
};
343
344
}
345
346
#endif
// MYXML_Element_hpp_included
XML::Element
Element represents a (tree of) XML elements.
Definition:
Element.h:56
XML::Element::Element
Element(int newType, const std::string &newNameOrContent="")
The standard constructor.
Definition:
Element.h:69
XML::Element::appendAttribute
void appendAttribute(const std::string &name, const T &t)
Append an attribute to this element.
Definition:
Element.h:216
XML::Element::theAttributes
std::map< std::string, std::string > theAttributes
The attributes of this element.
Definition:
Element.h:303
XML::Element::theNameOrContent
std::string theNameOrContent
The name or character content of the element.
Definition:
Element.h:298
XML::Element::prepend
Element & prepend(const Element &)
Prepend a child element to this element.
XML::Element::operator<<
Element & operator<<(const T &t)
Append to the content of this element.
Definition:
Element.h:126
XML::Element::Element
Element(const Element &other)
The copy constructor.
XML::Element::assertNamed
void assertNamed() const
Assert that this element is named.
XML::Element::name
const std::string & name() const
Return the name of this element.
XML::Element::findAll
std::pair< std::multimap< std::pair< int, std::string >, std::list< Element >::iterator >::const_iterator, std::multimap< std::pair< int, std::string >, std::list< Element >::iterator >::const_iterator > findAll(int type, const std::string &name) const
Find all elements of the given type and name.
XML::Element::hasChildren
bool hasChildren() const
Return true, if this element has children.
Definition:
Element.h:234
XML::Element::theType
int theType
The type of this element.
Definition:
Element.h:293
XML::Element::operator==
friend bool operator==(const XML::Element &one, const XML::Element &two)
Comparison operator.
XML::Element::assertAttributes
void assertAttributes() const
Assert that this element contains attributes.
XML::Element::theChildren
std::list< Element > theChildren
The children elements of this element.
Definition:
Element.h:308
XML::Element::attribute
std::string & attribute(const std::string &)
Access the attribute of the given name.
XML::Element::hasAttribute
bool hasAttribute(const std::string &) const
Return true, if this element contains an attribute of the given name.
XML::Element::append
Element & append(const Element &)
Append a child element to this element.
XML::Element::assertContent
void assertContent() const
Assert that this element got a character content.
XML::Element::erase
void erase(std::list< Element >::iterator)
Erase an element at the given position.
XML::Element::operator<<
Element & operator<<(const Attribute &)
Append an attribute to this element.
XML::Element::attributes
const std::map< std::string, std::string > & attributes() const
Return the attributes.
XML::Element::children
std::list< Element > & children()
Access the list of children elements.
XML::Element::hasAttributes
bool hasAttributes() const
Return true, if this element has attributes.
Definition:
Element.h:139
XML::Element::type
int type() const
Return the type of this element.
Definition:
Element.h:103
XML::Element::operator+
friend XML::Element operator+(const XML::Element &one, const XML::Element &two)
Combine operator.
XML::Element::assertChildren
void assertChildren() const
Assert that this element contains children elements.
XML::Element::operator=
Element & operator=(const Element &other)
Assignment.
XML::Element::getFromAttribute
void getFromAttribute(const std::string &name, T &t) const
Get a value from an attribute.
Definition:
Element.h:224
XML::Element::content
const std::string & content() const
Return the content of this element.
XML::Element::children
const std::list< Element > & children() const
Return the list of children elements.
XML::Element::insert
void insert(std::list< Element >::iterator, const Element &)
Insert an element before the given position.
XML::Element::operator<<
Element & operator<<(const Element &)
Append a child element to this element.
XML::Element::attribute
const std::string & attribute(const std::string &) const
Return the attribute of the given name.
XML::Element::theIndex
std::multimap< std::pair< int, std::string >, std::list< Element >::iterator > theIndex
Index children elements by type and name.
Definition:
Element.h:313
XML::Element::index
void index()
Index elements by type and name.
XML::Element::Element
Element()
The default constructor.
Definition:
Element.h:63
XML::Element::findFirst
std::list< Element >::const_iterator findFirst(int type, const std::string &name) const
Find the first element of the given type and name.
XML::Element::content
std::string & content()
Access the content of this element.
XML::ElementTypes::EnumerateElementTypes
EnumerateElementTypes
The element type enumeration.
Definition:
Element.h:29
XML::ElementTypes::ParsedCharacterData
@ ParsedCharacterData
Character data.
Definition:
Element.h:43
XML::ElementTypes::CharacterData
@ CharacterData
A processing instruction.
Definition:
Element.h:41
XML::ElementTypes::ProcessingInstruction
@ ProcessingInstruction
An element.
Definition:
Element.h:39
XML::ElementTypes::Element
@ Element
An empty element.
Definition:
Element.h:37
XML::ElementTypes::EmptyElement
@ EmptyElement
The entire document.
Definition:
Element.h:35
XML::ElementTypes::Root
@ Root
Unknown element type.
Definition:
Element.h:33
XML::ElementTypes::Comment
@ Comment
Parsed character data.
Definition:
Element.h:45
XML::Element::Attribute
Represent an attribute.
Definition:
Element.h:168
XML::Element::Attribute::value
std::string value
The attribute value.
Definition:
Element.h:178
XML::Element::Attribute::operator==
bool operator==(const Attribute &other)
Comparison operators for attributes.
Definition:
Element.h:198
XML::Element::Attribute::name
std::string name
The attribute name.
Definition:
Element.h:173
XML::Element::Attribute::Attribute
Attribute(const std::string &newName, const T &newValue)
Construct an attribute.
Definition:
Element.h:184
Generated on Thu Jun 20 2024 17:50:53 for Herwig by
1.9.6