source: NonGTP/Xerces/xerces/include/xercesc/util/XMLException.hpp @ 358

Revision 358, 10.0 KB checked in by bittner, 19 years ago (diff)

xerces added

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