X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=shibsp%2Futil%2FDOMPropertySet.cpp;h=54a5c3965139438eb27fd4aece9e91a478d89561;hb=c51bfd77603cf0ddb0b5e374c35586a8435895d6;hp=a4096c8e77f1fa4417d654b9f08733619e1f2756;hpb=a4eea4f6d49362d8858dec95345b3d1397d391f8;p=shibboleth%2Fcpp-sp.git diff --git a/shibsp/util/DOMPropertySet.cpp b/shibsp/util/DOMPropertySet.cpp index a4096c8..54a5c39 100644 --- a/shibsp/util/DOMPropertySet.cpp +++ b/shibsp/util/DOMPropertySet.cpp @@ -1,17 +1,21 @@ -/* - * Copyright 2001-2007 Internet2 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at +/** + * Licensed to the University Corporation for Advanced Internet + * Development, Inc. (UCAID) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for + * additional information regarding copyright ownership. + * + * UCAID licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the + * License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the License. */ /** @@ -24,24 +28,52 @@ #include "util/DOMPropertySet.h" #include +#include #include #include using namespace shibsp; using namespace xmltooling; using namespace xercesc; +using namespace boost; using namespace std; +PropertySet::PropertySet() +{ +} + +PropertySet::~PropertySet() +{ +} + +DOMPropertySet::DOMPropertySet() : m_parent(nullptr), m_root(nullptr) +{ +} + DOMPropertySet::~DOMPropertySet() { - for (map >::iterator i=m_map.begin(); i!=m_map.end(); i++) + for (map >::iterator i = m_map.begin(); i != m_map.end(); ++i) XMLString::release(&(i->second.first)); - for_each(m_nested.begin(),m_nested.end(),cleanup_pair()); +} + +const PropertySet* DOMPropertySet::getParent() const +{ + return m_parent; +} + +void DOMPropertySet::setParent(const PropertySet* parent) +{ + m_parent = parent; +} + +const DOMElement* DOMPropertySet::getElement() const +{ + return m_root; } void DOMPropertySet::load( const DOMElement* e, - Category& log, + Category* log, DOMNodeFilter* filter, const std::map* remapper ) @@ -52,6 +84,8 @@ void DOMPropertySet::load( if (!e) return; m_root=e; + if (!log) + log = &Category::getInstance(SHIBSP_LOGCAT ".PropertySet"); // Process each attribute as a property. DOMNamedNodeMap* attrs=m_root->getAttributes(); @@ -68,7 +102,7 @@ void DOMPropertySet::load( if (remapper) { remap=remapper->find(realname); if (remap!=remapper->end()) { - log.warn("remapping property (%s) to (%s)",realname,remap->second.c_str()); + log->warn("deprecation - remapping property (%s) to (%s)",realname,remap->second.c_str()); realname=remap->second.c_str(); } } @@ -77,58 +111,58 @@ void DOMPropertySet::load( m_map[string("{") + remap->second.c_str() + '}' + realname]=pair(val,a->getNodeValue()); else m_map[string("{") + ns.get() + '}' + realname]=pair(val,a->getNodeValue()); - log.debug("added property {%s}%s (%s)",ns.get(),realname,val); + log->debug("added property {%s}%s (%s)",ns.get(),realname,val); } else { m_map[realname]=pair(val,a->getNodeValue()); - log.debug("added property %s (%s)",realname,val); + log->debug("added property %s (%s)",realname,val); } } } // Process non-excluded elements as nested sets. - DOMTreeWalker* walker= + DOMTreeWalker* walker = static_cast( m_root->getOwnerDocument())->createTreeWalker(const_cast(m_root),DOMNodeFilter::SHOW_ELEMENT,filter,false ); - e=static_cast(walker->firstChild()); + e = static_cast(walker->firstChild()); while (e) { auto_ptr_char ns(e->getNamespaceURI()); auto_ptr_char name(e->getLocalName()); const char* realname=name.get(); map::const_iterator remap; if (remapper) { - remap=remapper->find(realname); - if (remap!=remapper->end()) { - log.warn("remapping property set (%s) to (%s)",realname,remap->second.c_str()); - realname=remap->second.c_str(); + remap = remapper->find(realname); + if (remap != remapper->end()) { + log->warn("deprecation - remapping nested property set (%s) to (%s)", realname, remap->second.c_str()); + realname = remap->second.c_str(); } } string key; if (ns.get()) { - if (remapper && (remap=remapper->find(ns.get()))!=remapper->end()) - key=string("{") + remap->second.c_str() + '}' + realname; + if (remapper && (remap = remapper->find(ns.get())) != remapper->end()) + key = string("{") + remap->second.c_str() + '}' + realname; else - key=string("{") + ns.get() + '}' + realname; + key = string("{") + ns.get() + '}' + realname; } else - key=realname; - if (m_nested.find(key)!=m_nested.end()) - log.warn("load() skipping duplicate property set: %s",key.c_str()); + key = realname; + if (m_nested.find(key) != m_nested.end()) + log->warn("load() skipping duplicate property set: %s", key.c_str()); else { - DOMPropertySet* set=new DOMPropertySet(); - set->load(e,log,filter,remapper); - m_nested[key]=set; - log.debug("added nested property set: %s",key.c_str()); + boost::shared_ptr newset(new DOMPropertySet()); + newset->load(e,log,filter,remapper); + m_nested[key] = newset; + log->debug("added nested property set: %s", key.c_str()); } - e=static_cast(walker->nextSibling()); + e = static_cast(walker->nextSibling()); } walker->release(); } pair DOMPropertySet::getBool(const char* name, const char* ns) const { - map >::const_iterator i; + map< string,pair >::const_iterator i; if (ns) i=m_map.find(string("{") + ns + '}' + name); @@ -144,8 +178,8 @@ pair DOMPropertySet::getBool(const char* name, const char* ns) const pair DOMPropertySet::getString(const char* name, const char* ns) const { - pair ret(false,NULL); - map >::const_iterator i; + pair ret(false,nullptr); + map< string,pair >::const_iterator i; if (ns) i=m_map.find(string("{") + ns + '}' + name); @@ -153,15 +187,15 @@ pair DOMPropertySet::getString(const char* name, const char* n i=m_map.find(name); if (i!=m_map.end()) - return make_pair(true,i->second.first); + return pair(true,i->second.first); else if (m_parent) return m_parent->getString(name,ns); - return pair(false,NULL); + return pair(false,nullptr); } pair DOMPropertySet::getXMLString(const char* name, const char* ns) const { - map >::const_iterator i; + map< string,pair >::const_iterator i; if (ns) i=m_map.find(string("{") + ns + '}' + name); @@ -172,20 +206,26 @@ pair DOMPropertySet::getXMLString(const char* name, const cha return make_pair(true,i->second.second); else if (m_parent) return m_parent->getXMLString(name,ns); - return pair(false,NULL); + return pair(false,nullptr); } pair DOMPropertySet::getUnsignedInt(const char* name, const char* ns) const { - map >::const_iterator i; + map< string,pair >::const_iterator i; if (ns) i=m_map.find(string("{") + ns + '}' + name); else i=m_map.find(name); - if (i!=m_map.end()) - return pair(true,strtol(i->second.first,NULL,10)); + if (i!=m_map.end()) { + try { + return pair(true,lexical_cast(i->second.first)); + } + catch (bad_lexical_cast&) { + return pair(false,0); + } + } else if (m_parent) return m_parent->getUnsignedInt(name,ns); return pair(false,0); @@ -193,7 +233,7 @@ pair DOMPropertySet::getUnsignedInt(const char* name, const c pair DOMPropertySet::getInt(const char* name, const char* ns) const { - map >::const_iterator i; + map< string,pair >::const_iterator i; if (ns) i=m_map.find(string("{") + ns + '}' + name); @@ -207,14 +247,40 @@ pair DOMPropertySet::getInt(const char* name, const char* ns) const return pair(false,0); } +void DOMPropertySet::getAll(std::map& properties) const +{ + if (m_parent) + m_parent->getAll(properties); + for (map< string,pair >::const_iterator i = m_map.begin(); i != m_map.end(); ++i) + properties[i->first] = i->second.first; +} + const PropertySet* DOMPropertySet::getPropertySet(const char* name, const char* ns) const { - map::const_iterator i; + map< string,boost::shared_ptr >::const_iterator i; if (ns) - i=m_nested.find(string("{") + ns + '}' + name); + i = m_nested.find(string("{") + ns + '}' + name); else - i=m_nested.find(name); + i = m_nested.find(name); + + return (i != m_nested.end()) ? i->second.get() : (m_parent ? m_parent->getPropertySet(name,ns) : nullptr); +} + +bool DOMPropertySet::setProperty(const char* name, const char* val, const char* ns) +{ + string propname = ns ? (string("{") + ns + "}" + name) : name; + + // Erase existing property. + if (m_map.count(propname) > 0) { + XMLString::release(&m_map[propname].first); + m_map.erase(propname); + } + + char* dup = XMLString::replicate(val); + auto_ptr_XMLCh widedup(val); + m_injected.push_back(widedup.get()); + m_map[propname] = make_pair(dup, m_injected.back().c_str()); - return (i!=m_nested.end()) ? i->second : (m_parent ? m_parent->getPropertySet(name,ns) : NULL); + return true; }