Merge branch '1.x' of ssh://authdev.it.ohio-state.edu/~scantor/git/cpp-xmltooling...
[shibboleth/cpp-xmltooling.git] / xmltooling / exceptions.cpp
index 4b15451..d72c27a 100644 (file)
@@ -1,17 +1,21 @@
-/*
- *  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.
- * You may obtain a copy of the License at
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
  */
 
 /**
 #include "util/XMLHelper.h"
 
 #include <stdarg.h>
+#include <memory>
 #include <sstream>
 #include <xercesc/util/XMLUniDefs.hpp>
 
 using namespace xmltooling;
+using namespace xercesc;
 using namespace std;
 using xmlconstants::XMLTOOLING_NS;
 
@@ -128,13 +134,13 @@ inline const char* get_digit_character()
     return s_mid;
 }
 
-inline const char* unsigned_integer_to_string(char* buf, size_t cchBuf, int i)
+inline const char* unsigned_integer_to_string(char* buf, size_t cchBuf, size_t i)
 {
     char* psz=buf + cchBuf - 1;     // Set psz to last char
     *psz = 0;                       // Set terminating null
 
     do {
-        unsigned int lsd = i % 10;  // Get least significant
+        size_t lsd = i % 10;  // Get least significant
                                     // digit
 
         i /= 10;                    // Prepare for next most
@@ -152,7 +158,7 @@ inline const char* unsigned_integer_to_string(char* buf, size_t cchBuf, int i)
 void XMLToolingException::addProperties(const params& p)
 {
     m_processedmsg.erase();
-    int i=m_params.size()+1;
+    map<string,string>::size_type i=m_params.size()+1;
     char buf[20];
     const vector<const char*>& v=p.get();
     for (vector<const char*>::const_iterator ci=v.begin(); ci!=v.end(); ci++) {
@@ -175,13 +181,13 @@ const char* XMLToolingException::getProperty(unsigned int index) const
 {
     char buf[20];
     map<string,string>::const_iterator i=m_params.find(unsigned_integer_to_string(buf,sizeof(buf),index));
-    return (i==m_params.end()) ? NULL : i->second.c_str();
+    return (i==m_params.end()) ? nullptr : i->second.c_str();
 }
 
 const char* XMLToolingException::getProperty(const char* name) const
 {
     map<string,string>::const_iterator i=m_params.find(name);
-    return (i==m_params.end()) ? NULL : i->second.c_str();
+    return (i==m_params.end()) ? nullptr : i->second.c_str();
 }
 
 const char* XMLToolingException::getMessage() const
@@ -247,9 +253,10 @@ string XMLToolingException::toString() const
     const char* msg=getMessage();
     if (msg)
         xml_encode(xml, "<message>", msg, "</message>");
+    const URLEncoder* encoder = XMLToolingConfig::getConfig().getURLEncoder();
     for (map<string,string>::const_iterator i=m_params.begin(); i!=m_params.end(); i++) {
         xml_encode(xml, "<param name='", i->first.c_str(), "'");
-        xml_encode(xml, ">", i->second.c_str(), "</param>");
+        xml_encode(xml, ">", encoder->encode(i->second.c_str()).c_str(), "</param>");
     }
     xml+="</exception>";
     return xml;
@@ -284,7 +291,7 @@ XMLToolingException* XMLToolingException::fromStream(std::istream& in)
         throw XMLToolingException("Invalid root element on serialized exception.");
     }
     
-    auto_ptr_char classname(root->getAttributeNS(NULL,type));
+    auto_ptr_char classname(root->getAttributeNS(nullptr,type));
     auto_ptr<XMLToolingException> excep(XMLToolingException::getInstance(classname.get()));
     
     DOMElement* child=XMLHelper::getFirstChildElement(root,XMLTOOLING_NS,message);
@@ -293,13 +300,16 @@ XMLToolingException* XMLToolingException::fromStream(std::istream& in)
         excep->setMessage(m.get());
     }
     
+    const URLEncoder* encoder = XMLToolingConfig::getConfig().getURLEncoder();
     child=XMLHelper::getFirstChildElement(root,XMLTOOLING_NS,param);
     while (child && child->hasChildNodes()) {
-        auto_ptr_char n(child->getAttributeNS(NULL,name));
-        char* v=toUTF8(child->getFirstChild()->getNodeValue());
-        if (n.get() && v)
-            excep->addProperty(n.get(), v);
-        delete[] v;
+        auto_ptr_char n(child->getAttributeNS(nullptr,name));
+        char* encoded = XMLString::transcode(child->getFirstChild()->getNodeValue());
+        if (n.get() && encoded) {
+            encoder->decode(encoded);
+            excep->addProperty(n.get(), encoded);
+        }
+        XMLString::release(&encoded);
         child=XMLHelper::getNextSiblingElement(child,XMLTOOLING_NS,param);
     }