X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=xmltooling%2Futil%2FXMLHelper.h;h=777ce13ff2eb759677bbbf30ec4ab38662ada420;hb=9edc32bbe3f1e2b16ba6694bec8fe7a079eb1d16;hp=5d6925a8a1c8bcc128372ffc5d52ec30ebd473e6;hpb=652cd172b45f1c52e7add1cb63b71c28bd5c0220;p=shibboleth%2Fcpp-xmltooling.git diff --git a/xmltooling/util/XMLHelper.h b/xmltooling/util/XMLHelper.h index 5d6925a..777ce13 100644 --- a/xmltooling/util/XMLHelper.h +++ b/xmltooling/util/XMLHelper.h @@ -20,7 +20,7 @@ * A helper class for working with W3C DOM objects. */ -#if !defined(__xmltooling_xmlhelper_h__) +#ifndef __xmltooling_xmlhelper_h__ #define __xmltooling_xmlhelper_h__ #include @@ -31,6 +31,33 @@ using namespace xercesc; namespace xmltooling { /** + * RAII wrapper for Xerces resources. + */ + template class XercesJanitor + { + MAKE_NONCOPYABLE(XercesJanitor); + T* m_held; + public: + XercesJanitor(T* resource) : m_held(resource) {} + + ~XercesJanitor() { + if (m_held) + m_held->release(); + } + + /** + * Returns resource held by this object and releases it to the caller. + * + * @return the resource held or NULL + */ + T* release() { + T* ret=m_held; + m_held=NULL; + return ret; + } + }; + + /** * A helper class for working with W3C DOM objects. */ class XMLTOOL_API XMLHelper @@ -42,7 +69,7 @@ namespace xmltooling { * @param e the DOM element * @return true if there is a type, false if not */ - static bool hasXSIType(DOMElement* e); + static bool hasXSIType(const DOMElement* e); /** * Gets the XSI type for a given element if it has one. @@ -50,7 +77,7 @@ namespace xmltooling { * @param e the element * @return the type or null */ - static QName* getXSIType(DOMElement* e); + static QName* getXSIType(const DOMElement* e); /** * Gets the ID attribute of a DOM element. @@ -58,7 +85,7 @@ namespace xmltooling { * @param domElement the DOM element * @return the ID attribute or null if there isn't one */ - static DOMAttr* getIdAttribute(DOMElement* domElement); + static DOMAttr* getIdAttribute(const DOMElement* domElement); /** * Gets the QName for the given DOM node. @@ -66,7 +93,7 @@ namespace xmltooling { * @param domNode the DOM node * @return the QName for the element or null if the element was null */ - static QName* getNodeQName(DOMNode* domNode); + static QName* getNodeQName(const DOMNode* domNode); /** * Constructs a QName from an attributes value. @@ -74,7 +101,7 @@ namespace xmltooling { * @param attribute the attribute with a QName value * @return a QName from an attributes value, or null if the given attribute is null */ - static QName* getAttributeValueAsQName(DOMAttr* attribute); + static QName* getAttributeValueAsQName(const DOMAttr* attribute); /** * Appends the child Element to the parent Element, @@ -87,16 +114,111 @@ namespace xmltooling { static DOMElement* appendChildElement(DOMElement* parentElement, DOMElement* childElement); /** - * Checks the qualified name of an element. + * Checks the qualified name of a node. * - * @param e element to check + * @param n node to check * @param ns namespace to compare with * @param local local name to compare with - * @return true iff the element's qualified name matches the other parameters + * @return true iff the node's qualified name matches the other parameters */ - static bool isElementNamed(const DOMElement* e, const XMLCh* ns, const XMLCh* local) { - return (e && !XMLString::compareString(ns,e->getNamespaceURI()) && !XMLString::compareString(local,e->getLocalName())); + static bool isNodeNamed(const DOMNode* n, const XMLCh* ns, const XMLCh* local) { + return (n && XMLString::equals(local,n->getLocalName()) && XMLString::equals(ns,n->getNamespaceURI())); } + + /** + * Returns the first matching child element of the node if any. + * + * @param n node to check + * @param localName local name to compare with or NULL for any match + * @return the first matching child node of type Element, or NULL + */ + static DOMElement* getFirstChildElement(const DOMNode* n, const XMLCh* localName=NULL); + + /** + * Returns the last matching child element of the node if any. + * + * @param n node to check + * @param localName local name to compare with or NULL for any match + * @return the last matching child node of type Element, or NULL + */ + static DOMElement* getLastChildElement(const DOMNode* n, const XMLCh* localName=NULL); + + /** + * Returns the next matching sibling element of the node if any. + * + * @param n node to check + * @param localName local name to compare with or NULL for any match + * @return the next matching sibling node of type Element, or NULL + */ + static DOMElement* getNextSiblingElement(const DOMNode* n, const XMLCh* localName=NULL); + + /** + * Returns the previous matching sibling element of the node if any. + * + * @param n node to check + * @param localName local name to compare with or NULL for any match + * @return the previous matching sibling node of type Element, or NULL + */ + static DOMElement* getPreviousSiblingElement(const DOMNode* n, const XMLCh* localName=NULL); + + /** + * Returns the first matching child element of the node if any. + * + * @param n node to check + * @param ns namespace to compare with + * @param localName local name to compare with + * @return the first matching child node of type Element, or NULL + */ + static DOMElement* getFirstChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName); + + /** + * Returns the last matching child element of the node if any. + * + * @param n node to check + * @param ns namespace to compare with + * @param localName local name to compare with + * @return the last matching child node of type Element, or NULL + */ + static DOMElement* getLastChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName); + + /** + * Returns the next matching sibling element of the node if any. + * + * @param n node to check + * @param ns namespace to compare with + * @param localName local name to compare with + * @return the next matching sibling node of type Element, or NULL + */ + static DOMElement* getNextSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName); + + /** + * Returns the previous matching sibling element of the node if any. + * + * @param n node to check + * @param ns namespace to compare with + * @param localName local name to compare with + * @return the previous matching sibling node of type Element, or NULL + */ + static DOMElement* getPreviousSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName); + + /** + * Returns the content of the first Text node found in the element, if any. + * This is roughly similar to the DOM getTextContent function, but only + * examples the immediate children of the element. + * + * @param e element to examine + * @return the content of the first Text node found, or NULL + */ + static const XMLCh* getTextContent(const DOMElement* e); + + /** + * Serializes the DOM Element provided into a buffer using UTF-8 encoding and + * the default XML serializer available. No manipulation or formatting is applied. + * + * @param e element to serialize + * @param buf buffer to serialize element into + */ + static void serialize(const DOMElement* e, std::string& buf); }; };