73f208ea177acb2b5fa5fea2020f758cc1210b22
[shibboleth/sp.git] / shibsp / attribute / ScopedAttribute.h
1 /*
2  *  Copyright 2001-2006 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     /**
31      * An Attribute whose values are relations of a value and a scope.
32      * 
33      * <p>In practice, scoped attributes are simple pairs of strings instead
34      * of a single string. They can be expressed as a string easily using a delimeter,
35      * typically an '@' symbol. The scope concept allows certain kinds of filtering to
36      * be performed more intelligently and efficiently, although not all scoped
37      * attributes can be effectively filtered (e.g. if the set of scope values is
38      * unconstrained).
39      */
40     class SHIBSP_API ScopedAttribute : public Attribute
41     {
42     public:
43         /**
44          * Constructor
45          * 
46          * @param id    Attribute identifier
47          */
48         ScopedAttribute(const char* id) : Attribute(id) {}
49         
50         virtual ~ScopedAttribute() {}
51         
52         /**
53          * Returns the set of values encoded as UTF-8 strings.
54          * 
55          * <p>Each compound value is a pair containing the simple value and the scope. 
56          * 
57          * @return  a mutable vector of the values
58          */
59         std::vector< std::pair<std::string,std::string> >& getValues() {
60             return m_values;
61         }
62
63         size_t valueCount() const {
64             return m_values.size();
65         }
66         
67         void clearSerializedValues() {
68             m_serialized.clear();
69         }
70         
71         const std::vector<std::string>& getSerializedValues() const {
72             if (m_serialized.empty()) {
73                 for (std::vector< std::pair<std::string,std::string> >::const_iterator i=m_values.begin(); i!=m_values.end(); ++i)
74                     m_serialized.push_back(i->first + '@' + i->second);
75             }
76             return Attribute::getSerializedValues();
77         }
78
79         DDF marshall() const {
80             DDF ddf = Attribute::marshall();
81             ddf.name("ScopedAttribute");
82             DDF vlist = ddf.addmember("values").list();
83             for (std::vector< std::pair<std::string,std::string> >::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
84                 DDF val = DDF(NULL).structure();
85                 val.addmember("value").string(i->first.c_str());
86                 val.addmember("scope").string(i->second.c_str());
87                 vlist.add(val);
88             }
89             return ddf;
90         }
91     
92     private:
93         std::vector< std::pair<std::string,std::string> > m_values;
94     };
95
96 };
97
98 #endif /* __shibsp_scopedattr_h__ */