Namespace handling fixes
[shibboleth/cpp-xmltooling.git] / xmltooling / io / AbstractXMLObjectMarshaller.cpp
index 42a4080..d5c4ac0 100644 (file)
@@ -39,12 +39,8 @@ using namespace std;
 \r
 #define XT_log (*static_cast<Category*>(m_log))\r
 \r
-AbstractXMLObjectMarshaller::AbstractXMLObjectMarshaller(const XMLCh* targetNamespaceURI, const XMLCh* targetLocalName)\r
-        : m_targetQName(targetNamespaceURI, targetLocalName),\r
-        m_log(&Category::getInstance(XMLTOOLING_LOGCAT".Marshaller")) {\r
-    if (!targetLocalName || !*targetLocalName)\r
-        throw MarshallingException("targetLocalName cannot be null or empty");\r
-}\r
+AbstractXMLObjectMarshaller::AbstractXMLObjectMarshaller()\r
+    : m_log(&Category::getInstance(XMLTOOLING_LOGCAT".Marshaller")) {}\r
 \r
 DOMElement* AbstractXMLObjectMarshaller::marshall(XMLObject* xmlObject, DOMDocument* document) const\r
 {\r
@@ -165,7 +161,8 @@ DOMElement* AbstractXMLObjectMarshaller::marshall(XMLObject* xmlObject, DOMEleme
         \r
 void AbstractXMLObjectMarshaller::marshallInto(XMLObject& xmlObject, DOMElement* targetElement) const\r
 {\r
-    targetElement->setPrefix(xmlObject.getElementQName().getPrefix());\r
+    if (xmlObject.getElementQName().hasPrefix())\r
+        targetElement->setPrefix(xmlObject.getElementQName().getPrefix());\r
     marshallElementType(xmlObject, targetElement);\r
     marshallNamespaces(xmlObject, targetElement);\r
     marshallAttributes(xmlObject, targetElement);\r
@@ -246,8 +243,12 @@ public:
 \r
     const XMLCh* lookupNamespaceURI(const DOMNode* n, const XMLCh* prefix) const {\r
         // Return NULL if no declaration in effect. The empty string signifies the null namespace.\r
-        if (!n || n->getNodeType()!=DOMNode::ELEMENT_NODE)\r
+        if (!n || n->getNodeType()!=DOMNode::ELEMENT_NODE) {\r
+            // At the root, the default namespace is set to the null namespace.\r
+            if (!prefix || !*prefix)\r
+                return &chNull;\r
             return NULL;    // we're done\r
+        }\r
         DOMNamedNodeMap* attributes = static_cast<const DOMElement*>(n)->getAttributes();\r
         if (!attributes)\r
             return lookupNamespaceURI(n->getParentNode(),prefix);   // defer to parent\r
@@ -261,7 +262,7 @@ public:
             if (!XMLString::equals(attribute->getNamespaceURI(),XMLConstants::XMLNS_NS))\r
                 continue;   // not a namespace declaration\r
             // Local name should be the prefix and the value would be the URI, except for the default namespace.\r
-            if (!prefix && XMLString::equals(attribute->getLocalName(),XMLConstants::XMLNS_PREFIX))\r
+            if ((!prefix || !*prefix) && XMLString::equals(attribute->getLocalName(),XMLConstants::XMLNS_PREFIX))\r
                 return attribute->getNodeValue();\r
             else if (XMLString::equals(prefix,attribute->getLocalName()))\r
                 return attribute->getNodeValue();\r