From 73308303d23af62494bfeef0c0d5224c344c6d6f Mon Sep 17 00:00:00 2001 From: Scott Cantor Date: Thu, 22 Dec 2011 22:33:55 +0000 Subject: [PATCH] Additional macros and cleanup for cloning --- xmltooling/base.h | 67 +++++++++++- xmltooling/encryption/impl/EncryptionImpl.cpp | 23 ++-- xmltooling/signature/impl/KeyInfoImpl.cpp | 145 +++++--------------------- xmltooling/soap/impl/SOAPImpl.cpp | 6 +- 4 files changed, 100 insertions(+), 141 deletions(-) diff --git a/xmltooling/base.h b/xmltooling/base.h index f70e0f6..5ae111c 100644 --- a/xmltooling/base.h +++ b/xmltooling/base.h @@ -1366,6 +1366,17 @@ set##proper(src.get##proper()) /** + * Implements cloning of a child attribute in a foreign namespace, for use in copy constructor or + * deferred clone methods. + * + * proper the proper name of the attribute to clone + */ +#define IMPL_CLONE_FOREIGN_ATTRIB(proper) \ + set##proper(src.get##proper()); \ + if (src.m_##proper##Prefix) \ + m_##proper##Prefix = xercesc::XMLString::replicate(src.m_##proper##Prefix) + +/** * Implements cloning of an integer child attribute, for use in copy constructor or * deferred clone methods. * @@ -1406,14 +1417,16 @@ /** * Implements cloning of an untyped child collection, for use in copy constructor or * deferred clone methods. + * + * proper the proper name of the child type to clone */ -#define IMPL_CLONE_XMLOBJECT_CHILDREN() \ +#define IMPL_CLONE_XMLOBJECT_CHILDREN(proper) \ static void (VectorOf(XMLObject)::* XMLObject_push_back)(XMLObject* const&) = &VectorOf(XMLObject)::push_back; \ - VectorOf(XMLObject) cXMLObject = getUnknownXMLObjects(); \ + VectorOf(XMLObject) c##proper = get##proper##s(); \ std::for_each( \ - src.m_UnknownXMLObjects.begin(), src.m_UnknownXMLObjects.end(), \ + src.m_##proper##s.begin(), src.m_##proper##s.end(), \ boost::lambda::if_(boost::lambda::_1 != ((XMLObject*)nullptr)) \ - [boost::lambda::bind(XMLObject_push_back, boost::ref(cXMLObject), boost::lambda::bind(&XMLObject::clone, boost::lambda::_1))] \ + [boost::lambda::bind(XMLObject_push_back, boost::ref(c##proper), boost::lambda::bind(&XMLObject::clone, boost::lambda::_1))] \ ) /** @@ -1448,6 +1461,52 @@ ) /** + * Opens an iteration loop over all of the children of an object. + */ +#define IMPL_CLONE_CHILDBAG_BEGIN \ + for (list::const_iterator _bagit = src.m_children.begin(); _bagit != src.m_children.end(); ++_bagit) { + +/** + * Closes an iteration loop over all of the children of an object. + */ +#define IMPL_CLONE_CHILDBAG_END } + +/** + * Implements cloning of a typed child in a bag iteration loop based on a cast check. + * + * @param proper the proper name of the child type to clone + */ +#define IMPL_CLONE_TYPED_CHILD_IN_BAG(proper) \ + proper* _##proper##cast = dynamic_cast(*_bagit); \ + if (_##proper##cast) { \ + get##proper##s().push_back(_##proper##cast->clone##proper()); \ + continue; \ + } + +/** + * Implements cloning of a typed child in a forign namespace in a bag iteration loop based on a cast check. + * + * @param proper the proper name of the child type to clone + * @param ns the namespace of the child type + */ +#define IMPL_CLONE_TYPED_FOREIGN_CHILD_IN_BAG(proper,ns) \ + ns::proper* _##proper##cast = dynamic_cast(*_bagit); \ + if (_##proper##cast) { \ + get##proper##s().push_back(_##proper##cast->clone##proper()); \ + continue; \ + } + +/** + * Implements cloning of an XMLObject child in a bag iteration loop. + * + * @param proper the proper name of the child to clone + */ +#define IMPL_CLONE_XMLOBJECT_CHILD_IN_BAG(proper) \ + if (*_bagit) { \ + get##proper##s().push_back((*_bagit)->clone()); \ + } + +/** * Declares an XMLObject specialization with a simple content model and type, * handling it as string data. * diff --git a/xmltooling/encryption/impl/EncryptionImpl.cpp b/xmltooling/encryption/impl/EncryptionImpl.cpp index 439e031..9e766e2 100644 --- a/xmltooling/encryption/impl/EncryptionImpl.cpp +++ b/xmltooling/encryption/impl/EncryptionImpl.cpp @@ -93,7 +93,7 @@ namespace xmlencryption { IMPL_CLONE_ATTRIB(Algorithm); IMPL_CLONE_TYPED_CHILD(KeySize); IMPL_CLONE_TYPED_CHILD(OAEPparams); - IMPL_CLONE_XMLOBJECT_CHILDREN(); + IMPL_CLONE_XMLOBJECT_CHILDREN(UnknownXMLObject); } IMPL_XMLOBJECT_CLONE(EncryptionMethod); @@ -278,7 +278,7 @@ namespace xmlencryption { init(); IMPL_CLONE_ATTRIB(Id); IMPL_CLONE_ATTRIB(Target); - IMPL_CLONE_XMLOBJECT_CHILDREN(); + IMPL_CLONE_XMLOBJECT_CHILDREN(UnknownXMLObject); } IMPL_XMLOBJECT_CLONE(EncryptionProperty); @@ -390,7 +390,7 @@ namespace xmlencryption { void _clone(const ReferenceTypeImpl& src) { IMPL_CLONE_ATTRIB(URI); - IMPL_CLONE_XMLOBJECT_CHILDREN(); + IMPL_CLONE_XMLOBJECT_CHILDREN(UnknownXMLObject); } IMPL_XMLOBJECT_CLONE_EX(ReferenceType); @@ -453,19 +453,10 @@ namespace xmlencryption { ReferenceListImpl(const ReferenceListImpl& src) : AbstractXMLObject(src), AbstractComplexElement(src), AbstractDOMCachingXMLObject(src) { - for (list::const_iterator i = src.m_children.begin(); i != src.m_children.end(); ++i) { - DataReference* data=dynamic_cast(*i); - if (data) { - getDataReferences().push_back(data->cloneDataReference()); - continue; - } - - KeyReference* key=dynamic_cast(*i); - if (key) { - getKeyReferences().push_back(key->cloneKeyReference()); - continue; - } - } + IMPL_CLONE_CHILDBAG_BEGIN; + IMPL_CLONE_TYPED_CHILD_IN_BAG(DataReference); + IMPL_CLONE_TYPED_CHILD_IN_BAG(KeyReference); + IMPL_CLONE_CHILDBAG_END; } IMPL_XMLOBJECT_CLONE(ReferenceList); diff --git a/xmltooling/signature/impl/KeyInfoImpl.cpp b/xmltooling/signature/impl/KeyInfoImpl.cpp index df20b66..a260727 100644 --- a/xmltooling/signature/impl/KeyInfoImpl.cpp +++ b/xmltooling/signature/impl/KeyInfoImpl.cpp @@ -395,17 +395,10 @@ namespace xmlsignature { TransformImpl(const TransformImpl& src) : AbstractXMLObject(src), AbstractComplexElement(src), AbstractDOMCachingXMLObject(src), m_Algorithm(nullptr) { IMPL_CLONE_ATTRIB(Algorithm); - for (list::const_iterator i = src.m_children.begin(); i != src.m_children.end(); ++i) { - XPath* x=dynamic_cast(*i); - if (x) { - getXPaths().push_back(x->cloneXPath()); - continue; - } - - if (*i) { - getUnknownXMLObjects().push_back((*i)->clone()); - } - } + IMPL_CLONE_CHILDBAG_BEGIN; + IMPL_CLONE_TYPED_CHILD_IN_BAG(XPath); + IMPL_CLONE_XMLOBJECT_CHILD_IN_BAG(UnknownXMLObject); + IMPL_CLONE_CHILDBAG_END; } IMPL_XMLOBJECT_CLONE(Transform); @@ -613,53 +606,16 @@ namespace xmlsignature { X509DataImpl(const X509DataImpl& src) : AbstractXMLObject(src), AbstractComplexElement(src), AbstractDOMCachingXMLObject(src) { - for (list::const_iterator i = src.m_children.begin(); i != src.m_children.end(); ++i) { - X509Certificate* xcert=dynamic_cast(*i); - if (xcert) { - getX509Certificates().push_back(xcert->cloneX509Certificate()); - continue; - } - - X509CRL* xcrl=dynamic_cast(*i); - if (xcrl) { - getX509CRLs().push_back(xcrl->cloneX509CRL()); - continue; - } - - X509SubjectName* xsn=dynamic_cast(*i); - if (xsn) { - getX509SubjectNames().push_back(xsn->cloneX509SubjectName()); - continue; - } - - X509IssuerSerial* xis=dynamic_cast(*i); - if (xis) { - getX509IssuerSerials().push_back(xis->cloneX509IssuerSerial()); - continue; - } - - X509SKI* xski=dynamic_cast(*i); - if (xski) { - getX509SKIs().push_back(xski->cloneX509SKI()); - continue; - } - - X509Digest* xdig=dynamic_cast(*i); - if (xdig) { - getX509Digests().push_back(xdig->cloneX509Digest()); - continue; - } - - OCSPResponse* ocsp=dynamic_cast(*i); - if (ocsp) { - getOCSPResponses().push_back(ocsp->cloneOCSPResponse()); - continue; - } - - if (*i) { - getUnknownXMLObjects().push_back((*i)->clone()); - } - } + IMPL_CLONE_CHILDBAG_BEGIN; + IMPL_CLONE_TYPED_CHILD_IN_BAG(X509Certificate); + IMPL_CLONE_TYPED_CHILD_IN_BAG(X509CRL); + IMPL_CLONE_TYPED_CHILD_IN_BAG(X509SubjectName); + IMPL_CLONE_TYPED_CHILD_IN_BAG(X509IssuerSerial); + IMPL_CLONE_TYPED_CHILD_IN_BAG(X509SKI); + IMPL_CLONE_TYPED_CHILD_IN_BAG(X509Digest); + IMPL_CLONE_TYPED_CHILD_IN_BAG(OCSPResponse); + IMPL_CLONE_XMLOBJECT_CHILD_IN_BAG(UnknownXMLObject); + IMPL_CLONE_CHILDBAG_END; } IMPL_XMLOBJECT_CLONE(X509Data); @@ -784,7 +740,7 @@ namespace xmlsignature { init(); IMPL_CLONE_TYPED_CHILD(PGPKeyID); IMPL_CLONE_TYPED_CHILD(PGPKeyPacket); - IMPL_CLONE_XMLOBJECT_CHILDREN(); + IMPL_CLONE_XMLOBJECT_CHILDREN(UnknownXMLObject); } IMPL_XMLOBJECT_CLONE(PGPData); @@ -871,65 +827,18 @@ namespace xmlsignature { KeyInfoImpl(const KeyInfoImpl& src) : AbstractXMLObject(src), AbstractComplexElement(src), AbstractDOMCachingXMLObject(src), m_Id(nullptr) { IMPL_CLONE_ATTRIB(Id); - for (list::const_iterator i = src.m_children.begin(); i != src.m_children.end(); ++i) { - X509Data* xd=dynamic_cast(*i); - if (xd) { - getX509Datas().push_back(xd->cloneX509Data()); - continue; - } - - KeyName* kn=dynamic_cast(*i); - if (kn) { - getKeyNames().push_back(kn->cloneKeyName()); - continue; - } - - KeyValue* kv=dynamic_cast(*i); - if (kv) { - getKeyValues().push_back(kv->cloneKeyValue()); - continue; - } - - DEREncodedKeyValue* ekv=dynamic_cast(*i); - if (ekv) { - getDEREncodedKeyValues().push_back(ekv->cloneDEREncodedKeyValue()); - continue; - } - - RetrievalMethod* rm=dynamic_cast(*i); - if (rm) { - getRetrievalMethods().push_back(rm->cloneRetrievalMethod()); - continue; - } - - MgmtData* md=dynamic_cast(*i); - if (md) { - getMgmtDatas().push_back(md->cloneMgmtData()); - continue; - } - - SPKIData* sd=dynamic_cast(*i); - if (sd) { - getSPKIDatas().push_back(sd->cloneSPKIData()); - continue; - } - - PGPData* pd=dynamic_cast(*i); - if (pd) { - getPGPDatas().push_back(pd->clonePGPData()); - continue; - } - - KeyInfoReference* kref=dynamic_cast(*i); - if (kref) { - getKeyInfoReferences().push_back(kref->cloneKeyInfoReference()); - continue; - } - - if (*i) { - getUnknownXMLObjects().push_back((*i)->clone()); - } - } + IMPL_CLONE_CHILDBAG_BEGIN; + IMPL_CLONE_TYPED_CHILD_IN_BAG(X509Data); + IMPL_CLONE_TYPED_CHILD_IN_BAG(KeyName); + IMPL_CLONE_TYPED_CHILD_IN_BAG(KeyValue); + IMPL_CLONE_TYPED_CHILD_IN_BAG(DEREncodedKeyValue); + IMPL_CLONE_TYPED_CHILD_IN_BAG(RetrievalMethod); + IMPL_CLONE_TYPED_CHILD_IN_BAG(MgmtData); + IMPL_CLONE_TYPED_CHILD_IN_BAG(SPKIData); + IMPL_CLONE_TYPED_CHILD_IN_BAG(PGPData); + IMPL_CLONE_TYPED_CHILD_IN_BAG(KeyInfoReference); + IMPL_CLONE_XMLOBJECT_CHILD_IN_BAG(UnknownXMLObject); + IMPL_CLONE_CHILDBAG_END; } IMPL_XMLOBJECT_CLONE(KeyInfo); diff --git a/xmltooling/soap/impl/SOAPImpl.cpp b/xmltooling/soap/impl/SOAPImpl.cpp index dc9f1d6..f6b5bd6 100644 --- a/xmltooling/soap/impl/SOAPImpl.cpp +++ b/xmltooling/soap/impl/SOAPImpl.cpp @@ -117,7 +117,7 @@ namespace { AbstractAttributeExtensibleXMLObject(src), AbstractComplexElement(src), AbstractDOMCachingXMLObject(src) { - IMPL_CLONE_XMLOBJECT_CHILDREN(); + IMPL_CLONE_XMLOBJECT_CHILDREN(UnknownXMLObject); } IMPL_XMLOBJECT_CLONE(Detail); @@ -219,7 +219,7 @@ namespace { AbstractAttributeExtensibleXMLObject(src), AbstractComplexElement(src), AbstractDOMCachingXMLObject(src) { - IMPL_CLONE_XMLOBJECT_CHILDREN(); + IMPL_CLONE_XMLOBJECT_CHILDREN(UnknownXMLObject); } IMPL_XMLOBJECT_CLONE(Body); @@ -259,7 +259,7 @@ namespace { AbstractAttributeExtensibleXMLObject(src), AbstractComplexElement(src), AbstractDOMCachingXMLObject(src) { - IMPL_CLONE_XMLOBJECT_CHILDREN(); + IMPL_CLONE_XMLOBJECT_CHILDREN(UnknownXMLObject); } IMPL_XMLOBJECT_CLONE(Header); -- 2.1.4