VS10 solution files, convert from NULL macro to nullptr.
[shibboleth/sp.git] / shibsp / attribute / filtering / impl / AttributeRequesterRegexFunctor.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  * AttributeRequesterRegexFunctor.cpp
19  * 
20  * A match function that evaluates to true if the Attribute requester matches the provided regular
21  * expression.
22  */
23
24 #include "internal.h"
25 #include "exceptions.h"
26 #include "attribute/filtering/FilteringContext.h"
27 #include "attribute/filtering/FilterPolicyContext.h"
28 #include "attribute/filtering/MatchFunctor.h"
29
30 #include <xercesc/util/regx/RegularExpression.hpp>
31
32 namespace shibsp {
33
34     static const XMLCh options[] =  UNICODE_LITERAL_7(o,p,t,i,o,n,s);
35     static const XMLCh regex[] =    UNICODE_LITERAL_5(r,e,g,e,x);
36     
37     /**
38      * A match function that evaluates to true if the Attribute requester matches the provided regular
39      * expression.
40      */
41     class SHIBSP_DLLLOCAL AttributeRequesterRegexFunctor : public MatchFunctor
42     {
43         RegularExpression* m_regex;
44     public:
45         AttributeRequesterRegexFunctor(const DOMElement* e) {
46             const XMLCh* r = e ? e->getAttributeNS(nullptr,regex) : nullptr;
47             if (!r || !*r)
48                 throw ConfigurationException("AttributeRequesterRegex MatchFunctor requires non-empty regex attribute.");
49             try {
50                 m_regex = new RegularExpression(r, e->getAttributeNS(nullptr,options));
51             }
52             catch (XMLException& ex) {
53                 xmltooling::auto_ptr_char temp(ex.getMessage());
54                 throw ConfigurationException(temp.get());
55             }
56         }
57
58         virtual ~AttributeRequesterRegexFunctor() {
59             delete m_regex;
60         }
61
62         bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
63             return m_regex->matches(filterContext.getAttributeRequester());
64         }
65
66         bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
67             return m_regex->matches(filterContext.getAttributeRequester());
68         }
69     };
70
71     MatchFunctor* SHIBSP_DLLLOCAL AttributeRequesterRegexFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
72     {
73         return new AttributeRequesterRegexFunctor(p.second);
74     }
75
76 };