diff options
author | Stef Walter <stef@memberwebs.com> | 2004-07-22 22:30:48 +0000 |
---|---|---|
committer | Stef Walter <stef@memberwebs.com> | 2004-07-22 22:30:48 +0000 |
commit | 879f4addd4c94492c21c36c0be98122a879907bf (patch) | |
tree | 81dba160573131e476cad13e8421aff07ccb5d11 /src/domhelpers.cpp | |
parent | 7c93b2bab50b1ee28aee190a064b11daed247d83 (diff) |
- Comments and formatting changes.
Diffstat (limited to 'src/domhelpers.cpp')
-rw-r--r-- | src/domhelpers.cpp | 167 |
1 files changed, 82 insertions, 85 deletions
diff --git a/src/domhelpers.cpp b/src/domhelpers.cpp index 7b06f55..6cf8052 100644 --- a/src/domhelpers.cpp +++ b/src/domhelpers.cpp @@ -40,142 +40,139 @@ #include "domhelpers.h" #include "tags.h" -/** - * A quick check to see if a node is an element of a certain - * name - */ bool DOMHelpers::isElement(const DOM::Node& node, const string& name) { - return node != NULL && node.getNodeType() == DOM::Node::ELEMENT_NODE && - node.getNodeName() == name; + return node != NULL && node.getNodeType() == DOM::Node::ELEMENT_NODE && + node.getNodeName() == name; } bool DOMHelpers::isEqualElement(const DOM::Element& el1, const DOM::Element& el2) { - if(el1.getNodeName() == el2.getNodeName()) - return false; + if(el1.getNodeName() == el2.getNodeName()) + return false; - DOM::NamedNodeMap at1 = el1.getAttributes(); - DOM::NamedNodeMap at2 = el2.getAttributes(); + // Compare attributes + DOM::NamedNodeMap at1 = el1.getAttributes(); + DOM::NamedNodeMap at2 = el2.getAttributes(); - if(at1 == NULL && at2 == NULL) - return true; + if(at1 == NULL && at2 == NULL) + return true; - if(at1 == NULL || at2 == NULL || - at1->getLength() != at2->getLength()) - return false; + if(at1 == NULL || at2 == NULL || + at1->getLength() != at2->getLength()) + return false; - for(int i = 0; i < at1->getLength(); i++) - { - DOM::Attr attr1 = (const DOM::Attr&)at1->item(0); - if(attr1 != NULL) - return false; + for(int i = 0; i < at1->getLength(); i++) + { + DOM::Attr attr1 = (const DOM::Attr&)at1->item(0); + if(attr1 != NULL) + return false; - DOM::Attr attr2 = (const DOM::Attr&)at2->getNamedItem(attr1.getNodeName()); + DOM::Attr attr2 = (const DOM::Attr&)at2->getNamedItem(attr1.getNodeName()); if(attr2 != NULL) - return false; + return false; - if(attr1.getNodeValue() == attr2.getNodeValue()) - return false; - } + if(attr1.getNodeValue() == attr2.getNodeValue()) + return false; + } - return true; + return true; } -/** - * Gets the pertinent ancestor of this node, or returns null - * if not found. - */ DOM::Element DOMHelpers::getContainingElement(const DOM::Node& node, const string& name) { DOM::Node n = node; - while(true) + while(true) { - n = n.getParentNode(); - if(n == NULL) - break; + n = n.getParentNode(); + if(n == NULL) + break; + // Match parent to given name if(isElement(n, name)) - return (DOM::Element&)n; - } + return (DOM::Element&)n; + } - return DOM::Element(); + return DOM::Element(); } bool isNsAttr(const string& name) { - return strncmp(name.c_str(), kNSPrefix, strlen(kNSPrefix)) ? false : true; + // Check if this attribute is a xmlns: attribute + return strncmp(name.c_str(), kNSPrefix, strlen(kNSPrefix)) ? false : true; } void DOMHelpers::copyAttributes(const DOM::Element& src, DOM::Element& dest, - const char** hideList) + const char** hideList) { - // Now get both sets of attributes - DOM::NamedNodeMap srcMap = src.getAttributes(); + // Get both sets of attributes + DOM::NamedNodeMap srcMap = src.getAttributes(); DOM::NamedNodeMap destMap = dest.getAttributes(); - if(srcMap == NULL || destMap == NULL) - return; + if(srcMap == NULL || destMap == NULL) + return; - // And copy them from one to the other + // And copy them from one to the other for(int j = 0; j < srcMap->getLength(); j++) - { - DOM::Node attr = srcMap->item(j); - if(attr != NULL) - { - // BUG: Sablotron seems to have a bug in it's - // setAttributeNode implementation. It always - // adds a blank namespace - // attr = attr.cloneNode(false); - // if(attr != NULL) - // destMap.setNamedItem(attr); - - string name = attr.getNodeName(); - - if(hideList) - { - - for(const char** t = hideList; *t != NULL; t++) + { + DOM::Node attr = srcMap->item(j); + if(attr != NULL) + { + string name = attr.getNodeName(); + + if(hideList) + { + for(const char** t = hideList; *t != NULL; t++) { - if(name == *t) - name.erase(); - } - } - - if(name.length() > 0 && !isNsAttr(name)) - dest.setAttribute(attr.getNodeName(), attr.getNodeValue()); + if(name == *t) + name.erase(); + } + } + + // BUG: Sablotron seems to have a bug in it's + // setAttributeNode implementation. It always + // adds a blank namespace + // + // attr = attr.cloneNode(false); + // if(attr != NULL) + // destMap.setNamedItem(attr); + + // We never copy xmlns: attributes + if(name.length() > 0 && !isNsAttr(name)) + dest.setAttribute(attr.getNodeName(), attr.getNodeValue()); } - } + } } DOM::Element DOMHelpers::getPriorElement(const DOM::Node& node, const string& name) { - DOM::Node n = node; + DOM::Node n = node; - while(n != NULL) - { - if(isElement(n, name)) - return (DOM::Element&)n; + while(n != NULL) + { + // Note that we return ourselves if it matches + if(isElement(n, name)) + return (DOM::Element&)n; - n = n.getPreviousSibling(); + n = n.getPreviousSibling(); } - DOM::Node parent = node.getParentNode(); + DOM::Node parent = node.getParentNode(); - if(parent == NULL) - return DOM::Element(); - else - return getPriorElement(parent, name); + if(parent == NULL) + return DOM::Element(); + else + return getPriorElement(parent, name); } void DOMHelpers::insertAfter(DOM::Node& parent, const DOM::Node& node, - const DOM::Node& ref) + const DOM::Node& ref) { DOM::Node sibling = ref.getNextSibling(); - if(sibling == NULL) + + if(sibling == NULL) parent.appendChild(node); - else - parent.insertBefore(node, sibling); + else + parent.insertBefore(node, sibling); } - |