2 * Copyright 2001-2006 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
18 * @file exceptions.h
\r
23 #ifndef __xmltooling_exceptions_h__
\r
24 #define __xmltooling_exceptions_h__
\r
30 #include <xmltooling/base.h>
\r
33 * Declares a derived exception class
\r
35 * @param name the exception class
\r
36 * @param base the base class
\r
39 #define DECL_XMLTOOLING_EXCEPTION(name,base,desc) \
\r
41 class XMLTOOL_EXCEPTIONAPI(XMLTOOL_API) name : public xmltooling::base { \
\r
43 /##** base##::##base(const char*,const xmltooling::params&) */ \
\r
44 name(const char* msg=NULL, const xmltooling::params& p=xmltooling::params()) \
\r
45 : xmltooling::base(msg,p) {} \
\r
46 /##** base##::##base(const char*,const xmltooling::namedparams&) */ \
\r
47 name(const char* msg, const xmltooling::namedparams& p) \
\r
48 : xmltooling::base(msg,p) {} \
\r
49 /##** base##::##base(const std::string&,const xmltooling::params&) */ \
\r
50 name(const std::string& msg, const xmltooling::params& p=xmltooling::params()) \
\r
51 : xmltooling::base(msg,p) {} \
\r
52 /##** base##::##base(const std::string&,const xmltooling::namedparams&) */ \
\r
53 name(const std::string& msg, const xmltooling::namedparams& p) \
\r
54 : xmltooling::base(msg,p) {} \
\r
55 virtual ~name() {} \
\r
56 virtual const char* getClassName() const { return "xmltooling::"#name; } \
\r
57 void raise() const {throw *this;} \
\r
61 * Declares a factory function for an exception class.
\r
63 * @param name the exception class name
\r
65 #define DECL_EXCEPTION_FACTORY(name) \
\r
66 xmltooling::XMLToolingException* name##Factory() \
\r
68 return new xmltooling::name(); \
\r
72 * Registers a factory for an exception class.
\r
74 * @param name the exception class name
\r
76 #define REGISTER_EXCEPTION_FACTORY(name) XMLToolingException::registerFactory("xmltooling::"#name,name##Factory)
\r
78 #if defined (_MSC_VER)
\r
79 #pragma warning( push )
\r
80 #pragma warning( disable : 4250 4251 )
\r
83 namespace xmltooling {
\r
86 * Wrapper around a variable number of arguments.
\r
88 class XMLTOOL_API params
\r
92 * Initializes with zero parameters.
\r
97 * Initializes the parameter set.
\r
99 * @param count the number of parameters that follow
\r
101 params(int count,...);
\r
104 * Returns an immutable reference to the set of parameters.
\r
106 * @return the parameter set
\r
108 const std::vector<const char*>& get() const {return v;}
\r
111 /** Contains the parameters being passed. */
\r
112 std::vector<const char*> v;
\r
116 * Wrapper around a variable number of name/value pairs.
\r
118 class XMLTOOL_API namedparams : public params
\r
122 * Initializes with zero parameters.
\r
127 * Initializes the named parameter set.
\r
129 * @param count the number of name/value pairs that follow (must be even)
\r
131 namedparams(int count,...);
\r
134 class XMLTOOL_EXCEPTIONAPI(XMLTOOL_API) XMLToolingException;
\r
136 /** A factory function that returns an empty exception object of a given type. */
\r
137 typedef XMLToolingException* ExceptionFactory();
\r
140 * Base exception class, supports parametrized messages and XML serialization.
\r
141 * Parameters are prefixed with a dollar sign ($) and can be positional ($1)
\r
142 * or named ($info).
\r
144 class XMLTOOL_EXCEPTIONAPI(XMLTOOL_API) XMLToolingException
\r
147 virtual ~XMLToolingException() {}
\r
150 * Constructs an exception using a message and positional parameters.
\r
152 * @param msg error message
\r
153 * @param p an ordered set of positional parameter strings
\r
155 XMLToolingException(const char* msg=NULL, const params& p=params());
\r
158 * Constructs an exception using a message and named parameters.
\r
160 * @param msg error message
\r
161 * @param p a set of named parameter strings
\r
163 XMLToolingException(const char* msg, const namedparams& p);
\r
166 * Constructs an exception using a message and positional parameters.
\r
168 * @param msg error message
\r
169 * @param p an ordered set of positional parameter strings
\r
171 XMLToolingException(const std::string& msg, const params& p=params());
\r
174 * Constructs an exception using a message and named parameters.
\r
176 * @param msg error message
\r
177 * @param p a set of named parameter strings
\r
179 XMLToolingException(const std::string& msg, const namedparams& p);
\r
182 * Returns the error message, after processing any parameter references.
\r
184 * @return the processed message
\r
186 const char* getMessage() const;
\r
189 * Returns the error message, after processing any parameter references.
\r
191 * @return the processed message
\r
193 const char* what() const {return getMessage();}
\r
196 * Sets the error message.
\r
198 * @param msg the error message
\r
200 void setMessage(const char* msg);
\r
203 * Sets the error message.
\r
205 * @param msg the error message
\r
207 void setMessage(const std::string& msg) {
\r
208 setMessage(msg.c_str());
\r
212 * Attach a set of positional parameters to the exception.
\r
214 * @param p an ordered set of named parameter strings
\r
216 void addProperties(const params& p);
\r
219 * Attach a set of named parameters to the exception.
\r
221 * @param p a set of named parameter strings
\r
223 void addProperties(const namedparams& p);
\r
226 * Attach a single positional parameter at the next available position.
\r
228 * @param value the parameter value
\r
230 void addProperty(const char* value) {
\r
231 addProperties(params(1,value));
\r
235 * Attach a single named parameter.
\r
237 * @param name the parameter name
\r
238 * @param value the parameter value
\r
240 void addProperty(const char* name, const char* value) {
\r
241 addProperties(namedparams(1,name,value));
\r
245 * Returns the parameter property with the designated position (based from one).
\r
247 * @param index position to access
\r
248 * @return the parameter property or NULL
\r
250 const char* getProperty(unsigned int index) const;
\r
253 * Returns the parameter property with the designated name.
\r
255 * @param name named parameter to access
\r
256 * @return the parameter property or NULL
\r
258 const char* getProperty(const char* name) const;
\r
261 * Raises an exception using itself.
\r
262 * Used to raise an exception of a derived type.
\r
264 virtual void raise() const {
\r
269 * Returns a unique name for the exception class.
\r
271 * @return class name
\r
273 virtual const char* getClassName() const {
\r
274 return "xmltooling::XMLToolingException";
\r
278 * Returns a string containing a serialized representation of the exception.
\r
280 * @return the serialization
\r
282 std::string toString() const;
\r
286 mutable std::string m_processedmsg;
\r
287 std::map<std::string,std::string> m_params;
\r
291 * Builds an empty exception of the given type.
\r
293 * @param exceptionClass the name of the exception type to build
\r
294 * @return an empty exception object
\r
296 static XMLToolingException* getInstance(const char* exceptionClass);
\r
299 * Builds an exception from a serialized input stream.
\r
301 * @param in input stream
\r
302 * @return the exception object found in the stream
\r
304 static XMLToolingException* fromStream(std::istream& in);
\r
307 * Builds an exception from a serialized input buffer.
\r
309 * @param s input buffer
\r
310 * @return the exception object found in the buffer
\r
312 static XMLToolingException* fromString(const char* s);
\r
315 * Registers a factory to create exceptions of a given class name.
\r
317 * @param exceptionClass name of exception type
\r
318 * @param factory factory function to build exceptions with
\r
320 static void registerFactory(const char* exceptionClass, ExceptionFactory* factory) {
\r
321 m_factoryMap[exceptionClass] = factory;
\r
325 * Unregisters the factory for a given class name.
\r
327 * @param exceptionClass name of exception type
\r
329 static void deregisterFactory(const char* exceptionClass) {
\r
330 m_factoryMap.erase(exceptionClass);
\r
334 typedef std::map<std::string,ExceptionFactory*> ExceptionFactoryMap;
\r
335 static ExceptionFactoryMap m_factoryMap;
\r
338 DECL_XMLTOOLING_EXCEPTION(XMLParserException,XMLToolingException,Exceptions related to XML parsing);
\r
339 DECL_XMLTOOLING_EXCEPTION(XMLObjectException,XMLToolingException,Exceptions in basic object usage);
\r
340 DECL_XMLTOOLING_EXCEPTION(MarshallingException,XMLToolingException,Exceptions during object marshalling);
\r
341 DECL_XMLTOOLING_EXCEPTION(UnmarshallingException,XMLToolingException,Exceptions during object unmarshalling);
\r
342 DECL_XMLTOOLING_EXCEPTION(UnknownElementException,XMLToolingException,Exceptions due to processing of unknown element content);
\r
343 DECL_XMLTOOLING_EXCEPTION(UnknownAttributeException,XMLToolingException,Exceptions due to processing of unknown attributes);
\r
344 DECL_XMLTOOLING_EXCEPTION(ValidationException,XMLToolingException,Exceptions during object validation);
\r
345 DECL_XMLTOOLING_EXCEPTION(SignatureException,XMLToolingException,Exceptions in signature processing);
\r
349 #if defined (_MSC_VER)
\r
350 #pragma warning( pop )
\r
353 #endif /* __xmltooling_exceptions_h__ */
\r