Multi-line svn commit, see body.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / XMLHelper.h
index 689370a..df33c63 100644 (file)
@@ -20,7 +20,7 @@
  * A helper class for working with W3C DOM objects. \r
  */\r
 \r
-#if !defined(__xmltooling_xmlhelper_h__)\r
+#ifndef __xmltooling_xmlhelper_h__\r
 #define __xmltooling_xmlhelper_h__\r
 \r
 #include <xmltooling/QName.h>\r
@@ -31,6 +31,33 @@ using namespace xercesc;
 namespace xmltooling {\r
     \r
     /**\r
+     * RAII wrapper for Xerces resources.\r
+     */\r
+    template<class T> class XercesJanitor\r
+    {\r
+        MAKE_NONCOPYABLE(XercesJanitor);\r
+        T* m_held;\r
+    public:\r
+        XercesJanitor(T* resource) : m_held(resource) {}\r
+        \r
+        ~XercesJanitor() {\r
+            if (m_held)\r
+                m_held->release();\r
+        }\r
+        \r
+        /**\r
+         * Returns resource held by this object and releases it to the caller.\r
+         * \r
+         * @return  the resource held or NULL\r
+         */\r
+        T* release() {\r
+            T* ret=m_held;\r
+            m_held=NULL;\r
+            return ret;\r
+        }\r
+    };\r
+    \r
+    /**\r
      * A helper class for working with W3C DOM objects. \r
      */\r
     class XMLTOOL_API XMLHelper\r
@@ -42,7 +69,7 @@ namespace xmltooling {
          * @param e the DOM element\r
          * @return true if there is a type, false if not\r
          */\r
-        static bool hasXSIType(DOMElement* e);\r
+        static bool hasXSIType(const DOMElement* e);\r
 \r
         /**\r
          * Gets the XSI type for a given element if it has one.\r
@@ -50,7 +77,7 @@ namespace xmltooling {
          * @param e the element\r
          * @return the type or null\r
          */\r
-        static QName* getXSIType(DOMElement* e);\r
+        static QName* getXSIType(const DOMElement* e);\r
 \r
         /**\r
          * Gets the ID attribute of a DOM element.\r
@@ -58,7 +85,7 @@ namespace xmltooling {
          * @param domElement the DOM element\r
          * @return the ID attribute or null if there isn't one\r
          */\r
-        static DOMAttr* getIdAttribute(DOMElement* domElement);\r
+        static DOMAttr* getIdAttribute(const DOMElement* domElement);\r
 \r
         /**\r
          * Gets the QName for the given DOM node.\r
@@ -66,15 +93,15 @@ namespace xmltooling {
          * @param domNode the DOM node\r
          * @return the QName for the element or null if the element was null\r
          */\r
-        static QName* getNodeQName(DOMNode* domNode);\r
+        static QName* getNodeQName(const DOMNode* domNode);\r
 \r
         /**\r
-         * Constructs a QName from an attributes value.\r
+         * Constructs a QName from an attribute's value.\r
          * \r
          * @param attribute the attribute with a QName value\r
-         * @return a QName from an attributes value, or null if the given attribute is null\r
+         * @return a QName from an attribute's value, or null if the given attribute is null\r
          */\r
-        static QName* getAttributeValueAsQName(DOMAttr* attribute);\r
+        static QName* getAttributeValueAsQName(const DOMAttr* attribute);\r
 \r
         /**\r
          * Appends the child Element to the parent Element,\r
@@ -85,6 +112,113 @@ namespace xmltooling {
          * @return the child Element that was added (may be an imported copy)\r
          */\r
         static DOMElement* appendChildElement(DOMElement* parentElement, DOMElement* childElement);\r
+        \r
+        /**\r
+         * Checks the qualified name of a node.\r
+         * \r
+         * @param n     node to check\r
+         * @param ns    namespace to compare with\r
+         * @param local local name to compare with\r
+         * @return  true iff the node's qualified name matches the other parameters\r
+         */\r
+        static bool isNodeNamed(const DOMNode* n, const XMLCh* ns, const XMLCh* local) {\r
+            return (n && XMLString::equals(local,n->getLocalName()) && XMLString::equals(ns,n->getNamespaceURI()));\r
+        }\r
+\r
+        /**\r
+         * Returns the first matching child element of the node if any.\r
+         * \r
+         * @param n         node to check\r
+         * @param localName local name to compare with or NULL for any match\r
+         * @return  the first matching child node of type Element, or NULL\r
+         */\r
+        static DOMElement* getFirstChildElement(const DOMNode* n, const XMLCh* localName=NULL);\r
+        \r
+        /**\r
+         * Returns the last matching child element of the node if any.\r
+         * \r
+         * @param n     node to check\r
+         * @param localName local name to compare with or NULL for any match\r
+         * @return  the last matching child node of type Element, or NULL\r
+         */\r
+        static DOMElement* getLastChildElement(const DOMNode* n, const XMLCh* localName=NULL);\r
+        \r
+        /**\r
+         * Returns the next matching sibling element of the node if any.\r
+         * \r
+         * @param n     node to check\r
+         * @param localName local name to compare with or NULL for any match\r
+         * @return  the next matching sibling node of type Element, or NULL\r
+         */\r
+        static DOMElement* getNextSiblingElement(const DOMNode* n, const XMLCh* localName=NULL);\r
+        \r
+        /**\r
+         * Returns the previous matching sibling element of the node if any.\r
+         * \r
+         * @param n     node to check\r
+         * @param localName local name to compare with or NULL for any match\r
+         * @return  the previous matching sibling node of type Element, or NULL\r
+         */\r
+        static DOMElement* getPreviousSiblingElement(const DOMNode* n, const XMLCh* localName=NULL);\r
+        \r
+        /**\r
+         * Returns the first matching child element of the node if any.\r
+         * \r
+         * @param n         node to check\r
+         * @param ns        namespace to compare with\r
+         * @param localName local name to compare with\r
+         * @return  the first matching child node of type Element, or NULL\r
+         */\r
+        static DOMElement* getFirstChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);\r
+        \r
+        /**\r
+         * Returns the last matching child element of the node if any.\r
+         * \r
+         * @param n         node to check\r
+         * @param ns        namespace to compare with\r
+         * @param localName local name to compare with\r
+         * @return  the last matching child node of type Element, or NULL\r
+         */\r
+        static DOMElement* getLastChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);\r
+        \r
+        /**\r
+         * Returns the next matching sibling element of the node if any.\r
+         * \r
+         * @param n         node to check\r
+         * @param ns        namespace to compare with\r
+         * @param localName local name to compare with\r
+         * @return  the next matching sibling node of type Element, or NULL\r
+         */\r
+        static DOMElement* getNextSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);\r
+        \r
+        /**\r
+         * Returns the previous matching sibling element of the node if any.\r
+         * \r
+         * @param n         node to check\r
+         * @param ns        namespace to compare with\r
+         * @param localName local name to compare with\r
+         * @return  the previous matching sibling node of type Element, or NULL\r
+         */\r
+        static DOMElement* getPreviousSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);\r
+\r
+        /**\r
+         * Returns the content of the first Text node found in the element, if any.\r
+         * This is roughly similar to the DOM getTextContent function, but only\r
+         * examples the immediate children of the element.\r
+         *\r
+         * @param e     element to examine\r
+         * @return the content of the first Text node found, or NULL\r
+         */\r
+        static const XMLCh* getTextContent(const DOMElement* e);\r
+\r
+        /**\r
+         * Serializes the DOM Element provided into a buffer using UTF-8 encoding and\r
+         * the default XML serializer available. No manipulation or formatting is applied.\r
+         * \r
+         * @param e     element to serialize\r
+         * @param buf   buffer to serialize element into\r
+         */\r
+        static void serialize(const DOMElement* e, std::string& buf);\r
     };\r
 \r
 };\r