[cpp-xmltooling] branch master updated: CPPXT-126 Move Xml Encoding into xmltooling::XMLHelper

Rod Widdowson rdw at steadingsoftware.com
Thu Jan 25 15:19:31 EST 2018


This is an automated email from the git hooks/post-receive script.

rdw pushed a commit to branch master
in repository cpp-xmltooling.

View the commit online:
http://git.shibboleth.net/view/?p=cpp-xmltooling.git;a=commit;h=211d59c0ea1869253fd65d968e84026b9bd2264d

The following commit(s) were added to refs/heads/master by this push:
       new  211d59c   CPPXT-126  Move Xml Encoding into xmltooling::XMLHelper
211d59c is described below

commit 211d59c0ea1869253fd65d968e84026b9bd2264d
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Thu Jan 25 20:18:34 2018 +0000

    CPPXT-126  Move Xml Encoding into xmltooling::XMLHelper
    
    https://issues.shibboleth.net/jira/browse/CPPXT-126
---
 xmltooling/util/XMLHelper.cpp   | 32 ++++++++++++++++++++++++++++++++
 xmltooling/util/XMLHelper.h     | 20 ++++++++++++++++++++
 xmltoolingtest/xmltoolingtest.h |  5 +++++
 3 files changed, 57 insertions(+)

diff --git a/xmltooling/util/XMLHelper.cpp b/xmltooling/util/XMLHelper.cpp
index df2ffc1..9214f30 100644
--- a/xmltooling/util/XMLHelper.cpp
+++ b/xmltooling/util/XMLHelper.cpp
@@ -32,6 +32,7 @@
 #include "util/XMLHelper.h"
 #include "util/XMLConstants.h"
 
+#include <strstream>
 #include <boost/lambda/bind.hpp>
 #include <boost/lambda/if.hpp>
 #include <boost/lambda/lambda.hpp>
@@ -396,6 +397,37 @@ bool XMLHelper::getCaseSensitive(const xercesc::DOMElement* e, bool defValue, co
     return result;
 }
 
+void XMLHelper::encode(std::ostream& os, const char* str)
+{
+    size_t pos;
+    while (str && *str) {
+        pos = strcspn(str, "\"<>&");
+        if (pos > 0) {
+            os.write(str, pos);
+            str += pos;
+        }
+        else {
+            switch (*str) {
+            case '"':   os << """;     break;
+            case '<':   os << "<";       break;
+            case '>':   os << ">";       break;
+            case '&':   os << "&";      break;
+            default:    os << *str;
+            }
+            str++;
+        }
+    }
+}
+
+std::string XMLHelper::encode(const char* str)
+{
+    ostrstream stream;
+
+    encode(stream, str);
+    stream << ends;
+    return std::string(stream.str());
+}
+
 void XMLHelper::serialize(const DOMNode* n, std::string& buf, bool pretty)
 {
     static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
diff --git a/xmltooling/util/XMLHelper.h b/xmltooling/util/XMLHelper.h
index fd57f8a..424885d 100644
--- a/xmltooling/util/XMLHelper.h
+++ b/xmltooling/util/XMLHelper.h
@@ -347,6 +347,26 @@ namespace xmltooling {
             );
 
         /**
+         *
+         * Perform XMLEncoding on the inout string into the provided stream.
+         * Handled (output) symbols are " < > &
+         *
+         * @param os  where to put the encoded string
+         * @param str what to encode
+         */
+        static void encode(std::ostream& os, const char* str);
+
+        /**
+        *
+        * Perform XMLEncoding on the inout string into the provided stream.
+        * Handled (output) symbols are " < > &
+        *
+        * @param  str what to encode
+        * @return the encoded input
+        */
+        static std::string encode(const char* str);
+
+        /**
          * Serializes the DOM node provided into a buffer using UTF-8 encoding and
          * the default XML serializer available. No manipulation or formatting is applied.
          *
diff --git a/xmltoolingtest/xmltoolingtest.h b/xmltoolingtest/xmltoolingtest.h
index 6229a1a..e89ed2b 100644
--- a/xmltoolingtest/xmltoolingtest.h
+++ b/xmltoolingtest/xmltoolingtest.h
@@ -165,6 +165,11 @@ public:
         el = XMLHelper::getFirstChildElement(parent, Test, Default);
         TS_ASSERT(!XMLHelper::getCaseSensitive(el, false));
         TS_ASSERT(XMLHelper::getCaseSensitive(el, true));
+
+        static const std::string input("&thing>thong\"<thang");
+        static const std::string output("&thing>thong"<thang");
+        TS_ASSERT(output == XMLHelper::encode(input.c_str()));
+
     }
 };
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list