Stop defaulting in xercesc namespace.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / XMLHelper.h
index c09d3d9..a3b587b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2006 Internet2
+ *  Copyright 2001-2007 Internet2
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #ifndef __xmltooling_xmlhelper_h__
 #define __xmltooling_xmlhelper_h__
 
-#include <xmltooling/QName.h>
+#include <xmltooling/XMLObject.h>
 #include <xercesc/dom/DOM.hpp>
 
 #include <iostream>
 
-using namespace xercesc;
-
 namespace xmltooling {
     
     /**
@@ -40,6 +38,11 @@ namespace xmltooling {
         MAKE_NONCOPYABLE(XercesJanitor);
         T* m_held;
     public:
+        /**
+         * Constructor
+         * 
+         * @param resource  object to release when leaving scope
+         */
         XercesJanitor(T* resource) : m_held(resource) {}
         
         ~XercesJanitor() {
@@ -48,6 +51,24 @@ namespace xmltooling {
         }
         
         /**
+         * Returns resource held by this object.
+         * 
+         * @return  the resource held or NULL
+         */
+        T* get() {
+            return m_held;
+        }
+
+        /**
+         * Returns resource held by this object.
+         * 
+         * @return  the resource held or NULL
+         */
+        T* operator->() {
+            return m_held;
+        }
+
+        /**
          * Returns resource held by this object and releases it to the caller.
          * 
          * @return  the resource held or NULL
@@ -71,7 +92,7 @@ namespace xmltooling {
          * @param e the DOM element
          * @return true if there is a type, false if not
          */
-        static bool hasXSIType(const DOMElement* e);
+        static bool hasXSIType(const xercesc::DOMElement* e);
 
         /**
          * Gets the XSI type for a given element if it has one.
@@ -79,7 +100,7 @@ namespace xmltooling {
          * @param e the element
          * @return the type or null
          */
-        static QName* getXSIType(const DOMElement* e);
+        static QName* getXSIType(const xercesc::DOMElement* e);
 
         /**
          * Gets the ID attribute of a DOM element.
@@ -87,7 +108,18 @@ namespace xmltooling {
          * @param domElement the DOM element
          * @return the ID attribute or null if there isn't one
          */
-        static DOMAttr* getIdAttribute(const DOMElement* domElement);
+        static xercesc::DOMAttr* getIdAttribute(const xercesc::DOMElement* domElement);
+
+        /**
+         * Attempts to locate an XMLObject from this point downward in the tree whose
+         * XML ID matches the supplied value.
+         * 
+         * @param tree  root of tree to search
+         * @param id    ID value to locate
+         * @return XMLObject in the tree with a matching ID value, or NULL
+         */
+        static const XMLObject* getXMLObjectById(const XMLObject& tree, const XMLCh* id);
+        
 
         /**
          * Gets the QName for the given DOM node.
@@ -95,7 +127,7 @@ namespace xmltooling {
          * @param domNode the DOM node
          * @return the QName for the element or null if the element was null
          */
-        static QName* getNodeQName(const DOMNode* domNode);
+        static QName* getNodeQName(const xercesc::DOMNode* domNode);
 
         /**
          * Constructs a QName from an attribute's value.
@@ -103,7 +135,7 @@ namespace xmltooling {
          * @param attribute the attribute with a QName value
          * @return a QName from an attribute's value, or null if the given attribute is null
          */
-        static QName* getAttributeValueAsQName(const DOMAttr* attribute);
+        static QName* getAttributeValueAsQName(const xercesc::DOMAttr* attribute);
 
         /**
          * Appends the child Element to the parent Element,
@@ -113,7 +145,7 @@ namespace xmltooling {
          * @param childElement the child Element
          * @return the child Element that was added (may be an imported copy)
          */
-        static DOMElement* appendChildElement(DOMElement* parentElement, DOMElement* childElement);
+        static xercesc::DOMElement* appendChildElement(xercesc::DOMElement* parentElement, xercesc::DOMElement* childElement);
         
         /**
          * Checks the qualified name of a node.
@@ -123,8 +155,8 @@ namespace xmltooling {
          * @param local local name to compare with
          * @return  true iff the node's qualified name matches the other parameters
          */
-        static bool isNodeNamed(const DOMNode* n, const XMLCh* ns, const XMLCh* local) {
-            return (n && XMLString::equals(local,n->getLocalName()) && XMLString::equals(ns,n->getNamespaceURI()));
+        static bool isNodeNamed(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* local) {
+            return (n && xercesc::XMLString::equals(local,n->getLocalName()) && xercesc::XMLString::equals(ns,n->getNamespaceURI()));
         }
 
         /**
@@ -134,7 +166,7 @@ namespace xmltooling {
          * @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);
+        static xercesc::DOMElement* getFirstChildElement(const xercesc::DOMNode* n, const XMLCh* localName=NULL);
         
         /**
          * Returns the last matching child element of the node if any.
@@ -143,7 +175,7 @@ namespace xmltooling {
          * @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);
+        static xercesc::DOMElement* getLastChildElement(const xercesc::DOMNode* n, const XMLCh* localName=NULL);
         
         /**
          * Returns the next matching sibling element of the node if any.
@@ -152,7 +184,7 @@ namespace xmltooling {
          * @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);
+        static xercesc::DOMElement* getNextSiblingElement(const xercesc::DOMNode* n, const XMLCh* localName=NULL);
         
         /**
          * Returns the previous matching sibling element of the node if any.
@@ -161,7 +193,7 @@ namespace xmltooling {
          * @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);
+        static xercesc::DOMElement* getPreviousSiblingElement(const xercesc::DOMNode* n, const XMLCh* localName=NULL);
         
         /**
          * Returns the first matching child element of the node if any.
@@ -171,7 +203,7 @@ namespace xmltooling {
          * @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);
+        static xercesc::DOMElement* getFirstChildElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
         
         /**
          * Returns the last matching child element of the node if any.
@@ -181,7 +213,7 @@ namespace xmltooling {
          * @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);
+        static xercesc::DOMElement* getLastChildElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
         
         /**
          * Returns the next matching sibling element of the node if any.
@@ -191,7 +223,7 @@ namespace xmltooling {
          * @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);
+        static xercesc::DOMElement* getNextSiblingElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
         
         /**
          * Returns the previous matching sibling element of the node if any.
@@ -201,7 +233,7 @@ namespace xmltooling {
          * @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);
+        static xercesc::DOMElement* getPreviousSiblingElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
 
         /**
          * Returns the content of the first Text node found in the element, if any.
@@ -211,27 +243,49 @@ namespace xmltooling {
          * @param e     element to examine
          * @return the content of the first Text node found, or NULL
          */
-        static const XMLCh* getTextContent(const DOMElement* e);
+        static const XMLCh* getTextContent(const xercesc::DOMElement* e);
 
         /**
-         * Serializes the DOM Element provided into a buffer using UTF-8 encoding and
+         * Serializes the DOM node 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 n     node to serialize
          * @param buf   buffer to serialize element into
          */
-        static void serialize(const DOMElement* e, std::string& buf);
+        static void serialize(const xercesc::DOMNode* n, std::string& buf);
 
         /**
-         * Serializes the DOM Element provided to a stream using UTF-8 encoding and
+         * Serializes the DOM node provided to a stream using UTF-8 encoding and
          * the default XML serializer available. No manipulation or formatting is applied.
          * 
-         * @param e     element to serialize
+         * @param n     node to serialize
          * @param out   stream to serialize element into
+         * @return reference to output stream
          */
-        static void serialize(const DOMElement* e, std::ostream& out);
+        static std::ostream& serialize(const xercesc::DOMNode* n, std::ostream& out);
     };
 
+    /**
+     * Serializes the DOM node provided to a stream using UTF-8 encoding and
+     * the default XML serializer available. No manipulation or formatting is applied.
+     * 
+     * @param n      node to serialize
+     * @param ostr   stream to serialize element into
+     * @return reference to output stream
+     */
+    extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const xercesc::DOMNode& n);
+
+    /**
+     * Marshalls and serializes the XMLObject provided to a stream using UTF-8 encoding and
+     * the default XML serializer available. No manipulation or formatting is applied.
+     * 
+     * <p>The marshaller operation takes no parameters.
+     * 
+     * @param obj    object to serialize
+     * @param ostr   stream to serialize object into
+     * @return reference to output stream
+     */
+    extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const XMLObject& obj);
 };
 
 #endif /* __xmltooling_xmlhelper_h__ */