00001 #include "UAPNode.hpp"
00002
00003 using namespace std;
00004 namespace BU = BasicUtilities;
00005
00006
00007
00008 UAPNode::UAPNode(enum UAPNode_type _type, const string& _name) :
00009 type(_type), parent(NULL), twin(NULL), connect(NULL),
00010 children(), slaves(), masters(), controllers(), ix_child(-1) {
00011
00012 if (!BU::splitXMLName (_name, xml_uri, xml_prefix, name)) {
00013 cout << "Bad name argument in UAPNode constructor: " << _name << endl;
00014 throw;
00015 }
00016
00017 }
00018
00019
00020
00021 UAPNode::UAPNode(const string& _name) :
00022 type(ELEMENT_NODE), parent(NULL), twin(NULL), connect(NULL),
00023 children(), slaves(), masters(), controllers(), ix_child(-1) {
00024
00025 if (!BU::splitXMLName (_name, xml_uri, xml_prefix, name)) {
00026 cout << "Bad name argument in UAPNode constructor: " << _name << endl;
00027 throw;
00028 }
00029
00030 }
00031
00032
00033
00034 UAPNode::UAPNode(const UAPNode& node){
00035 parent = NULL;
00036 name = node.name;
00037 xml_prefix = node.xml_prefix;
00038 xml_uri = node.xml_uri;
00039 type = node.type;
00040 connect = node.connect;
00041 twin = node.twin;
00042 masters = node.masters;
00043 slaves = node.slaves;
00044 controllers = node.controllers;
00045 for (NodeVecCIter it = node.children.begin(); it != node.children.end(); ++it)
00046 addChildCopy(*it);
00047 for (AttributeVecCIter itt = node.attributes.begin(); itt != node.attributes.end(); ++itt)
00048 addAttribute(itt->getName(), itt->getValue(), true);
00049 }
00050
00051
00052
00053 void UAPNode::getPath(IntArray& path) const {
00054 this->getPath2(path, 0);
00055 }
00056
00057
00058
00059 void UAPNode::getPath2(IntArray& path, int n) const {
00060
00061 if (parent)
00062 parent->getPath2(path, n+1);
00063 else
00064 path.resize(n+1);
00065
00066 path[path.size()-n-1] = ix_child;
00067
00068 }
00069
00070
00071
00072 string UAPNode::getName() const{
00073 return name;
00074 }
00075
00076
00077
00078 void UAPNode::setName(const string& _name) {
00079 name = _name;
00080 }
00081
00082
00083
00084 enum UAPNode_type UAPNode::getType() const{
00085 return type;
00086 }
00087
00088
00089
00090 void UAPNode::setType(enum UAPNode_type _type) {
00091 type = _type;
00092 }
00093
00094
00095
00096 UAPNode* UAPNode::getParent() const{
00097 return parent;
00098 }
00099
00100
00101
00102 void UAPNode::setParent(UAPNode* _parent) {
00103 parent = _parent;
00104 }
00105
00106
00107
00108 UAPNode* UAPNode::getTwin() const{
00109 return twin;
00110 }
00111
00112
00113
00114 void UAPNode::setTwin(UAPNode* _twin) {
00115 twin = _twin;
00116 }
00117
00118
00119
00120 UAPNode* UAPNode::getConnect() const{
00121 return connect;
00122 }
00123
00124
00125
00126 void UAPNode::setConnect(UAPNode* _connect) {
00127 connect = _connect;
00128 }
00129
00130
00131
00132 NodeVec& UAPNode::getChildren() {
00133 return children;
00134 }
00135
00136
00137
00138 NodeVecIter UAPNode::getChildIter (const UAPNode* child) {
00139 for (NodeVecIter it=children.begin(); it!=children.end(); ++it)
00140 if (*it == child) return it;
00141 return children.end();
00142 }
00143
00144
00145
00146 UAPNode* UAPNode::detachNode() {
00147
00148 if (!parent) return this;
00149
00150
00151
00152 NodeVecIter it = parent->getChildIter(this);
00153 parent->children.erase(it);
00154
00155
00156
00157 int ix_child = 0;
00158 for (NodeVecIter it = parent->children.begin(); it != parent->children.end(); it++)
00159 (*it)->ix_child = ix_child++;
00160
00161
00162
00163 this->setParent(NULL);
00164 return this;
00165
00166 }
00167
00168
00169
00170 UAPNode* UAPNode::addChild(const string& _name, UAPNode* old_child, enum UAPNode_type _type) {
00171
00172 UAPNode* new_node = new UAPNode(_type, _name);
00173 if (!addChild(new_node, old_child)) {
00174 cout << "UAPNode::addChild: old_child not found for: " << old_child->toString() << endl;
00175 cout << " For parent: " << this->toString() << endl;
00176 return NULL;
00177 }
00178
00179
00180
00181
00182 bool prefix_found;
00183 string uri, prefix, loc_name;
00184 BU::splitXMLName(_name, uri, prefix, loc_name, prefix_found);
00185
00186
00187
00188
00189
00190
00191
00192
00193 if (uri == "") {
00194 UAPNode* elder = this;
00195 while (true) {
00196
00197 if (!elder) {
00198 if (!prefix_found) return new_node;
00199 cout << "UAPNode::addChild: No elder found with matching XML prefix for: "
00200 << new_node->toString() << endl;
00201 return NULL;
00202 }
00203
00204 if (elder->xml_prefix == prefix) {
00205 new_node->xml_uri = elder->xml_uri;
00206 return new_node;
00207 }
00208
00209 elder = elder->parent;
00210
00211 }
00212 }
00213
00214
00215
00216
00217 if (uri != "" && !prefix_found) {
00218 UAPNode* elder = this;
00219 while (true) {
00220
00221 if (!elder) {
00222 cout << "UAPNode::addChild: No elder found with matching XML URI for: "
00223 << new_node->toString() << endl;
00224 return NULL;
00225 }
00226
00227 if (elder->xml_uri == uri) {
00228 new_node->xml_prefix = elder->xml_prefix;
00229 return new_node;
00230 }
00231
00232 elder = elder->parent;
00233
00234 }
00235
00236 }
00237
00238
00239
00240 return new_node;
00241
00242 }
00243
00244
00245
00246 UAPNode* UAPNode::addChild(const string& _name, enum UAPNode_type _type, UAPNode* old_child) {
00247 return addChild(_name, old_child, _type);
00248 }
00249
00250
00251
00252 UAPNode* UAPNode::addChildCopy(const UAPNode* node, const UAPNode* old_child) {
00253 assert(node);
00254 UAPNode* new_node = new UAPNode(*node);
00255 addChild(new_node, old_child);
00256 return new_node;
00257 }
00258
00259
00260 UAPNode* UAPNode::addChild(UAPNode* node, const UAPNode* old_child) {
00261
00262 node->setParent(this);
00263
00264 if (!old_child) {
00265 children.push_back(node);
00266 node->ix_child = children.size() - 1;
00267
00268 } else {
00269 NodeVecIter iter = this->getChildIter(old_child);
00270 children.insert(iter, node);
00271 int ix_c = 0;
00272 for(iter = children.begin(); iter != children.end(); iter++) {
00273 (*iter)->ix_child = ix_c++;
00274 }
00275 }
00276
00277 return node;
00278 }
00279
00280
00281
00282 AttributeVec& UAPNode::getAttributes() {
00283 return attributes;
00284 }
00285
00286
00287
00288 UAPAttribute* UAPNode::getAttribute(const string& _name){
00289 for (AttributeVecIter it=attributes.begin(); it!=attributes.end(); it++)
00290 if (it->getName() == _name)
00291 return &(*it);
00292
00293 return NULL;
00294 }
00295
00296
00297
00298 string UAPNode::getAttributeString(const string& _name) {
00299 for (AttributeVecIter it=attributes.begin(); it!=attributes.end(); ++it)
00300 if (it->getName() == _name) return it->getValue();
00301
00302 return "";
00303 }
00304
00305
00306
00307 bool UAPNode::removeAttribute(const string& _name) {
00308 for (AttributeVecIter it=attributes.begin(); it!=attributes.end(); ++it) {
00309 if (it->getName() == _name) {
00310 attributes.erase(it);
00311 return true;
00312 }
00313 }
00314 return false;
00315 }
00316
00317
00318
00319 UAPNode* UAPNode::addAttribute(const string& _name, const string& _value, bool allow_repeats) {
00320
00321 if (!allow_repeats) {
00322 for (AttributeVecIter it=attributes.begin(); it!=attributes.end(); ++it) {
00323 if (it->getName() == _name) {
00324 it->setValue(_value);
00325 return this;
00326 }
00327 }
00328 }
00329
00330 UAPAttribute new_attribute(_name, _value);
00331 attributes.push_back(new_attribute);
00332 return this;
00333
00334 }
00335
00336
00337
00338 NodeVec UAPNode::getChildrenByName(const string& _name) {
00339 string uri, prefix, loc_name;
00340 BU::splitXMLName(_name, uri, prefix, loc_name);
00341
00342 NodeVec result;
00343 for (NodeVecIter it=children.begin(); it!=children.end(); ++it) {
00344 UAPNode* node = *it;
00345 if (prefix != "" && uri == "") {
00346 if (node->name == loc_name && node->xml_prefix == prefix) result.push_back(node);
00347 } else {
00348 if (node->name == loc_name && node->xml_uri == uri) result.push_back(node);
00349 }
00350 }
00351
00352 return result;
00353 }
00354
00355
00356
00357 UAPNode* UAPNode::getChildByName (const string& _name, bool create) {
00358
00359 if (!this) return NULL;
00360
00361 string uri, prefix, loc_name;
00362 BU::splitXMLName(_name, uri, prefix, loc_name);
00363
00364 for (NodeVecIter it=children.begin(); it!=children.end(); ++it) {
00365 UAPNode* node = *it;
00366 if (node->name == loc_name && node->xml_uri == uri)
00367 return (*it);
00368 }
00369
00370 if (create) {
00371 UAPNode* child = this->addChild(loc_name);
00372 child->setXMLURI(uri);
00373 return child;
00374 }
00375
00376 return NULL;
00377 }
00378
00379
00380
00381 UAPNode* UAPNode::add (UAPNode_list list_name, UAPNode* new_ele, UAPNode* old_ele) {
00382 NodeVec& this_list = getList(list_name);
00383 if (!old_ele) {
00384 this_list.push_back(new_ele);
00385 return new_ele;
00386 }
00387 for (NodeVecIter it = this_list.begin(); it != this_list.end(); it++) {
00388 if (*it == old_ele) {
00389 this_list.insert(it, new_ele);
00390 return new_ele;
00391 }
00392 }
00393 return NULL;
00394 }
00395
00396
00397
00398 UAPNode* UAPNode::add (UAPNode_list list_name, UAPNode* new_ele, NodeVecIter insert_pt) {
00399 NodeVec& this_list = getList(list_name);
00400 this_list.insert(insert_pt, new_ele);
00401 return new_ele;
00402 }
00403
00404
00405
00406 NodeVec& UAPNode::getList(UAPNode_list list_name) {
00407 switch (list_name) {
00408 case SLAVE: return slaves;
00409 case MASTER: return masters;
00410 case CONTROLLER: return controllers;
00411 }
00412 bool ok;
00413 cout << "Bad argument in UAPNode::get: " <<
00414 BasicUtilities::int_to_string(list_name, ok) << endl;
00415 throw;
00416 }
00417
00418
00419
00420 void UAPNode::deleteNode() {
00421 if (parent) this->detachNode();
00422 this->deleteTree2();
00423 }
00424
00425
00426
00427 void UAPNode::deleteTree2() {
00428 for (NodeVecIter it=children.begin(); it!=children.end(); ++it) {
00429 (*it)->deleteTree2();
00430 }
00431 delete this;
00432 }
00433
00434
00435
00436 string UAPNode::toString(int indent, bool encode_all, const string& prefix) const {
00437
00438 string result = string(indent, ' ') + prefix;
00439 if (type == ELEMENT_NODE) {
00440 result += "<" + this->getQName();
00441 } else if (type == TEXT_NODE)
00442 result += "TYPE: \"" + this->getQName() + "\"";
00443 else
00444 result += type + ": <" + this->getQName();
00445
00446
00447 for (AttributeVecCIter it=attributes.begin(); it!=attributes.end(); ++it)
00448 result += " " + it->toString();
00449
00450 if (type != TEXT_NODE)
00451 result += ">";
00452
00453 if (!encode_all) return result;
00454
00455
00456 if (twin) result += "\n" + string(indent+7, ' ') +
00457 "Twin: " + twin->toString(0, false);
00458
00459
00460 if (connect) result += "\n" + string(indent+7, ' ') +
00461 "Connect: " + connect->toString(0, false);
00462
00463
00464 NodeVecCIter itt;
00465 for (itt = masters.begin(); itt != masters.end(); itt++) {
00466 result += "\n" + string(indent+7, ' ') + "Master: " + (*itt)->toString(0, false);
00467 }
00468
00469
00470 for (itt = slaves.begin(); itt != slaves.end(); itt++) {
00471 result += "\n" + string(indent+7, ' ') + "Slave: " + (*itt)->toString(0, false);
00472 }
00473
00474
00475 for (itt = controllers.begin(); itt != controllers.end(); itt++) {
00476 if ((*itt)->name == "slave") {
00477 result += "\n" + string(indent+7, ' ') + "Controller: " +
00478 (*itt)->parent->toString(0, false);
00479 result += "\n" + string(indent+7, ' ') + " " + (*itt)->toString(0, false);
00480 } else {
00481 result += "\n" + string(indent+7, ' ') + "Controller: " + (*itt)->toString(0, false);
00482 }
00483 }
00484
00485
00486 return result;
00487 }
00488
00489
00490
00491 string UAPNode::toStringTree(int indent) const {
00492 return toStringTree2(indent, 0);
00493 }
00494
00495
00496
00497 string UAPNode::toStringTree2(int indent, int depth) const {
00498
00499 string result = "";
00500
00501 if (depth == 0) result += toString(indent);
00502 else result += toString(3*depth+indent, true, "|- ");
00503
00504
00505
00506 if (!children.empty()) {
00507 for (NodeVecCIter it=children.begin(); it!=children.end(); ++it) {
00508 result += "\n" + (*it)->toStringTree2(indent, depth+1);
00509 }
00510 }
00511
00512 return result;
00513 }
00514
00515
00516
00517 string UAPNode::toXMLTree (int indent) const {
00518
00519 string result = string(indent, ' ');
00520 if (type == ELEMENT_NODE)
00521 result += "<" + name;
00522 else if (type == TEXT_NODE) {
00523 result += name;
00524 return result;
00525 } else {
00526 result += "???";
00527 return result;
00528 }
00529
00530
00531 for (AttributeVecCIter it=attributes.begin(); it!=attributes.end(); ++it)
00532 result += " " + it->toString();
00533
00534
00535
00536 if (children.empty()) {
00537 result += " />";
00538
00539 } else {
00540 result += ">";
00541 for (NodeVecCIter it=children.begin(); it!=children.end(); ++it) {
00542 result += "\n" + (*it)->toXMLTree (indent+2);
00543 }
00544 result = result + "\n" + string(indent, ' ') + "</" + name + ">";
00545 }
00546
00547 return result;
00548
00549 }
00550
00551
00552
00553 NodeVec UAPNode::getSubNodesByName(const string& _name) {
00554
00555 NodeVec nList;
00556
00557 if (!this) return nList;
00558
00559 string uri, prefix, loc_name;
00560 BU::splitXMLName(_name, uri, prefix, loc_name);
00561
00562 for (NodeVecIter it=children.begin(); it!=children.end(); ++it) {
00563 UAPNode* node = *it;
00564 if (node->name == loc_name && node->xml_uri == uri)
00565 nList.push_back(*it);
00566 node->getSubNodesByName (_name, nList);
00567 }
00568 return nList;
00569 }
00570
00571
00572
00573 void UAPNode::getSubNodesByName(const string& _name, NodeVec& nodeList) {
00574
00575 string uri, prefix, loc_name;
00576 BU::splitXMLName(_name, uri, prefix, loc_name);
00577
00578 for (NodeVecIter it=children.begin(); it!=children.end(); ++it) {
00579 UAPNode* node = *it;
00580 if (node->name == loc_name && node->xml_uri == uri)
00581 nodeList.push_back(*it);
00582 node->getSubNodesByName (_name, nodeList);
00583 }
00584 return;
00585 }
00586
00587
00588
00589 void UAPNode::setChildIndexNumber (const int ix_c) {
00590 ix_child = ix_c;
00591 }
00592
00593
00594
00595 int UAPNode::getChildIndexNumber () const {
00596 return ix_child;
00597 }
00598
00599
00600 void UAPNode::checkTree () {
00601
00602 int ix_child = 0;
00603
00604 for (NodeVecCIter it = children.begin(); it != children.end(); it++) {
00605 UAPNode* child = *it;
00606 if (child->parent != this) {
00607 cout << "Error: Parent/Child pointer mismatch!" << endl;
00608 cout << this->toString(4, false, "Parent: ") << endl;
00609 cout << child->toString(4, false, "Child: ") << endl;
00610 }
00611 if (child->ix_child != ix_child) {
00612 cout << "Error: Bad ix_child value!" << endl;
00613 cout << child->toString(4, false, "Child: ") << endl;
00614 cout << " Ix_child value: " << child->ix_child << endl;
00615 cout << " Correct ix_child value: " << ix_child << endl;
00616 }
00617 child->checkTree();
00618 ix_child++;
00619 }
00620
00621 }
00622
00623
00624
00625 string UAPNode::getXMLURI() const {
00626 return xml_uri;
00627 }
00628
00629
00630
00631 void UAPNode::setXMLURI(const string& uri) {
00632 xml_uri = uri;
00633 }
00634
00635
00636
00637 string UAPNode::getXMLPrefix() const {
00638 return xml_prefix;
00639 }
00640
00641
00642
00643 void UAPNode::setXMLPrefix(const string& prefix) {
00644 xml_prefix = prefix;
00645 }
00646
00647
00648
00649 string UAPNode::getUniversalName () const {
00650
00651 if (xml_uri == "")
00652 return name;
00653 else
00654 return "{" + xml_uri + "}" + name;
00655
00656 }
00657
00658
00659
00660 string UAPNode::getQName () const {
00661
00662 if (xml_prefix == "")
00663 return name;
00664 else
00665 return xml_prefix + ":" + name;
00666
00667 }
00668
00669
00670
00671 void UAPNode::addXMLNSAttributes (bool close_to_root) {
00672
00673 StrMap existing_xmlns, needed_xmlns;
00674
00675 if (close_to_root)
00676 this->add_xmlns_close_to_root(true, existing_xmlns, needed_xmlns);
00677 else
00678 this->add_xmlns_far_from_root(existing_xmlns);
00679
00680 }
00681
00682
00683
00684 void UAPNode::add_xmlns_close_to_root(bool at_top_level,
00685 StrMap existing_xmlns, StrMap& needed_xmlns) {
00686
00687
00688
00689 StrMap xmlns_here;
00690 for (AttributeVecCIter in = attributes.begin(); in != attributes.end(); in++) {
00691 string name = in->getName();
00692 if (name.substr(0,5) != "xmlns") continue;
00693 if (name.size() > 5 && name[5] != ':') continue;
00694 string prefix;
00695 if (name.size() > 5) prefix = name.substr(6);
00696 if (xmlns_here.find(prefix) != xmlns_here.end()) {
00697 cout << "Multiple XML prefixes of the same name: " + prefix << endl;
00698 cout << "Found in node:" << this->toString();
00699 throw;
00700 }
00701 xmlns_here[prefix] = in->getValue();
00702 }
00703
00704
00705
00706 for (StrMapCIter is = xmlns_here.begin(); is != xmlns_here.end(); is++)
00707 existing_xmlns[is->first] = is->second;
00708
00709
00710
00711
00712 if (xml_uri != "" && existing_xmlns.find(xml_prefix) == existing_xmlns.end())
00713 needed_xmlns[xml_prefix] = xml_uri;
00714
00715
00716
00717
00718
00719
00720
00721 for (NodeVecCIter in = children.begin(); in != children.end(); in++) {
00722 UAPNode* child = *in;
00723 StrMap more_needed_xmlns;
00724 child->add_xmlns_close_to_root (false, existing_xmlns, more_needed_xmlns);
00725 for (StrMapCIter is = more_needed_xmlns.begin(); is != more_needed_xmlns.end(); is++) {
00726
00727 if (existing_xmlns.find(is->first) != existing_xmlns.end() &&
00728 existing_xmlns[is->first] != is->second) {
00729 if (is->first == "")
00730 child->addAttribute("xmlns", is->second);
00731 else
00732 child->addAttribute("xmlns:" + is->first, is->second);
00733 } else if (at_top_level) {
00734 if (is->first == "")
00735 this->addAttribute("xmlns", is->second);
00736 else
00737 this->addAttribute("xmlns:" + is->first, is->second);
00738 existing_xmlns[is->first] = is->second;
00739 } else {
00740 needed_xmlns[is->first] = is->second;
00741 }
00742 }
00743 }
00744
00745 }
00746
00747
00748
00749 void UAPNode::add_xmlns_far_from_root(StrMap existing_xmlns) {
00750
00751
00752
00753 StrMap xmlns_here;
00754 for (AttributeVecCIter in = attributes.begin(); in != attributes.end(); in++) {
00755 string name = in->getName();
00756 if (name.substr(0,5) != "xmlns") continue;
00757 if (name.size() > 5 && name[5] != ':') continue;
00758 string prefix;
00759 if (name.size() > 5) prefix = name.substr(6);
00760 if (xmlns_here.find(prefix) != xmlns_here.end()) {
00761 cout << "Multiple XML prefixes of the same name: " + prefix << endl;
00762 cout << "Found in node:" << this->toString();
00763 throw;
00764 }
00765 xmlns_here[prefix] = in->getValue();
00766 }
00767
00768
00769
00770 for (StrMapCIter is = xmlns_here.begin(); is == xmlns_here.end(); is++)
00771 existing_xmlns[is->first] = is->second;
00772
00773
00774
00775
00776 if (xml_uri != "" && existing_xmlns.find(xml_prefix) == existing_xmlns.end()) {
00777 if (xml_prefix == "")
00778 this->addAttribute("xmlns", xml_uri);
00779 else
00780 this->addAttribute("xmlns:" + xml_prefix, xml_uri);
00781 existing_xmlns[xml_prefix] = xml_uri;
00782 }
00783
00784
00785
00786 for (NodeVecCIter in = children.begin(); in != children.end(); in++) {
00787 UAPNode* child = *in;
00788 child->add_xmlns_far_from_root (existing_xmlns);
00789 }
00790
00791 }
00792
00793
00794
00795 ostream& operator << (ostream& os, const UAPNode& node) {
00796 os << node.toString();
00797 return os;
00798 }
00799
00800
00801
00802 ostream& operator << (ostream& os, const UAPNode* node) {
00803 os << node->toString();
00804 return os;
00805 }
00806