VS10 solution files, convert from NULL macro to nullptr.
[shibboleth/sp.git] / shibsp / attribute / filtering / impl / NumberOfAttributeValuesFunctor.cpp
1 /*
2  *  Copyright 2001-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  * NumberOfAttributeValuesFunctor.cpp
19  * 
20  * A match function that evaluates to true if the given attribute has as a number
21  * of values that falls between the minimum and maximum.
22  */
23
24 #include "internal.h"
25 #include "exceptions.h"
26 #include "attribute/Attribute.h"
27 #include "attribute/filtering/FilteringContext.h"
28 #include "attribute/filtering/FilterPolicyContext.h"
29 #include "attribute/filtering/MatchFunctor.h"
30
31 using namespace shibsp;
32 using namespace std;
33
34 namespace shibsp {
35
36     static const XMLCh attributeID[] =  UNICODE_LITERAL_11(a,t,t,r,i,b,u,t,e,I,D);
37     static const XMLCh maximum[] =      UNICODE_LITERAL_7(m,a,x,i,m,u,m);
38     static const XMLCh minimum[] =      UNICODE_LITERAL_7(m,i,n,i,m,u,m);
39
40     /**
41      * A match function that evaluates to true if the given attribute has as a number
42      * of values that falls between the minimum and maximum.
43      */
44     class SHIBSP_DLLLOCAL NumberOfAttributeValuesFunctor : public MatchFunctor
45     {
46         unsigned int m_min,m_max;
47         xmltooling::auto_ptr_char m_attributeID;
48
49         size_t count(const FilteringContext& filterContext) const;
50
51     public:
52         NumberOfAttributeValuesFunctor(const DOMElement* e)
53                 : m_min(0), m_max(INT_MAX), m_attributeID(e ? e->getAttributeNS(nullptr,attributeID) : nullptr) {
54             if (!m_attributeID.get() || !*m_attributeID.get())
55                 throw ConfigurationException("No attributeID specified.");
56             const XMLCh* num = e->getAttributeNS(nullptr, minimum);
57             if (num && *num)
58                 m_min = XMLString::parseInt(num);
59             num = e->getAttributeNS(nullptr, maximum);
60             if (num && *num)
61                 m_max = XMLString::parseInt(num);
62         }
63
64         bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
65             size_t c = count(filterContext);
66             return (m_min <= c && c <= m_max);
67         }
68
69         bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
70             size_t c = count(filterContext);
71             return (m_min <= c && c <= m_max);
72         }
73     };
74
75     MatchFunctor* SHIBSP_DLLLOCAL NumberOfAttributeValuesFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
76     {
77         return new NumberOfAttributeValuesFunctor(p.second);
78     }
79
80 };
81
82 size_t NumberOfAttributeValuesFunctor::count(const FilteringContext& filterContext) const
83 {
84     size_t count = 0;
85     pair<multimap<string,Attribute*>::const_iterator,multimap<string,Attribute*>::const_iterator> attrs =
86         filterContext.getAttributes().equal_range(m_attributeID.get());
87     for (; attrs.first != attrs.second; ++attrs.first)
88         count += attrs.first->second->valueCount();
89     return count;
90 }