From e93ce2c962c535ff88f26427e9aeb572f83976e1 Mon Sep 17 00:00:00 2001 From: Scott Cantor Date: Thu, 8 Dec 2011 05:20:11 +0000 Subject: [PATCH] More boostisms --- .../AbstractAttributeExtensibleXMLObject.cpp | 68 ++++++++++++---------- xmltooling/AbstractComplexElement.cpp | 30 +++++----- xmltooling/AbstractXMLObject.cpp | 12 ++-- xmltooling/AbstractXMLObject.h | 2 +- xmltooling/security/impl/InlineKeyResolver.cpp | 52 ++++++++++------- xmltooling/util/XMLHelper.cpp | 22 ++++--- 6 files changed, 105 insertions(+), 81 deletions(-) diff --git a/xmltooling/AbstractAttributeExtensibleXMLObject.cpp b/xmltooling/AbstractAttributeExtensibleXMLObject.cpp index c144fa9..1f819f6 100644 --- a/xmltooling/AbstractAttributeExtensibleXMLObject.cpp +++ b/xmltooling/AbstractAttributeExtensibleXMLObject.cpp @@ -31,15 +31,16 @@ #include #include +#include +#include #include using namespace xmltooling; +using namespace xercesc; +using namespace boost::lambda; +using namespace boost; using namespace std; -using xercesc::chColon; -using xercesc::DOMAttr; -using xercesc::DOMElement; -using xercesc::XMLString; ElementExtensibleXMLObject::ElementExtensibleXMLObject() { @@ -57,7 +58,7 @@ ElementProxy::~ElementProxy() { } -set AttributeExtensibleXMLObject::m_idAttributeSet; +set AttributeExtensibleXMLObject::m_idAttributeSet; AttributeExtensibleXMLObject::AttributeExtensibleXMLObject() { @@ -67,22 +68,22 @@ AttributeExtensibleXMLObject::~AttributeExtensibleXMLObject() { } -const set& AttributeExtensibleXMLObject::getRegisteredIDAttributes() +const set& AttributeExtensibleXMLObject::getRegisteredIDAttributes() { return m_idAttributeSet; } -bool AttributeExtensibleXMLObject::isRegisteredIDAttribute(const QName& name) +bool AttributeExtensibleXMLObject::isRegisteredIDAttribute(const xmltooling::QName& name) { return m_idAttributeSet.find(name)!=m_idAttributeSet.end(); } -void AttributeExtensibleXMLObject::registerIDAttribute(const QName& name) +void AttributeExtensibleXMLObject::registerIDAttribute(const xmltooling::QName& name) { m_idAttributeSet.insert(name); } -void AttributeExtensibleXMLObject::deregisterIDAttribute(const QName& name) +void AttributeExtensibleXMLObject::deregisterIDAttribute(const xmltooling::QName& name) { m_idAttributeSet.erase(name); } @@ -101,7 +102,7 @@ AbstractAttributeExtensibleXMLObject::AbstractAttributeExtensibleXMLObject(const : AbstractXMLObject(src) { m_idAttribute = m_attributeMap.end(); - for (map::const_iterator i=src.m_attributeMap.begin(); i!=src.m_attributeMap.end(); i++) { + for (map::const_iterator i = src.m_attributeMap.begin(); i != src.m_attributeMap.end(); ++i) { m_attributeMap[i->first] = XMLString::replicate(i->second); } if (src.m_idAttribute != src.m_attributeMap.end()) { @@ -111,36 +112,43 @@ AbstractAttributeExtensibleXMLObject::AbstractAttributeExtensibleXMLObject(const AbstractAttributeExtensibleXMLObject::~AbstractAttributeExtensibleXMLObject() { - for (map::iterator i=m_attributeMap.begin(); i!=m_attributeMap.end(); i++) - XMLString::release(&(i->second)); + static void (*release)(XMLCh**,MemoryManager*) = &XMLString::release; + for_each( + m_attributeMap.begin(), m_attributeMap.end(), + lambda::bind( + release, + &lambda::bind(&map::value_type::second, boost::ref(_1)), + XMLPlatformUtils::fgMemoryManager + ) + ); } -const XMLCh* AbstractAttributeExtensibleXMLObject::getAttribute(const QName& qualifiedName) const +const XMLCh* AbstractAttributeExtensibleXMLObject::getAttribute(const xmltooling::QName& qualifiedName) const { - map::const_iterator i=m_attributeMap.find(qualifiedName); - return (i==m_attributeMap.end()) ? nullptr : i->second; + map::const_iterator i = m_attributeMap.find(qualifiedName); + return (i != m_attributeMap.end()) ? i->second : nullptr; } -void AbstractAttributeExtensibleXMLObject::setAttribute(const QName& qualifiedName, const XMLCh* value, bool ID) +void AbstractAttributeExtensibleXMLObject::setAttribute(const xmltooling::QName& qualifiedName, const XMLCh* value, bool ID) { - map::iterator i=m_attributeMap.find(qualifiedName); - if (i!=m_attributeMap.end()) { + map::iterator i=m_attributeMap.find(qualifiedName); + if (i != m_attributeMap.end()) { releaseThisandParentDOM(); XMLString::release(&(i->second)); if (value && *value) { - i->second=XMLString::replicate(value); + i->second = XMLString::replicate(value); if (ID) - m_idAttribute=i; + m_idAttribute = i; } else { - if (m_idAttribute==i) - m_idAttribute=m_attributeMap.end(); + if (m_idAttribute == i) + m_idAttribute = m_attributeMap.end(); m_attributeMap.erase(i); } } else if (value && *value) { releaseThisandParentDOM(); - m_attributeMap[qualifiedName]=XMLString::replicate(value); + m_attributeMap[qualifiedName] = XMLString::replicate(value); if (ID) m_idAttribute = m_attributeMap.find(qualifiedName); Namespace newNamespace(qualifiedName.getNamespaceURI(), qualifiedName.getPrefix(), false, Namespace::VisiblyUsed); @@ -148,7 +156,7 @@ void AbstractAttributeExtensibleXMLObject::setAttribute(const QName& qualifiedNa } } -void AttributeExtensibleXMLObject::setAttribute(const QName& qualifiedName, const QName& value) +void AttributeExtensibleXMLObject::setAttribute(const xmltooling::QName& qualifiedName, const xmltooling::QName& value) { if (!value.hasLocalPart()) return; @@ -166,18 +174,18 @@ void AttributeExtensibleXMLObject::setAttribute(const QName& qualifiedName, cons addNamespace(newNamespace); } -const map& AbstractAttributeExtensibleXMLObject::getExtensionAttributes() const +const map& AbstractAttributeExtensibleXMLObject::getExtensionAttributes() const { return m_attributeMap; } const XMLCh* AbstractAttributeExtensibleXMLObject::getXMLID() const { - return (m_idAttribute == m_attributeMap.end()) ? nullptr : m_idAttribute->second; + return (m_idAttribute != m_attributeMap.end()) ? m_idAttribute->second : nullptr; } void AbstractAttributeExtensibleXMLObject::unmarshallExtensionAttribute(const DOMAttr* attribute) { - QName q(attribute->getNamespaceURI(),attribute->getLocalName(),attribute->getPrefix()); + xmltooling::QName q(attribute->getNamespaceURI(), attribute->getLocalName(), attribute->getPrefix()); bool ID = attribute->isId() || isRegisteredIDAttribute(q); setAttribute(q,attribute->getNodeValue(),ID); if (ID) { @@ -191,13 +199,13 @@ void AbstractAttributeExtensibleXMLObject::unmarshallExtensionAttribute(const DO void AbstractAttributeExtensibleXMLObject::marshallExtensionAttributes(DOMElement* domElement) const { - for (map::const_iterator i=m_attributeMap.begin(); i!=m_attributeMap.end(); i++) { - DOMAttr* attr=domElement->getOwnerDocument()->createAttributeNS(i->first.getNamespaceURI(),i->first.getLocalPart()); + for (map::const_iterator i = m_attributeMap.begin(); i != m_attributeMap.end(); ++i) { + DOMAttr* attr = domElement->getOwnerDocument()->createAttributeNS(i->first.getNamespaceURI(), i->first.getLocalPart()); if (i->first.hasPrefix()) attr->setPrefix(i->first.getPrefix()); attr->setNodeValue(i->second); domElement->setAttributeNodeNS(attr); - if (m_idAttribute==i) { + if (m_idAttribute == i) { #ifdef XMLTOOLING_XERCESC_BOOLSETIDATTRIBUTE domElement->setIdAttributeNode(attr, true); #else diff --git a/xmltooling/AbstractComplexElement.cpp b/xmltooling/AbstractComplexElement.cpp index 7448602..fa1e6ef 100644 --- a/xmltooling/AbstractComplexElement.cpp +++ b/xmltooling/AbstractComplexElement.cpp @@ -28,17 +28,15 @@ #include "AbstractComplexElement.h" #include +#include +#include using namespace xmltooling; +using namespace xercesc; +using namespace boost::lambda; +using namespace boost; using namespace std; -using xercesc::XMLString; - -namespace { - bool _nonnull(const XMLObject* ptr) { - return (ptr!=nullptr); - } -} AbstractComplexElement::AbstractComplexElement() { @@ -46,21 +44,27 @@ AbstractComplexElement::AbstractComplexElement() AbstractComplexElement::AbstractComplexElement(const AbstractComplexElement& src) { - for (vector::const_iterator i=src.m_text.begin(); i!=src.m_text.end(); ++i) - m_text.push_back(XMLString::replicate(*i)); + static void (vector::* push_back)(XMLCh* const&) = &vector::push_back; + static XMLCh* (*replicate)(const XMLCh*,MemoryManager*) = &XMLString::replicate; + + for_each( + src.m_text.begin(), src.m_text.end(), + lambda::bind(push_back, boost::ref(m_text), lambda::bind(replicate, _1, XMLPlatformUtils::fgMemoryManager)) + ); } AbstractComplexElement::~AbstractComplexElement() { + static void (*release)(XMLCh**,MemoryManager*) = &XMLString::release; + for_each(m_children.begin(), m_children.end(), cleanup()); - for (vector::iterator i=m_text.begin(); i!=m_text.end(); ++i) - XMLString::release(&(*i)); + for_each(m_text.begin(), m_text.end(), lambda::bind(release, &_1, XMLPlatformUtils::fgMemoryManager)); } bool AbstractComplexElement::hasChildren() const { if (m_children.empty()) return false; - return (find_if(m_children.begin(), m_children.end(), _nonnull) != m_children.end()); + return (find_if(m_children.begin(), m_children.end(), (_1 != nullptr)) != m_children.end()); } const list& AbstractComplexElement::getOrderedChildren() const @@ -87,5 +91,5 @@ void AbstractComplexElement::setTextContent(const XMLCh* value, unsigned int pos m_text.push_back(nullptr); ++size; } - m_text[position]=prepareForAssignment(m_text[position],value); + m_text[position] = prepareForAssignment(m_text[position], value); } diff --git a/xmltooling/AbstractXMLObject.cpp b/xmltooling/AbstractXMLObject.cpp index 33f6cd5..cb2d7f4 100644 --- a/xmltooling/AbstractXMLObject.cpp +++ b/xmltooling/AbstractXMLObject.cpp @@ -59,11 +59,11 @@ void XMLObject::releaseThisAndChildrenDOM() const AbstractXMLObject::AbstractXMLObject(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType) : m_log(logging::Category::getInstance(XMLTOOLING_LOGCAT".XMLObject")), m_schemaLocation(nullptr), m_noNamespaceSchemaLocation(nullptr), m_nil(xmlconstants::XML_BOOL_NULL), - m_parent(nullptr), m_elementQname(nsURI, localName, prefix), m_typeQname(nullptr) + m_parent(nullptr), m_elementQname(nsURI, localName, prefix) { addNamespace(Namespace(nsURI, prefix, false, Namespace::VisiblyUsed)); if (schemaType) { - m_typeQname = new QName(*schemaType); + m_typeQname.reset(new QName(*schemaType)); addNamespace(Namespace(m_typeQname->getNamespaceURI(), m_typeQname->getPrefix(), false, Namespace::NonVisiblyUsed)); } } @@ -71,15 +71,13 @@ AbstractXMLObject::AbstractXMLObject(const XMLCh* nsURI, const XMLCh* localName, AbstractXMLObject::AbstractXMLObject(const AbstractXMLObject& src) : m_namespaces(src.m_namespaces), m_log(src.m_log), m_schemaLocation(XMLString::replicate(src.m_schemaLocation)), m_noNamespaceSchemaLocation(XMLString::replicate(src.m_noNamespaceSchemaLocation)), m_nil(src.m_nil), - m_parent(nullptr), m_elementQname(src.m_elementQname), m_typeQname(nullptr) + m_parent(nullptr), m_elementQname(src.m_elementQname), + m_typeQname(src.m_typeQname.get() ? new QName(*src.m_typeQname) : nullptr) { - if (src.m_typeQname) - m_typeQname=new QName(*src.m_typeQname); } AbstractXMLObject::~AbstractXMLObject() { - delete m_typeQname; xercesc::XMLString::release(&m_schemaLocation); xercesc::XMLString::release(&m_noNamespaceSchemaLocation); } @@ -168,7 +166,7 @@ void AbstractXMLObject::removeNamespace(const Namespace& ns) const QName* AbstractXMLObject::getSchemaType() const { - return m_typeQname; + return m_typeQname.get(); } const XMLCh* AbstractXMLObject::getXMLID() const diff --git a/xmltooling/AbstractXMLObject.h b/xmltooling/AbstractXMLObject.h index 946b167..49a0bbf 100644 --- a/xmltooling/AbstractXMLObject.h +++ b/xmltooling/AbstractXMLObject.h @@ -190,7 +190,7 @@ namespace xmltooling { private: XMLObject* m_parent; QName m_elementQname; - QName* m_typeQname; + std::auto_ptr m_typeQname; }; }; diff --git a/xmltooling/security/impl/InlineKeyResolver.cpp b/xmltooling/security/impl/InlineKeyResolver.cpp index 1fab308..0246cb8 100644 --- a/xmltooling/security/impl/InlineKeyResolver.cpp +++ b/xmltooling/security/impl/InlineKeyResolver.cpp @@ -38,6 +38,7 @@ #include "util/XMLConstants.h" #include "validation/ValidatorSuite.h" +#include #include #include #include @@ -54,6 +55,7 @@ using namespace xmlsignature; using namespace xmltooling::logging; using namespace xmltooling; using namespace xercesc; +using namespace boost; using namespace std; namespace xmltooling { @@ -203,10 +205,11 @@ void InlineCredential::resolve(const KeyInfo* keyInfo, int types, bool followRef const XMLCh* n; char* kn; const vector& knames=keyInfo->getKeyNames(); - for (vector::const_iterator kn_i=knames.begin(); kn_i!=knames.end(); ++kn_i) { - n=(*kn_i)->getName(); + for (indirect_iterator::const_iterator> kn_i = make_indirect_iterator(knames.begin()); + kn_i != make_indirect_iterator(knames.end()); ++kn_i) { + n = kn_i->getName(); if (n && *n) { - kn=toUTF8(n); + kn = toUTF8(n); m_keyNames.insert(kn); delete[] kn; } @@ -249,10 +252,11 @@ bool InlineCredential::resolveKey(const KeyInfo* keyInfo, bool followRefs) // Check for ds:KeyValue const vector& keyValues = keyInfo->getKeyValues(); - for (vector::const_iterator i = keyValues.begin(); i != keyValues.end(); ++i) { + for (indirect_iterator::const_iterator> i = make_indirect_iterator(keyValues.begin()); + i != make_indirect_iterator(keyValues.end()); ++i) { try { - SchemaValidators.validate(*i); // see if it's a "valid" key - RSAKeyValue* rsakv = (*i)->getRSAKeyValue(); + SchemaValidators.validate(*(i.base())); // see if it's a "valid" key + RSAKeyValue* rsakv = i->getRSAKeyValue(); if (rsakv) { log.debug("resolving ds:RSAKeyValue"); auto_ptr_char mod(rsakv->getModulus()->getValue()); @@ -263,7 +267,7 @@ bool InlineCredential::resolveKey(const KeyInfo* keyInfo, bool followRefs) m_key = rsa.release(); return true; } - DSAKeyValue* dsakv = (*i)->getDSAKeyValue(); + DSAKeyValue* dsakv = i->getDSAKeyValue(); if (dsakv) { log.debug("resolving ds:DSAKeyValue"); auto_ptr dsa(XSECPlatformUtils::g_cryptoProvider->keyDSA()); @@ -285,7 +289,7 @@ bool InlineCredential::resolveKey(const KeyInfo* keyInfo, bool followRefs) return true; } #ifdef XMLTOOLING_XMLSEC_ECC - ECKeyValue* eckv = (*i)->getECKeyValue(); + ECKeyValue* eckv = i->getECKeyValue(); if (eckv && eckv->getNamedCurve() && eckv->getPublicKey()) { log.warn("resolving ds11:ECKeyValue"); auto_ptr ec(XSECPlatformUtils::g_cryptoProvider->keyEC()); @@ -313,9 +317,10 @@ bool InlineCredential::resolveKey(const KeyInfo* keyInfo, bool followRefs) // Check for ds11:DEREncodedKeyValue const vector& derValues = keyInfo->getDEREncodedKeyValues(); - for (vector::const_iterator j = derValues.begin(); j != derValues.end(); ++j) { + for (indirect_iterator::const_iterator> j = make_indirect_iterator(derValues.begin()); + j != make_indirect_iterator(derValues.end()); ++j) { log.debug("resolving ds11:DEREncodedKeyValue"); - m_key = SecurityHelper::fromDEREncoding((*j)->getValue()); + m_key = SecurityHelper::fromDEREncoding(j->getValue()); if (m_key) return true; log.warn("failed to resolve ds11:DEREncodedKeyValue"); @@ -327,8 +332,9 @@ bool InlineCredential::resolveKey(const KeyInfo* keyInfo, bool followRefs) const XMLCh* fragID=nullptr; const XMLObject* treeRoot=nullptr; const vector& refs = keyInfo->getKeyInfoReferences(); - for (vector::const_iterator ref = refs.begin(); ref != refs.end(); ++ref) { - fragID = (*ref)->getURI(); + for (indirect_iterator::const_iterator> ref = make_indirect_iterator(refs.begin()); + ref != make_indirect_iterator(refs.end()); ++ref) { + fragID = ref->getURI(); if (!fragID || *fragID != chPound || !*(fragID+1)) { log.warn("skipping ds11:KeyInfoReference with an empty or non-local reference"); continue; @@ -357,11 +363,12 @@ bool InlineCredential::resolveCerts(const KeyInfo* keyInfo, bool followRefs) // Check for ds:X509Data const vector& x509Datas=keyInfo->getX509Datas(); - for (vector::const_iterator j=x509Datas.begin(); m_xseccerts.empty() && j!=x509Datas.end(); ++j) { + for (vector::const_iterator j = x509Datas.begin(); m_xseccerts.empty() && j != x509Datas.end(); ++j) { const vector x509Certs=const_cast(*j)->getX509Certificates(); - for (vector::const_iterator k=x509Certs.begin(); k!=x509Certs.end(); ++k) { + for (indirect_iterator::const_iterator> k = make_indirect_iterator(x509Certs.begin()); + k != make_indirect_iterator(x509Certs.end()); ++k) { try { - auto_ptr_char x((*k)->getValue()); + auto_ptr_char x(k->getValue()); if (!x.get()) { log.warn("skipping empty ds:X509Certificate"); } @@ -387,8 +394,9 @@ bool InlineCredential::resolveCerts(const KeyInfo* keyInfo, bool followRefs) const XMLCh* fragID=NULL; const XMLObject* treeRoot=NULL; const vector& refs = keyInfo->getKeyInfoReferences(); - for (vector::const_iterator ref = refs.begin(); ref != refs.end(); ++ref) { - fragID = (*ref)->getURI(); + for (indirect_iterator::const_iterator> ref = make_indirect_iterator(refs.begin()); + ref != make_indirect_iterator(refs.end()); ++ref) { + fragID = ref->getURI(); if (!fragID || *fragID != chPound || !*(fragID+1)) { log.warn("skipping ds11:KeyInfoReference with an empty or non-local reference"); continue; @@ -421,9 +429,10 @@ bool InlineCredential::resolveCRLs(const KeyInfo* keyInfo, bool followRefs) const vector& x509Datas=keyInfo->getX509Datas(); for (vector::const_iterator j=x509Datas.begin(); j!=x509Datas.end(); ++j) { const vector x509CRLs=const_cast(*j)->getX509CRLs(); - for (vector::const_iterator k=x509CRLs.begin(); k!=x509CRLs.end(); ++k) { + for (indirect_iterator::const_iterator> k = make_indirect_iterator(x509CRLs.begin()); + k != make_indirect_iterator(x509CRLs.end()); ++k) { try { - auto_ptr_char x((*k)->getValue()); + auto_ptr_char x(k->getValue()); if (!x.get()) { log.warn("skipping empty ds:X509CRL"); } @@ -449,8 +458,9 @@ bool InlineCredential::resolveCRLs(const KeyInfo* keyInfo, bool followRefs) const XMLCh* fragID=NULL; const XMLObject* treeRoot=NULL; const vector& refs = keyInfo->getKeyInfoReferences(); - for (vector::const_iterator ref = refs.begin(); ref != refs.end(); ++ref) { - fragID = (*ref)->getURI(); + for (indirect_iterator::const_iterator> ref = make_indirect_iterator(refs.begin()); + ref != make_indirect_iterator(refs.end()); ++ref) { + fragID = ref->getURI(); if (!fragID || *fragID != chPound || !*(fragID+1)) { log.warn("skipping ds11:KeyInfoReference with an empty or non-local reference"); continue; diff --git a/xmltooling/util/XMLHelper.cpp b/xmltooling/util/XMLHelper.cpp index f257b8c..be7401d 100644 --- a/xmltooling/util/XMLHelper.cpp +++ b/xmltooling/util/XMLHelper.cpp @@ -31,11 +31,16 @@ #include "util/XMLHelper.h" #include "util/XMLConstants.h" +#include +#include +#include #include #include using namespace xmltooling; using namespace xercesc; +using namespace boost::lambda; +using namespace boost; using namespace std; static const XMLCh type[]={chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull }; @@ -77,7 +82,7 @@ DOMAttr* XMLHelper::getIdAttribute(const DOMElement* domElement) DOMNamedNodeMap* attributes = domElement->getAttributes(); DOMAttr* attribute; - for(XMLSize_t i = 0; i < attributes->getLength(); i++) { + for(XMLSize_t i = 0; i < attributes->getLength(); ++i) { attribute = static_cast(attributes->item(i)); if(attribute->isId()) { return attribute; @@ -94,7 +99,7 @@ const XMLObject* XMLHelper::getXMLObjectById(const XMLObject& tree, const XMLCh* const XMLObject* ret; const list& children = tree.getOrderedChildren(); - for (list::const_iterator i=children.begin(); i!=children.end(); ++i) { + for (list::const_iterator i = children.begin(); i != children.end(); ++i) { if (*i) { ret = getXMLObjectById(*(*i), id); if (ret) @@ -109,10 +114,10 @@ XMLObject* XMLHelper::getXMLObjectById(XMLObject& tree, const XMLCh* id) { if (XMLString::equals(id, tree.getXMLID())) return &tree; - + XMLObject* ret; const list& children = tree.getOrderedChildren(); - for (list::const_iterator i=children.begin(); i!=children.end(); ++i) { + for (list::const_iterator i = children.begin(); i != children.end(); ++i) { if (*i) { ret = getXMLObjectById(*(*i), id); if (ret) @@ -126,11 +131,10 @@ XMLObject* XMLHelper::getXMLObjectById(XMLObject& tree, const XMLCh* id) void XMLHelper::getNonVisiblyUsedPrefixes(const XMLObject& tree, map& prefixes) { map child_prefixes; - const list& children = tree.getOrderedChildren(); - for (list::const_iterator i = children.begin(); i != children.end(); ++i) { - if (*i) - getNonVisiblyUsedPrefixes(*(*i), child_prefixes); - } + for_each( + tree.getOrderedChildren().begin(), tree.getOrderedChildren().end(), + if_(_1 != nullptr)[lambda::bind(&getNonVisiblyUsedPrefixes, boost::ref(*_1), boost::ref(child_prefixes))] + ); const set& nsset = tree.getNamespaces(); for (set::const_iterator ns = nsset.begin(); ns != nsset.end(); ++ns) { // Check for xmlns:xml. -- 2.1.4