https://issues.shibboleth.net/jira/browse/CPPOST-41
authorScott Cantor <cantor.2@osu.edu>
Sat, 21 Aug 2010 03:33:15 +0000 (03:33 +0000)
committerScott Cantor <cantor.2@osu.edu>
Sat, 21 Aug 2010 03:33:15 +0000 (03:33 +0000)
xmltooling/AbstractXMLObject.cpp
xmltooling/io/AbstractXMLObjectMarshaller.cpp
xmltooling/io/AbstractXMLObjectMarshaller.h
xmltooling/util/XMLHelper.cpp
xmltooling/util/XMLHelper.h

index 10d5844..b7c3cac 100644 (file)
@@ -130,24 +130,31 @@ void XMLObject::setNil(const XMLCh* value)
 
 void AbstractXMLObject::addNamespace(const Namespace& ns) const
 {
-    std::set<Namespace>::iterator i = m_namespaces.find(ns);
-    if (i == m_namespaces.end())
-        m_namespaces.insert(ns);
-    else {
-        if (ns.alwaysDeclare())
-            const_cast<Namespace&>(*i).setAlwaysDeclare(true);
-        switch (ns.usage()) {
-            case Namespace::Indeterminate:
-                break;
-            case Namespace::VisiblyUsed:
-                const_cast<Namespace&>(*i).setUsage(Namespace::VisiblyUsed);
-                break;
-            case Namespace::NonVisiblyUsed:
-                if (i->usage() == Namespace::Indeterminate)
-                    const_cast<Namespace&>(*i).setUsage(Namespace::NonVisiblyUsed);
-                break;
+    for (set<Namespace>::const_iterator n = m_namespaces.begin(); n != m_namespaces.end(); ++n) {
+        // Look for the prefix in the existing set.
+        if (XMLString::equals(ns.getNamespacePrefix(), n->getNamespacePrefix())) {
+            // See if it's the same declaration, and overlay various properties if so.
+            if (XMLString::equals(ns.getNamespaceURI(), n->getNamespaceURI())) {
+                if (ns.alwaysDeclare())
+                    const_cast<Namespace&>(*n).setAlwaysDeclare(true);
+                switch (ns.usage()) {
+                    case Namespace::Indeterminate:
+                        break;
+                    case Namespace::VisiblyUsed:
+                        const_cast<Namespace&>(*n).setUsage(Namespace::VisiblyUsed);
+                        break;
+                    case Namespace::NonVisiblyUsed:
+                        if (n->usage() == Namespace::Indeterminate)
+                            const_cast<Namespace&>(*n).setUsage(Namespace::NonVisiblyUsed);
+                        break;
+                }
+            }
+            return;
         }
     }
+
+    // If the prefix is now, go ahead and add it.
+    m_namespaces.insert(ns);
 }
 
 void AbstractXMLObject::removeNamespace(const Namespace& ns)
index c28bcbf..d3af0cb 100644 (file)
@@ -96,6 +96,8 @@ DOMElement* AbstractXMLObjectMarshaller::marshall(
     }
     
     // If we get here, we didn't have a usable DOM (and/or we released the one we had).
+    prepareForMarshalling();
+
     // We may need to create our own document.
     bool bindDocument=false;
     if (!document) {
@@ -161,6 +163,8 @@ DOMElement* AbstractXMLObjectMarshaller::marshall(
     }
     
     // If we get here, we didn't have a usable DOM (and/or we released the one we had).
+    prepareForMarshalling();
+
     m_log.debug("creating root element to marshall");
     DOMElement* domElement = parentElement->getOwnerDocument()->createElementNS(
         getElementQName().getNamespaceURI(), getElementQName().getLocalPart()
@@ -382,3 +386,7 @@ void AbstractXMLObjectMarshaller::marshallContent(
 void AbstractXMLObjectMarshaller::marshallAttributes(DOMElement* domElement) const
 {
 }
+
+void AbstractXMLObjectMarshaller::prepareForMarshalling() const
+{
+}
index 16eb776..c22b173 100644 (file)
@@ -141,6 +141,12 @@ namespace xmltooling {
          * @throws MarshallingException thrown if there is a problem marshalling an attribute
          */
         virtual void marshallAttributes(xercesc::DOMElement* domElement) const;
+
+        /**
+         * Called before marshalling in the event that a new DOM is being generated.
+         * <p>Allows objects to adjust internal state prior to the marshalling step.
+         */
+        virtual void prepareForMarshalling() const;
     };
     
 };
index 34d82f6..7bc43ba 100644 (file)
@@ -119,9 +119,9 @@ XMLObject* XMLHelper::getXMLObjectById(XMLObject& tree, const XMLCh* id)
     return nullptr;
 }
 
-void XMLHelper::getNonVisiblyUsedPrefixes(const XMLObject& tree, set<xstring>& prefixes)
+void XMLHelper::getNonVisiblyUsedPrefixes(const XMLObject& tree, map<xstring,xstring>& prefixes)
 {
-    set<xstring> child_prefixes;
+    map<xstring,xstring> child_prefixes;
     const list<XMLObject*>& children = tree.getOrderedChildren();
     for (list<XMLObject*>::const_iterator i = children.begin(); i != children.end(); ++i) {
         if (*i)
@@ -136,13 +136,26 @@ void XMLHelper::getNonVisiblyUsedPrefixes(const XMLObject& tree, set<xstring>& p
             case Namespace::Indeterminate:
                 break;
             case Namespace::VisiblyUsed:
-                child_prefixes.erase(ns->getNamespacePrefix() ? ns->getNamespacePrefix() : &chNull);
+            {
+                // See if the prefix was noted as non-visible below.
+                const XMLCh* p = ns->getNamespacePrefix() ? ns->getNamespacePrefix() : &chNull;
+                map<xstring,xstring>::iterator decl = child_prefixes.find(p);
+                if (decl != child_prefixes.end()) {
+                    // It's declared below, see if it's the same namespace. If so, pull it from the set,
+                    // otherwise leave it in the set.
+                    if (decl->second == (ns->getNamespaceURI() ? ns->getNamespaceURI() : &chNull))
+                        child_prefixes.erase(decl);
+                }
                 break;
+            }
             case Namespace::NonVisiblyUsed:
-                prefixes.insert(ns->getNamespacePrefix() ? ns->getNamespacePrefix() : &chNull);
+                // It may already be in the map from another branch of the tree, but as long
+                // as it's set to something so the parent knows about it, we're good.
+                prefixes[ns->getNamespacePrefix() ? ns->getNamespacePrefix() : &chNull] = (ns->getNamespaceURI() ? ns->getNamespaceURI() : &chNull);
                 break;
         }
     }
+
     prefixes.insert(child_prefixes.begin(), child_prefixes.end());
 }
 
index 7198823..ade134c 100644 (file)
@@ -135,12 +135,13 @@ namespace xmltooling {
         static XMLObject* getXMLObjectById(XMLObject& tree, const XMLCh* id);
 
         /**
-         * Returns a list of non-visibly-used namespace prefixes found in a tree.
+         * Returns the set of non-visibly-used namespace declarations found in a tree.
+         * <p>Each member of the set is a prefix/URI pair.
          *
          * @param tree      root of tree to search
-         * @param prefixes  container to store prefix list
+         * @param prefixes  container to store declarations
          */
-        static void getNonVisiblyUsedPrefixes(const XMLObject& tree, std::set<xstring>& prefixes);
+        static void getNonVisiblyUsedPrefixes(const XMLObject& tree, std::map<xstring,xstring>& prefixes);
 
         /**
          * Gets the QName for the given DOM node.