1 | /* |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
3 | * contributor license agreements. See the NOTICE file distributed with |
---|
4 | * this work for additional information regarding copyright ownership. |
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
6 | * (the "License"); you may not use this file except in compliance with |
---|
7 | * the License. You may obtain a copy of the License at |
---|
8 | * |
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
10 | * |
---|
11 | * Unless required by applicable law or agreed to in writing, software |
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
14 | * See the License for the specific language governing permissions and |
---|
15 | * limitations under the License. |
---|
16 | */ |
---|
17 | |
---|
18 | /* |
---|
19 | * $Id: XMLException.hpp 568078 2007-08-21 11:43:25Z amassari $ |
---|
20 | */ |
---|
21 | |
---|
22 | #if !defined(EXCEPTION_HPP) |
---|
23 | #define EXCEPTION_HPP |
---|
24 | |
---|
25 | #include <xercesc/util/XMemory.hpp> |
---|
26 | #include <xercesc/util/XMLExceptMsgs.hpp> |
---|
27 | #include <xercesc/util/XMLUni.hpp> |
---|
28 | #include <xercesc/framework/XMLErrorReporter.hpp> |
---|
29 | |
---|
30 | XERCES_CPP_NAMESPACE_BEGIN |
---|
31 | |
---|
32 | // --------------------------------------------------------------------------- |
---|
33 | // This is the base class from which all the XML parser exceptions are |
---|
34 | // derived. The virtual interface is very simple and most of the functionality |
---|
35 | // is in this class. |
---|
36 | // |
---|
37 | // Because all derivatives are EXACTLY the same except for the static |
---|
38 | // string that is used to hold the name of the class, a macro is provided |
---|
39 | // below via which they are all created. |
---|
40 | // --------------------------------------------------------------------------- |
---|
41 | class XMLUTIL_EXPORT XMLException : public XMemory |
---|
42 | { |
---|
43 | public: |
---|
44 | // ----------------------------------------------------------------------- |
---|
45 | // Virtual Destructor |
---|
46 | // ----------------------------------------------------------------------- |
---|
47 | virtual ~XMLException(); |
---|
48 | |
---|
49 | |
---|
50 | // ----------------------------------------------------------------------- |
---|
51 | // The XML exception virtual interface |
---|
52 | // ----------------------------------------------------------------------- |
---|
53 | virtual const XMLCh* getType() const = 0; |
---|
54 | |
---|
55 | |
---|
56 | // ----------------------------------------------------------------------- |
---|
57 | // Getter methods |
---|
58 | // ----------------------------------------------------------------------- |
---|
59 | XMLExcepts::Codes getCode() const; |
---|
60 | const XMLCh* getMessage() const; |
---|
61 | const char* getSrcFile() const; |
---|
62 | unsigned int getSrcLine() const; |
---|
63 | XMLErrorReporter::ErrTypes getErrorType() const; |
---|
64 | |
---|
65 | |
---|
66 | // ----------------------------------------------------------------------- |
---|
67 | // Setter methods |
---|
68 | // ----------------------------------------------------------------------- |
---|
69 | void setPosition(const char* const file, const unsigned int line); |
---|
70 | |
---|
71 | |
---|
72 | // ----------------------------------------------------------------------- |
---|
73 | // Hidden constructors and operators |
---|
74 | // |
---|
75 | // NOTE: Technically, these should be protected, since this is a |
---|
76 | // base class that is never used directly. However, VC++ 6.0 will |
---|
77 | // fail to catch via a reference to base class if the ctors are |
---|
78 | // not public!! This seems to have been caused by the install |
---|
79 | // of IE 5.0. |
---|
80 | // ----------------------------------------------------------------------- |
---|
81 | XMLException(); |
---|
82 | XMLException(const char* const srcFile, const unsigned int srcLine, MemoryManager* const memoryManager = 0); |
---|
83 | XMLException(const XMLException& toCopy); |
---|
84 | XMLException& operator=(const XMLException& toAssign); |
---|
85 | |
---|
86 | // ----------------------------------------------------------------------- |
---|
87 | // Notification that lazy data has been deleted |
---|
88 | // ----------------------------------------------------------------------- |
---|
89 | static void reinitMsgMutex(); |
---|
90 | |
---|
91 | static void reinitMsgLoader(); |
---|
92 | |
---|
93 | protected : |
---|
94 | // ----------------------------------------------------------------------- |
---|
95 | // Protected methods |
---|
96 | // ----------------------------------------------------------------------- |
---|
97 | void loadExceptText |
---|
98 | ( |
---|
99 | const XMLExcepts::Codes toLoad |
---|
100 | ); |
---|
101 | void loadExceptText |
---|
102 | ( |
---|
103 | const XMLExcepts::Codes toLoad |
---|
104 | , const XMLCh* const text1 |
---|
105 | , const XMLCh* const text2 = 0 |
---|
106 | , const XMLCh* const text3 = 0 |
---|
107 | , const XMLCh* const text4 = 0 |
---|
108 | ); |
---|
109 | void loadExceptText |
---|
110 | ( |
---|
111 | const XMLExcepts::Codes toLoad |
---|
112 | , const char* const text1 |
---|
113 | , const char* const text2 = 0 |
---|
114 | , const char* const text3 = 0 |
---|
115 | , const char* const text4 = 0 |
---|
116 | ); |
---|
117 | |
---|
118 | |
---|
119 | private : |
---|
120 | // ----------------------------------------------------------------------- |
---|
121 | // Data members |
---|
122 | // |
---|
123 | // fCode |
---|
124 | // The error code that this exception represents. |
---|
125 | // |
---|
126 | // fSrcFile |
---|
127 | // fSrcLine |
---|
128 | // These are the file and line information from the source where the |
---|
129 | // exception was thrown from. |
---|
130 | // |
---|
131 | // fMsg |
---|
132 | // The loaded message text for this exception. |
---|
133 | // ----------------------------------------------------------------------- |
---|
134 | XMLExcepts::Codes fCode; |
---|
135 | char* fSrcFile; |
---|
136 | unsigned int fSrcLine; |
---|
137 | XMLCh* fMsg; |
---|
138 | |
---|
139 | protected: |
---|
140 | MemoryManager* fMemoryManager; |
---|
141 | }; |
---|
142 | |
---|
143 | // --------------------------------------------------------------------------- |
---|
144 | // XMLException: Getter methods |
---|
145 | // --------------------------------------------------------------------------- |
---|
146 | inline XMLExcepts::Codes XMLException::getCode() const |
---|
147 | { |
---|
148 | return fCode; |
---|
149 | } |
---|
150 | |
---|
151 | inline const XMLCh* XMLException::getMessage() const |
---|
152 | { |
---|
153 | return fMsg; |
---|
154 | } |
---|
155 | |
---|
156 | inline const char* XMLException::getSrcFile() const |
---|
157 | { |
---|
158 | if (!fSrcFile) |
---|
159 | return ""; |
---|
160 | return fSrcFile; |
---|
161 | } |
---|
162 | |
---|
163 | inline unsigned int XMLException::getSrcLine() const |
---|
164 | { |
---|
165 | return fSrcLine; |
---|
166 | } |
---|
167 | |
---|
168 | inline XMLErrorReporter::ErrTypes XMLException::getErrorType() const |
---|
169 | { |
---|
170 | if ((fCode >= XMLExcepts::W_LowBounds) && (fCode <= XMLExcepts::W_HighBounds)) |
---|
171 | return XMLErrorReporter::ErrType_Warning; |
---|
172 | else if ((fCode >= XMLExcepts::F_LowBounds) && (fCode <= XMLExcepts::F_HighBounds)) |
---|
173 | return XMLErrorReporter::ErrType_Fatal; |
---|
174 | else if ((fCode >= XMLExcepts::E_LowBounds) && (fCode <= XMLExcepts::E_HighBounds)) |
---|
175 | return XMLErrorReporter::ErrType_Error; |
---|
176 | return XMLErrorReporter::ErrTypes_Unknown; |
---|
177 | } |
---|
178 | |
---|
179 | // --------------------------------------------------------------------------- |
---|
180 | // This macro is used to create derived classes. They are all identical |
---|
181 | // except the name of the exception, so it crazy to type them in over and |
---|
182 | // over. |
---|
183 | // --------------------------------------------------------------------------- |
---|
184 | #define MakeXMLException(theType, expKeyword) \ |
---|
185 | class expKeyword theType : public XMLException \ |
---|
186 | { \ |
---|
187 | public: \ |
---|
188 | \ |
---|
189 | theType(const char* const srcFile \ |
---|
190 | , const unsigned int srcLine \ |
---|
191 | , const XMLExcepts::Codes toThrow \ |
---|
192 | , MemoryManager* memoryManager = 0) : \ |
---|
193 | XMLException(srcFile, srcLine, memoryManager) \ |
---|
194 | { \ |
---|
195 | loadExceptText(toThrow); \ |
---|
196 | } \ |
---|
197 | \ |
---|
198 | theType(const theType& toCopy) : \ |
---|
199 | \ |
---|
200 | XMLException(toCopy) \ |
---|
201 | { \ |
---|
202 | } \ |
---|
203 | \ |
---|
204 | theType(const char* const srcFile \ |
---|
205 | , const unsigned int srcLine \ |
---|
206 | , const XMLExcepts::Codes toThrow \ |
---|
207 | , const XMLCh* const text1 \ |
---|
208 | , const XMLCh* const text2 = 0 \ |
---|
209 | , const XMLCh* const text3 = 0 \ |
---|
210 | , const XMLCh* const text4 = 0 \ |
---|
211 | , MemoryManager* memoryManager = 0) : \ |
---|
212 | XMLException(srcFile, srcLine, memoryManager) \ |
---|
213 | { \ |
---|
214 | loadExceptText(toThrow, text1, text2, text3, text4); \ |
---|
215 | } \ |
---|
216 | \ |
---|
217 | theType(const char* const srcFile \ |
---|
218 | , const unsigned int srcLine \ |
---|
219 | , const XMLExcepts::Codes toThrow \ |
---|
220 | , const char* const text1 \ |
---|
221 | , const char* const text2 = 0 \ |
---|
222 | , const char* const text3 = 0 \ |
---|
223 | , const char* const text4 = 0 \ |
---|
224 | , MemoryManager* memoryManager = 0) : \ |
---|
225 | XMLException(srcFile, srcLine, memoryManager) \ |
---|
226 | { \ |
---|
227 | loadExceptText(toThrow, text1, text2, text3, text4); \ |
---|
228 | } \ |
---|
229 | \ |
---|
230 | virtual ~theType() {} \ |
---|
231 | \ |
---|
232 | theType& operator=(const theType& toAssign) \ |
---|
233 | { \ |
---|
234 | XMLException::operator=(toAssign); \ |
---|
235 | return *this; \ |
---|
236 | } \ |
---|
237 | \ |
---|
238 | virtual XMLException* duplicate() const \ |
---|
239 | { \ |
---|
240 | return new (fMemoryManager) theType(*this); \ |
---|
241 | } \ |
---|
242 | \ |
---|
243 | virtual const XMLCh* getType() const \ |
---|
244 | { \ |
---|
245 | return XMLUni::fg##theType##_Name; \ |
---|
246 | } \ |
---|
247 | \ |
---|
248 | private : \ |
---|
249 | theType(); \ |
---|
250 | }; |
---|
251 | |
---|
252 | |
---|
253 | |
---|
254 | // --------------------------------------------------------------------------- |
---|
255 | // This macros is used to actually throw an exception. It is used in order |
---|
256 | // to make sure that source code line/col info is stored correctly, and to |
---|
257 | // give flexibility for other stuff in the future. |
---|
258 | // --------------------------------------------------------------------------- |
---|
259 | |
---|
260 | #define ThrowXML(type,code) throw type(__FILE__, __LINE__, code) |
---|
261 | |
---|
262 | #define ThrowXML1(type,code,p1) throw type(__FILE__, __LINE__, code, p1) |
---|
263 | |
---|
264 | #define ThrowXML2(type,code,p1,p2) throw type(__FILE__, __LINE__, code, p1, p2) |
---|
265 | |
---|
266 | #define ThrowXML3(type,code,p1,p2,p3) throw type(__FILE__, __LINE__, code, p1, p2, p3) |
---|
267 | |
---|
268 | #define ThrowXML4(type,code,p1,p2,p3,p4) throw type(__FILE__, __LINE__, code, p1, p2, p3, p4) |
---|
269 | |
---|
270 | #define ThrowXMLwithMemMgr(type,code,memMgr) throw type(__FILE__, __LINE__, code, memMgr) |
---|
271 | |
---|
272 | #define ThrowXMLwithMemMgr1(type,code,p1,memMgr) throw type(__FILE__, __LINE__, code, p1, 0, 0, 0, memMgr) |
---|
273 | |
---|
274 | #define ThrowXMLwithMemMgr2(type,code,p1,p2,memMgr) throw type(__FILE__, __LINE__, code, p1, p2, 0, 0, memMgr) |
---|
275 | |
---|
276 | #define ThrowXMLwithMemMgr3(type,code,p1,p2,p3,memMgr) throw type(__FILE__, __LINE__, code, p1, p2, p3, 0, memMgr) |
---|
277 | |
---|
278 | #define ThrowXMLwithMemMgr4(type,code,p1,p2,p3,p4,memMgr) throw type(__FILE__, __LINE__, code, p1, p2, p3, p4, memMgr) |
---|
279 | |
---|
280 | XERCES_CPP_NAMESPACE_END |
---|
281 | |
---|
282 | #endif |
---|