d7dc70f2df5d99367a920295db54aee2760e79d2
[shibboleth/sp.git] / shibsp / util / DOMPropertySet.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  * DOMPropertySet.cpp
19  * 
20  * DOM-based property set implementation.
21  */
22
23 #include "internal.h"
24 #include "util/DOMPropertySet.h"
25
26 #include <algorithm>
27 #include <xmltooling/util/NDC.h>
28 #include <xmltooling/util/XMLConstants.h>
29
30 using namespace shibsp;
31 using namespace xmltooling;
32 using namespace xercesc;
33 using namespace std;
34
35 DOMPropertySet::~DOMPropertySet()
36 {
37     for (map<string,pair<char*,const XMLCh*> >::iterator i=m_map.begin(); i!=m_map.end(); i++)
38         XMLString::release(&(i->second.first));
39     for_each(m_nested.begin(),m_nested.end(),cleanup_pair<string,DOMPropertySet>());
40 }
41
42 void DOMPropertySet::load(
43     const DOMElement* e,
44     Category& log,
45     DOMNodeFilter* filter,
46     const std::map<std::string,std::string>* remapper
47     )
48 {
49 #ifdef _DEBUG
50     NDC ndc("load");
51 #endif
52     if (!e)
53         return;
54     m_root=e;
55
56     // Process each attribute as a property.
57     DOMNamedNodeMap* attrs=m_root->getAttributes();
58     for (XMLSize_t i=0; i<attrs->getLength(); i++) {
59         DOMNode* a=attrs->item(i);
60         if (!XMLString::compareString(a->getNamespaceURI(),xmlconstants::XMLNS_NS))
61             continue;
62         char* val=XMLString::transcode(a->getNodeValue());
63         if (val && *val) {
64             auto_ptr_char ns(a->getNamespaceURI());
65             auto_ptr_char name(a->getLocalName());
66             const char* realname=name.get();
67             map<string,string>::const_iterator remap;
68             if (remapper) {
69                 remap=remapper->find(realname);
70                 if (remap!=remapper->end()) {
71                     log.warn("remapping property (%s) to (%s)",realname,remap->second.c_str());
72                     realname=remap->second.c_str();
73                 }
74             }
75             if (ns.get()) {
76                 if (remapper && (remap=remapper->find(ns.get()))!=remapper->end())
77                     m_map[string("{") + remap->second.c_str() + '}' + realname]=pair<char*,const XMLCh*>(val,a->getNodeValue());
78                 else
79                     m_map[string("{") + ns.get() + '}' + realname]=pair<char*,const XMLCh*>(val,a->getNodeValue());
80                 log.debug("added property {%s}%s (%s)",ns.get(),realname,val);
81             }
82             else {
83                 m_map[realname]=pair<char*,const XMLCh*>(val,a->getNodeValue());
84                 log.debug("added property %s (%s)",realname,val);
85             }
86         }
87     }
88     
89     // Process non-excluded elements as nested sets.
90     DOMTreeWalker* walker=
91         static_cast<DOMDocumentTraversal*>(
92             m_root->getOwnerDocument())->createTreeWalker(const_cast<DOMElement*>(m_root),DOMNodeFilter::SHOW_ELEMENT,filter,false
93             );
94     e=static_cast<DOMElement*>(walker->firstChild());
95     while (e) {
96         auto_ptr_char ns(e->getNamespaceURI());
97         auto_ptr_char name(e->getLocalName());
98         const char* realname=name.get();
99         map<string,string>::const_iterator remap;
100         if (remapper) {
101             remap=remapper->find(realname);
102             if (remap!=remapper->end()) {
103                 log.warn("remapping property set (%s) to (%s)",realname,remap->second.c_str());
104                 realname=remap->second.c_str();
105             }
106         }
107         string key;
108         if (ns.get()) {
109             if (remapper && (remap=remapper->find(ns.get()))!=remapper->end())
110                 key=string("{") + remap->second.c_str() + '}' + realname;
111             else
112                 key=string("{") + ns.get() + '}' + realname;
113         }
114         else
115             key=realname;
116         if (m_nested.find(key)!=m_nested.end())
117             log.warn("load() skipping duplicate property set: %s",key.c_str());
118         else {
119             DOMPropertySet* set=new DOMPropertySet();
120             set->load(e,log,filter,remapper);
121             m_nested[key]=set;
122             log.debug("added nested property set: %s",key.c_str());
123         }
124         e=static_cast<DOMElement*>(walker->nextSibling());
125     }
126     walker->release();
127 }
128
129 pair<bool,bool> DOMPropertySet::getBool(const char* name, const char* ns) const
130 {
131     map<string,pair<char*,const XMLCh*> >::const_iterator i;
132
133     if (ns)
134         i=m_map.find(string("{") + ns + '}' + name);
135     else
136         i=m_map.find(name);
137
138     if (i!=m_map.end())
139         return make_pair(true,(!strcmp(i->second.first,"true") || !strcmp(i->second.first,"1")));
140     else if (m_parent)
141         return m_parent->getBool(name,ns);
142     return make_pair(false,false);
143 }
144
145 pair<bool,const char*> DOMPropertySet::getString(const char* name, const char* ns) const
146 {
147     pair<bool,const char*> ret(false,NULL);
148     map<string,pair<char*,const XMLCh*> >::const_iterator i;
149
150     if (ns)
151         i=m_map.find(string("{") + ns + '}' + name);
152     else
153         i=m_map.find(name);
154
155     if (i!=m_map.end())
156         return pair<bool,const char*>(true,i->second.first);
157     else if (m_parent)
158         return m_parent->getString(name,ns);
159     return pair<bool,const char*>(false,NULL);
160 }
161
162 pair<bool,const XMLCh*> DOMPropertySet::getXMLString(const char* name, const char* ns) const
163 {
164     map<string,pair<char*,const XMLCh*> >::const_iterator i;
165
166     if (ns)
167         i=m_map.find(string("{") + ns + '}' + name);
168     else
169         i=m_map.find(name);
170
171     if (i!=m_map.end())
172         return make_pair(true,i->second.second);
173     else if (m_parent)
174         return m_parent->getXMLString(name,ns);
175     return pair<bool,const XMLCh*>(false,NULL);
176 }
177
178 pair<bool,unsigned int> DOMPropertySet::getUnsignedInt(const char* name, const char* ns) const
179 {
180     map<string,pair<char*,const XMLCh*> >::const_iterator i;
181
182     if (ns)
183         i=m_map.find(string("{") + ns + '}' + name);
184     else
185         i=m_map.find(name);
186
187     if (i!=m_map.end())
188         return pair<bool,unsigned int>(true,strtol(i->second.first,NULL,10));
189     else if (m_parent)
190         return m_parent->getUnsignedInt(name,ns);
191     return pair<bool,unsigned int>(false,0);
192 }
193
194 pair<bool,int> DOMPropertySet::getInt(const char* name, const char* ns) const
195 {
196     map<string,pair<char*,const XMLCh*> >::const_iterator i;
197
198     if (ns)
199         i=m_map.find(string("{") + ns + '}' + name);
200     else
201         i=m_map.find(name);
202
203     if (i!=m_map.end())
204         return pair<bool,int>(true,atoi(i->second.first));
205     else if (m_parent)
206         return m_parent->getInt(name,ns);
207     return pair<bool,int>(false,0);
208 }
209
210 void DOMPropertySet::getAll(std::map<std::string,const char*>& properties) const
211 {
212     if (m_parent)
213         m_parent->getAll(properties);
214     for (map< string,pair<char*,const XMLCh*> >::const_iterator i = m_map.begin(); i != m_map.end(); ++i)
215         properties[i->first] = i->second.first;
216 }
217
218 const PropertySet* DOMPropertySet::getPropertySet(const char* name, const char* ns) const
219 {
220     map<string,DOMPropertySet*>::const_iterator i;
221
222     if (ns)
223         i=m_nested.find(string("{") + ns + '}' + name);
224     else
225         i=m_nested.find(name);
226
227     return (i!=m_nested.end()) ? i->second : (m_parent ? m_parent->getPropertySet(name,ns) : NULL);
228 }