2 * Copyright 2001-2007 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include "exceptions.h"
25 #include "XMLToolingConfig.h"
26 #include "util/URLEncoder.h"
27 #include "util/XMLConstants.h"
28 #include "util/XMLHelper.h"
32 #include <xercesc/util/XMLUniDefs.hpp>
34 using namespace xmltooling;
36 using xmlconstants::XMLTOOLING_NS;
38 params::params(int count,...)
43 v.push_back(va_arg(args,char*));
47 namedparams::namedparams(int count,...)
53 v.push_back(va_arg(args,char*));
57 XMLToolingException::ExceptionFactoryMap XMLToolingException::m_factoryMap;
59 XMLToolingException* XMLToolingException::getInstance(const char* exceptionClass)
62 ExceptionFactoryMap::const_iterator i=m_factoryMap.find(exceptionClass);
63 if (i!=m_factoryMap.end())
66 return new XMLToolingException();
69 XMLToolingException::XMLToolingException(const char* msg, const params& p)
76 XMLToolingException::XMLToolingException(const char* msg, const namedparams& p)
83 XMLToolingException::XMLToolingException(const std::string& msg, const params& p) : m_msg(msg)
88 XMLToolingException::XMLToolingException(const std::string& msg, const namedparams& p) : m_msg(msg)
93 void XMLToolingException::setMessage(const char* msg)
99 m_processedmsg.erase();
102 inline const char* get_digit_character()
104 static const char s_characters[19] =
126 static const char *s_mid = s_characters + 9;
131 inline const char* unsigned_integer_to_string(char* buf, size_t cchBuf, int i)
133 char* psz=buf + cchBuf - 1; // Set psz to last char
134 *psz = 0; // Set terminating null
137 unsigned int lsd = i % 10; // Get least significant
140 i /= 10; // Prepare for next most
145 *psz = get_digit_character()[lsd]; // Place the digit
147 } while(i!=0 && psz>buf);
152 void XMLToolingException::addProperties(const params& p)
154 m_processedmsg.erase();
155 int i=m_params.size()+1;
157 const vector<const char*>& v=p.get();
158 for (vector<const char*>::const_iterator ci=v.begin(); ci!=v.end(); ci++) {
159 m_params[unsigned_integer_to_string(buf,sizeof(buf),i++)] = *ci;
163 void XMLToolingException::addProperties(const namedparams& p)
165 m_processedmsg.erase();
166 const vector<const char*>& v=p.get();
167 for (vector<const char*>::const_iterator ci=v.begin(); ci!=v.end(); ci++) {
169 m_params[*ci] = *(ci+1);
170 ci++; // advance past name to value, then loop will advance it again
174 const char* XMLToolingException::getProperty(unsigned int index) const
177 map<string,string>::const_iterator i=m_params.find(unsigned_integer_to_string(buf,sizeof(buf),index));
178 return (i==m_params.end()) ? NULL : i->second.c_str();
181 const char* XMLToolingException::getProperty(const char* name) const
183 map<string,string>::const_iterator i=m_params.find(name);
184 return (i==m_params.end()) ? NULL : i->second.c_str();
187 const char* XMLToolingException::getMessage() const
189 if (!m_processedmsg.empty())
190 return m_processedmsg.c_str();
191 else if (m_params.empty())
192 return m_msg.c_str();
194 static const char* legal="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_";
196 // Replace any parameters in the message.
197 string::size_type i=0,start=0;
198 while (start!=string::npos && start<m_msg.length() && (i=m_msg.find("$",start))!=string::npos) {
200 m_processedmsg += m_msg.substr(start,i-start); // append everything in between
201 start=i+1; // move start to the beginning of the token name
202 i=m_msg.find_first_not_of(legal,start); // find token delimiter
203 if (i==start) { // append a non legal character
204 m_processedmsg+=m_msg[start++];
208 // search for token in map
209 map<string,string>::const_iterator param=m_params.find(m_msg.substr(start,(i==string::npos) ? i : i-start));
210 if (param!=m_params.end()) {
211 m_processedmsg+=param->second;
215 if (start!=string::npos && start<m_msg.length())
216 m_processedmsg += m_msg.substr(start,i); // append rest of string
217 return m_processedmsg.c_str();
220 void xml_encode(string& s, const char* pre, const char* start, const char* post)
224 while (start && *start) {
225 pos = strcspn(start, "\"<>&");
227 s.append(start, pos);
232 case '\'': s += "'"; break;
233 case '<': s += "<"; break;
234 case '>': s += ">"; break;
235 case '&': s += "&"; break;
236 default: s += *start;
244 string XMLToolingException::toString() const
246 string xml=string("<exception xmlns='http://www.opensaml.org/xmltooling' type='") + getClassName() + "'>";
247 const char* msg=getMessage();
249 xml_encode(xml, "<message>", msg, "</message>");
250 for (map<string,string>::const_iterator i=m_params.begin(); i!=m_params.end(); i++) {
251 xml_encode(xml, "<param name='", i->first.c_str(), "'");
252 xml_encode(xml, ">", i->second.c_str(), "</param>");
258 string XMLToolingException::toQueryString() const
261 const URLEncoder* enc = XMLToolingConfig::getConfig().getURLEncoder();
262 for (map<string,string>::const_iterator i=m_params.begin(); i!=m_params.end(); i++) {
265 q = q + i->first + '=' + enc->encode(i->second.c_str());
270 XMLToolingException* XMLToolingException::fromStream(std::istream& in)
272 static const XMLCh exception[] = UNICODE_LITERAL_9(e,x,c,e,p,t,i,o,n);
273 static const XMLCh message[] = UNICODE_LITERAL_7(m,e,s,s,a,g,e);
274 static const XMLCh name[] = UNICODE_LITERAL_4(n,a,m,e);
275 static const XMLCh param[] = UNICODE_LITERAL_5(p,a,r,a,m);
276 static const XMLCh type[] = UNICODE_LITERAL_4(t,y,p,e);
278 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
280 // Check root element.
281 const DOMElement* root=doc->getDocumentElement();
282 if (!XMLHelper::isNodeNamed(root,XMLTOOLING_NS,exception)) {
284 throw XMLToolingException("Invalid root element on serialized exception.");
287 auto_ptr_char classname(root->getAttributeNS(NULL,type));
288 auto_ptr<XMLToolingException> excep(XMLToolingException::getInstance(classname.get()));
290 DOMElement* child=XMLHelper::getFirstChildElement(root,XMLTOOLING_NS,message);
291 if (child && child->hasChildNodes()) {
292 auto_ptr_char m(child->getFirstChild()->getNodeValue());
293 excep->setMessage(m.get());
296 child=XMLHelper::getFirstChildElement(root,XMLTOOLING_NS,param);
297 while (child && child->hasChildNodes()) {
298 auto_ptr_char n(child->getAttributeNS(NULL,name));
299 char* v=toUTF8(child->getFirstChild()->getNodeValue());
301 excep->addProperty(n.get(), v);
303 child=XMLHelper::getNextSiblingElement(child,XMLTOOLING_NS,param);
307 return excep.release();
310 XMLToolingException* XMLToolingException::fromString(const char* s)
313 return fromStream(in);