Changeset 28163 in vbox
- Timestamp:
- Apr 11, 2010 5:15:21 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/cpp/xml.h
r27918 r28163 48 48 typedef xmlError *xmlErrorPtr; 49 49 50 typedef struct _xmlAttr xmlAttr; 51 typedef struct _xmlNode xmlNode; 52 50 53 namespace xml 51 54 { … … 454 457 int isElement() 455 458 { 456 return m Type == IsElement;459 return m_Type == IsElement; 457 460 } 458 461 459 462 protected: 460 463 typedef enum {IsElement, IsAttribute, IsContent} EnumType; 461 EnumType mType; 464 465 EnumType m_Type; 466 Node *m_pParent; 467 xmlNode *m_plibNode; // != NULL if this is an element or content node 468 xmlAttr *m_plibAttr; // != NULL if this is an attribute node 469 const char *m_pcszNamespace; 470 const char *m_pcszName; // element or attribute name, points either into plibNode or plibAttr; 471 // NULL if this is a content node 462 472 463 473 // hide the default constructor so people use only our factory methods 464 Node(EnumType type); 474 Node(EnumType type, 475 Node *pParent, 476 xmlNode *plibNode, 477 xmlAttr *plibAttr); 465 478 Node(const Node &x); // no copying 466 479 … … 517 530 protected: 518 531 // hide the default constructor so people use only our factory methods 519 ElementNode( );532 ElementNode(Node *pParent, xmlNode *plibNode); 520 533 ElementNode(const ElementNode &x); // no copying 521 534 … … 531 544 protected: 532 545 // hide the default constructor so people use only our factory methods 533 ContentNode( );546 ContentNode(Node *pParent, xmlNode *plibNode); 534 547 ContentNode(const ContentNode &x); // no copying 535 548 … … 544 557 protected: 545 558 // hide the default constructor so people use only our factory methods 546 AttributeNode( );559 AttributeNode(Node *pParent, xmlAttr *plibAttr); 547 560 AttributeNode(const AttributeNode &x); // no copying 548 561 -
trunk/src/VBox/Runtime/r3/xml.cpp
r27918 r28163 420 420 struct Node::Data 421 421 { 422 xmlNode *plibNode; // != NULL if this is an element or content node423 xmlAttr *plibAttr; // != NULL if this is an attribute node424 425 Node *pParent; // NULL only for the root element426 const char *pcszName; // element or attribute name, points either into plibNode or plibAttr;427 // NULL if this is a content node428 429 422 struct compare_const_char 430 423 { … … 444 437 }; 445 438 446 Node::Node(EnumType type) 447 : mType(type), 439 Node::Node(EnumType type, 440 Node *pParent, 441 xmlNode *plibNode, 442 xmlAttr *plibAttr) 443 : m_Type(type), 444 m_pParent(pParent), 445 m_plibNode(plibNode), 446 m_plibAttr(plibAttr), 447 m_pcszNamespace(NULL), 448 m_pcszName(NULL), 448 449 m(new Data) 449 450 { 450 m->plibNode = NULL;451 m->plibAttr = NULL;452 m->pParent = NULL;453 451 } 454 452 … … 461 459 { 462 460 // go thru this element's attributes 463 xmlAttr *plibAttr = m ->plibNode->properties;461 xmlAttr *plibAttr = m_plibNode->properties; 464 462 while (plibAttr) 465 463 { 466 464 const char *pcszAttribName = (const char*)plibAttr->name; 467 boost::shared_ptr<AttributeNode> pNew(new AttributeNode); 468 pNew->m->plibAttr = plibAttr; 469 pNew->m->pcszName = (const char*)plibAttr->name; 470 pNew->m->pParent = this; 465 boost::shared_ptr<AttributeNode> pNew(new AttributeNode(this, plibAttr)); 471 466 // store 472 467 m->attribs[pcszAttribName] = pNew; … … 476 471 477 472 // go thru this element's child elements 478 xmlNodePtr plibNode = m ->plibNode->children;473 xmlNodePtr plibNode = m_plibNode->children; 479 474 while (plibNode) 480 475 { … … 482 477 483 478 if (plibNode->type == XML_ELEMENT_NODE) 484 pNew = boost::shared_ptr<Node>(new ElementNode );479 pNew = boost::shared_ptr<Node>(new ElementNode(this, plibNode)); 485 480 else if (plibNode->type == XML_TEXT_NODE) 486 pNew = boost::shared_ptr<Node>(new ContentNode );481 pNew = boost::shared_ptr<Node>(new ContentNode(this, plibNode)); 487 482 if (pNew) 488 483 { 489 pNew->m->plibNode = plibNode;490 pNew->m->pcszName = (const char*)plibNode->name;491 pNew->m->pParent = this;492 484 // store 493 485 m->children.push_back(pNew); … … 503 495 const char* Node::getName() const 504 496 { 505 return m ->pcszName;497 return m_pcszName; 506 498 } 507 499 … … 514 506 bool Node::nameEquals(const char *pcszNamespace, const char *pcsz) const 515 507 { 516 if (m ->pcszName == pcsz)508 if (m_pcszName == pcsz) 517 509 return true; 518 if (m ->pcszName == NULL)510 if (m_pcszName == NULL) 519 511 return false; 520 512 if (pcsz == NULL) 521 513 return false; 522 if (strcmp(m ->pcszName, pcsz))514 if (strcmp(m_pcszName, pcsz)) 523 515 return false; 524 516 … … 527 519 return true; 528 520 // caller wants namespace: 529 if ( !m->plibNode->ns 530 || !m->plibNode->ns->prefix 531 || !*m->plibNode->ns->prefix 532 ) 521 if (!m_pcszNamespace) 533 522 // but node has no namespace: 534 523 return false; 535 return !strcmp( (const char*)m->plibNode->ns->prefix, pcszNamespace);524 return !strcmp(m_pcszNamespace, pcszNamespace); 536 525 } 537 526 … … 544 533 const char* Node::getValue() const 545 534 { 546 if ( (m ->plibAttr)547 && (m ->plibAttr->children)535 if ( (m_plibAttr) 536 && (m_plibAttr->children) 548 537 ) 549 538 // libxml hides attribute values in another node created as a 550 539 // single child of the attribute node, and it's in the content field 551 return (const char*)m ->plibAttr->children->content;552 553 if ( (m ->plibNode)554 && (m ->plibNode->children)540 return (const char*)m_plibAttr->children->content; 541 542 if ( (m_plibNode) 543 && (m_plibNode->children) 555 544 ) 556 return (const char*)m ->plibNode->children->content;545 return (const char*)m_plibNode->children->content; 557 546 558 547 return NULL; … … 634 623 int Node::getLineNumber() const 635 624 { 636 if (m->plibAttr) 637 return m->pParent->m->plibNode->line; 638 639 return m->plibNode->line; 640 } 641 642 ElementNode::ElementNode() 643 : Node(IsElement) 644 { 625 if (m_plibAttr) 626 return m_pParent->m_plibNode->line; 627 628 return m_plibNode->line; 629 } 630 631 ElementNode::ElementNode(Node *pParent, xmlNode *plibNode) 632 : Node(IsElement, 633 pParent, 634 plibNode, 635 NULL) 636 { 637 m_pcszName = (const char*)plibNode->name; 638 639 if ( plibNode->ns 640 && plibNode->ns->prefix 641 ) 642 m_pcszNamespace = (const char*)m_plibNode->ns->prefix; 645 643 } 646 644 … … 921 919 { 922 920 // we must be an element, not an attribute 923 if (!m ->plibNode)921 if (!m_plibNode) 924 922 throw ENodeIsNotElement(RT_SRC_POS); 925 923 … … 929 927 (const xmlChar*)pcszElementName))) 930 928 throw std::bad_alloc(); 931 xmlAddChild(m ->plibNode, plibNode);929 xmlAddChild(m_plibNode, plibNode); 932 930 933 931 // now wrap this in C++ 934 ElementNode *p = new ElementNode ;932 ElementNode *p = new ElementNode(this, plibNode); 935 933 boost::shared_ptr<ElementNode> pNew(p); 936 pNew->m->plibNode = plibNode;937 pNew->m->pcszName = (const char*)plibNode->name;938 939 934 m->children.push_back(pNew); 940 935 … … 956 951 if (!(plibNode = xmlNewText((const xmlChar*)pcszContent))) 957 952 throw std::bad_alloc(); 958 xmlAddChild(m ->plibNode, plibNode);953 xmlAddChild(m_plibNode, plibNode); 959 954 960 955 // now wrap this in C++ 961 ContentNode *p = new ContentNode ;956 ContentNode *p = new ContentNode(this, plibNode); 962 957 boost::shared_ptr<ContentNode> pNew(p); 963 pNew->m->plibNode = plibNode;964 pNew->m->pcszName = NULL;965 966 958 m->children.push_back(pNew); 967 959 … … 988 980 { 989 981 // libxml side: xmlNewProp creates an attribute 990 xmlAttr *plibAttr = xmlNewProp(m ->plibNode, (xmlChar*)pcszName, (xmlChar*)pcszValue);982 xmlAttr *plibAttr = xmlNewProp(m_plibNode, (xmlChar*)pcszName, (xmlChar*)pcszValue); 991 983 const char *pcszAttribName = (const char*)plibAttr->name; 992 984 993 985 // C++ side: create an attribute node around it 994 boost::shared_ptr<AttributeNode> pNew(new AttributeNode); 995 pNew->m->plibAttr = plibAttr; 996 pNew->m->pcszName = (const char*)plibAttr->name; 997 pNew->m->pParent = this; 986 boost::shared_ptr<AttributeNode> pNew(new AttributeNode(this, plibAttr)); 998 987 // store 999 988 m->attribs[pcszAttribName] = pNew; … … 1059 1048 } 1060 1049 1061 AttributeNode::AttributeNode() 1062 : Node(IsAttribute) 1063 { 1064 } 1065 1066 ContentNode::ContentNode() 1067 : Node(IsContent) 1050 AttributeNode::AttributeNode(Node *pParent, xmlAttr *plibAttr) 1051 : Node(IsAttribute, 1052 pParent, 1053 NULL, 1054 plibAttr) 1055 { 1056 m_pcszName = (const char*)plibAttr->name; 1057 } 1058 1059 ContentNode::ContentNode(Node *pParent, xmlNode *plibNode) 1060 : Node(IsContent, 1061 pParent, 1062 plibNode, 1063 NULL) 1068 1064 { 1069 1065 } … … 1195 1191 void Document::refreshInternals() // private 1196 1192 { 1197 m->pRootElement = new ElementNode(); 1198 m->pRootElement->m->plibNode = xmlDocGetRootElement(m->plibDocument); 1199 m->pRootElement->m->pcszName = (const char*)m->pRootElement->m->plibNode->name; 1193 m->pRootElement = new ElementNode(NULL, xmlDocGetRootElement(m->plibDocument)); 1200 1194 1201 1195 m->pRootElement->buildChildren(); … … 1240 1234 1241 1235 // now wrap this in C++ 1242 m->pRootElement = new ElementNode(); 1243 m->pRootElement->m->plibNode = plibRootNode; 1244 m->pRootElement->m->pcszName = (const char*)plibRootNode->name; 1236 m->pRootElement = new ElementNode(NULL, plibRootNode); 1245 1237 1246 1238 return m->pRootElement;
Note:
See TracChangeset
for help on using the changeset viewer.