Moved DOM methods up the tree, add copy c'tors, KeyInfo sample
[shibboleth/xmltooling.git] / xmltooling / AbstractXMLObject.h
index 9a00401..6beecfd 100644 (file)
@@ -23,7 +23,6 @@
 #if !defined(__xmltooling_abstractxmlobj_h__)\r
 #define __xmltooling_abstractxmlobj_h__\r
 \r
-#include <algorithm>\r
 #include <xmltooling/XMLObject.h>\r
 \r
 #if defined (_MSC_VER)\r
@@ -39,58 +38,30 @@ namespace xmltooling {
     class XMLTOOL_API AbstractXMLObject : public virtual XMLObject\r
     {\r
     public:\r
-        virtual ~AbstractXMLObject() {\r
-            delete m_typeQname;\r
-            std::for_each(m_children.begin(), m_children.end(), cleanup<XMLObject>());\r
-        }\r
+        virtual ~AbstractXMLObject();\r
 \r
-        /**\r
-         * @see XMLObject::getElementQName()\r
-         */\r
         const QName& getElementQName() const {\r
             return m_elementQname;\r
         }\r
 \r
-        /**\r
-         * @see XMLObject::setElementNamespacePrefix()\r
-         */\r
-        void setElementNamespacePrefix(const XMLCh* prefix) {\r
-            m_elementQname.setPrefix(prefix);\r
-        }\r
-\r
-        /**\r
-         * @see XMLObject::getNamespaces()\r
-         */\r
         const std::set<Namespace>& getNamespaces() const {\r
             return m_namespaces;\r
         }\r
     \r
-        /**\r
-         * @see XMLObject::addNamespace()\r
-         */\r
-        void addNamespace(const Namespace& ns) {\r
+        void addNamespace(const Namespace& ns) const {\r
             if (ns.alwaysDeclare() || m_namespaces.find(ns)==m_namespaces.end()) {\r
                 m_namespaces.insert(ns);\r
             }\r
         }\r
     \r
-        /**\r
-         * @see XMLObject::removeNamespace()\r
-         */\r
         void removeNamespace(const Namespace& ns) {\r
             m_namespaces.erase(ns);\r
         }\r
         \r
-        /**\r
-         * @see XMLObject::getSchemaType()\r
-         */\r
         const QName* getSchemaType() const {\r
             return m_typeQname;\r
         }\r
     \r
-        /**\r
-         * @see XMLObject::setSchemaType()\r
-         */\r
         void setSchemaType(const QName* type) {\r
             delete m_typeQname;\r
             m_typeQname = NULL;\r
@@ -100,37 +71,22 @@ namespace xmltooling {
             }\r
         }\r
     \r
-        /**\r
-         * @see XMLObject::hasParent()\r
-         */\r
         bool hasParent() const {\r
             return m_parent != NULL;\r
         }\r
      \r
-        /**\r
-         * @see XMLObject::getParent()\r
-         */\r
         XMLObject* getParent() const {\r
             return m_parent;\r
         }\r
     \r
-        /**\r
-         * @see XMLObject::setParent()\r
-         */\r
         void setParent(XMLObject* parent) {\r
             m_parent = parent;\r
         }\r
 \r
-        /**\r
-         * @see XMLObject::hasChildren()\r
-         */\r
         bool hasChildren() const {\r
             return !m_children.empty();\r
         }\r
 \r
-        /**\r
-         * @see XMLObject::getOrderedChildren()\r
-         */\r
         const std::list<XMLObject*>& getOrderedChildren() const {\r
             return m_children;\r
         }\r
@@ -143,12 +99,49 @@ namespace xmltooling {
          * @param elementLocalName the local name of the XML element this Object represents\r
          * @param namespacePrefix the namespace prefix to use\r
          */\r
-        AbstractXMLObject(const XMLCh* namespaceURI=NULL, const XMLCh* elementLocalName=NULL, const XMLCh* namespacePrefix=NULL)\r
-            : m_elementQname(namespaceURI,elementLocalName, namespacePrefix), m_typeQname(NULL), m_parent(NULL) {\r
-            addNamespace(Namespace(namespaceURI, namespacePrefix));\r
+        AbstractXMLObject(const XMLCh* namespaceURI=NULL, const XMLCh* elementLocalName=NULL, const XMLCh* namespacePrefix=NULL);\r
+\r
+        /** Copy constructor. */\r
+        AbstractXMLObject(const AbstractXMLObject& src);\r
+        \r
+        /**\r
+         * A helper function for derived classes.\r
+         * This 'normalizes' newString, and then if it is different from oldString,\r
+         * it invalidates the DOM, frees the old string, and return the new.\r
+         * If not different, it frees the new string and just returns the old value.\r
+         * \r
+         * @param oldValue - the current value\r
+         * @param newValue - the new value\r
+         * \r
+         * @return the value that should be assigned\r
+         */\r
+        XMLCh* prepareForAssignment(XMLCh* oldValue, const XMLCh* newValue) {\r
+            XMLCh* newString = XMLString::replicate(newValue);\r
+            XMLString::trim(newString);\r
+            if (!XMLString::equals(oldValue,newValue)) {\r
+                releaseThisandParentDOM();\r
+                XMLString::release(&oldValue);\r
+                return newString;\r
+            }\r
+            XMLString::release(&newString);\r
+            return oldValue;            \r
         }\r
 \r
         /**\r
+         * A helper function for derived classes, for assignment of (singleton) XML objects.\r
+         * \r
+         * It is indifferent to whether either the old or the new version of the value is null. \r
+         * This method will do a safe compare of the objects and will also invalidate the DOM if appropriate\r
+         * \r
+         * @param oldValue - current value\r
+         * @param newValue - proposed new value\r
+         * @return the value to assign \r
+         * \r
+         * @throws XMLObjectException if the new child already has a parent.\r
+         */\r
+        XMLObject* prepareForAssignment(XMLObject* oldValue, XMLObject* newValue);\r
+\r
+        /**\r
          * Underlying list of child objects.\r
          * Manages the lifetime of the children.\r
          */\r
@@ -157,8 +150,13 @@ namespace xmltooling {
         /**\r
          * Set of namespaces associated with the object.\r
          */\r
-        std::set<Namespace> m_namespaces;\r
-        \r
+        mutable std::set<Namespace> m_namespaces;\r
+\r
+        /**\r
+         * Logging object.\r
+         */\r
+        void* m_log;\r
+\r
     private:\r
         XMLObject* m_parent;\r
         QName m_elementQname;\r