X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=xmltooling%2Fexceptions.cpp;h=d72c27a62a3a405c872d3a2acaec4030ccf1fa5a;hb=ab362f9c3195134452702c1ba7108258e81d8841;hp=6002fb5354da43324ce19681bf698fbdd4b401fa;hpb=a34a7db1f348229bb80e0da8171d3b3692b4a8cf;p=shibboleth%2Fcpp-xmltooling.git diff --git a/xmltooling/exceptions.cpp b/xmltooling/exceptions.cpp index 6002fb5..d72c27a 100644 --- a/xmltooling/exceptions.cpp +++ b/xmltooling/exceptions.cpp @@ -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. + * + * 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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. + * 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. */ /** @@ -28,10 +32,12 @@ #include "util/XMLHelper.h" #include +#include #include #include 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::size_type i=m_params.size()+1; char buf[20]; const vector& v=p.get(); for (vector::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::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::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 @@ -217,35 +223,64 @@ const char* XMLToolingException::getMessage() const return m_processedmsg.c_str(); } +void xml_encode(string& s, const char* pre, const char* start, const char* post) +{ + s += pre; + size_t pos; + while (start && *start) { + pos = strcspn(start, "\"<>&"); + if (pos > 0) { + s.append(start, pos); + start += pos; + } + else { + switch (*start) { + case '\'': s += "'"; break; + case '<': s += "<"; break; + case '>': s += ">"; break; + case '&': s += "&"; break; + default: s += *start; + } + start++; + } + } + s += post; +} + string XMLToolingException::toString() const { - string xml=string(""; + string xml=string(""; const char* msg=getMessage(); if (msg) - xml=xml + "" + msg + ""; - for (map::const_iterator i=m_params.begin(); i!=m_params.end(); i++) - xml=xml + "first + "\">" + i->second + ""; + xml_encode(xml, "", msg, ""); + const URLEncoder* encoder = XMLToolingConfig::getConfig().getURLEncoder(); + for (map::const_iterator i=m_params.begin(); i!=m_params.end(); i++) { + xml_encode(xml, "", encoder->encode(i->second.c_str()).c_str(), ""); + } xml+=""; return xml; } string XMLToolingException::toQueryString() const { + string q; const URLEncoder* enc = XMLToolingConfig::getConfig().getURLEncoder(); - string q("errorType="); - q = q + enc->encode(getClassName()) + "&errorText=" + enc->encode(what()); - for (map::const_iterator i=m_params.begin(); i!=m_params.end(); i++) - q = q + '&' + i->first + '=' + enc->encode(i->second.c_str()); + for (map::const_iterator i=m_params.begin(); i!=m_params.end(); i++) { + if (!q.empty()) + q += '&'; + q = q + i->first + '=' + enc->encode(i->second.c_str()); + } return q; } XMLToolingException* XMLToolingException::fromStream(std::istream& in) { - static const XMLCh exception[] = { chLatin_e, chLatin_x, chLatin_c, chLatin_e, chLatin_p, chLatin_t, chLatin_i, chLatin_o, chLatin_n, chNull }; - static const XMLCh message[] = { chLatin_m, chLatin_e, chLatin_s, chLatin_s, chLatin_a, chLatin_g, chLatin_e, chNull }; - static const XMLCh name[] = { chLatin_n, chLatin_a, chLatin_m, chLatin_e, chNull }; - static const XMLCh param[] = { chLatin_p, chLatin_a, chLatin_r, chLatin_a, chLatin_m, chNull }; - static const XMLCh type[] = { chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull }; + static const XMLCh exception[] = UNICODE_LITERAL_9(e,x,c,e,p,t,i,o,n); + static const XMLCh message[] = UNICODE_LITERAL_7(m,e,s,s,a,g,e); + static const XMLCh name[] = UNICODE_LITERAL_4(n,a,m,e); + static const XMLCh param[] = UNICODE_LITERAL_5(p,a,r,a,m); + static const XMLCh type[] = UNICODE_LITERAL_4(t,y,p,e); DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in); @@ -256,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 excep(XMLToolingException::getInstance(classname.get())); DOMElement* child=XMLHelper::getFirstChildElement(root,XMLTOOLING_NS,message); @@ -265,14 +300,17 @@ 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); - XMLString::release(&v); - child=XMLHelper::getNextSiblingElement(root,XMLTOOLING_NS,param); + 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); } doc->release();