Imported Upstream version 2.2.1+dfsg
[shibboleth/sp.git] / shibsp / attribute / ScopedAttribute.h
1 /*
2  *  Copyright 2001-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  * @file shibsp/attribute/ScopedAttribute.h
19  * 
20  * An Attribute whose values are relations of a value and a scope.
21  */
22
23 #ifndef __shibsp_scopedattr_h__
24 #define __shibsp_scopedattr_h__
25
26 #include <shibsp/attribute/Attribute.h>
27
28 namespace shibsp {
29
30 #if defined (_MSC_VER)
31     #pragma warning( push )
32     #pragma warning( disable : 4251 )
33 #endif
34
35     /**
36      * An Attribute whose values are relations of a value and a scope.
37      * 
38      * <p>In practice, scoped attributes are simple pairs of strings instead
39      * of a single string. They can be expressed as a string easily using a delimeter,
40      * typically an '@' symbol. The scope concept allows certain kinds of filtering to
41      * be performed more intelligently and efficiently, although not all scoped
42      * attributes can be effectively filtered (e.g. if the set of scope values is
43      * unconstrained).
44      */
45     class SHIBSP_API ScopedAttribute : public Attribute
46     {
47     public:
48         /**
49          * Constructor.
50          * 
51          * @param ids   array with primary identifier in first position, followed by any aliases
52          * @param delimeter value/scope delimeter when serializing
53          */
54         ScopedAttribute(const std::vector<std::string>& ids, char delimeter='@')
55             : Attribute(ids), m_delimeter(delimeter) {
56         }
57
58         /**
59          * Constructs based on a remoted ScopedAttribute.
60          * 
61          * @param in    input object containing marshalled ScopedAttribute
62          */
63         ScopedAttribute(DDF& in) : Attribute(in), m_delimeter('@') {
64             DDF val = in["_delimeter"];
65             if (val.isint())
66                 m_delimeter = static_cast<char>(val.integer());
67             val = in.first().first();
68             while (val.name() && val.string()) {
69                 m_values.push_back(std::make_pair(std::string(val.name()), std::string(val.string())));
70                 val = in.first().next();
71             }
72         }
73         
74         virtual ~ScopedAttribute() {}
75
76         /**
77          * Returns the set of values encoded as UTF-8 strings.
78          * 
79          * <p>Each compound value is a pair containing the simple value and the scope. 
80          * 
81          * @return  a mutable vector of the values
82          */
83         std::vector< std::pair<std::string,std::string> >& getValues() {
84             return m_values;
85         }
86
87         /**
88          * Returns the set of values encoded as UTF-8 strings.
89          * 
90          * <p>Each compound value is a pair containing the simple value and the scope. 
91          * 
92          * @return  an immutable vector of the values
93          */
94         const std::vector< std::pair<std::string,std::string> >& getValues() const {
95             return m_values;
96         }
97
98         size_t valueCount() const {
99             return m_values.size();
100         }
101         
102         void clearSerializedValues() {
103             m_serialized.clear();
104         }
105         
106         const char* getString(size_t index) const {
107             return m_values[index].first.c_str();
108         }
109
110         const char* getScope(size_t index) const {
111             return m_values[index].second.c_str();
112         }
113
114         void removeValue(size_t index) {
115             Attribute::removeValue(index);
116             if (index < m_values.size())
117                 m_values.erase(m_values.begin() + index);
118         }
119
120         const std::vector<std::string>& getSerializedValues() const {
121             if (m_serialized.empty()) {
122                 for (std::vector< std::pair<std::string,std::string> >::const_iterator i=m_values.begin(); i!=m_values.end(); ++i)
123                     m_serialized.push_back(i->first + m_delimeter + i->second);
124             }
125             return Attribute::getSerializedValues();
126         }
127
128         DDF marshall() const {
129             DDF ddf = Attribute::marshall();
130             ddf.name("Scoped");
131             if (m_delimeter != '@')
132                 ddf.addmember("_delimeter").integer(m_delimeter);
133             DDF vlist = ddf.first();
134             for (std::vector< std::pair<std::string,std::string> >::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
135                 DDF val = DDF(i->first.c_str()).string(i->second.c_str());
136                 vlist.add(val);
137             }
138             return ddf;
139         }
140     
141     private:
142         char m_delimeter;
143         std::vector< std::pair<std::string,std::string> > m_values;
144     };
145
146 #if defined (_MSC_VER)
147     #pragma warning( pop )
148 #endif
149
150 };
151
152 #endif /* __shibsp_scopedattr_h__ */