Imported Upstream version 2.2.1+dfsg
[shibboleth/sp.git] / shibsp / attribute / XMLAttribute.h
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  * @file shibsp/attribute/XMLAttribute.h
19  *
20  * An Attribute whose values are serialized XML.
21  */
22
23 #ifndef __shibsp_xmlattr_h__
24 #define __shibsp_xmlattr_h__
25
26 #include <shibsp/attribute/Attribute.h>
27
28 namespace shibsp {
29
30     /**
31      * An Attribute whose values are serialized XML.
32      */
33     class SHIBSP_API XMLAttribute : public Attribute
34     {
35     public:
36         /**
37          * Constructor.
38          *
39          * @param ids   array with primary identifier in first position, followed by any aliases
40          */
41         XMLAttribute(const std::vector<std::string>& ids) : Attribute(ids) {}
42
43         /**
44          * Constructs based on a remoted XMLAttribute.
45          *
46          * @param in    input object containing marshalled XMLAttribute
47          */
48         XMLAttribute(DDF& in) : Attribute(in) {
49             DDF val = in.first().first();
50             while (val.string()) {
51                 m_values.push_back(val.string());
52                 val = in.first().next();
53             }
54         }
55
56         virtual ~XMLAttribute() {}
57
58         /**
59          * Returns the set of values encoded as XML.
60          *
61          * @return  a mutable vector of the values
62          */
63         std::vector<std::string>& getValues() {
64             return m_values;
65         }
66
67         /**
68          * Returns the set of values encoded as XML.
69          *
70          * @return  an immutable vector of the values
71          */
72         const std::vector<std::string>& getValues() const {
73             return m_values;
74         }
75
76         size_t valueCount() const {
77             return m_values.size();
78         }
79
80         void clearSerializedValues() {
81             m_serialized.clear();
82         }
83
84         const char* getString(size_t index) const {
85             return m_values[index].c_str();
86         }
87
88         void removeValue(size_t index) {
89             Attribute::removeValue(index);
90             if (index < m_values.size())
91                 m_values.erase(m_values.begin() + index);
92         }
93
94         const std::vector<std::string>& getSerializedValues() const;
95
96         DDF marshall() const {
97             DDF ddf = Attribute::marshall();
98             ddf.name("XML");
99             DDF vlist = ddf.first();
100             for (std::vector<std::string>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i)
101                 vlist.add(DDF(NULL).string(i->c_str()));
102             return ddf;
103         }
104
105     private:
106         std::vector<std::string> m_values;
107     };
108
109 };
110
111 #endif /* __shibsp_xmlattr_h__ */