2 * Copyright 2001-2006 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.
23 #ifndef __xmltooling_exceptions_h__
24 #define __xmltooling_exceptions_h__
30 #include <xmltooling/base.h>
33 * Declares a derived exception class
35 * @param name the exception class
36 * @param linkage linkage specification for class
37 * @param ns the exception class C++ namespace
38 * @param base the base class
39 * @param desc documentation comment for class
41 #define DECL_XMLTOOLING_EXCEPTION(name,linkage,ns,base,desc) \
42 XMLTOOLING_DOXYGEN(desc) \
43 class linkage name : public base { \
45 XMLTOOLING_DOXYGEN(Constructor) \
46 name(const char* msg=NULL, const xmltooling::params& p=xmltooling::params()) : base(msg,p) {} \
47 XMLTOOLING_DOXYGEN(Constructor) \
48 name(const char* msg, const xmltooling::namedparams& p) : base(msg,p) {} \
49 XMLTOOLING_DOXYGEN(Constructor) \
50 name(const std::string& msg, const xmltooling::params& p=xmltooling::params()) : base(msg,p) {} \
51 XMLTOOLING_DOXYGEN(Constructor) \
52 name(const std::string& msg, const xmltooling::namedparams& p) : base(msg,p) {} \
54 virtual const char* getClassName() const { return #ns"::"#name; } \
55 void raise() const {throw *this;} \
59 * Declares a factory function for an exception class.
61 * @param name the exception class name
62 * @param ns the exception class C++ namespace
64 #define DECL_EXCEPTION_FACTORY(name,ns) \
65 xmltooling::XMLToolingException* name##Factory() \
67 return new ns::name(); \
71 * Registers a factory for an exception class.
73 * @param name the exception class name
74 * @param ns the exception class C++ namespace
76 #define REGISTER_EXCEPTION_FACTORY(name,ns) XMLToolingException::registerFactory(#ns"::"#name,name##Factory)
78 #if defined (_MSC_VER)
79 #pragma warning( push )
80 #pragma warning( disable : 4250 4251 )
83 namespace xmltooling {
86 * Wrapper around a variable number of arguments.
88 class XMLTOOL_API params
92 * Initializes with zero parameters.
97 * Initializes the parameter set.
99 * @param count the number of parameters that follow
101 params(int count,...);
104 * Returns an immutable reference to the set of parameters.
106 * @return the parameter set
108 const std::vector<const char*>& get() const {return v;}
111 /** Contains the parameters being passed. */
112 std::vector<const char*> v;
116 * Wrapper around a variable number of name/value pairs.
118 class XMLTOOL_API namedparams : public params
122 * Initializes with zero parameters.
127 * Initializes the named parameter set.
129 * @param count the number of name/value pairs that follow (must be even)
131 namedparams(int count,...);
134 class XMLTOOL_EXCEPTIONAPI(XMLTOOL_API) XMLToolingException;
136 /** A factory function that returns an empty exception object of a given type. */
137 typedef XMLToolingException* ExceptionFactory();
140 * Base exception class, supports parametrized messages and XML serialization.
141 * Parameters are prefixed with a dollar sign ($) and can be positional ($1)
144 class XMLTOOL_EXCEPTIONAPI(XMLTOOL_API) XMLToolingException
147 virtual ~XMLToolingException() {}
150 * Constructs an exception using a message and positional parameters.
152 * @param msg error message
153 * @param p an ordered set of positional parameter strings
155 XMLToolingException(const char* msg=NULL, const params& p=params());
158 * Constructs an exception using a message and named parameters.
160 * @param msg error message
161 * @param p a set of named parameter strings
163 XMLToolingException(const char* msg, const namedparams& p);
166 * Constructs an exception using a message and positional parameters.
168 * @param msg error message
169 * @param p an ordered set of positional parameter strings
171 XMLToolingException(const std::string& msg, const params& p=params());
174 * Constructs an exception using a message and named parameters.
176 * @param msg error message
177 * @param p a set of named parameter strings
179 XMLToolingException(const std::string& msg, const namedparams& p);
182 * Returns the error message, after processing any parameter references.
184 * @return the processed message
186 const char* getMessage() const;
189 * Returns the error message, after processing any parameter references.
191 * @return the processed message
193 const char* what() const {return getMessage();}
196 * Sets the error message.
198 * @param msg the error message
200 void setMessage(const char* msg);
203 * Sets the error message.
205 * @param msg the error message
207 void setMessage(const std::string& msg) {
208 setMessage(msg.c_str());
212 * Attach a set of positional parameters to the exception.
214 * @param p an ordered set of named parameter strings
216 void addProperties(const params& p);
219 * Attach a set of named parameters to the exception.
221 * @param p a set of named parameter strings
223 void addProperties(const namedparams& p);
226 * Attach a single positional parameter at the next available position.
228 * @param value the parameter value
230 void addProperty(const char* value) {
231 addProperties(params(1,value));
235 * Attach a single named parameter.
237 * @param name the parameter name
238 * @param value the parameter value
240 void addProperty(const char* name, const char* value) {
241 addProperties(namedparams(1,name,value));
245 * Returns the parameter property with the designated position (based from one).
247 * @param index position to access
248 * @return the parameter property or NULL
250 const char* getProperty(unsigned int index) const;
253 * Returns the parameter property with the designated name.
255 * @param name named parameter to access
256 * @return the parameter property or NULL
258 const char* getProperty(const char* name) const;
261 * Raises an exception using itself.
262 * Used to raise an exception of a derived type.
264 virtual void raise() const {
269 * Returns a unique name for the exception class.
273 virtual const char* getClassName() const {
274 return "xmltooling::XMLToolingException";
278 * Returns a string containing a serialized representation of the exception.
280 * @return the serialization
282 std::string toString() const;
286 mutable std::string m_processedmsg;
287 std::map<std::string,std::string> m_params;
291 * Builds an empty exception of the given type.
293 * @param exceptionClass the name of the exception type to build
294 * @return an empty exception object
296 static XMLToolingException* getInstance(const char* exceptionClass);
299 * Builds an exception from a serialized input stream.
301 * @param in input stream
302 * @return the exception object found in the stream
304 static XMLToolingException* fromStream(std::istream& in);
307 * Builds an exception from a serialized input buffer.
309 * @param s input buffer
310 * @return the exception object found in the buffer
312 static XMLToolingException* fromString(const char* s);
315 * Registers a factory to create exceptions of a given class name.
317 * @param exceptionClass name of exception type
318 * @param factory factory function to build exceptions with
320 static void registerFactory(const char* exceptionClass, ExceptionFactory* factory) {
321 m_factoryMap[exceptionClass] = factory;
325 * Unregisters the factory for a given class name.
327 * @param exceptionClass name of exception type
329 static void deregisterFactory(const char* exceptionClass) {
330 m_factoryMap.erase(exceptionClass);
334 * Unregisters all factories.
336 static void deregisterFactories() {
337 m_factoryMap.clear();
341 typedef std::map<std::string,ExceptionFactory*> ExceptionFactoryMap;
342 static ExceptionFactoryMap m_factoryMap;
345 DECL_XMLTOOLING_EXCEPTION(XMLParserException,XMLTOOL_EXCEPTIONAPI(XMLTOOL_API),xmltooling,XMLToolingException,Exceptions related to XML parsing);
346 DECL_XMLTOOLING_EXCEPTION(XMLObjectException,XMLTOOL_EXCEPTIONAPI(XMLTOOL_API),xmltooling,XMLToolingException,Exceptions in basic object usage);
347 DECL_XMLTOOLING_EXCEPTION(MarshallingException,XMLTOOL_EXCEPTIONAPI(XMLTOOL_API),xmltooling,XMLToolingException,Exceptions during object marshalling);
348 DECL_XMLTOOLING_EXCEPTION(UnmarshallingException,XMLTOOL_EXCEPTIONAPI(XMLTOOL_API),xmltooling,XMLToolingException,Exceptions during object unmarshalling);
349 DECL_XMLTOOLING_EXCEPTION(UnknownElementException,XMLTOOL_EXCEPTIONAPI(XMLTOOL_API),xmltooling,XMLToolingException,Exceptions due to processing of unknown element content);
350 DECL_XMLTOOLING_EXCEPTION(UnknownAttributeException,XMLTOOL_EXCEPTIONAPI(XMLTOOL_API),xmltooling,XMLToolingException,Exceptions due to processing of unknown attributes);
351 DECL_XMLTOOLING_EXCEPTION(UnknownExtensionException,XMLTOOL_EXCEPTIONAPI(XMLTOOL_API),xmltooling,XMLToolingException,Exceptions from use of an unrecognized extension/plugin);
352 DECL_XMLTOOLING_EXCEPTION(ValidationException,XMLTOOL_EXCEPTIONAPI(XMLTOOL_API),xmltooling,XMLToolingException,Exceptions during object validation);
356 #if defined (_MSC_VER)
357 #pragma warning( pop )
360 #endif /* __xmltooling_exceptions_h__ */