-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElementNode.cpp
107 lines (82 loc) · 2.73 KB
/
ElementNode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
////////////////////////////////////////////////////////////////////////////////
//! \file ElementNode.cpp
//! \brief The ElementNode class definition.
//! \author Chris Oldwood
#include "Common.hpp"
#include "ElementNode.hpp"
#include "TextNode.hpp"
namespace XML
{
////////////////////////////////////////////////////////////////////////////////
//! Default constructor.
ElementNode::ElementNode()
: NodeContainer(this)
, m_name()
, m_attributes()
{
}
////////////////////////////////////////////////////////////////////////////////
//! Construction from the element name.
ElementNode::ElementNode(const tstring& name_)
: NodeContainer(this)
, m_name(name_)
, m_attributes()
{
}
////////////////////////////////////////////////////////////////////////////////
//! Construction from an element name and single attribute.
ElementNode::ElementNode(const tstring& name_, AttributePtr attribute)
: NodeContainer(this)
, m_name(name_)
, m_attributes(attribute)
{
}
////////////////////////////////////////////////////////////////////////////////
//! Construction from an element name and attributes.
ElementNode::ElementNode(const tstring& name_, const Attributes& attributes)
: NodeContainer(this)
, m_name(name_)
, m_attributes(attributes)
{
}
////////////////////////////////////////////////////////////////////////////////
//! Construction from an element name and a range of child nodes.
ElementNode::ElementNode(const tstring& name_, NodePtr* begin, NodePtr* end)
: NodeContainer(this)
, m_name(name_)
{
for (NodePtr* it = begin; it != end; ++it)
appendChild(*it);
}
////////////////////////////////////////////////////////////////////////////////
//! Destructor.
ElementNode::~ElementNode()
{
}
////////////////////////////////////////////////////////////////////////////////
//! Get the child text node value if it exists.
tstring ElementNode::getTextValue() const
{
if (getChildCount() == 0)
return TXT("");
if (getChildCount() != 1)
throw Core::BadLogicException(TXT("Can't retrieve text value when more than 1 child node exists"));
NodePtr child = getChild(0);
if (child->type() != TEXT_NODE)
throw Core::BadLogicException(TXT("Can't retrieve text value when child not a text node"));
return Core::dynamic_ptr_cast<TextNode>(child)->text();
}
////////////////////////////////////////////////////////////////////////////////
//! Find the first element node matching the given name.
Core::RefCntPtr<ElementNode> ElementNode::findFirstElement(const tstring& name_) const
{
for (const_iterator it = beginChild(); it != endChild(); ++it)
{
ElementNodePtr element = Core::dynamic_ptr_cast<ElementNode>(*it);
if ( (!element.empty()) && (element->name() == name_) )
return element;
}
return ElementNodePtr();
}
//namespace XML
}