Update ctors to use new attribute shortcuts.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / XMLHelper.cpp
index 5ffd63d..20d4058 100644 (file)
@@ -177,6 +177,20 @@ xmltooling::QName* XMLHelper::getNodeValueAsQName(const DOMNode* domNode)
     return new xmltooling::QName(domNode->lookupNamespaceURI(nullptr), value);
 }
 
+bool XMLHelper::getNodeValueAsBool(const xercesc::DOMNode* domNode, bool def)
+{
+    if (!domNode)
+        return def;
+    const XMLCh* value = domNode->getNodeValue();
+    if (!value || !*value)
+        return def;
+    if (*value == chLatin_t || *value == chDigit_1)
+        return true;
+    else if (*value == chLatin_f || *value == chDigit_0)
+        return false;
+    return def;
+}
+
 DOMElement* XMLHelper::appendChildElement(DOMElement* parentElement, DOMElement* childElement)
 {
     DOMDocument* parentDocument = parentElement->getOwnerDocument();
@@ -284,6 +298,43 @@ DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n, const XMLCh*
     return e;
 }
 
+string XMLHelper::getAttrString(const DOMElement* e, const char* defValue, const XMLCh* localName, const XMLCh* ns)
+{
+    if (e) {
+        auto_ptr_char val(e->getAttributeNS(ns, localName));
+        if (val.get() && *val.get())
+            return val.get();
+    }
+    return defValue ? defValue : "";
+}
+
+int XMLHelper::getAttrInt(const DOMElement* e, int defValue, const XMLCh* localName, const XMLCh* ns)
+{
+    if (e) {
+        const XMLCh* val = e->getAttributeNS(ns, localName);
+        if (val && *val) {
+            int i = XMLString::parseInt(val);
+            if (i)
+                return i;
+        }
+    }
+    return defValue;
+}
+
+bool XMLHelper::getAttrBool(const DOMElement* e, bool defValue, const XMLCh* localName, const XMLCh* ns)
+{
+    if (e) {
+        const XMLCh* val = e->getAttributeNS(ns, localName);
+        if (val) {
+            if (*val == chLatin_t || *val == chDigit_1)
+                return true;
+            if (*val == chLatin_f || *val == chDigit_0)
+                return false;
+        }
+    }
+    return defValue;
+}
+
 void XMLHelper::serialize(const DOMNode* n, std::string& buf, bool pretty)
 {
     static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };