Convert logging to log4shib via compile time switch.
[shibboleth/cpp-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     m_root=e;
53
54     // Process each attribute as a property.
55     DOMNamedNodeMap* attrs=m_root->getAttributes();
56     for (XMLSize_t i=0; i<attrs->getLength(); i++) {
57         DOMNode* a=attrs->item(i);
58         if (!XMLString::compareString(a->getNamespaceURI(),xmlconstants::XMLNS_NS))
59             continue;
60         char* val=XMLString::transcode(a->getNodeValue());
61         if (val && *val) {
62             auto_ptr_char ns(a->getNamespaceURI());
63             auto_ptr_char name(a->getLocalName());
64             const char* realname=name.get();
65             map<string,string>::const_iterator remap;
66             if (remapper) {
67                 remap=remapper->find(realname);
68                 if (remap!=remapper->end()) {
69                     log.warn("remapping property (%s) to (%s)",realname,remap->second.c_str());
70                     realname=remap->second.c_str();
71                 }
72             }
73             if (ns.get()) {
74                 if (remapper && (remap=remapper->find(ns.get()))!=remapper->end())
75                     m_map[string("{") + remap->second.c_str() + '}' + realname]=pair<char*,const XMLCh*>(val,a->getNodeValue());
76                 else
77                     m_map[string("{") + ns.get() + '}' + realname]=pair<char*,const XMLCh*>(val,a->getNodeValue());
78                 log.debug("added property {%s}%s (%s)",ns.get(),realname,val);
79             }
80             else {
81                 m_map[realname]=pair<char*,const XMLCh*>(val,a->getNodeValue());
82                 log.debug("added property %s (%s)",realname,val);
83             }
84         }
85     }
86     
87     // Process non-excluded elements as nested sets.
88     DOMTreeWalker* walker=
89         static_cast<DOMDocumentTraversal*>(
90             m_root->getOwnerDocument())->createTreeWalker(const_cast<DOMElement*>(m_root),DOMNodeFilter::SHOW_ELEMENT,filter,false
91             );
92     e=static_cast<DOMElement*>(walker->firstChild());
93     while (e) {
94         auto_ptr_char ns(e->getNamespaceURI());
95         auto_ptr_char name(e->getLocalName());
96         const char* realname=name.get();
97         map<string,string>::const_iterator remap;
98         if (remapper) {
99             remap=remapper->find(realname);
100             if (remap!=remapper->end()) {
101                 log.warn("remapping property set (%s) to (%s)",realname,remap->second.c_str());
102                 realname=remap->second.c_str();
103             }
104         }
105         string key;
106         if (ns.get()) {
107             if (remapper && (remap=remapper->find(ns.get()))!=remapper->end())
108                 key=string("{") + remap->second.c_str() + '}' + realname;
109             else
110                 key=string("{") + ns.get() + '}' + realname;
111         }
112         else
113             key=realname;
114         if (m_nested.find(key)!=m_nested.end())
115             log.warn("load() skipping duplicate property set: %s",key.c_str());
116         else {
117             DOMPropertySet* set=new DOMPropertySet();
118             set->load(e,log,filter,remapper);
119             m_nested[key]=set;
120             log.debug("added nested property set: %s",key.c_str());
121         }
122         e=static_cast<DOMElement*>(walker->nextSibling());
123     }
124     walker->release();
125 }
126
127 pair<bool,bool> DOMPropertySet::getBool(const char* name, const char* ns) const
128 {
129     map<string,pair<char*,const XMLCh*> >::const_iterator i;
130
131     if (ns)
132         i=m_map.find(string("{") + ns + '}' + name);
133     else
134         i=m_map.find(name);
135
136     if (i!=m_map.end())
137         return make_pair(true,(!strcmp(i->second.first,"true") || !strcmp(i->second.first,"1")));
138     else if (m_parent)
139         return m_parent->getBool(name,ns);
140     return make_pair(false,false);
141 }
142
143 pair<bool,const char*> DOMPropertySet::getString(const char* name, const char* ns) const
144 {
145     pair<bool,const char*> ret(false,NULL);
146     map<string,pair<char*,const XMLCh*> >::const_iterator i;
147
148     if (ns)
149         i=m_map.find(string("{") + ns + '}' + name);
150     else
151         i=m_map.find(name);
152
153     if (i!=m_map.end())
154         return make_pair(true,i->second.first);
155     else if (m_parent)
156         return m_parent->getString(name,ns);
157     return pair<bool,const char*>(false,NULL);
158 }
159
160 pair<bool,const XMLCh*> DOMPropertySet::getXMLString(const char* name, const char* ns) const
161 {
162     map<string,pair<char*,const XMLCh*> >::const_iterator i;
163
164     if (ns)
165         i=m_map.find(string("{") + ns + '}' + name);
166     else
167         i=m_map.find(name);
168
169     if (i!=m_map.end())
170         return make_pair(true,i->second.second);
171     else if (m_parent)
172         return m_parent->getXMLString(name,ns);
173     return pair<bool,const XMLCh*>(false,NULL);
174 }
175
176 pair<bool,unsigned int> DOMPropertySet::getUnsignedInt(const char* name, const char* ns) const
177 {
178     map<string,pair<char*,const XMLCh*> >::const_iterator i;
179
180     if (ns)
181         i=m_map.find(string("{") + ns + '}' + name);
182     else
183         i=m_map.find(name);
184
185     if (i!=m_map.end())
186         return pair<bool,unsigned int>(true,strtol(i->second.first,NULL,10));
187     else if (m_parent)
188         return m_parent->getUnsignedInt(name,ns);
189     return pair<bool,unsigned int>(false,0);
190 }
191
192 pair<bool,int> DOMPropertySet::getInt(const char* name, const char* ns) const
193 {
194     map<string,pair<char*,const XMLCh*> >::const_iterator i;
195
196     if (ns)
197         i=m_map.find(string("{") + ns + '}' + name);
198     else
199         i=m_map.find(name);
200
201     if (i!=m_map.end())
202         return pair<bool,int>(true,atoi(i->second.first));
203     else if (m_parent)
204         return m_parent->getInt(name,ns);
205     return pair<bool,int>(false,0);
206 }
207
208 const PropertySet* DOMPropertySet::getPropertySet(const char* name, const char* ns) const
209 {
210     map<string,DOMPropertySet*>::const_iterator i;
211
212     if (ns)
213         i=m_nested.find(string("{") + ns + '}' + name);
214     else
215         i=m_nested.find(name);
216
217     return (i!=m_nested.end()) ? i->second : (m_parent ? m_parent->getPropertySet(name,ns) : NULL);
218 }