Debian Codegraph @

protolib/src/manet/manetGraphML.cpp

raw
1 #include <manetGraphML.h>
2 #include <protoDebug.h>
3
4 ManetGraphMLParser::ManetGraphMLParser() : XMLName(NULL), indexes(0)
5 {
6 xmlInitParser();
7 }
8
9 ManetGraphMLParser::~ManetGraphMLParser()
10 {
11 xmlCleanupParser();
12 oldindexkeylist.Empty();
13 indexkeylist.Empty();
14 namedkeylist.Destroy();
15
16 attributelist.Destroy();
17 if (NULL != XMLName)
18 {
19 delete[] XMLName;
20 XMLName = NULL;
21 }
22 }
23
24 const char* ManetGraphMLParser::GetString(NetGraph::Interface& iface) const
25 {
26 if(iface.GetDefaultAddress().IsValid())
27 {
28 return iface.GetDefaultAddress().GetHostString();
29 }
30 else
31 {
32 return iface.GetName();
33 }
34 }
35
36 const char* ManetGraphMLParser::GetString(NetGraph::Node& node) const
37 {
38 return GetString(*node.GetDefaultInterface());
39 }
40
41
42 bool ManetGraphMLParser::AttributeKey::Init(const char* theIndex,const char* theName,const char* theType,const char* theDomain, const char* theOldIndex,const char*theDefault)
43 {
44 if((NULL != index) || (NULL != name) || (NULL != oldindex) || (Types::INVALID !=type) || (Domains::INVALID != domain) )
45 {
46 PLOG(PL_ERROR,"ManetGraphMLParser::AttributeKey::Init: Error attempting to Init AttributeKey which has already called Init!\n");
47 return false;
48 }
49 index = new char[strlen(theIndex)+1];
50 name = new char[strlen(theName)+1];
51
52 if((NULL == index) || (NULL == name))
53 {
54 PLOG(PL_ERROR,"ManetGraphMLParser::AttributeKey::Init: Error allocating space for index or name strings\n");
55 return false;
56 }
57 if(NULL != theOldIndex)
58 {
59 oldindex = new char[strlen(theOldIndex)+1];
60 if(NULL == oldindex)
61 {
62 PLOG(PL_ERROR,"ManetGraphMLParser::AttributeKey::Init: Error allocating space for oldindex string\n");
63 return false;
64 }
65 strcpy(oldindex,theOldIndex);
66 }
67 strcpy(name,theName);
68 strcpy(index,theIndex);
69 if(!SetType(theType))
70 {
71 PLOG(PL_ERROR,"ManetGraphMLParser::AttributeKey::Init: Error setting the type to %s\n",theType);
72 return false;
73 }
74 if(NULL != theDomain)
75 {
76 if(!SetDomain(theDomain))
77 {
78 PLOG(PL_ERROR,"ManetGraphMLParser::AttributeKey::Init: Error setting the domain to %s\n",theDomain);
79 return false;
80 }
81 }
82 if(NULL != theDefault)
83 {
84 if(!SetDefault(theDefault))
85 {
86 PLOG(PL_ERROR,"ManetGraphMLParser::AttributeKey::Init: Error setting the default to %s\n",theDefault);
87 return false;
88 }
89 }
90 return true;
91 }
92
93
94 bool ManetGraphMLParser::AttributeKey::Set(const char* theIndex,const char* theName,const char* theType,const char* theDomain, const char* theOldIndex,const char*theDefault)
95 {
96 if(NULL != index)
97 delete[] index;
98 if(NULL != name)
99 delete[] name;
100 if(NULL !=oldindex)
101 delete[] oldindex;
102 index = new char[strlen(theIndex)+1];
103 name = new char[strlen(theName)+1];
104
105 if((NULL == index) || (NULL == name))
106 {
107 PLOG(PL_ERROR,"ManetGraphMLParser::AttributeKey::Set: Error allocating space for index or name strings\n");
108 return false;
109 }
110 if(NULL != theOldIndex)
111 {
112 oldindex = new char[strlen(theOldIndex)+1];
113 if(NULL == oldindex)
114 {
115 PLOG(PL_ERROR,"ManetGraphMLParser::AttributeKey::Set: Error allocating space for oldindex string\n");
116 return false;
117 }
118 strcpy(oldindex,theOldIndex);
119 }
120 strcpy(name,theName);
121 strcpy(index,theIndex);
122
123 if(!SetType(theType))
124 {
125 PLOG(PL_ERROR,"ManetGraphMLParser::AttributeKey::Set: Error setting the type to %s\n",theType);
126 return false;
127 }
128 if(NULL != theDomain)
129 {
130 if(!SetDomain(theDomain))
131 {
132 PLOG(PL_ERROR,"ManetGraphMLParser::AttributeKey::Set: Error setting the domain to %s\n",theDomain);
133 return false;
134 }
135 }
136 if(NULL != theDefault)
137 {
138 if(!SetDefault(theDefault))
139 {
140 PLOG(PL_ERROR,"ManetGraphMLParser::AttributeKey::Set: Error setting the default to %s\n",theDefault);
141 return false;
142 }
143 }
144 return true;
145 }
146
147 bool ManetGraphMLParser::AttributeKey::SetType(const char* theType)
148 {
149 if((!strcmp(theType,"bool")) ||
150 (!strcmp(theType,"Bool")) ||
151 (!strcmp(theType,"BOOL")) ||
152 (!strcmp(theType,"boolean")) ||
153 (!strcmp(theType,"Boolean")) ||
154 (!strcmp(theType,"BOOLEAN"))) {
155 type = Types::BOOL;
156 } else if
157 ((!strcmp(theType,"int")) ||
158 (!strcmp(theType,"Int")) ||
159 (!strcmp(theType,"INT")) ||
160 (!strcmp(theType,"integer")) ||
161 (!strcmp(theType,"Integer")) ||
162 (!strcmp(theType,"INTEGER"))) {
163 type = Types::INT;
164 } else if
165 ((!strcmp(theType,"long")) ||
166 (!strcmp(theType,"Long")) ||
167 (!strcmp(theType,"LONG"))) {
168 type = Types::LONG;
169 } else if
170 ((!strcmp(theType,"float")) ||
171 (!strcmp(theType,"Float")) ||
172 (!strcmp(theType,"FLOAT"))) {
173 type = Types::FLOAT;
174 } else if
175 ((!strcmp(theType,"double")) ||
176 (!strcmp(theType,"Double")) ||
177 (!strcmp(theType,"DOUBLE"))) {
178 type = Types::DOUBLE;
179 } else if
180 ((!strcmp(theType,"string")) ||
181 (!strcmp(theType,"String")) ||
182 (!strcmp(theType,"STRING"))) {
183 type = Types::STRING;
184 } else {
185 type = Types::INVALID;
186 return false;
187 }
188 return true;
189 }
190 bool
191 ManetGraphMLParser::AttributeKey::SetDomain(const char* theDomain)
192 {
193 if((!strcmp(theDomain,"graph")) ||
194 (!strcmp(theDomain,"Graph")) ||
195 (!strcmp(theDomain,"GRAPH"))) {
196 domain = Domains::GRAPH;
197 } else if
198 ((!strcmp(theDomain,"node")) ||
199 (!strcmp(theDomain,"Node")) ||
200 (!strcmp(theDomain,"NODE"))) {
201 domain = Domains::NODE;
202 } else if
203 ((!strcmp(theDomain,"edge")) ||
204 (!strcmp(theDomain,"Edge")) ||
205 (!strcmp(theDomain,"EDGE"))) {
206 domain = Domains::EDGE;
207 } else if
208 ((!strcmp(theDomain,"all")) ||
209 (!strcmp(theDomain,"All")) ||
210 (!strcmp(theDomain,"ALL"))) {
211 domain = Domains::ALL;
212 } else {
213 domain = Domains::INVALID;
214 return false;
215 }
216 return true;
217 }
218 bool
219 ManetGraphMLParser::AttributeKey::SetDefault(const char* theDefault)
220 {
221 if(NULL == theDefault)
222 {
223 PLOG(PL_ERROR,"ManetGraphMLParser::AttributeKey::SetDefault: default value pointer is NULL!\n");
224 return false;
225 }
226 if(NULL != defaultvalue)
227 {
228 //PLOG(PL_WARN,"ManetGraphMLParser::AttributeKey::SetDefault: default has already been set to \"%s\" and is being switched to \"%s\"\n",defaultvalue,theDefault);
229 delete[] defaultvalue;
230 }
231 defaultvalue = new char[strlen(theDefault)+1];
232 if(NULL == defaultvalue)
233 {
234 PLOG(PL_ERROR,"ManetGraphMLParser::AttributeKey::SetDefault: error allocing space for the defaultvalue pointer!\n");
235 return false;
236 }
237 if(!strcpy(defaultvalue,theDefault))
238 {
239 PLOG(PL_ERROR,"ManetGraphMLParser::AttributeKey::SetDefault: Error copying the string!\n");
240 delete[] defaultvalue;
241 return false;
242 }
243 return true;
244 }
245 ManetGraphMLParser::AttributeKey::~AttributeKey()
246 {
247 if(NULL != index)
248 {
249 delete[] index;
250 index = NULL;
251 }
252 if(NULL != name)
253 {
254 delete[] name;
255 name = NULL;
256 }
257 if(NULL != oldindex)
258 {
259 delete[] oldindex;
260 oldindex = NULL;
261 }
262 if(NULL != defaultvalue)
263 {
264 delete[] defaultvalue;
265 defaultvalue = NULL;
266 }
267 }
268 bool
269 ManetGraphMLParser::SetXMLName(const char* theName)
270 {
271 if(NULL == theName)
272 {
273 PLOG(PL_ERROR,"ManetGraphMLParser::SetXMLName() error setting NULL name!\n");
274 return false;
275 }
276 if(NULL != XMLName)
277 {
278 delete[] XMLName;
279 }
280 XMLName = new char[strlen(theName)+1];
281 if(NULL == XMLName)
282 {
283 PLOG(PL_ERROR,"ManetGraphMLParser::SetXMLName() error allocating space for name array!\n");
284 return false;
285 }
286 strcpy(XMLName,theName);
287 return true;
288 }
289 bool
290 ManetGraphMLParser::SetAttributeKey(const char* theName,const char* theType, const char* theDomain, const char* theOldKey,const char* theDefault)
291 {
292 PLOG(PL_INFO,"ManetGraphMLParser::SetAttributeKey: Enter\n");
293 //lets look to see if the name exists already
294 AttributeKey* theKey;
295 theKey = FindAttributeKey(theName);
296
297 if(NULL == theKey)
298 {
299 PLOG(PL_DETAIL,"ManetGraphMLParser::SetAttributeKey: didn't find the key\n");
300 if(!AddAttributeKey(theName,theType,theDomain,theOldKey,theDefault))
301 {
302 PLOG(PL_ERROR,"ManetGraphMLParser::SetAttributeKey: error adding the key \"%s\"\n",theName);
303 return false;
304 }
305 return true;
306 } else {
307 char tempIndex[20];
308 strcpy(tempIndex,theKey->GetIndex()); //we need to copy it over because setting it will delete the old value.
309 if(!theKey->Set(tempIndex,theName,theType,theDomain,theOldKey,theDefault))
310 {
311 PLOG(PL_ERROR,"ManetGraphMLParser::SetAttributeKey Error Init'ing the theKey with index \"%s\" and name \"%s\"\n",theKey->GetIndex(),theName);
312 return false;
313 }
314 return true;
315 }
316 PLOG(PL_ERROR,"ManetGraphMLParser::SetAttributeKey: error got to the end of the function and shouldn't!\n");
317
318 return false; //shouldn't get here
319 }
320
321 bool
322 ManetGraphMLParser::SetAttribute(const char* theName, const char* theValue)
323 {
324 PLOG(PL_DETAIL,"ManetGraphMLParser::SetAttribute(name=%s,theValue=%s)\n",theName,theValue);
325 const char* theIndex = FindAttributeIndex(theName);
326 if(NULL == theIndex)
327 {
328 PLOG(PL_ERROR,"ManetGraphMLParser::SetAttribute(Graph): Error finding index for attribute %s\n",theName);
329 return false;
330 }
331 PLOG(PL_DETAIL,"ManetGraphMLParser::SetAttribute(Node,name=%s,theValue=%s) found index %s\n",theName,theValue,theIndex);
332 char theLookup[250];
333 GetLookup(theLookup,250);
334 ManetGraphMLParser::Attribute* theAttribute = attributelist.FindAttribute(theLookup,theIndex);
335 //this doesn't work it only finds one not all!
336 //ManetGraphMLParser::Attribute* theAttribute = attributelist.Find(theLookup,strlen(theLookup)*8);
337 if(NULL == theAttribute)
338 {
339 PLOG(PL_DETAIL,"ManetGraphMLParser::SetAttribute(name=%s,theValue=%s) making new one\n",theName,theValue);
340 return AddAttribute(theName,theValue);
341 }
342 else
343 {
344 char tempIndex[20];
345 strcpy(tempIndex,theIndex); //we need to copy it over because setting it will delete the old value.
346 PLOG(PL_DETAIL,"ManetGraphMLParser::SetAttribute(name=%s,theValue=%s) Updating old one with index=%s\n",theName,theValue,theIndex);
347 if(!theAttribute->Set(theLookup,tempIndex,theValue))
348 {
349 PLOG(PL_ERROR,"ManetGraphMLParser::SetAttribute(): Error setting the attribute\n");
350 return false;
351 }
352 return true;
353 }
354 return false; //should never get here
355 }
356
357 bool
358 ManetGraphMLParser::SetAttribute(NetGraph::Node& node, const char* theName, const char* theValue)
359 {
360 PLOG(PL_DETAIL,"ManetGraphMLParser::SetAttribute(Node,name=%s,theValue=%s)\n",theName,theValue);
361 const char* theIndex = FindAttributeIndex(theName);
362 if(NULL == theIndex)
363 {
364 PLOG(PL_ERROR,"ManetGraphMLParser::SetAttribute(Node): Error finding index for attribute %s\n",theName);
365 return false;
366 }
367 PLOG(PL_DETAIL,"ManetGraphMLParser::SetAttribute(Node,name=%s,theValue=%s) found index %s\n",theName,theValue,theIndex);
368 char theLookup[250];
369 GetLookup(theLookup,250,node);
370 ManetGraphMLParser::Attribute* theAttribute = attributelist.FindAttribute(theLookup,theIndex);
371 //this doesn't work it only finds one not all!
372 //ManetGraphMLParser::Attribute* theAttribute = attributelist.Find(theLookup,strlen(theLookup)*8);
373 if(NULL == theAttribute)
374 {
375 PLOG(PL_DETAIL,"ManetGraphMLParser::SetAttribute(Node,name=%s,theValue=%s) making new one\n",theName,theValue);
376 return AddAttribute(node,theName,theValue);
377 }
378 else
379 {
380 char tempIndex[20];
381 strcpy(tempIndex,theIndex); //we need to copy it over because setting it will delete the old value.
382 PLOG(PL_DETAIL,"ManetGraphMLParser::SetAttribute(Node,name=%s,theValue=%s) Updating old one with index=%s\n",theName,theValue,theIndex);
383 if(!theAttribute->Set(theLookup,tempIndex,theValue))
384 {
385 PLOG(PL_ERROR,"ManetGraphMLParser::SetAttribute(Node): Error setting the attribute\n");
386 return false;
387 }
388 return true;
389 }
390 return false; //should never get here
391 }
392 bool ManetGraphMLParser::SetAttribute(NetGraph::Link& link, const char* theName, const char* theValue)
393 {
394 PLOG(PL_INFO,"ManetGraphMLParser::SetAttribute(Link): Enter\n");
395 const char* theIndex = FindAttributeIndex(theName);
396 if(NULL == theIndex)
397 {
398 PLOG(PL_ERROR,"ManetGraphMLParser::SetAttribute(Link): Error finding index for attribute %s\n",theName);
399 return false;
400 }
401 char theLookup[250];
402 GetLookup(theLookup,250,link);
403
404 ManetGraphMLParser::Attribute* theAttribute = attributelist.FindAttribute(theLookup,theIndex);
405 if(NULL == theAttribute)
406 {
407 PLOG(PL_DETAIL,"ManetGraphMLParser::SetAttribute(Link): didn't find existing attribute calling AddAttribute with theName=\"%s\", theValue=\"%s\"\n",theName,theValue);
408 if(!AddAttribute(link,theName,theValue))
409 {
410 PLOG(PL_ERROR,"ManetGraphMLParser::SetAttribute: error on calling AddAttribute\n");
411 return false;
412 }
413 return true;
414 }
415 else
416 {
417 PLOG(PL_DETAIL,"ManetGraphMLParser::SetAttribute(Link): found existing attribute Attribute.Set with theName=\"%s\", theValue=\"%s\"\n",theName,theValue);
418 char tempIndex[20];
419 strcpy(tempIndex,theIndex); //we need to copy it over because setting it will delete the old value.
420 if(!theAttribute->Set(theLookup,tempIndex,theValue))
421 {
422 PLOG(PL_ERROR,"ManetGraphMLParser::SetAttribute(link): Error setting the attribute\n");
423 return false;
424 }
425 return true;
426 }
427 PLOG(PL_ERROR,"ManetGraphMLParser::SetAttribute(link): Error reaching bottom of function and it shouldn't!\n");
428 return false; //should never get here
429 }
430
431 bool ManetGraphMLParser::SetAttribute(NetGraph::Interface& interface, const char* theName, const char* theValue)
432 {
433 const char* theIndex = FindAttributeIndex(theName);
434 if(NULL == theIndex)
435 {
436 PLOG(PL_ERROR,"ManetGraphMLParser::SetAttribute(Interface): Error finding index for attribute %s\n",theName);
437 return false;
438 }
439 char theLookup[250];
440 GetLookup(theLookup,250,interface);
441 ManetGraphMLParser::Attribute* theAttribute = attributelist.FindAttribute(theLookup,theIndex);
442 //this doesn't work it only finds one not all!
443
444 //ManetGraphMLParser::Attribute* theAttribute = attributelist.Find(theLookup,strlen(theLookup)*8);
445 if(NULL == theAttribute)
446 {
447 return AddAttribute(interface,theName,theValue);
448 }
449 else
450 {
451 if(!theAttribute->Set(theLookup,theIndex,theValue))
452 {
453 PLOG(PL_ERROR,"ManetGraphMLParser::SetAttribute(link): Error setting the attribute\n");
454 return false;
455 }
456 return true;
457 }
458 return false; //should never get here
459 }
460
461
462 bool
463 ManetGraphMLParser::AddAttributeKey(const char* theName,const char* theType, const char* theDomain, const char* theOldKey,const char* theDefault)
464 {
465 //lets look to see if the name exists already
466 PLOG(PL_INFO,"ManetGraphMLParser::AddAttributeKey(): Enter\n");
467 if(NULL != FindAttributeIndex(theName))
468 {
469 PLOG(PL_ERROR,"ManetGraphMLParser::AddAttributeKey has been called with attribute name \"%s\" which already exists!\n",theName);
470 return false;
471 }
472 PLOG(PL_DETAIL,"ManetGraphMLParser::AddAttributeKey() making new key\n");
473 AttributeKey* newKey = new AttributeKey;
474 if(NULL == newKey)
475 {
476 PLOG(PL_ERROR,"ManetGraphMLParser::AddAttributeKey Error allocating a new AttributeKey\n");
477 return false;
478 }
479 char theIndex[20];//this will work as we are only using d%d as our indexes.
480 sprintf(theIndex,"d%d",indexes++);
481 PLOG(PL_DETAIL,"ManetGraphMLParser::AddAttributeKey() made key initing with \"%s\"\n",theIndex);
482 if(!newKey->Init(theIndex,theName,theType,theDomain,theOldKey,theDefault))
483 {
484 PLOG(PL_ERROR,"ManetGraphMLParser::AddAttributeKey Error Init'ing the newKey with index \"d%d\" and name \"%s\"\n");
485 delete newKey;
486 indexes--;
487 return false;
488 }
489 indexkeylist.Insert(*newKey);
490 namedkeylist.Insert(*newKey);
491 if(NULL != theOldKey)
492 oldindexkeylist.Insert(*newKey);
493 PLOG(PL_DETAIL,"ManetGraphMLParser::AddAttributeKey() return true\n");
494 return true;
495 }
496
497 bool ManetGraphMLParser::AddAttribute(const char* theName, const char* theValue)
498 {
499 const char* theIndex = FindAttributeIndex(theName);
500 if(NULL == theIndex)
501 {
502 PLOG(PL_ERROR,"ManetGraphMLParser::AddAttribute(): Error finding index for attribute %s\n",theName);
503 return false;
504 }
505 Attribute* newAttribute = new Attribute();
506 if(NULL == newAttribute)
507 {
508 PLOG(PL_ERROR,"ManetGraphMLParser::AddAttribute(): Error allocating attribute\n");
509 return false;
510 }
511
512 char theLookup[250]; //TBD
513 GetLookup(theLookup,250);
514 if(!newAttribute->Init(theLookup,theIndex,theValue))
515 {
516 PLOG(PL_ERROR,"ManetGraphMLParser::AddAttribute(Node): Error init the attribute\n");
517 return false;
518 }
519 if(!attributelist.Insert(*newAttribute))
520 {
521 PLOG(PL_ERROR,"ManetGraphMLParser::AddAttribute(): Error inserting the attribute\n");
522 return false;
523 }
524 DMSG(7,"ManetGraphMLParser::AddAttribute(%s,%s) added successfully\n",theName,theValue);
525 return true;
526 }
527
528 bool ManetGraphMLParser::AddAttribute(NetGraph::Node& node, const char* theName, const char* theValue)
529 {
530 const char* theIndex = FindAttributeIndex(theName);
531 if(NULL == theIndex)
532 {
533 PLOG(PL_ERROR,"ManetGraphMLParser::AddAttribute(Node): Error finding index for attribute %s\n",theName);
534 return false;
535 }
536 Attribute* newAttribute = new Attribute();
537 if(NULL == newAttribute)
538 {
539 PLOG(PL_ERROR,"ManetGraphMLParser::AddAttribute(Node): Error allocating attribute\n");
540 return false;
541 }
542
543 char theLookup[250]; //TBD
544 GetLookup(theLookup,250,node);
545 if(!newAttribute->Init(theLookup,theIndex,theValue))
546 {
547 PLOG(PL_ERROR,"ManetGraphMLParser::AddAttribute(Node): Error init the attribute\n");
548 return false;
549 }
550 if(!attributelist.Insert(*newAttribute))
551 {
552 PLOG(PL_ERROR,"ManetGraphMLParser::AddAttribute(Node): Error inserting the attribute\n");
553 return false;
554 }
555 DMSG(7,"ManetGraphMLParser::AddAttribute(node,%s,%s) added successfully\n",theName,theValue);
556 return true;
557 }
558 bool ManetGraphMLParser::AddAttribute(NetGraph::Link& link, const char* theName, const char* theValue)
559 {
560 const char* theIndex = FindAttributeIndex(theName);
561 if(NULL == theIndex)
562 {
563 PLOG(PL_ERROR,"ManetGraphMLParser::AddAttribute(Link): Error finding index for attribute %s\n",theName);
564 return false;
565 }
566 Attribute* newAttribute = new Attribute();
567 if(NULL == newAttribute)
568 {
569 PLOG(PL_ERROR,"ManetGraphMLParser::AddAttribute(Link): Error allocating attribute\n");
570 return false;
571 }
572 char theLookup[250];
573 GetLookup(theLookup,250,link);
574
575 if(!newAttribute->Init(theLookup,theIndex,theValue))
576 {
577 PLOG(PL_ERROR,"ManetGraphMLParser::AddAttribute(Link): Error initing the attribute\n");
578 return false;
579 }
580 if(!attributelist.Insert(*newAttribute))
581 {
582 PLOG(PL_ERROR,"ManetGraphMLParser::AddAttribute(Link): Error inserting the attribute\n");
583 return false;
584 }
585 return true;
586 }
587 bool ManetGraphMLParser::AddAttribute(NetGraph::Interface& interface, const char* theName, const char* theValue)
588 {
589 const char* theIndex = FindAttributeIndex(theName);
590 if(NULL == theIndex)
591 {
592 PLOG(PL_ERROR,"ManetGraphMLParser::AddAttribute(interface): Error finding index for attribute %s\n",theName);
593 return false;
594 }
595 Attribute* newAttribute = new Attribute();
596 if(NULL == newAttribute)
597 {
598 PLOG(PL_ERROR,"ManetGraphMLParser::AddAttribute(Link): Error allocating attribute\n");
599 return false;
600 }
601 char theLookup[250];
602
603 GetLookup(theLookup,250,interface);
604 if(!newAttribute->Init(theLookup,theIndex,theValue))
605 {
606 PLOG(PL_ERROR,"ManetGraphMLParser::AddAttribute(Interface): Error init the attribute\n");
607 return false;
608 }
609 if(!attributelist.Insert(*newAttribute))
610 {
611 PLOG(PL_ERROR,"ManetGraphMLParser::AddAttribute(Interface): Error inserting the attribute\n");
612 return false;
613 }
614 return true;
615 }
616
617 bool ManetGraphMLParser::GetLookup(char* theLookup,unsigned int maxlen)
618 {
619 sprintf(theLookup,"thisGraph");//don't name interfaces/links/nodes thisGraph!
620 return true;
621 }
622
623 bool ManetGraphMLParser::GetLookup(char* theLookup,unsigned int maxlen,NetGraph::Node& node)
624 {
625 if(strlen(GetString(node))>maxlen)
626 {
627 PLOG(PL_ERROR,"ManetGraphMLParser::GetLookup(node) node string is longer than max leng\n");
628 return false;
629 }
630 sprintf(theLookup,"node:%s",GetString(node));
631 return true;
632 }
633 bool ManetGraphMLParser::GetLookup(char* theLookup,unsigned int maxlen,NetGraph::Link& link)
634 {
635 char* sourceName;
636 char* sourcePortName;
637 char* targetName;
638 char* targetPortName;
639 sourceName = new char[strlen(GetString(link.GetSrc()->GetNode()))+1];
640 strcpy(sourceName,GetString(link.GetSrc()->GetNode()));
641 sourcePortName = new char[strlen(GetString(*link.GetSrc()))+1];
642 strcpy(sourcePortName,GetString(*link.GetSrc()));
643 targetName = new char[strlen(GetString(link.GetDst()->GetNode()))+1];
644 strcpy(targetName,GetString(link.GetDst()->GetNode()));
645 targetPortName = new char[strlen(GetString(*link.GetDst()))+1];
646 strcpy(targetPortName,GetString(*link.GetDst()));
647 if(strlen(sourceName)+strlen(sourcePortName)+strlen(targetName)+strlen(targetPortName)+21>maxlen)
648 {
649 PLOG(PL_ERROR,"ManetGraphMLParser::GetLookup(link) link string is longer than max length\n");
650 delete[] sourceName;
651 delete[] sourcePortName;
652 delete[] targetName;
653 delete[] targetPortName;
654 return false;
655 }
656 sprintf(theLookup,"edge:source:%s:%s:dest:%s:%s",sourceName,sourcePortName,targetName,targetPortName);
657
658 delete[] sourceName;
659 delete[] sourcePortName;
660 delete[] targetName;
661 delete[] targetPortName;
662
663 return true;
664 }
665 bool ManetGraphMLParser::GetLookup(char* theLookup,unsigned int maxlen,NetGraph::Interface& interface)
666 {
667 char *portName;
668 portName = new char[strlen(GetString(interface))+1];
669 strcpy(portName,GetString(interface));
670 if(strlen(portName)+strlen(GetString(interface.GetNode()))+12>maxlen)
671 {
672 PLOG(PL_ERROR,"ManetGraphMLParser::GetLookup(interface) interface string is longer than max length\n");
673 delete[] portName;
674 return false;
675 }
676 sprintf(theLookup,"node:%s:port:%s",GetString(interface.GetNode()),portName);
677 delete[] portName;
678 return true;
679 }
680
681 const char* ManetGraphMLParser::FindAttributeIndex(const char* theName)
682 {
683 PLOG(PL_DETAIL,"ManetGraphMLParser::FindAttributeIndex(%s): Enter\n",theName);
684 AttributeKey* theKey = namedkeylist.Find(theName,strlen(theName)*8);
685 //PLOG(PL_DETAIL,"ManetGraphMLParser::FindAttributeIndex(): after the find call\n");
686 if(NULL == theKey)
687 {
688 PLOG(PL_INFO,"ManetGraphMLParser::FindAttributeIndex(): didn't exist returning NULL\n");
689 return NULL;
690 }
691 //PLOG(PL_DETAIL,"ManetGraphMLParser::FindAttributeIndex(): Found a key returning its index\n");
692 return theKey->GetIndex();
693 }
694 ManetGraphMLParser::AttributeKey* ManetGraphMLParser::FindAttributeKey(const char* theName)
695 {
696 return namedkeylist.Find(theName,strlen(theName)*8);
697 }
698 ManetGraphMLParser::AttributeKey* ManetGraphMLParser::FindAttributeKeyByOldIndex(const char* theOldIndex)
699 {
700 return oldindexkeylist.Find(theOldIndex,strlen(theOldIndex)*8);
701 }
702
703 bool
704 ManetGraphMLParser::Attribute::Set(const char* theLookupvalue, const char* theIndex,const char* theValue)
705 {
706 if(NULL!=lookupvalue)
707 {
708 if(strcmp(theLookupvalue,lookupvalue))
709 {
710 PLOG(PL_ERROR,"ManetGraphMLParser::Attribute::Set: Error attempting to Set Attribute with new lookupvalue! new!=old \"%s\"!=\"%s\"\n",theLookupvalue,lookupvalue);
711 return false;
712 }
713 delete[] lookupvalue;
714 lookupvalue = NULL;
715 }
716 if(NULL != index)
717 {
718 delete[] index;
719 index = NULL;
720 }
721 if(NULL != value)
722 {
723 delete[] value;
724 value = NULL;
725 }
726 //PLOG(PL_INFO,"ManetGraphMLParser::Attribute::Set: before calling Init\n");
727 if(!Init(theLookupvalue,theIndex,theValue))
728 {
729 PLOG(PL_ERROR,"ManetGraphMLParser::Attribute::Set: Error calling init with new lookup value %s\n",theLookupvalue);
730 return false;
731 }
732 //PLOG(PL_INFO,"ManetGraphMLParser::Attribute::Set: returning true %s\n",theLookupvalue);
733 return true;
734 }
735 bool
736 ManetGraphMLParser::Attribute::Init(const char* theLookupvalue, const char* theIndex,const char* theValue)
737 {
738 if((NULL!=lookupvalue) || (NULL != index) || (NULL != value))
739 {
740 PLOG(PL_ERROR,"ManetGraphMLParser::Attribute::Init: Error attempting to Init Attribute which has already called Init!\n");
741 return false;
742 }
743 lookupvalue = new char[strlen(theLookupvalue)+1];
744 index = new char[strlen(theIndex)+1];
745 value = new char[strlen(theValue)+1];
746 if((NULL == lookupvalue) || (NULL == index) || (NULL == value))
747 {
748 PLOG(PL_ERROR,"ManetGraphMLParser::Attribute::Init: Error allocating space for lookup/index/value strings\n");
749 return false;
750 }
751 strcpy(lookupvalue,theLookupvalue);
752 strcpy(value,theValue);
753 strcpy(index,theIndex);
754 return true;
755 }
756 ManetGraphMLParser::Attribute::~Attribute()
757 {
758 delete[] lookupvalue;
759 delete[] index;
760 delete[] value;
761 }
762 bool ManetGraphMLParser::ReadXMLNode(xmlTextReader* readerPtr,
763 NetGraph& graph,
764 char* parentXMLNodeID,
765 bool& isDuplex)
766 {
767 //const xmlChar *name, *value;
768 //int count, depth, type, isempty;
769 const xmlChar *name;
770 int type;
771 type = xmlTextReaderNodeType(readerPtr);
772 if(XML_READER_TYPE_END_ELEMENT==type)
773 return true;
774
775 name = xmlTextReaderConstName(readerPtr);
776 if (name == NULL)
777 name = BAD_CAST "--";
778 //value = xmlTextReaderConstValue(readerPtr);
779 //depth = xmlTextReaderDepth(readerPtr);
780 //isempty = xmlTextReaderIsEmptyElement(readerPtr);
781 //count = xmlTextReaderAttributeCount(readerPtr);
782 //printf("processsing depth=%d, type=%d, name=%s, isempty=%d, value=%s, attributes=%d\n",depth,type,name,isempty,value,count);
783 if(!strcmp("graph",(const char*)name))
784 {
785 //printf("processing graph node\n");
786 xmlChar* graphId;
787 graphId = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"id");
788 if(NULL == graphId)
789 {
790 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode(): Error missing graph id attribute!\n");
791 return false;
792 }
793 SetXMLName((const char*)graphId);
794 xmlFree(graphId);
795 xmlChar* graphEdgeType;
796 graphEdgeType = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"edgedefault");
797 if(!strcmp("directed",(const char*)graphEdgeType))
798 {
799 isDuplex = false;
800 }
801 else
802 {
803 isDuplex = true;
804 }
805 xmlFree(graphEdgeType);
806 //printf("exiting graph node id = \"%s\" is duplex is %s\n",graphId,isDuplex ? "true" : "false");
807 }
808 else if(!strcmp("node",(const char*)name))
809 {
810 xmlChar* nodeId = NULL;
811 while (xmlTextReaderMoveToNextAttribute(readerPtr)>0)
812 {
813 // TRACE(" found attribute %s with name %s\n",xmlTextReaderName(readerPtr),xmlTextReaderConstValue(readerPtr));
814 if(!strcmp((const char*)xmlTextReaderConstName(readerPtr),"id"))
815 {
816 nodeId = xmlTextReaderValue(readerPtr);
817 }
818 }
819 if(NULL == nodeId)
820 {
821 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode(): Error missing node id attribute!\n");
822 return false;
823 }
824 if(strlen((const char*)nodeId) > MAXXMLIDLENGTH)
825 {
826 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode: Error the node id value of \"%s\" is too large\n",nodeId);
827 return false;
828 }
829 memset(parentXMLNodeID, 0, MAXXMLIDLENGTH+1);
830 strcpy(parentXMLNodeID,(const char*)nodeId);
831
832 NetGraph::Interface* interface;
833 ProtoAddress addr;
834 addr.ConvertFromString((const char*)nodeId);
835 if(addr.IsValid())
836 {
837 interface = graph.FindInterface(addr);
838 }
839 else
840 {
841 interface = graph.FindInterfaceByName((const char*)nodeId);
842 }
843 if (NULL == interface)
844 {
845 //Create new node and associated interface
846 NetGraph::Node* node = CreateNode();
847
848 if(NULL == node)
849 {
850 PLOG(PL_ERROR, "NetGraph::ProcessXMLNode: error Creating new node!\n");
851 return -1;
852 }
853 if(addr.IsValid())
854 {
855 interface = CreateInterface(*node,addr);
856 }
857 else
858 {
859 interface = CreateInterface(*node);
860 interface->SetName((const char*)nodeId);
861 }
862 //node->AddInterface(*interface,true);
863 AddInterfaceToNode(*node,*interface,true);
864 //graph.InsertInterface(*interface);
865 AddInterfaceToGraph(graph,*interface);
866 AddNodeToGraph(graph,*node);//this is a hook to let derived graph classes know about added nodes
867 xmlFree(nodeId);
868 }
869 else
870 {
871 //existing node so update anything we might have on it
872 }
873 //TRACE(" exiting node node\n");
874 }
875 else if(!strcmp("port",(const char*)name))
876 {
877 //printf("processing port node\n");
878 xmlChar* portname;
879 portname = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"name");
880 NetGraph::Interface* portinterface;
881 portinterface = graph.FindInterfaceByString((const char*)portname);
882
883 ProtoAddress portAddr;
884 portAddr.ResolveFromString((const char*)portname);
885 if (NULL == portinterface)
886 {
887 //Find the parent node and associated with this interface
888 NetGraph::Interface* interface;
889 interface = graph.FindInterfaceByString(parentXMLNodeID);
890 ProtoAddress addr;
891 addr.ResolveFromString(parentXMLNodeID);
892 if(NULL == interface)
893 {
894 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode: Error finding the parent \"%s\" interface in the graph for port \"%s\"!\n",parentXMLNodeID,portname);
895 return false;
896 }
897
898 NetGraph::Node& node = interface->GetNode();
899 //NetGraph::Node *node = &interface->GetNode();
900 if(portAddr.IsValid())
901 {
902 portinterface = new NetGraph::Interface(node,portAddr);
903 }
904 else
905 {
906 portinterface = CreateInterface(node);
907 portinterface->SetName((const char*)portname);
908 }
909 //node->SetPosition(x, y);
910 //node.AddInterface(*portinterface,false);
911 AddInterfaceToNode(node,*portinterface,false);
912 //graph.InsertInterface(*portinterface);
913 AddInterfaceToGraph(graph,*interface);
914 //printf(" just added port \"%s\" to node \"%s\"\n",portname,parentXMLNodeID);
915 }
916 else
917 {
918 //portname exists already so we just need to update any info associated with it
919 }
920 //printf("exiting port node\n");
921 /* PLOG(PL_ERROR,"nodeid=%s, depth=%d, type=%d, name=%s, isEmpty=%d, hasValue=%d",
922 parentXMLNodeID,
923 xmlTextReaderDepth(readerPtr),
924 xmlTextReaderNodeType(readerPtr),
925 name,
926 xmlTextReaderIsEmptyElement(readerPtr),
927 xmlTextReaderHasValue(readerPtr));
928 //printf("is a port\n");*/
929 }
930 else if(!strcmp("edge",(const char*)name))
931 {
932 //printf("processing edge node\n");
933 //check to see if this edge is a port type first
934 xmlChar *targetPortName, *sourcePortName;
935 targetPortName = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"targetport");
936 sourcePortName = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"sourceport");
937 if((NULL != targetPortName) && (NULL != sourcePortName))
938 {
939 //find the target port interface
940 NetGraph::Interface* targetPortInterface;
941 NetGraph::Interface* sourcePortInterface;
942 targetPortInterface = graph.FindInterfaceByString((const char*)targetPortName);
943 sourcePortInterface = graph.FindInterfaceByString((const char*)sourcePortName);
944 if((NULL == sourcePortInterface) || (NULL == targetPortInterface))
945 {
946 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode: Error finding the source interface \"%s\" or target interface \"%s\" to create an edge!\n",sourcePortName,targetPortName);
947 return false;
948 }
949 NetGraph::Cost* mycost = CreateCost(1.0);
950 if(!Connect(*sourcePortInterface,*targetPortInterface,*mycost,isDuplex))
951 {
952 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode: Error connecting the source interface \"%s\" or target interface \"%s\" to create an edge!\n",sourcePortName,targetPortName);
953 return false;
954 }
955 delete mycost; //should we have a virtual override on this too? TBD
956 //printf(" connected interface ports %s to %s\n",sourcePortName,targetPortName);
957 }
958 else //isn't a port type just a normal source target type
959 {
960 xmlChar *targetName, *sourceName;
961 targetName = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"target");
962 sourceName = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"source");
963 //find the target interface
964 NetGraph::Interface* targetInterface;
965 NetGraph::Interface* sourceInterface;
966 sourceInterface = graph.FindInterfaceByString((const char*)sourceName);
967 targetInterface = graph.FindInterfaceByString((const char*)targetName);
968 if((NULL == sourceInterface) || (NULL == targetInterface))
969 {
970 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode: Error finding the source interface \"%s\" or target interface \"%s\" to create an edge!\n",sourceName,targetName);
971 return false;
972 }
973 NetGraph::Cost* mycost = CreateCost(1.0);
974 if(!Connect(*sourceInterface,*targetInterface,*mycost,isDuplex))
975 {
976 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode: Error connecting the source interface \"%s\" or target interface \"%s\" to create an edge!\n",sourceName,targetName);
977 return false;
978 }
979 delete mycost; //should we have a virtual override on this too? TBD
980 xmlFree(sourceName);
981 xmlFree(targetName);
982 //printf(" connected interface %s to %s\n",sourceName, targetName);
983 }
984 //printf("exiting edge node\n");
985 }
986 else if(!strcmp("key",(const char*)name))
987 {
988 xmlChar* oldkey = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"id");
989 xmlChar* attr_type = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"attr.type");
990 xmlChar* attr_name = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"attr.name");
991 xmlChar* domain = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"for");
992
993 xmlChar* defaultvalue = NULL;
994 xmlNode* myXMLNode = xmlTextReaderExpand(readerPtr);
995 xmlNode* childXMLNode = myXMLNode->children;
996 while(NULL != childXMLNode)
997 {
998 if(!strcmp((char*)childXMLNode->name,"default"))
999 {
1000 if(NULL == childXMLNode->children)
1001 {
1002 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode(): %s has an empty default value!\n",attr_name);
1003 return false;
1004 }
1005 defaultvalue = childXMLNode->children->content;
1006 }
1007 childXMLNode = childXMLNode->next;
1008 }
1009 //printf("reading in attribute name=%s key=%s defaultvalue=%s\n",name,oldkey,defaultvalue);
1010 if(!AddAttributeKey((const char*)attr_name,(const char*)attr_type,(const char*)domain,(const char*)oldkey,(const char*)defaultvalue))
1011 {
1012 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode(): Error adding attribute (name=%s,oldkey=%s)\n",attr_name,oldkey);
1013 return false;
1014 }
1015 xmlFree(oldkey);
1016 xmlFree(attr_type);
1017 xmlFree(attr_name);
1018 xmlFree(domain);
1019 }
1020 else if(!strcmp("data",(const char*)name))
1021 {
1022 char newlookup[250];//this really should be dynamic and checks on size should take place TBD
1023 xmlChar* oldIndex;
1024 const char* newIndex;
1025 xmlChar* newValue;
1026 xmlNode* myxmlnode = xmlTextReaderExpand(readerPtr);
1027
1028 if(!strcmp((const char*)myxmlnode->parent->name,"node"))
1029 {
1030 sprintf(newlookup,"node:%s",parentXMLNodeID);
1031 } else if(!strcmp((const char*)myxmlnode->parent->name,"port")) {
1032 xmlAttr* tempattribute = myxmlnode->parent->properties;
1033 const xmlChar *portName = NULL;
1034 while(NULL != tempattribute)
1035 {
1036 if(!strcmp((const char*)tempattribute->name,"name"))
1037 portName = tempattribute->children->content;
1038 tempattribute= tempattribute->next;
1039 }
1040 sprintf(newlookup,"node:%s:port:%s",parentXMLNodeID,portName);
1041
1042 } else if(!strcmp((const char*)myxmlnode->parent->name,"edge")) {
1043 xmlAttr* tempattribute = myxmlnode->parent->properties;
1044 const xmlChar *targetPortName(NULL), *sourcePortName(NULL), *targetName(NULL), *sourceName(NULL);
1045 while(NULL != tempattribute)
1046 {
1047 if(!strcmp((const char*)tempattribute->name,"sourceport"))
1048 sourcePortName = tempattribute->children->content;
1049 if(!strcmp((const char*)tempattribute->name,"targetport"))
1050 targetPortName = tempattribute->children->content;
1051 if(!strcmp((const char*)tempattribute->name,"source"))
1052 sourceName = tempattribute->children->content;
1053 if(!strcmp((const char*)tempattribute->name,"target"))
1054 targetName = tempattribute->children->content;
1055 tempattribute= tempattribute->next;
1056 }
1057 if((NULL == targetName) || (NULL == sourceName))
1058 {
1059 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode(): Error finding target/source for link data\n");
1060 return false;
1061 }
1062 if(NULL == sourcePortName)
1063 {
1064 sourcePortName = sourceName;
1065 }
1066 if(NULL == targetPortName)
1067 {
1068 targetPortName = targetName;
1069 }
1070 sprintf(newlookup,"edge:source:%s:%s:dest:%s:%s",sourceName,sourcePortName,targetName,targetPortName);
1071 } else {
1072 PLOG(PL_WARN,"ManetGraphMLParser::ReadXMLNode(): Ignoring data for unknown node type\n");
1073 }
1074 oldIndex = xmlTextReaderGetAttribute(readerPtr,(xmlChar*)"key");
1075 AttributeKey* tempakey = FindAttributeKeyByOldIndex((const char*)oldIndex);
1076 if(NULL == tempakey)
1077 {
1078 PLOG(PL_ERROR,"ManetGraphMLParser::ReadXMLNode(): found data with key type %s but it wasn't listed at one of the keys\n",oldIndex);
1079 return false;
1080 } else {
1081 newIndex = tempakey->GetIndex();
1082 //printf("newIndex =%s\n",newIndex);
1083 }
1084 if(!xmlTextReaderRead(readerPtr))
1085 return false;
1086 newValue = xmlTextReaderValue(readerPtr);
1087 //printf("found data for \"%s\" with key %s with value %s\n",newlookup,oldIndex,newValue);
1088
1089 Attribute* newAttribute = new Attribute();
1090 newAttribute->Init(newlookup,(const char*)newIndex,(const char*)newValue);
1091 //newAttribute->Init(newlookup,newIndex,(const char*)newValue);
1092 attributelist.Insert(*newAttribute);
1093 xmlFree(oldIndex);
1094 xmlFree(newValue);
1095 }
1096 else
1097 {
1098 //ignorning xml node
1099 //TRACE(" ignoring xml node %s\n", name);
1100 }
1101 //TRACE("exit ManetGraphMLParser::ReadXMLNode()\n");
1102 return true;
1103
1104 } // end ManetGraphMLParser::ReadXMLNode()
1105
1106
1107 bool ManetGraphMLParser::Read(const char* path, NetGraph& graph)
1108 {
1109 // Iteratively read the file's XML tree and build up "graph"
1110 //xmlTextReader* readerPtr = xmlReaderForFile(path, NULL, 1);
1111 xmlTextReader* readerPtr = xmlReaderForFile(path, "", 0);
1112 if (NULL == readerPtr)
1113 {
1114 PLOG(PL_ERROR,"ManetGraphMLParser::Read() xmlReaderForFile(%s) error: %s\n",path, GetErrorString());
1115 return false;
1116 }
1117
1118 bool isDuplex = true;
1119 char parentXMLNodeID[MAXXMLIDLENGTH+1];
1120 memset(parentXMLNodeID, 0, MAXXMLIDLENGTH+1);
1121 int result = xmlTextReaderRead(readerPtr);
1122 while (1 == result)
1123 {
1124 if (!ReadXMLNode(readerPtr, graph, parentXMLNodeID, isDuplex))
1125 {
1126 PLOG(PL_ERROR,"ManetGraphMLParser::Read() error: invalid XML node!\n");
1127 break;
1128 }
1129 result = xmlTextReaderRead(readerPtr);
1130 }
1131 xmlFreeTextReader(readerPtr);
1132 if (0 != result)
1133 PLOG(PL_ERROR,"ManetGraphMLParser::Read() error: invalid XML file %s\n", path);
1134 //xmlCleanupParser(); //this doesn't do what it should!!! it only frees up global state memory and should only be called upon destruction!
1135 return (0 == result);
1136 } // end ManetGraphMLParser::Read()
1137
1138
1139 bool ManetGraphMLParser::Write(NetGraph& graph, const char* path, char* buffer, unsigned int* len_ptr)
1140 {
1141 PLOG(PL_INFO,"ManetGraphMLParser::Write: Enter!\n");
1142 /* Create a new XmlWriter for DOM, with no compression. */
1143 int returnvalue;
1144 xmlTextWriter* writerPtr;
1145 xmlDoc* docPtr;
1146
1147 writerPtr = xmlNewTextWriterDoc(&docPtr, 0);
1148 if (writerPtr == NULL) {
1149 PLOG(PL_ERROR,"ManetGraphMLParser::Write::testXmlwriterPtrDoc: Error creating the xml writer\n");
1150 return false;
1151 }
1152
1153 /* Start the docPtrument with the xml default for the version,
1154 * encoding UTF-8 and the default for the standalone
1155 * declarao*/
1156 returnvalue = xmlTextWriterStartDocument(writerPtr, NULL, MY_GRAPHML_ENCODING, NULL);
1157 if (returnvalue < 0) {
1158 PLOG(PL_ERROR,"ManetGraphMLParser::Write::testXmlwriterPtrDoc: Error at xmlTextWriterStartDocument\n");
1159 return false;;
1160 }
1161
1162 /* Start an element named "graphml". Since thist is the first
1163 * element, this will be the root element of the docPtrument. */
1164 returnvalue = xmlTextWriterStartElement(writerPtr, BAD_CAST "graphml");
1165 if (returnvalue < 0) {
1166 PLOG(PL_ERROR,"ManetGraphMLParser::Write::testXmlwriterPtrDoc: Error at xmlTextWriterStartElement\n");
1167 return false;
1168 }
1169 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "xmlns", BAD_CAST "http://graphml.graphdrawing.org/xmlns");
1170 if (returnvalue < 0) {
1171 PLOG(PL_ERROR,"ManetGraphMLParser::Write::testXmlWriterDoc: Error at xmlTextWriterWriteAttribute\n");
1172 return false;
1173 }
1174 if(!UpdateKeys(graph))
1175 {
1176 PLOG(PL_ERROR,"ManetGraphMLParser::Write::testXmlWriterDoc: Error updating key elements in the header\n");
1177 return false;
1178 }
1179 if(!WriteLocalKeys(writerPtr))
1180 {
1181 PLOG(PL_ERROR,"ManetGraphMLParser::Write::testXmlWriterDoc: Error at writing key elements in header\n");
1182 return false;
1183 }
1184 if(!WriteLocalAttributes(writerPtr))
1185 {
1186 PLOG(PL_ERROR,"ManetGraphMLParser::Write::testXmlWriterDoc: Error at writing graph attributes in header\n");
1187 return false;
1188 }
1189
1190 /* We are done with the header so now we go through the actual graph and add each node and edge */
1191 /* We are adding each node */
1192 returnvalue = xmlTextWriterStartElement(writerPtr, BAD_CAST "graph");
1193 if (returnvalue < 0)
1194 {
1195 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error starting XML graph element\n");
1196 return false;
1197 }
1198 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "id",BAD_CAST XMLName);
1199 if (returnvalue < 0)
1200 {
1201 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error setting XML graph attribute id\n");
1202 return false;
1203 }
1204 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "edgedefault",BAD_CAST "directed");
1205 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error setting graph attribute directed\n"); return false;}
1206
1207 NetGraph::InterfaceIterator it(graph);
1208 NetGraph::Interface* iface;
1209 //adding the nodes to the xml
1210 while (NULL != (iface = it.GetNextInterface()))
1211 {
1212 //check to see if this is a default "node" interface
1213 //if(!iface->IsPort())
1214 if (iface == iface->GetNode().GetDefaultInterface())
1215 {
1216 //Node& node = static_cast<Node&>(iface->GetNode());
1217 returnvalue = xmlTextWriterStartElement(writerPtr, BAD_CAST "node");
1218 if (returnvalue < 0)
1219 {
1220 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error adding XML node\n");
1221 return false;
1222 }
1223 if(iface->GetAddress().IsValid())
1224 {
1225 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "id", BAD_CAST iface->GetAddress().GetHostString());
1226 //printf("writing node %s\n",iface->GetAddress().GetHostString());
1227 }
1228 else
1229 {
1230 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "id", BAD_CAST iface->GetName());
1231 //printf("writing node %s\n",iface->GetName());
1232 }
1233 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error adding setting node id\n"); return false;}
1234
1235 //update the node attributes using the virtual function
1236 if(!UpdateNodeAttributes(iface->GetNode()))
1237 {
1238 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error updating the node attributes\n");
1239 return false;
1240 }
1241 //call the local function write the attributes out
1242 if(!WriteLocalNodeAttributes(writerPtr,iface->GetNode()))
1243 {
1244 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error writing local node attributes\n");
1245 return false;
1246 }
1247 //call the virtual function to allow the node to fill in any attributes if there
1248 /* if(!WriteNodeAttributes(writerPtr,iface->GetNode())) //this shouldn't really have to be called if we add/update the attributes correctly TBD
1249 {
1250 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error writing node attributes\n");
1251 return false;
1252 }*/
1253
1254 //we need to iterate through the any "port" interfaces which we may have
1255 NetGraph::Node *theNode = &iface->GetNode();
1256 NetGraph::Node::InterfaceIterator nodeIt(*theNode);
1257 NetGraph::Interface* portIface;
1258 while (NULL != (portIface = static_cast<NetGraph::Interface*>(nodeIt.GetNextInterface())))
1259 {
1260 //we only want ports
1261 if(!(portIface == portIface->GetNode().GetDefaultInterface()))
1262 // if(portIface->IsPort())
1263 {
1264 returnvalue = xmlTextWriterStartElement(writerPtr, BAD_CAST "port");
1265 if (returnvalue < 0)
1266 {
1267 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error adding XML node\n");
1268 return false;
1269 }
1270 if(portIface->GetAddress().IsValid())
1271 {
1272 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "name", BAD_CAST portIface->GetAddress().GetHostString());
1273 //printf("writing interface %s\n",iface->GetAddress().GetHostString());
1274 }
1275 else
1276 {
1277 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "name", BAD_CAST portIface->GetName());
1278 //printf("writing node %s\n",iface->GetName());
1279 }
1280 if (returnvalue < 0)
1281 {
1282 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error adding setting node id\n");
1283 return false;
1284 }
1285 //update attributes to the port/interface using the virutal function
1286 if(!UpdateInterfaceAttributes(*portIface))
1287 {
1288 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error updating the interface attributes\n");
1289 return false;
1290 }
1291 //write the attributes to the port/interface
1292 if(!WriteLocalInterfaceAttributes(writerPtr,*portIface))
1293 {
1294 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error writing local interface attributes\n");
1295 return false;
1296 }
1297 //write any attributes to the port/interface using the virutal function
1298 /* if(!WriteInterfaceAttributes(writerPtr,*portIface))//this needn't be called if we add/update the attributes in the local list TBD
1299 {
1300 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error writing interface attributes\n");
1301 return false;
1302 }*/
1303 returnvalue = xmlTextWriterEndElement(writerPtr);
1304 if (returnvalue < 0)
1305 {
1306 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error ending node element\n");
1307 return false;
1308 }
1309 }
1310 }
1311 //close up the node node element
1312 returnvalue = xmlTextWriterEndElement(writerPtr);
1313 if (returnvalue < 0)
1314 {
1315 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error ending node element\n");
1316 return false;
1317 }
1318 }
1319 }
1320 //adding the directional links to the xml
1321 it.Reset();
1322 while (NULL != (iface = it.GetNextInterface()))
1323 {
1324 //if(!iface->IsPort())//not a port node so we can just print it normally
1325 if(iface == iface->GetNode().GetDefaultInterface())
1326 {
1327 NetGraph::AdjacencyIterator iteratorN1(*iface);
1328 NetGraph::Interface* nbrIface;
1329 while (NULL != (nbrIface = iteratorN1.GetNextAdjacency()))
1330 {
1331 returnvalue = xmlTextWriterStartElement(writerPtr, BAD_CAST "edge");
1332 if (returnvalue < 0)
1333 {
1334 PLOG(PL_ERROR,"ManetGraphMLParser::Write Error adding edge\n"); return false;
1335 }
1336 if(iface->GetAddress().IsValid())
1337 {
1338 //printf("writing connection %s ->",iface->GetAddress().GetHostString());
1339 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "source", BAD_CAST iface->GetAddress().GetHostString());
1340 }else
1341 {
1342 //printf("writing connection %s ->",iface->GetName());
1343 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "source", BAD_CAST iface->GetName());
1344 }
1345 if (returnvalue < 0)
1346 {
1347 PLOG(PL_ERROR,"ManetGraphMLParser::Write Error adding setting source attribute\n");
1348 return false;
1349 }
1350 if(nbrIface->GetAddress().IsValid()) {
1351 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "target", BAD_CAST nbrIface->GetAddress().GetHostString());
1352 ////printf("%s\n",nbrIface->GetAddress().GetHostString());
1353 }
1354 else
1355 {
1356 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "target", BAD_CAST nbrIface->GetName());
1357 ////printf("%s\n",nbrIface->GetName());
1358 }
1359 if (returnvalue < 0)
1360 {
1361 PLOG(PL_ERROR,"ManetGraphMLParser::Write Error adding setting source attribute\n");
1362 return false;
1363 }
1364 NetGraph::Link* link = iface->GetLinkTo(*nbrIface);
1365 if(NULL == link)
1366 {
1367 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error getting link which should always exist\n");
1368 return false;
1369 }
1370 //update the link attributes using the virtual function
1371 if(!UpdateLinkAttributes(*link))
1372 {
1373 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error updating the link attributes\n");
1374 return false;
1375 }
1376 //actually write the attributes out
1377 if(!WriteLocalLinkAttributes(writerPtr,*link))
1378 {
1379 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error writing link attributes\n");
1380 return false;
1381 }
1382 returnvalue = xmlTextWriterEndElement(writerPtr);
1383 if (returnvalue < 0)
1384 {
1385 PLOG(PL_ERROR,"ManetGraphMLParser::Write Error ending node element\n");
1386 return false;
1387 }
1388 }
1389 }
1390 else //it is a port interface so we need to find the "node" interface
1391 {
1392 //lets first find the "src" node interface
1393 NetGraph::Interface* nodeIface = iface->GetNode().GetDefaultInterface();
1394 if(!(nodeIface == nodeIface->GetNode().GetDefaultInterface()))
1395 // if(nodeIface->IsPort())
1396 {
1397 PLOG(PL_ERROR,"ManetGraphMLParser::Write Error the default interface was not the \"node\" interface for the src. You should iterate over them all and find the right one\n");
1398 return false;
1399 }
1400
1401 NetGraph::AdjacencyIterator iteratorN1(*iface);
1402 NetGraph::Interface* nbrIface, *nbrNodeIface;
1403 while (NULL != (nbrIface = iteratorN1.GetNextAdjacency()))
1404 {
1405 //lets find the "nbr" node interface
1406 nbrNodeIface = static_cast<NetGraph::Interface*>(nbrIface->GetNode().GetDefaultInterface());
1407 //if(nbrNodeIface->IsPort())
1408 if(!(nbrNodeIface == nbrNodeIface->GetNode().GetDefaultInterface()))
1409 {
1410 PLOG(PL_ERROR,"ManetGraphMLParser::Write Error the default interface was not the \"node\" interface for the nbr. You should iterate over them all and find the right one\n");
1411 return false;
1412 }
1413 returnvalue = xmlTextWriterStartElement(writerPtr, BAD_CAST "edge");
1414 if (returnvalue < 0)
1415 {
1416 PLOG(PL_ERROR,"ManetGraphMLParser::Write Error adding edge\n");
1417 return false;
1418 }
1419 if(nodeIface->GetAddress().IsValid())
1420 {
1421 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "source", BAD_CAST nodeIface->GetAddress().GetHostString());
1422 }
1423 else
1424 {
1425 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "source", BAD_CAST nodeIface->GetName());
1426 }
1427 if (returnvalue < 0)
1428 {
1429 PLOG(PL_ERROR,"ManetGraphMLParser::Write Error adding setting source attribute\n");
1430 return false;
1431 }
1432 if(nbrNodeIface->GetAddress().IsValid())
1433 {
1434 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "target", BAD_CAST nbrNodeIface->GetAddress().GetHostString());
1435 }
1436 else
1437 {
1438 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "target", BAD_CAST nbrNodeIface->GetName());
1439 }
1440 if (returnvalue < 0)
1441 {
1442 PLOG(PL_ERROR,"ManetGraphMLParser::Write Error adding setting source attribute\n");
1443 return false;
1444 }
1445 if(iface->GetAddress().IsValid())
1446 {
1447 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "sourceport", BAD_CAST iface->GetAddress().GetHostString());
1448 }
1449 else
1450 {
1451 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "sourceport", BAD_CAST iface->GetName());
1452 }
1453 if (returnvalue < 0)
1454 {
1455 PLOG(PL_ERROR,"ManetGraphMLParser::Write Error adding setting source attribute\n");
1456 return false;
1457 }
1458 if(nbrIface->GetAddress().IsValid())
1459 {
1460 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "targetport", BAD_CAST nbrIface->GetAddress().GetHostString());
1461 }
1462 else
1463 {
1464 returnvalue = xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "targetport", BAD_CAST nbrIface->GetName());
1465 }
1466 if (returnvalue < 0)
1467 {
1468 PLOG(PL_ERROR,"ManetGraphMLParser::Write Error adding setting source attribute\n");
1469 return false;
1470 }
1471
1472 NetGraph::Link* link = iface->GetLinkTo(*nbrIface);
1473 if(NULL == link)
1474 {
1475 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error getting link which should always exist\n");
1476 return false;
1477 }
1478 //update the link attributes using the virtual function
1479 if(!UpdateLinkAttributes(*link))
1480 {
1481 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error updating the link attributes\n");
1482 return false;
1483 }
1484 //actually write the attributes out
1485 if(!WriteLocalLinkAttributes(writerPtr,*link))
1486 {
1487 PLOG(PL_ERROR,"ManetGraphMLParser::Write: Error writing link attributes\n");
1488 return false;
1489 }
1490 returnvalue = xmlTextWriterEndElement(writerPtr);
1491 if (returnvalue < 0)
1492 {
1493 PLOG(PL_ERROR,"ManetGraphMLParser::Write Error ending node element\n");
1494 return false;
1495 }
1496 }
1497 }
1498 }
1499
1500 returnvalue = xmlTextWriterEndDocument(writerPtr);
1501 if (returnvalue < 0) { PLOG(PL_ERROR,"ManetGraphMLParser::Write:testXmlwriterPtrDoc: Error at xmlTextWriterEndDocument\n"); return false;}
1502
1503 xmlFreeTextWriter(writerPtr);
1504
1505 if(NULL != path)
1506 {
1507 xmlSaveFormatFileEnc(path, docPtr, MY_GRAPHML_ENCODING,1);
1508 }
1509 if(NULL != buffer && NULL != len_ptr)
1510 {
1511 //xmlOutputBuffer xmlbuff;
1512 //xmlSaveFormatFileTo(&xmlbuff,docPtr,MY_GRAPHML_ENCODING,1);
1513 //if (xmlbuff.written > (int)*len_ptr){
1514 // DMSG(0,"bunny in buffer section\n");
1515
1516 xmlChar* tempout;
1517 int size;
1518 xmlDocDumpFormatMemoryEnc(docPtr,&tempout,&size,MY_GRAPHML_ENCODING,1);
1519 if(size > (int)*len_ptr)
1520 {
1521 PLOG(PL_ERROR,"ManetGraphMLParser::Write: size of document %d is larger than the allocated space of %d\n",size, (int)*len_ptr);
1522 return false;
1523 }
1524 *len_ptr = size;
1525 memcpy(buffer,tempout,size);
1526 xmlFree(tempout);
1527 xmlCleanupParser();
1528 }
1529 xmlFreeDoc(docPtr);
1530
1531 return true;
1532 } // end ManetGraphMLParser::Write()
1533
1534 ManetGraphMLParser::Attribute*
1535 ManetGraphMLParser::AttributeList::FindAttribute(const char *theLookup,const char* theIndex)
1536 {
1537 AttributeList::Iterator it(*this,false,theLookup,strlen(theLookup)*8);
1538 Attribute* attr(NULL);
1539 attr = it.GetNextItem();
1540 while(NULL !=attr)
1541 {
1542 if(strcmp(attr->GetLookup(),theLookup))
1543 {
1544 attr = NULL;
1545 //we didn't find the entry
1546 }
1547 else
1548 {
1549 if(strcmp(attr->GetIndex(),theIndex))
1550 {
1551 //this attribute does not have the index we are looking for
1552 attr = it.GetNextItem();
1553 } else {
1554 return attr;
1555 }
1556 }
1557 }
1558 return attr;
1559 }
1560
1561 bool ManetGraphMLParser::WriteLocalKeys(xmlTextWriter* writerPtr)
1562 {
1563 int rv = 0;
1564 IndexKeylist::Iterator it(indexkeylist);
1565 AttributeKey* key(NULL);
1566 while(NULL != (key = it.GetNextItem()))
1567 {
1568 rv += xmlTextWriterStartElement(writerPtr, BAD_CAST "key");
1569 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "id",BAD_CAST key->GetIndex());
1570 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "attr.name",BAD_CAST key->GetName());
1571 switch (key->GetType())
1572 {
1573 case AttributeKey::Types::BOOL:
1574 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "attr.type",BAD_CAST "boolean");
1575 break;
1576 case AttributeKey::Types::INT:
1577 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "attr.type",BAD_CAST "int");
1578 break;
1579 case AttributeKey::Types::LONG:
1580 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "attr.type",BAD_CAST "long");
1581 break;
1582 case AttributeKey::Types::FLOAT:
1583 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "attr.type",BAD_CAST "float");
1584 break;
1585 case AttributeKey::Types::DOUBLE:
1586 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "attr.type",BAD_CAST "double");
1587 break;
1588 case AttributeKey::Types::STRING:
1589 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "attr.type",BAD_CAST "string");
1590 break;
1591 default:
1592 PLOG(PL_ERROR,"ManetGraphMLParser::WriteLocalKeys: type is invalid!\n");
1593 return false;
1594 }
1595 switch (key->GetDomain())
1596 {
1597 case AttributeKey::Domains::GRAPH:
1598 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "for",BAD_CAST "graph");
1599 break;
1600 case AttributeKey::Domains::NODE:
1601 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "for",BAD_CAST "node");
1602 break;
1603 case AttributeKey::Domains::EDGE:
1604 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "for",BAD_CAST "edge");
1605 break;
1606 case AttributeKey::Domains::ALL:
1607 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "for",BAD_CAST "all");
1608 break;
1609 default:
1610 break;
1611 }
1612 if (NULL != key->GetDefault())
1613 {
1614 rv += xmlTextWriterWriteElement(writerPtr,BAD_CAST "default", BAD_CAST key->GetDefault());
1615 }
1616 rv += xmlTextWriterEndElement(writerPtr);
1617 }
1618 if(rv < 0)
1619 return false;
1620 return true;
1621 }
1622
1623
1624 bool ManetGraphMLParser::WriteLocalNodeAttributes(xmlTextWriter* writerPtr,NetGraph::Node& theNode)
1625 {
1626 PLOG(PL_DETAIL,"ManetGraphMLParser::WriteLocalNodeAttributes: Enter\n");
1627 bool rv = true;
1628 char key[255];//this should be dynamic or checks added TBD
1629 sprintf(key,"node:%s",GetString(theNode));
1630 AttributeList::Iterator it(attributelist,false,key,strlen(key)*8);
1631 Attribute* attr(NULL);
1632 //iterate over items which have the matching keys
1633 attr = it.GetNextItem();
1634 while(NULL != attr)
1635 {
1636 if(strcmp(attr->GetLookup(),key))
1637 {
1638 //PLOG(PL_DETAIL,"ManetGraphMLParser::WriteLocalNodeAttributes():mykey=\"%s\",lookup=\"%s\",key=\"%s\",value=\"%s\"\n",key,attr->GetLookup(),attr->GetIndex(),attr->GetValue());
1639 attr = NULL;
1640 //attr = it.GetNextItem();
1641 }
1642 else
1643 {
1644 rv += xmlTextWriterStartElement(writerPtr, BAD_CAST "data");
1645 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "key",BAD_CAST attr->GetIndex());
1646 rv += xmlTextWriterWriteString(writerPtr, BAD_CAST attr->GetValue());
1647 rv += xmlTextWriterEndElement(writerPtr);
1648 PLOG(PL_DETAIL,"ManetGraphMLParser::WriteLocalNodeAttributes():key=\"%s\",value=\"%s\"\n",attr->GetIndex(),attr->GetValue());
1649 attr = it.GetNextItem();
1650 }
1651 }
1652 return rv;
1653 }
1654
1655 bool ManetGraphMLParser::WriteLocalAttributes(xmlTextWriter* writerPtr)
1656 {
1657 PLOG(PL_DETAIL,"ManetGraphMLParser::WriteLocalAttributes: Enter\n");
1658 bool rv = true;
1659 char key[255];//this should be dynamic or checks added TBD
1660 sprintf(key,"thisGraph");
1661 AttributeList::Iterator it(attributelist,false,key,strlen(key)*8);
1662 //AttributeList::Iterator it(attributelist);
1663
1664 Attribute* attr(NULL);
1665
1666 //iterate over items which have the matching keys
1667 attr = it.GetNextItem();
1668 while(NULL != attr)
1669 {
1670 if(strcmp(attr->GetLookup(),key))
1671 {
1672 //PLOG(PL_DETAIL,"ManetGraphMLParser::WriteLocalAttributes():mykey=\"%s\",lookup=\"%s\",key=\"%s\",value=\"%s\"\n",key,attr->GetLookup(),attr->GetIndex(),attr->GetValue());
1673 attr = NULL;
1674 //attr = it.GetNextItem();
1675 }
1676 else
1677 {
1678 rv += xmlTextWriterStartElement(writerPtr, BAD_CAST "data");
1679 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "key",BAD_CAST attr->GetIndex());
1680 rv += xmlTextWriterWriteString(writerPtr, BAD_CAST attr->GetValue());
1681 rv += xmlTextWriterEndElement(writerPtr);
1682 PLOG(PL_DETAIL,"ManetGraphMLParser::WriteLocalAttributes():key=\"%s\",value=\"%s\"\n",attr->GetIndex(),attr->GetValue());
1683 attr = it.GetNextItem();
1684 }
1685 }
1686 return rv;
1687 }
1688
1689 bool ManetGraphMLParser::WriteLocalInterfaceAttributes(xmlTextWriter* writerPtr,NetGraph::Interface& theInterface)
1690 {
1691 bool rv = true;
1692 char key[255];//this should be dynamic or checks added TBD
1693 sprintf(key,"node:%s",GetString(theInterface.GetNode()));
1694 sprintf(key,"%s:port:%s",key,GetString(theInterface));
1695 AttributeList::Iterator it(attributelist,false,key,strlen(key)*8);
1696 Attribute* attr(NULL);
1697 attr = it.GetNextItem();
1698 while(NULL != attr)
1699 {
1700 if(strcmp(attr->GetLookup(),key))
1701 {
1702 attr = NULL;
1703 }
1704 else
1705 {
1706 rv += xmlTextWriterStartElement(writerPtr, BAD_CAST "data");
1707 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "key",BAD_CAST attr->GetIndex());
1708 rv += xmlTextWriterWriteString(writerPtr, BAD_CAST attr->GetValue());
1709 rv += xmlTextWriterEndElement(writerPtr);
1710 attr = it.GetNextItem();
1711 }
1712 }
1713 return rv;
1714 }
1715
1716 bool ManetGraphMLParser::WriteLocalLinkAttributes(xmlTextWriter* writerPtr,NetGraph::Link& theLink)
1717 {
1718 PLOG(PL_DETAIL,"ManetGraphMLParser::WriteLocalLinkAttributes()\n");
1719 bool rv = true;
1720 char key[255];//this should be dynamic or checkes added TBD
1721 sprintf(key,"edge:source:%s",GetString(theLink.GetSrc()->GetNode()));
1722 sprintf(key,"%s:%s",key,GetString(*theLink.GetSrc()));
1723 sprintf(key,"%s:dest:%s",key,GetString(theLink.GetDst()->GetNode()));
1724 sprintf(key,"%s:%s",key,GetString(*theLink.GetDst()));
1725
1726 AttributeList::Iterator it(attributelist,false,key,strlen(key)*8);
1727 Attribute* attr(NULL);
1728 attr = it.GetNextItem();
1729 while(NULL != attr)
1730 {
1731 if(strcmp(attr->GetLookup(),key))
1732 {
1733 attr = NULL;
1734 }
1735 else
1736 {
1737 rv += xmlTextWriterStartElement(writerPtr, BAD_CAST "data");
1738 rv += xmlTextWriterWriteAttribute(writerPtr, BAD_CAST "key",BAD_CAST attr->GetIndex());
1739 rv += xmlTextWriterWriteString(writerPtr, BAD_CAST attr->GetValue());
1740 rv += xmlTextWriterEndElement(writerPtr);
1741 attr = it.GetNextItem();
1742 }
1743 }
1744 return rv;
1745 }