From c76965e640ade93cadffe03fdbb8f0f8290e3878 Mon Sep 17 00:00:00 2001 From: Scott Cantor Date: Wed, 22 Nov 2006 18:15:42 +0000 Subject: [PATCH] Stream operators (Unicode string operator is an experiment) --- xmltooling/soap/impl/SOAPClient.cpp | 2 +- xmltooling/unicode.cpp | 10 ++++++++++ xmltooling/unicode.h | 10 ++++++++++ xmltooling/util/XMLHelper.cpp | 20 ++++++++++++++++---- xmltooling/util/XMLHelper.h | 35 ++++++++++++++++++++++++++++------- 5 files changed, 65 insertions(+), 12 deletions(-) diff --git a/xmltooling/soap/impl/SOAPClient.cpp b/xmltooling/soap/impl/SOAPClient.cpp index f8a84b5..01583a3 100644 --- a/xmltooling/soap/impl/SOAPClient.cpp +++ b/xmltooling/soap/impl/SOAPClient.cpp @@ -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); diff --git a/xmltooling/unicode.cpp b/xmltooling/unicode.cpp index dd8a45e..1101cf2 100644 --- a/xmltooling/unicode.cpp +++ b/xmltooling/unicode.cpp @@ -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; +} diff --git a/xmltooling/unicode.h b/xmltooling/unicode.h index f8888d3..163b8d3 100644 --- a/xmltooling/unicode.h +++ b/xmltooling/unicode.h @@ -26,6 +26,7 @@ #include #include +#include #include 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. * diff --git a/xmltooling/util/XMLHelper.cpp b/xmltooling/util/XMLHelper.cpp index d6506a0..7932644 100644 --- a/xmltooling/util/XMLHelper.cpp +++ b/xmltooling/util/XMLHelper.cpp @@ -29,6 +29,7 @@ #include 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 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(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 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(); +} diff --git a/xmltooling/util/XMLHelper.h b/xmltooling/util/XMLHelper.h index c09d3d9..92d2f2f 100644 --- a/xmltooling/util/XMLHelper.h +++ b/xmltooling/util/XMLHelper.h @@ -23,7 +23,7 @@ #ifndef __xmltooling_xmlhelper_h__ #define __xmltooling_xmlhelper_h__ -#include +#include #include #include @@ -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. + * + *

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__ */ -- 2.1.4