VS10 solution files, convert from NULL macro to nullptr.
[shibboleth/sp.git] / shibsp / attribute / XMLAttribute.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  * XMLAttribute.cpp
19  *
20  * An Attribute whose values are serialized XML.
21  */
22
23 #include "internal.h"
24 #include "attribute/XMLAttribute.h"
25
26 #include <xercesc/util/Base64.hpp>
27
28 #ifndef SHIBSP_LITE
29 # include <xsec/framework/XSECDefs.hpp>
30 #endif
31
32 using namespace shibsp;
33 using namespace std;
34
35 namespace shibsp {
36     SHIBSP_DLLLOCAL Attribute* XMLAttributeFactory(DDF& in) {
37         return new XMLAttribute(in);
38     }
39 };
40
41 XMLAttribute::XMLAttribute(const vector<string>& ids) : Attribute(ids)
42 {
43 }
44
45 XMLAttribute::XMLAttribute(DDF& in) : Attribute(in)
46 {
47     DDF val = in.first().first();
48     while (val.string()) {
49         m_values.push_back(val.string());
50         val = in.first().next();
51     }
52 }
53
54 XMLAttribute::~XMLAttribute()
55 {
56 }
57
58 vector<string>& XMLAttribute::getValues()
59 {
60     return m_values;
61 }
62
63 const vector<string>& XMLAttribute::getValues() const
64 {
65     return m_values;
66 }
67
68 size_t XMLAttribute::valueCount() const
69 {
70     return m_values.size();
71 }
72
73 void XMLAttribute::clearSerializedValues()
74 {
75     m_serialized.clear();
76 }
77
78 const char* XMLAttribute::getString(size_t index) const
79 {
80     return m_values[index].c_str();
81 }
82
83 void XMLAttribute::removeValue(size_t index)
84 {
85     Attribute::removeValue(index);
86     if (index < m_values.size())
87         m_values.erase(m_values.begin() + index);
88 }
89
90 const vector<string>& XMLAttribute::getSerializedValues() const
91 {
92     xsecsize_t len;
93     XMLByte *pos, *pos2;
94     if (m_serialized.empty()) {
95         for (vector<string>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
96             XMLByte* enc = Base64::encode(reinterpret_cast<const XMLByte*>(i->data()), i->size(), &len);
97             if (enc) {
98                 for (pos=enc, pos2=enc; *pos2; pos2++)
99                     if (isgraph(*pos2))
100                         *pos++=*pos2;
101                 *pos=0;
102                 m_serialized.push_back(reinterpret_cast<char*>(enc));
103 #ifdef SHIBSP_XERCESC_HAS_XMLBYTE_RELEASE
104                 XMLString::release(&enc);
105 #else
106                 XMLString::release((char**)&enc);
107 #endif
108             }
109         }
110     }
111     return Attribute::getSerializedValues();
112 }
113
114 DDF XMLAttribute::marshall() const
115 {
116     DDF ddf = Attribute::marshall();
117     ddf.name("XML");
118     DDF vlist = ddf.first();
119     for (vector<string>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i)
120         vlist.add(DDF(nullptr).string(i->c_str()));
121     return ddf;
122 }