VS10 solution files, convert from NULL macro to nullptr.
[shibboleth/sp.git] / shibsp / attribute / XMLAttributeDecoder.cpp
1 /*
2  *  Copyright 2009-2010 Internet2
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * XMLAttributeDecoder.cpp
19  *
20  * Decodes arbitrary XML into an XMLAttribute.
21  */
22
23 #include "internal.h"
24 #include "attribute/AttributeDecoder.h"
25 #include "attribute/XMLAttribute.h"
26
27 #include <saml/saml1/core/Assertions.h>
28 #include <saml/saml2/core/Assertions.h>
29 #include <xmltooling/util/XMLHelper.h>
30
31 using namespace shibsp;
32 using namespace opensaml;
33 using namespace xmltooling;
34 using namespace std;
35
36 namespace shibsp {
37     class SHIBSP_DLLLOCAL XMLAttributeDecoder : virtual public AttributeDecoder
38     {
39     public:
40         XMLAttributeDecoder(const DOMElement* e) : AttributeDecoder(e) {}
41         ~XMLAttributeDecoder() {}
42
43         Attribute* decode(
44             const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
45             ) const;
46
47     private:
48         DDF convert(DOMElement* e, bool nameit=true) const;
49         auto_ptr_char m_formatter;
50         map<pair<xstring,xstring>,string> m_tagMap;
51     };
52
53     AttributeDecoder* SHIBSP_DLLLOCAL XMLAttributeDecoderFactory(const DOMElement* const & e)
54     {
55         return new XMLAttributeDecoder(e);
56     }
57 };
58
59
60 Attribute* XMLAttributeDecoder::decode(
61     const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
62     ) const
63 {
64     if (!xmlObject)
65         return nullptr;
66
67     Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.XML");
68
69     auto_ptr<XMLAttribute> attr(new XMLAttribute(ids));
70     vector<string>& dest = attr->getValues();
71
72     // Handle any non-Attribute object directly.
73     if (!xmlObject || !XMLString::equals(saml1::Attribute::LOCAL_NAME, xmlObject->getElementQName().getLocalPart())) {
74         DOMElement* e = xmlObject->getDOM();
75         if (e) {
76             if (log.isDebugEnabled()) {
77                 log.debug(
78                     "decoding XMLAttribute (%s) from XMLObject (%s)",
79                     ids.front().c_str(),
80                     (xmlObject->getSchemaType() ? xmlObject->getSchemaType()->toString() : xmlObject->getElementQName().toString()).c_str()
81                     );
82             }
83             dest.push_back(string());
84             XMLHelper::serialize(e, dest.back());
85         }
86         else {
87             log.warn("skipping XMLObject without a backing DOM");
88         }
89         return dest.empty() ? nullptr : _decode(attr.release());
90     }
91
92     vector<XMLObject*>::const_iterator v,stop;
93
94     const saml2::Attribute* saml2attr = dynamic_cast<const saml2::Attribute*>(xmlObject);
95     if (saml2attr) {
96         const vector<XMLObject*>& values = saml2attr->getAttributeValues();
97         v = values.begin();
98         stop = values.end();
99         if (log.isDebugEnabled()) {
100             auto_ptr_char n(saml2attr->getName());
101             log.debug(
102                 "decoding XMLAttribute (%s) from SAML 2 Attribute (%s) with %lu value(s)",
103                 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
104                 );
105         }
106     }
107     else {
108         const saml1::Attribute* saml1attr = dynamic_cast<const saml1::Attribute*>(xmlObject);
109         if (saml1attr) {
110             const vector<XMLObject*>& values = saml1attr->getAttributeValues();
111             v = values.begin();
112             stop = values.end();
113             if (log.isDebugEnabled()) {
114                 auto_ptr_char n(saml1attr->getAttributeName());
115                 log.debug(
116                     "decoding XMLAttribute (%s) from SAML 1 Attribute (%s) with %lu value(s)",
117                     ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
118                     );
119             }
120         }
121         else {
122             log.warn("XMLObject type not recognized by XMLAttributeDecoder, no values returned");
123             return nullptr;
124         }
125     }
126
127     for (; v!=stop; ++v) {
128         DOMElement* e = (*v)->getDOM();
129         if (e) {
130             dest.push_back(string());
131             XMLHelper::serialize(e, dest.back());
132         }
133         else
134             log.warn("skipping AttributeValue without a backing DOM");
135     }
136
137     return dest.empty() ? nullptr : _decode(attr.release());
138 }