Imported Upstream version 2.3+dfsg
[shibboleth/sp.git] / shibsp / attribute / ScopedAttribute.cpp
1 /*
2  *  Copyright 2009 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  * ScopedAttribute.cpp
19  *
20  * An Attribute whose values are relations of a value and a scope.
21  */
22
23 #include "internal.h"
24 #include "attribute/ScopedAttribute.h"
25
26 using namespace shibsp;
27 using namespace std;
28
29 namespace shibsp {
30     SHIBSP_DLLLOCAL Attribute* ScopedAttributeFactory(DDF& in) {
31         return new ScopedAttribute(in);
32     }
33 };
34
35 ScopedAttribute::ScopedAttribute(const vector<string>& ids, char delimeter) : Attribute(ids), m_delimeter(delimeter)
36 {
37 }
38
39 ScopedAttribute::ScopedAttribute(DDF& in) : Attribute(in), m_delimeter('@')
40 {
41     DDF val = in["_delimeter"];
42     if (val.isint())
43         m_delimeter = static_cast<char>(val.integer());
44     val = in.first().first();
45     while (val.name() && val.string()) {
46         m_values.push_back(make_pair(string(val.name()), string(val.string())));
47         val = in.first().next();
48     }
49 }
50
51 ScopedAttribute::~ScopedAttribute()
52 {
53 }
54
55 vector< pair<string,string> >& ScopedAttribute::getValues()
56 {
57     return m_values;
58 }
59
60 const vector< pair<string,string> >& ScopedAttribute::getValues() const
61 {
62     return m_values;
63 }
64
65 size_t ScopedAttribute::valueCount() const
66 {
67     return m_values.size();
68 }
69
70 void ScopedAttribute::clearSerializedValues()
71 {
72     m_serialized.clear();
73 }
74
75 const char* ScopedAttribute::getString(size_t index) const
76 {
77     return m_values[index].first.c_str();
78 }
79
80 const char* ScopedAttribute::getScope(size_t index) const
81 {
82     return m_values[index].second.c_str();
83 }
84
85 void ScopedAttribute::removeValue(size_t index)
86 {
87     Attribute::removeValue(index);
88     if (index < m_values.size())
89         m_values.erase(m_values.begin() + index);
90 }
91
92 const vector<string>& ScopedAttribute::getSerializedValues() const
93 {
94     if (m_serialized.empty()) {
95         for (vector< pair<string,string> >::const_iterator i=m_values.begin(); i!=m_values.end(); ++i)
96             m_serialized.push_back(i->first + m_delimeter + i->second);
97     }
98     return Attribute::getSerializedValues();
99 }
100
101 DDF ScopedAttribute::marshall() const
102 {
103     DDF ddf = Attribute::marshall();
104     ddf.name("Scoped");
105     if (m_delimeter != '@')
106         ddf.addmember("_delimeter").integer(m_delimeter);
107     DDF vlist = ddf.first();
108     for (vector< pair<string,string> >::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
109         DDF val = DDF(i->first.c_str()).string(i->second.c_str());
110         vlist.add(val);
111     }
112     return ddf;
113 }