2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
24 * DOM-based property set implementation.
28 #include "util/DOMPropertySet.h"
31 #include <xmltooling/util/NDC.h>
32 #include <xmltooling/util/XMLConstants.h>
34 using namespace shibsp;
35 using namespace xmltooling;
36 using namespace xercesc;
39 PropertySet::PropertySet()
43 PropertySet::~PropertySet()
47 DOMPropertySet::DOMPropertySet() : m_parent(nullptr), m_root(nullptr)
51 DOMPropertySet::~DOMPropertySet()
53 for (map<string,pair<char*,const XMLCh*> >::iterator i = m_map.begin(); i != m_map.end(); ++i)
54 XMLString::release(&(i->second.first));
57 const PropertySet* DOMPropertySet::getParent() const
62 void DOMPropertySet::setParent(const PropertySet* parent)
67 const DOMElement* DOMPropertySet::getElement() const
72 void DOMPropertySet::load(
75 DOMNodeFilter* filter,
76 const std::map<std::string,std::string>* remapper
86 log = &Category::getInstance(SHIBSP_LOGCAT".PropertySet");
88 // Process each attribute as a property.
89 DOMNamedNodeMap* attrs=m_root->getAttributes();
90 for (XMLSize_t i=0; i<attrs->getLength(); i++) {
91 DOMNode* a=attrs->item(i);
92 if (!XMLString::compareString(a->getNamespaceURI(),xmlconstants::XMLNS_NS))
94 char* val=XMLString::transcode(a->getNodeValue());
96 auto_ptr_char ns(a->getNamespaceURI());
97 auto_ptr_char name(a->getLocalName());
98 const char* realname=name.get();
99 map<string,string>::const_iterator remap;
101 remap=remapper->find(realname);
102 if (remap!=remapper->end()) {
103 log->warn("deprecation - remapping property (%s) to (%s)",realname,remap->second.c_str());
104 realname=remap->second.c_str();
108 if (remapper && (remap=remapper->find(ns.get()))!=remapper->end())
109 m_map[string("{") + remap->second.c_str() + '}' + realname]=pair<char*,const XMLCh*>(val,a->getNodeValue());
111 m_map[string("{") + ns.get() + '}' + realname]=pair<char*,const XMLCh*>(val,a->getNodeValue());
112 log->debug("added property {%s}%s (%s)",ns.get(),realname,val);
115 m_map[realname]=pair<char*,const XMLCh*>(val,a->getNodeValue());
116 log->debug("added property %s (%s)",realname,val);
121 // Process non-excluded elements as nested sets.
122 DOMTreeWalker* walker =
123 static_cast<DOMDocumentTraversal*>(
124 m_root->getOwnerDocument())->createTreeWalker(const_cast<DOMElement*>(m_root),DOMNodeFilter::SHOW_ELEMENT,filter,false
126 e = static_cast<DOMElement*>(walker->firstChild());
128 auto_ptr_char ns(e->getNamespaceURI());
129 auto_ptr_char name(e->getLocalName());
130 const char* realname=name.get();
131 map<string,string>::const_iterator remap;
133 remap = remapper->find(realname);
134 if (remap != remapper->end()) {
135 log->warn("deprecation - remapping nested property set (%s) to (%s)", realname, remap->second.c_str());
136 realname = remap->second.c_str();
141 if (remapper && (remap = remapper->find(ns.get())) != remapper->end())
142 key = string("{") + remap->second.c_str() + '}' + realname;
144 key = string("{") + ns.get() + '}' + realname;
148 if (m_nested.find(key) != m_nested.end())
149 log->warn("load() skipping duplicate property set: %s", key.c_str());
151 boost::shared_ptr<DOMPropertySet> newset(new DOMPropertySet());
152 newset->load(e,log,filter,remapper);
153 m_nested[key] = newset;
154 log->debug("added nested property set: %s", key.c_str());
156 e = static_cast<DOMElement*>(walker->nextSibling());
161 pair<bool,bool> DOMPropertySet::getBool(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,(!strcmp(i->second.first,"true") || !strcmp(i->second.first,"1")));
173 return m_parent->getBool(name,ns);
174 return make_pair(false,false);
177 pair<bool,const char*> DOMPropertySet::getString(const char* name, const char* ns) const
179 pair<bool,const char*> ret(false,nullptr);
180 map<string,pair<char*,const XMLCh*> >::const_iterator i;
183 i=m_map.find(string("{") + ns + '}' + name);
188 return pair<bool,const char*>(true,i->second.first);
190 return m_parent->getString(name,ns);
191 return pair<bool,const char*>(false,nullptr);
194 pair<bool,const XMLCh*> DOMPropertySet::getXMLString(const char* name, const char* ns) const
196 map<string,pair<char*,const XMLCh*> >::const_iterator i;
199 i=m_map.find(string("{") + ns + '}' + name);
204 return make_pair(true,i->second.second);
206 return m_parent->getXMLString(name,ns);
207 return pair<bool,const XMLCh*>(false,nullptr);
210 pair<bool,unsigned int> DOMPropertySet::getUnsignedInt(const char* name, const char* ns) const
212 map<string,pair<char*,const XMLCh*> >::const_iterator i;
215 i=m_map.find(string("{") + ns + '}' + name);
220 return pair<bool,unsigned int>(true,strtol(i->second.first,nullptr,10));
222 return m_parent->getUnsignedInt(name,ns);
223 return pair<bool,unsigned int>(false,0);
226 pair<bool,int> DOMPropertySet::getInt(const char* name, const char* ns) const
228 map<string,pair<char*,const XMLCh*> >::const_iterator i;
231 i=m_map.find(string("{") + ns + '}' + name);
236 return pair<bool,int>(true,atoi(i->second.first));
238 return m_parent->getInt(name,ns);
239 return pair<bool,int>(false,0);
242 void DOMPropertySet::getAll(std::map<std::string,const char*>& properties) const
245 m_parent->getAll(properties);
246 for (map< string,pair<char*,const XMLCh*> >::const_iterator i = m_map.begin(); i != m_map.end(); ++i)
247 properties[i->first] = i->second.first;
250 const PropertySet* DOMPropertySet::getPropertySet(const char* name, const char* ns) const
252 map<string,boost::shared_ptr<DOMPropertySet>>::const_iterator i;
255 i = m_nested.find(string("{") + ns + '}' + name);
257 i = m_nested.find(name);
259 return (i != m_nested.end()) ? i->second.get() : (m_parent ? m_parent->getPropertySet(name,ns) : nullptr);
262 bool DOMPropertySet::setProperty(const char* name, const char* val, const char* ns)
264 string propname = ns ? (string("{") + ns + "}" + name) : name;
266 // Erase existing property.
267 if (m_map.count(propname) > 0) {
268 XMLString::release(&m_map[propname].first);
269 m_map.erase(propname);
272 char* dup = XMLString::replicate(val);
273 auto_ptr_XMLCh widedup(val);
274 m_injected.push_back(widedup.get());
275 m_map[propname] = make_pair(dup, m_injected.back().c_str());