Manage disposal of new objects when exceptions are thrown.
authorcantor <cantor@de75baf8-a10c-0410-a50a-987c0e22f00f>
Thu, 2 Mar 2006 17:41:44 +0000 (17:41 +0000)
committercantor <cantor@de75baf8-a10c-0410-a50a-987c0e22f00f>
Thu, 2 Mar 2006 17:41:44 +0000 (17:41 +0000)
git-svn-id: https://svn.middleware.georgetown.edu/cpp-xmltooling/trunk@36 de75baf8-a10c-0410-a50a-987c0e22f00f

xmltooling/io/AbstractXMLObjectUnmarshaller.cpp

index ad168d1..ab69684 100644 (file)
@@ -56,17 +56,19 @@ XMLObject* AbstractXMLObjectUnmarshaller::unmarshall(DOMElement* element, bool b
         XT_log.debug("unmarshalling DOM element %s", dname.get());\r
     }\r
 \r
-    XMLObject* xmlObject = buildXMLObject(element);\r
+    auto_ptr<XMLObject> xmlObject(buildXMLObject(element));\r
 \r
     if (element->hasAttributes()) {\r
-        unmarshallAttributes(element, *xmlObject);\r
+        unmarshallAttributes(element, *(xmlObject.get()));\r
     }\r
 \r
-    if (element->getTextContent()) {\r
-        processElementContent(*xmlObject, element->getTextContent());\r
+    const XMLCh* textContent=element->getTextContent();\r
+    if (textContent && *textContent) {\r
+        XT_log.debug("processing element content");\r
+        processElementContent(*(xmlObject.get()), textContent);\r
     }\r
 \r
-    unmarshallChildElements(element, *xmlObject);\r
+    unmarshallChildElements(element, *(xmlObject.get()));\r
 \r
     /* TODO: Signing\r
     if (xmlObject instanceof SignableXMLObject) {\r
@@ -74,11 +76,13 @@ XMLObject* AbstractXMLObjectUnmarshaller::unmarshall(DOMElement* element, bool b
     }\r
     */\r
 \r
-    DOMCachingXMLObject* dc=dynamic_cast<DOMCachingXMLObject*>(xmlObject);\r
+    DOMCachingXMLObject* dc=dynamic_cast<DOMCachingXMLObject*>(xmlObject.get());\r
     if (dc)\r
         dc->setDOM(element,bindDocument);\r
+    else if (bindDocument)\r
+        throw UnmarshallingException("Unable to bind document to non-DOM caching XMLObject instance.");\r
         \r
-    return xmlObject;\r
+    return xmlObject.release();\r
 }\r
 \r
 XMLObject* AbstractXMLObjectUnmarshaller::buildXMLObject(const DOMElement* domElement) const\r
@@ -178,7 +182,11 @@ void AbstractXMLObjectUnmarshaller::unmarshallChildElements(const DOMElement* do
                 auto_ptr<QName> cname(XMLHelper::getNodeQName(childNode));\r
                 XT_log.debug("unmarshalling child element %s", cname->toString().c_str());\r
             }\r
-            processChildElement(xmlObject, unmarshaller->unmarshall(static_cast<DOMElement*>(childNode)));\r
+\r
+            // Retain ownership of the unmarshalled child until it's processed by the parent.\r
+            auto_ptr<XMLObject> childObject(unmarshaller->unmarshall(static_cast<DOMElement*>(childNode)));\r
+            processChildElement(xmlObject, childObject.get());\r
+            childObject.release();\r
         }\r
     }\r
 }\r