3809e972a9f69c2894f87a0238b19c9e87451d4b
[shibboleth/cpp-sp.git] / shibsp / attribute / Attribute.cpp
1 /*
2  *  Copyright 2001-2007 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  * shibsp/attribute/Attribute.cpp
19  * 
20  * A resolved attribute.
21  */
22
23 #include "internal.h"
24 #include "SPConfig.h"
25 #include "attribute/AttributeDecoder.h"
26 #include "attribute/SimpleAttribute.h"
27 #include "attribute/ScopedAttribute.h"
28 #include "attribute/NameIDAttribute.h"
29 #include "util/SPConstants.h"
30
31 #include <xercesc/util/XMLUniDefs.hpp>
32
33 using namespace shibsp;
34 using namespace xmltooling;
35 using namespace std;
36
37 namespace shibsp {
38
39     SHIBSP_DLLLOCAL Attribute* SimpleAttributeFactory(DDF& in) {
40         return new SimpleAttribute(in);
41     }
42     
43     SHIBSP_DLLLOCAL Attribute* ScopedAttributeFactory(DDF& in) {
44         return new ScopedAttribute(in);
45     }
46     
47     SHIBSP_DLLLOCAL Attribute* NameIDAttributeFactory(DDF& in) {
48         return new NameIDAttribute(in);
49     }
50     
51     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,QName,const DOMElement*>::Factory StringAttributeDecoderFactory;
52     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,QName,const DOMElement*>::Factory ScopedAttributeDecoderFactory;
53     SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,QName,const DOMElement*>::Factory NameIDAttributeDecoderFactory;
54
55     static const XMLCh _StringAttributeDecoder[] = UNICODE_LITERAL_22(S,t,r,i,n,g,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
56     static const XMLCh _ScopedAttributeDecoder[] = UNICODE_LITERAL_22(S,c,o,p,e,d,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
57     static const XMLCh _NameIDAttributeDecoder[] = UNICODE_LITERAL_22(N,a,m,e,I,D,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
58
59     static const XMLCh caseSensitive[] =           UNICODE_LITERAL_13(c,a,s,e,S,e,n,s,i,t,i,v,e);
60 };
61
62 QName shibsp::StringAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _StringAttributeDecoder);
63 QName shibsp::ScopedAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _ScopedAttributeDecoder);
64 QName shibsp::NameIDAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _NameIDAttributeDecoder);
65
66 void shibsp::registerAttributeDecoders()
67 {
68     SPConfig& conf = SPConfig::getConfig();
69     conf.AttributeDecoderManager.registerFactory(StringAttributeDecoderType, StringAttributeDecoderFactory);
70     conf.AttributeDecoderManager.registerFactory(ScopedAttributeDecoderType, ScopedAttributeDecoderFactory);
71     conf.AttributeDecoderManager.registerFactory(NameIDAttributeDecoderType, NameIDAttributeDecoderFactory);
72 }
73
74 void shibsp::registerAttributeFactories()
75 {
76     Attribute::registerFactory("", SimpleAttributeFactory);
77     Attribute::registerFactory("Simple", SimpleAttributeFactory);
78     Attribute::registerFactory("Scoped", ScopedAttributeFactory);
79     Attribute::registerFactory("NameID", NameIDAttributeFactory);
80 }
81
82 std::map<std::string,Attribute::AttributeFactory*> Attribute::m_factoryMap;
83
84 Attribute* Attribute::unmarshall(DDF& in)
85 {
86     map<string,AttributeFactory*>::const_iterator i = m_factoryMap.find(in.name() ? in.name() : "");
87     if (i == m_factoryMap.end())
88         throw AttributeException("No registered factory for Attribute of type ($1).", xmltooling::params(1,in.name()));
89     return (i->second)(in);
90 }
91
92 AttributeDecoder::AttributeDecoder(const DOMElement *e) : m_caseSensitive(true)
93 {
94     if (e) {
95         const XMLCh* flag = e->getAttributeNS(NULL,caseSensitive);
96         if (flag && (*flag == chLatin_f || *flag == chDigit_0))
97             m_caseSensitive = false;
98     }
99 }