-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.cc
181 lines (166 loc) · 4.08 KB
/
Node.cc
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "Node.hh"
#include "YangAst.hh"
#include "YdbStr.hh"
#include "NetconfException.hh"
namespace YDB
{
// Node
Node::Node(DomUtils& domUtils, Node* parent, DOMNode* domNode) :
_domUtils(domUtils),
_parent(parent),
_domNode(domNode)
{
}
Node::~Node()
{
}
DOMNode*
Node::release()
{
std::cerr << "release" << std::endl;
if (_parent != nullptr) {
_parent->removeChild(this);
}
DOMNode* result = _domNode;
_domNode = nullptr;
return result;
}
void
Node::removeChild(Node* node)
{
Key key = node->getKey();
Nodes::iterator i = _nodes.find(key);
if (i != _nodes.end()) {
_nodes.erase(i);
}
}
// InteriorNode
InteriorNode::InteriorNode(DomUtils& domUtils,
Node* parent,
DOMNode* domNode) :
Node(domUtils, parent, domNode)
{
}
// Container
Container::Container(const Yang::Ast::Container* schema,
DomUtils& domUtils,
Node* parent,
DOMNode* domNode) :
InteriorNode(domUtils, parent, domNode),
_schema(schema)
{
YdbStr containerName(domNode->getLocalName());
DOMNodeList* nodeList = domNode->getChildNodes();
for (int i = 0; i < nodeList->getLength(); i++) {
std::cerr << "iu : " << i << " length: "<< nodeList->getLength() << std::endl;
DOMNode* n = nodeList->item(i);
if (n->getNodeType() != DOMNode::ELEMENT_NODE) {
continue;
}
YdbStr name(n->getLocalName());
std::cout << "child name " << name << std::endl;
const Yang::Ast::Node* schemaNode = _schema->findNode(name);
if (schemaNode == nullptr) {
std::stringstream ss;
ss << "Could not find: " << name << " in schema";
throw BadElement(ss, BadElement::Application);
}
std::cout << "schema node: " << schemaNode->getTypeName()
<< std::endl;
YDB::Node* node = schemaNode->create(this, _domUtils, n);
YDB::Key key = node->getKey();
Nodes::iterator j = _nodes.find(key);
if (j != _nodes.end()) {
j->second->merge(node);
} else {
_nodes[key] = node;
}
}
}
const YDB::Key
Container::getKey() const
{
return Key(_schema->getString());
}
void
Container::merge(Node* node)
{
std::cout << "container merge: "<< std::endl;
Nodes& mergeNodes = node->getNodes();
for (auto const& n : mergeNodes) {
Nodes::iterator i = _nodes.find(n.first);
if (i == _nodes.end()) {
_nodes[n.first] = i->second;
} else {
i->second->merge(n.second);
}
}
mergeNodes.clear();
delete node;
std::cout << "container merge end "<< std::endl;
}
// LeafNode
LeafNode::LeafNode(DomUtils& domUtils,
Node* parent,
DOMNode* domNode) :
Node(domUtils, parent, domNode)
{
}
// Leaf
Leaf::Leaf(const Yang::Ast::Leaf* schema,
DomUtils& domUtils,
Node* parent,
DOMNode* domNode) :
LeafNode(domUtils, parent, domNode),
_schema(schema)
{
std::cout << "leaf: " << (void*) domNode <<std::endl;
for (DOMNode* n = domNode->getFirstChild();
n != 0;
n = n->getNextSibling()) {
DOMNode::NodeType nodeType = n->getNodeType();
if (nodeType == DOMNode::NodeType::TEXT_NODE) {
char* value = XMLString::transcode(n->getNodeValue());
std::cout << "leafValue: " << value << std::endl;
XMLString::release(&value);
}
}
std::cout << "leaf end: "<< std::endl;
}
const YDB::Key
Leaf::getKey() const
{
return Key(_schema->getString());
}
void
Leaf::merge(Node* node)
{
std::cout << "LeafMerge" << std::endl;
// @todo refactor use set/getText
DOMNode* domNode = node->getDomNode();
for (DOMNode* n = domNode->getFirstChild();
n != 0;
n = n->getNextSibling()) {
DOMNode::NodeType nodeType = n->getNodeType();
if (nodeType == DOMNode::TEXT_NODE) {
const XMLCh* v = n->getNodeValue();
YdbStr vs(v);
std::cout << (void*) _domNode << " value: " << vs << std::endl;
setText(n->getNodeValue());
}
}
delete node;
std::cout << "LeafMerge end" << std::endl;
}
void
Leaf::setText(const XMLCh* text)
{
DOMNodeList* nodeList = _domNode->getChildNodes();
for (int i = 0; i < nodeList->getLength(); i++) {
DOMNode* n = nodeList->item(i);
if (n->getNodeType() == DOMNode::NodeType::TEXT_NODE) {
n->setNodeValue(text);
}
}
}
}