2 * Copyright 2001-2007 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * DOM-based property set implementation.
24 #include "util/DOMPropertySet.h"
27 #include <xmltooling/util/NDC.h>
28 #include <xmltooling/util/XMLConstants.h>
30 using namespace shibsp;
31 using namespace xmltooling;
32 using namespace log4cpp;
33 using namespace xercesc;
36 DOMPropertySet::~DOMPropertySet()
38 for (map<string,pair<char*,const XMLCh*> >::iterator i=m_map.begin(); i!=m_map.end(); i++)
39 XMLString::release(&(i->second.first));
40 for_each(m_nested.begin(),m_nested.end(),cleanup_pair<string,DOMPropertySet>());
43 void DOMPropertySet::load(
46 DOMNodeFilter* filter,
47 const std::map<std::string,std::string>* remapper
55 // Process each attribute as a property.
56 DOMNamedNodeMap* attrs=m_root->getAttributes();
57 for (XMLSize_t i=0; i<attrs->getLength(); i++) {
58 DOMNode* a=attrs->item(i);
59 if (!XMLString::compareString(a->getNamespaceURI(),xmlconstants::XMLNS_NS))
61 char* val=XMLString::transcode(a->getNodeValue());
63 auto_ptr_char ns(a->getNamespaceURI());
64 auto_ptr_char name(a->getLocalName());
65 const char* realname=name.get();
66 map<string,string>::const_iterator remap;
68 remap=remapper->find(realname);
69 if (remap!=remapper->end()) {
70 log.warn("remapping property (%s) to (%s)",realname,remap->second.c_str());
71 realname=remap->second.c_str();
75 if (remapper && (remap=remapper->find(ns.get()))!=remapper->end())
76 m_map[string("{") + remap->second.c_str() + '}' + realname]=pair<char*,const XMLCh*>(val,a->getNodeValue());
78 m_map[string("{") + ns.get() + '}' + realname]=pair<char*,const XMLCh*>(val,a->getNodeValue());
79 log.debug("added property {%s}%s (%s)",ns.get(),realname,val);
82 m_map[realname]=pair<char*,const XMLCh*>(val,a->getNodeValue());
83 log.debug("added property %s (%s)",realname,val);
88 // Process non-excluded elements as nested sets.
89 DOMTreeWalker* walker=
90 static_cast<DOMDocumentTraversal*>(
91 m_root->getOwnerDocument())->createTreeWalker(const_cast<DOMElement*>(m_root),DOMNodeFilter::SHOW_ELEMENT,filter,false
93 e=static_cast<DOMElement*>(walker->firstChild());
95 auto_ptr_char ns(e->getNamespaceURI());
96 auto_ptr_char name(e->getLocalName());
97 const char* realname=name.get();
98 map<string,string>::const_iterator remap;
100 remap=remapper->find(realname);
101 if (remap!=remapper->end()) {
102 log.warn("remapping property set (%s) to (%s)",realname,remap->second.c_str());
103 realname=remap->second.c_str();
108 if (remapper && (remap=remapper->find(ns.get()))!=remapper->end())
109 key=string("{") + remap->second.c_str() + '}' + realname;
111 key=string("{") + ns.get() + '}' + realname;
115 if (m_nested.find(key)!=m_nested.end())
116 log.warn("load() skipping duplicate property set: %s",key.c_str());
118 DOMPropertySet* set=new DOMPropertySet();
119 set->load(e,log,filter,remapper);
121 log.debug("added nested property set: %s",key.c_str());
123 e=static_cast<DOMElement*>(walker->nextSibling());
128 pair<bool,bool> DOMPropertySet::getBool(const char* name, const char* ns) const
130 map<string,pair<char*,const XMLCh*> >::const_iterator i;
133 i=m_map.find(string("{") + ns + '}' + name);
138 return make_pair(true,(!strcmp(i->second.first,"true") || !strcmp(i->second.first,"1")));
140 return m_parent->getBool(name,ns);
141 return make_pair(false,false);
144 pair<bool,const char*> DOMPropertySet::getString(const char* name, const char* ns) const
146 pair<bool,const char*> ret(false,NULL);
147 map<string,pair<char*,const XMLCh*> >::const_iterator i;
150 i=m_map.find(string("{") + ns + '}' + name);
155 return make_pair(true,i->second.first);
157 return m_parent->getString(name,ns);
158 return pair<bool,const char*>(false,NULL);
161 pair<bool,const XMLCh*> DOMPropertySet::getXMLString(const char* name, const char* ns) const
163 map<string,pair<char*,const XMLCh*> >::const_iterator i;
166 i=m_map.find(string("{") + ns + '}' + name);
171 return make_pair(true,i->second.second);
173 return m_parent->getXMLString(name,ns);
174 return pair<bool,const XMLCh*>(false,NULL);
177 pair<bool,unsigned int> DOMPropertySet::getUnsignedInt(const char* name, const char* ns) const
179 map<string,pair<char*,const XMLCh*> >::const_iterator i;
182 i=m_map.find(string("{") + ns + '}' + name);
187 return pair<bool,unsigned int>(true,strtol(i->second.first,NULL,10));
189 return m_parent->getUnsignedInt(name,ns);
190 return pair<bool,unsigned int>(false,0);
193 pair<bool,int> DOMPropertySet::getInt(const char* name, const char* ns) const
195 map<string,pair<char*,const XMLCh*> >::const_iterator i;
198 i=m_map.find(string("{") + ns + '}' + name);
203 return pair<bool,int>(true,atoi(i->second.first));
205 return m_parent->getInt(name,ns);
206 return pair<bool,int>(false,0);
209 const PropertySet* DOMPropertySet::getPropertySet(const char* name, const char* ns) const
211 map<string,DOMPropertySet*>::const_iterator i;
214 i=m_nested.find(string("{") + ns + '}' + name);
216 i=m_nested.find(name);
218 return (i!=m_nested.end()) ? i->second : (m_parent ? m_parent->getPropertySet(name,ns) : NULL);