Add XML Attribute classes.
[shibboleth/cpp-sp.git] / shibsp / attribute / XMLAttributeDecoder.cpp
diff --git a/shibsp/attribute/XMLAttributeDecoder.cpp b/shibsp/attribute/XMLAttributeDecoder.cpp
new file mode 100644 (file)
index 0000000..2d59528
--- /dev/null
@@ -0,0 +1,138 @@
+/*\r
+ *  Copyright 2009 Internet2\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+/**\r
+ * XMLAttributeDecoder.cpp\r
+ *\r
+ * Decodes arbitrary XML into an XMLAttribute.\r
+ */\r
+\r
+#include "internal.h"\r
+#include "attribute/AttributeDecoder.h"\r
+#include "attribute/XMLAttribute.h"\r
+\r
+#include <saml/saml1/core/Assertions.h>\r
+#include <saml/saml2/core/Assertions.h>\r
+#include <xmltooling/util/XMLHelper.h>\r
+\r
+using namespace shibsp;\r
+using namespace opensaml;\r
+using namespace xmltooling;\r
+using namespace std;\r
+\r
+namespace shibsp {\r
+    class SHIBSP_DLLLOCAL XMLAttributeDecoder : virtual public AttributeDecoder\r
+    {\r
+    public:\r
+        XMLAttributeDecoder(const DOMElement* e) : AttributeDecoder(e) {}\r
+        ~XMLAttributeDecoder() {}\r
+\r
+        Attribute* decode(\r
+            const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=NULL, const char* relyingParty=NULL\r
+            ) const;\r
+\r
+    private:\r
+        DDF convert(DOMElement* e, bool nameit=true) const;\r
+        auto_ptr_char m_formatter;\r
+        map<pair<xstring,xstring>,string> m_tagMap;\r
+    };\r
+\r
+    AttributeDecoder* SHIBSP_DLLLOCAL XMLAttributeDecoderFactory(const DOMElement* const & e)\r
+    {\r
+        return new XMLAttributeDecoder(e);\r
+    }\r
+};\r
+\r
+\r
+Attribute* XMLAttributeDecoder::decode(\r
+    const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty\r
+    ) const\r
+{\r
+    if (!xmlObject)\r
+        return NULL;\r
+\r
+    Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.XML");\r
+\r
+    auto_ptr<XMLAttribute> attr(new XMLAttribute(ids));\r
+    vector<string>& dest = attr->getValues();\r
+\r
+    // Handle any non-Attribute object directly.\r
+    if (!xmlObject || !XMLString::equals(saml1::Attribute::LOCAL_NAME, xmlObject->getElementQName().getLocalPart())) {\r
+        DOMElement* e = xmlObject->getDOM();\r
+        if (e) {\r
+            if (log.isDebugEnabled()) {\r
+                log.debug(\r
+                    "decoding XMLAttribute (%s) from XMLObject (%s)",\r
+                    ids.front().c_str(),\r
+                    (xmlObject->getSchemaType() ? xmlObject->getSchemaType()->toString() : xmlObject->getElementQName().toString()).c_str()\r
+                    );\r
+            }\r
+            dest.push_back(string());\r
+            XMLHelper::serialize(e, dest.back());\r
+        }\r
+        else {\r
+            log.warn("skipping XMLObject without a backing DOM");\r
+        }\r
+        return dest.empty() ? NULL : _decode(attr.release());\r
+    }\r
+\r
+    vector<XMLObject*>::const_iterator v,stop;\r
+\r
+    const saml2::Attribute* saml2attr = dynamic_cast<const saml2::Attribute*>(xmlObject);\r
+    if (saml2attr) {\r
+        const vector<XMLObject*>& values = saml2attr->getAttributeValues();\r
+        v = values.begin();\r
+        stop = values.end();\r
+        if (log.isDebugEnabled()) {\r
+            auto_ptr_char n(saml2attr->getName());\r
+            log.debug(\r
+                "decoding XMLAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",\r
+                ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()\r
+                );\r
+        }\r
+    }\r
+    else {\r
+        const saml1::Attribute* saml1attr = dynamic_cast<const saml1::Attribute*>(xmlObject);\r
+        if (saml1attr) {\r
+            const vector<XMLObject*>& values = saml1attr->getAttributeValues();\r
+            v = values.begin();\r
+            stop = values.end();\r
+            if (log.isDebugEnabled()) {\r
+                auto_ptr_char n(saml1attr->getAttributeName());\r
+                log.debug(\r
+                    "decoding XMLAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",\r
+                    ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()\r
+                    );\r
+            }\r
+        }\r
+        else {\r
+            log.warn("XMLObject type not recognized by XMLAttributeDecoder, no values returned");\r
+            return NULL;\r
+        }\r
+    }\r
+\r
+    for (; v!=stop; ++v) {\r
+        DOMElement* e = (*v)->getDOM();\r
+        if (e) {\r
+            dest.push_back(string());\r
+            XMLHelper::serialize(e, dest.back());\r
+        }\r
+        else\r
+            log.warn("skipping AttributeValue without a backing DOM");\r
+    }\r
+\r
+    return dest.empty() ? NULL : _decode(attr.release());\r
+}\r