Menu

[r11]: / branches / dasho / DOM / src / NamedNodeMap.C  Maximize  Restore  History

Download this file

175 lines (137 with data), 4.3 kB

  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
#include "DOM.h"
#include <string.h>
Node* NamedNodeMap::getNamedItem(String name)
{
for (int i=0; i<getLength(); i++)
if ( strcmp(name, item(i)->getNodeName() ) == 0) return item(i);
return NULL;
}
Node* NamedNodeMap::setNamedItem(Node* arg) throw (DOMException&)
{
// not implemented yet:
// WRONG_DOCUMENT_ERR: Raised if arg was created from a different document
// than the one that created the NamedNodeMap.
// NO_MODIFICATION_ALLOWED_ERR: Raised if this NamedNodeMap is readonly.
// INUSE_ATTRIBUTE_ERR: Raised if arg is an Attr that is already an attribute
// of another Element object. The DOM user must explicitly clone Attr nodes
// to re-use them in other elements.
String arg_name = arg->getNodeName();
for (int i=0; i<getLength(); i++)
if ( strcmp(arg_name, array[i]->getNodeName() ) == 0)
{
Node* oldNode = array[i];
array[i] = arg;
return oldNode;
}
// a node with the same name is not there
if (length==size) enlarge_array();
array[length] = arg;
length++;
return NULL;
}
Node* NamedNodeMap::removeNamedItem(String name) throw (DOMException&)
{
int i;
for (i=0; i<getLength(); i++)
if ( strcmp(name, item(i)->getNodeName() ) == 0) break;
if (i == getLength()) // not found
throw( DOMException(DOMException::NOT_FOUND_ERR, "NamedNodeMap::removeNamedItem") );
// If the found node is an Attr with a default value don't remove it
// not implemented yet
// remove the found node
Node* node = item(i);
for ( ; i < length-1; i++) array[i] = array[i+1];
array[length-1] = NULL;
length--;
return node;
}
Node* NamedNodeMap::item(int index)
{
if ( (index < 0) || (index >= length) ) return NULL;
return array[index];
}
int NamedNodeMap::getLength()
{
return length;
}
void NamedNodeMap::enlarge_array()
{
size += NamedNodeMap::STEP;
Node** newArray = new Node*[size];
for (int i=0; i<size; i++)
newArray[i] = (i<length ? array[i] : NULL);
delete array;
array = newArray;
}
NamedNodeMap* NamedNodeMap::clone()
{
NamedNodeMap* newNNM = new NamedNodeMap;
newNNM->size = size;
newNNM->length = length;
newNNM->array = new Node*[size];
for (int i=0; i<size; i++) newNNM->array[i] = NULL;
for (int i=0; i<length; i++)
if (array[i] != NULL)
{
newNNM->array[i] = array[i]->cloneNode(0);
newNNM->array[i]->setParentNode(array[i]->getParentNode());
}
return newNNM;
}
NamedNodeMap::NamedNodeMap()
{
length = 0;
size = 0;
array = NULL;
}
NamedNodeMap::~NamedNodeMap()
{
length = 0;
size = 0;
delete array;
}
ostream& operator<< (ostream& os, NamedNodeMap& nnm)
{
os << " NamedNodeMap (length = " << nnm.getLength() << ") :" << endl;
for(int i=0; i<nnm.getLength(); i++)
if (nnm.item(i) != NULL)
os << " Name: " << nnm.item(i)->getNodeName()
<< " Value: " << nnm.item(i)->getNodeValue()
<< " Type: " << nnm.item(i)->getNodeType() << endl;
return os;
}
void NamedNodeMap::test()
{
// not implemented yet
NamedNodeMap* nnm = new NamedNodeMap;
cout << "Empty NNM: " << endl << *nnm << "-----------------------" << endl;
Node* n1 = new Node(1, "Element1", "Value");
Node* n2 = new Node(1, "Element2", "Value");
Node* n3 = new Node(1, "Element3", "Value");
Node* n4 = new Node(1, "Element4", "Value");
nnm->setNamedItem(n1);
nnm->setNamedItem(n2);
nnm->setNamedItem(n3);
nnm->setNamedItem(n4);
cout << *nnm << "-----------------------" << endl;
Node* n5 = new Node(2, "Element3", "Valueless");
Node* n6 = nnm->setNamedItem(n5);
cout << *n6 << endl;
cout << *nnm << "-----------------------" << endl;
n6 = nnm->getNamedItem("Element1");
if (n6==NULL) cout << "NULL" << endl;
else cout << *n6 << endl;
n6 = nnm->getNamedItem("XXX");
if (n6==NULL) cout << "NULL" << endl;
else cout << *n6 << endl;
n6 = nnm->removeNamedItem("Element1");
cout << *nnm << "-----------------------" << endl;
if (n6==NULL) cout << "NULL" << endl;
else cout << *n6 << endl;
try { nnm->removeNamedItem("XXX");}
catch (DOMException& e) { cout << e << endl; }
NamedNodeMap* nnm1 = nnm->clone();
nnm1->removeNamedItem("Element2");
cout << *nnm << "-----------------------" << endl;
cout << *nnm1 << "-----------------------" << endl;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.