Stream operators (Unicode string operator is an experiment)
authorScott Cantor <cantor.2@osu.edu>
Wed, 22 Nov 2006 18:15:42 +0000 (18:15 +0000)
committerScott Cantor <cantor.2@osu.edu>
Wed, 22 Nov 2006 18:15:42 +0000 (18:15 +0000)
xmltooling/soap/impl/SOAPClient.cpp
xmltooling/unicode.cpp
xmltooling/unicode.h
xmltooling/util/XMLHelper.cpp
xmltooling/util/XMLHelper.h

index f8a84b5..01583a3 100644 (file)
@@ -56,7 +56,7 @@ void SOAPClient::send(const Envelope* env, const KeyInfoSource& peer, const char
     
     // Serialize envelope.
     stringstream s;
-    XMLHelper::serialize(env->marshall(), s);
+    s << *env;
     
     // Send to peer.
     m_transport->send(s);
index dd8a45e..1101cf2 100644 (file)
@@ -57,3 +57,13 @@ XMLCh* xmltooling::fromUTF8(const char* src)
     delete[] sizes;
     return buf;
 }
+
+std::ostream& xmltooling::operator<<(std::ostream& ostr, const XMLCh* s)
+{
+    if (s) {
+        char* p=xmltooling::toUTF8(s);
+        ostr << p;
+        delete[] p;
+    }
+    return ostr;
+}
index f8888d3..163b8d3 100644 (file)
@@ -26,6 +26,7 @@
 #include <xmltooling/base.h>
 
 #include <string>
+#include <iostream>
 #include <xercesc/util/XMLString.hpp>
 
 using namespace xercesc;
@@ -55,6 +56,15 @@ namespace xmltooling {
     extern XMLTOOL_API XMLCh* fromUTF8(const char* src);
 
     /**
+     * Writes a Unicode string to an ASCII stream by transcoding to UTF8.
+     * 
+     * @param ostr  stream to write to
+     * @param s     string to write
+     * @return      reference to output stream
+     */
+    extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const XMLCh* s);
+
+    /**
      * A minimal auto_ptr-like class that can copy or transcode a buffer into
      * the local code page and free the result automatically.
      * 
index d6506a0..7932644 100644 (file)
@@ -29,6 +29,7 @@
 #include <xercesc/util/XMLUniDefs.hpp>
 
 using namespace xmltooling;
+using std::ostream;
 
 static const XMLCh type[]={chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull };
     
@@ -213,7 +214,7 @@ DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n, const XMLCh*
     return e;
 }
 
-void XMLHelper::serialize(const DOMElement* e, std::string& buf)
+void XMLHelper::serialize(const DOMNode* n, std::string& buf)
 {
     static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
     static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
@@ -222,7 +223,7 @@ void XMLHelper::serialize(const DOMElement* e, std::string& buf)
     XercesJanitor<DOMWriter> janitor(serializer);
     serializer->setEncoding(UTF8);
     MemBufFormatTarget target;
-    if (!serializer->writeNode(&target,*e))
+    if (!serializer->writeNode(&target,*n))
         throw XMLParserException("unable to serialize XML");
     buf.erase();
     buf.append(reinterpret_cast<const char*>(target.getRawBuffer()),target.getLen());
@@ -248,7 +249,7 @@ namespace {
     };
 };
 
-void XMLHelper::serialize(const DOMElement* e, std::ostream& out)
+void XMLHelper::serialize(const DOMNode* n, std::ostream& out)
 {
     static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
     static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
@@ -257,6 +258,17 @@ void XMLHelper::serialize(const DOMElement* e, std::ostream& out)
     XercesJanitor<DOMWriter> janitor(serializer);
     serializer->setEncoding(UTF8);
     StreamFormatTarget target(out);
-    if (!serializer->writeNode(&target,*e))
+    if (!serializer->writeNode(&target,*n))
         throw XMLParserException("unable to serialize XML");
 }
+
+ostream& xmltooling::operator<<(ostream& ostr, const DOMNode& node)
+{
+    XMLHelper::serialize(&node, ostr);
+    return ostr;
+}
+
+ostream& xmltooling::operator<<(ostream& ostr, const XMLObject& obj)
+{
+    return ostr << obj.marshall();
+}
index c09d3d9..92d2f2f 100644 (file)
@@ -23,7 +23,7 @@
 #ifndef __xmltooling_xmlhelper_h__
 #define __xmltooling_xmlhelper_h__
 
-#include <xmltooling/QName.h>
+#include <xmltooling/XMLObject.h>
 #include <xercesc/dom/DOM.hpp>
 
 #include <iostream>
@@ -214,24 +214,45 @@ namespace xmltooling {
         static const XMLCh* getTextContent(const 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 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
          */
-        static void serialize(const DOMElement* e, std::ostream& out);
+        static void serialize(const 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 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__ */