First cut at signing support.
[shibboleth/cpp-xmltooling.git] / xmltooling / impl / UnknownElement.cpp
index 745abe9..3d638ae 100644 (file)
@@ -70,7 +70,7 @@ void UnknownElementImpl::serialize(string& s) const
         XMLHelper::serialize(getDOM(),s);\r
 }\r
 \r
-DOMElement* UnknownElementMarshaller::marshall(XMLObject* xmlObject, DOMDocument* document) const\r
+DOMElement* UnknownElementMarshaller::marshall(XMLObject* xmlObject, DOMDocument* document, MarshallingContext* ctx) const\r
 {\r
 #ifdef _DEBUG\r
     xmltooling::NDC ndc("marshall");\r
@@ -93,13 +93,19 @@ DOMElement* UnknownElementMarshaller::marshall(XMLObject* xmlObject, DOMDocument
             return cachedDOM;\r
         }\r
         \r
-        // We have a DOM but it doesn't match the document we were given. This both sucks and blows.\r
-        // Without an adoptNode option to maintain the child pointers, we rely on our custom\r
-        // implementation class to preserve the XML when we release the existing DOM.\r
-        unk->releaseDOM();\r
+        // We have a DOM but it doesn't match the document we were given, so we import\r
+        // it into the new document.\r
+        cachedDOM=static_cast<DOMElement*>(document->importNode(cachedDOM, true));\r
+\r
+        // Recache the DOM.\r
+        setDocumentElement(document, cachedDOM);\r
+        log.debug("caching imported DOM for XMLObject");\r
+        unk->setDOM(cachedDOM, false);\r
+        unk->releaseParentDOM(true);\r
+        return cachedDOM;\r
     }\r
     \r
-    // If we get here, we didn't have a usable DOM (and/or we flushed the one we had).\r
+    // If we get here, we didn't have a usable DOM.\r
     // We need to reparse the XML we saved off into a new DOM.\r
     bool bindDocument=false;\r
     MemBufInputSource src(reinterpret_cast<const XMLByte*>(unk->m_xml.c_str()),unk->m_xml.length(),"UnknownElementImpl");\r
@@ -129,7 +135,7 @@ DOMElement* UnknownElementMarshaller::marshall(XMLObject* xmlObject, DOMDocument
     return cachedDOM;\r
 }\r
 \r
-DOMElement* UnknownElementMarshaller::marshall(XMLObject* xmlObject, DOMElement* parentElement) const\r
+DOMElement* UnknownElementMarshaller::marshall(XMLObject* xmlObject, DOMElement* parentElement, MarshallingContext* ctx) const\r
 {\r
 #ifdef _DEBUG\r
     xmltooling::NDC ndc("marshall");\r
@@ -151,10 +157,16 @@ DOMElement* UnknownElementMarshaller::marshall(XMLObject* xmlObject, DOMElement*
             return cachedDOM;\r
         }\r
         \r
-        // We have a DOM but it doesn't match the document we were given. This both sucks and blows.\r
-        // Without an adoptNode option to maintain the child pointers, we rely on our custom\r
-        // implementation class to preserve the XML when we release the existing DOM.\r
-        unk->releaseDOM();\r
+        // We have a DOM but it doesn't match the document we were given, so we import\r
+        // it into the new document.\r
+        cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(cachedDOM, true));\r
+\r
+        // Recache the DOM.\r
+        parentElement->appendChild(cachedDOM);\r
+        log.debug("caching imported DOM for XMLObject");\r
+        unk->setDOM(cachedDOM, false);\r
+        unk->releaseParentDOM(true);\r
+        return cachedDOM;\r
     }\r
     \r
     // If we get here, we didn't have a usable DOM (and/or we flushed the one we had).\r