2 * Copyright 2001-2006 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(),xmltooling::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();
67 map<string,string>::const_iterator 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();
74 m_map[string("{") + ns.get() + '}' + realname]=pair<char*,const XMLCh*>(val,a->getNodeValue());
75 log.debug("added property {%s}%s (%s)",ns.get(),realname,val);
78 m_map[realname]=pair<char*,const XMLCh*>(val,a->getNodeValue());
79 log.debug("added property %s (%s)",realname,val);
84 // Process non-excluded elements as nested sets.
85 DOMTreeWalker* walker=
86 static_cast<DOMDocumentTraversal*>(
87 m_root->getOwnerDocument())->createTreeWalker(const_cast<DOMElement*>(m_root),DOMNodeFilter::SHOW_ELEMENT,filter,false
89 e=static_cast<DOMElement*>(walker->firstChild());
91 auto_ptr_char ns(e->getNamespaceURI());
92 auto_ptr_char name(e->getLocalName());
93 const char* realname=name.get();
95 map<string,string>::const_iterator remap=remapper->find(realname);
96 if (remap!=remapper->end()) {
97 log.warn("remapping property set (%s) to (%s)",realname,remap->second.c_str());
98 realname=remap->second.c_str();
103 key=string("{") + ns.get() + '}' + realname;
106 if (m_nested.find(key)!=m_nested.end())
107 log.warn("load() skipping duplicate property set: %s",key.c_str());
109 DOMPropertySet* set=new DOMPropertySet();
110 set->load(e,log,filter,remapper);
112 log.debug("added nested property set: %s",key.c_str());
114 e=static_cast<DOMElement*>(walker->nextSibling());
119 pair<bool,bool> DOMPropertySet::getBool(const char* name, const char* ns) const
121 pair<bool,bool> ret(false,false);
122 map<string,pair<char*,const XMLCh*> >::const_iterator i;
125 i=m_map.find(string("{") + ns + '}' + name);
129 if (i!=m_map.end()) {
131 ret.second=(!strcmp(i->second.first,"true") || !strcmp(i->second.first,"1"));
136 pair<bool,const char*> DOMPropertySet::getString(const char* name, const char* ns) const
138 pair<bool,const char*> ret(false,NULL);
139 map<string,pair<char*,const XMLCh*> >::const_iterator i;
142 i=m_map.find(string("{") + ns + '}' + name);
146 if (i!=m_map.end()) {
148 ret.second=i->second.first;
153 pair<bool,const XMLCh*> DOMPropertySet::getXMLString(const char* name, const char* ns) const
155 pair<bool,const XMLCh*> ret(false,NULL);
156 map<string,pair<char*,const XMLCh*> >::const_iterator i;
159 i=m_map.find(string("{") + ns + '}' + name);
163 if (i!=m_map.end()) {
165 ret.second=i->second.second;
170 pair<bool,unsigned int> DOMPropertySet::getUnsignedInt(const char* name, const char* ns) const
172 pair<bool,unsigned int> ret(false,0);
173 map<string,pair<char*,const XMLCh*> >::const_iterator i;
176 i=m_map.find(string("{") + ns + '}' + name);
180 if (i!=m_map.end()) {
182 ret.second=strtol(i->second.first,NULL,10);
187 pair<bool,int> DOMPropertySet::getInt(const char* name, const char* ns) const
189 pair<bool,int> ret(false,0);
190 map<string,pair<char*,const XMLCh*> >::const_iterator i;
193 i=m_map.find(string("{") + ns + '}' + name);
197 if (i!=m_map.end()) {
199 ret.second=atoi(i->second.first);
204 const PropertySet* DOMPropertySet::getPropertySet(const char* name, const char* ns) const
206 map<string,DOMPropertySet*>::const_iterator i;
209 i=m_nested.find(string("{") + ns + '}' + name);
211 i=m_nested.find(name);
213 return (i!=m_nested.end()) ? i->second : NULL;