XMLException.hpp

Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  * 
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  * 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: XMLException.hpp 568078 2007-08-21 11:43:25Z amassari $
00020  */
00021 
00022 #if !defined(EXCEPTION_HPP)
00023 #define EXCEPTION_HPP
00024 
00025 #include <xercesc/util/XMemory.hpp>
00026 #include <xercesc/util/XMLExceptMsgs.hpp>
00027 #include <xercesc/util/XMLUni.hpp>
00028 #include <xercesc/framework/XMLErrorReporter.hpp>
00029 
00030 XERCES_CPP_NAMESPACE_BEGIN
00031 
00032 // ---------------------------------------------------------------------------
00033 //  This is the base class from which all the XML parser exceptions are
00034 //  derived. The virtual interface is very simple and most of the functionality
00035 //  is in this class.
00036 //
00037 //  Because all derivatives are EXACTLY the same except for the static
00038 //  string that is used to hold the name of the class, a macro is provided
00039 //  below via which they are all created.
00040 // ---------------------------------------------------------------------------
00041 class XMLUTIL_EXPORT XMLException : public XMemory
00042 {
00043 public:
00044     // -----------------------------------------------------------------------
00045     //  Virtual Destructor
00046     // -----------------------------------------------------------------------
00047     virtual ~XMLException();
00048 
00049 
00050     // -----------------------------------------------------------------------
00051     //  The XML exception virtual interface
00052     // -----------------------------------------------------------------------
00053     virtual const XMLCh* getType() const = 0;
00054 
00055 
00056     // -----------------------------------------------------------------------
00057     //  Getter methods
00058     // -----------------------------------------------------------------------
00059     XMLExcepts::Codes getCode() const;
00060     const XMLCh* getMessage() const;
00061     const char* getSrcFile() const;
00062     unsigned int getSrcLine() const;
00063     XMLErrorReporter::ErrTypes getErrorType() const;
00064 
00065 
00066     // -----------------------------------------------------------------------
00067     //  Setter methods
00068     // -----------------------------------------------------------------------
00069     void setPosition(const char* const file, const unsigned int line);
00070 
00071 
00072     // -----------------------------------------------------------------------
00073     //  Hidden constructors and operators
00074     //
00075     //  NOTE:   Technically, these should be protected, since this is a
00076     //          base class that is never used directly. However, VC++ 6.0 will
00077     //          fail to catch via a reference to base class if the ctors are
00078     //          not public!! This seems to have been caused by the install
00079     //          of IE 5.0.
00080     // -----------------------------------------------------------------------
00081     XMLException();
00082     XMLException(const char* const srcFile, const unsigned int srcLine, MemoryManager* const memoryManager = 0);
00083     XMLException(const XMLException& toCopy);
00084     XMLException& operator=(const XMLException& toAssign);
00085 
00086     // -----------------------------------------------------------------------
00087     //  Notification that lazy data has been deleted
00088     // -----------------------------------------------------------------------
00089     static void reinitMsgMutex();
00090 
00091     static void reinitMsgLoader();
00092 
00093 protected :
00094     // -----------------------------------------------------------------------
00095     //  Protected methods
00096     // -----------------------------------------------------------------------
00097     void loadExceptText
00098     (
00099         const   XMLExcepts::Codes toLoad
00100     );
00101     void loadExceptText
00102     (
00103         const   XMLExcepts::Codes toLoad
00104         , const XMLCh* const        text1
00105         , const XMLCh* const        text2 = 0
00106         , const XMLCh* const        text3 = 0
00107         , const XMLCh* const        text4 = 0
00108     );
00109     void loadExceptText
00110     (
00111         const   XMLExcepts::Codes toLoad
00112         , const char* const         text1
00113         , const char* const         text2 = 0
00114         , const char* const         text3 = 0
00115         , const char* const         text4 = 0
00116     );
00117 
00118 
00119 private :
00120     // -----------------------------------------------------------------------
00121     //  Data members
00122     //
00123     //  fCode
00124     //      The error code that this exception represents.
00125     //
00126     //  fSrcFile
00127     //  fSrcLine
00128     //      These are the file and line information from the source where the
00129     //      exception was thrown from.
00130     //
00131     //  fMsg
00132     //      The loaded message text for this exception.
00133     // -----------------------------------------------------------------------
00134     XMLExcepts::Codes       fCode;
00135     char*                   fSrcFile;
00136     unsigned int            fSrcLine;
00137     XMLCh*                  fMsg;
00138 
00139 protected:
00140     MemoryManager*          fMemoryManager;
00141 };
00142 
00143 // ---------------------------------------------------------------------------
00144 //  XMLException: Getter methods
00145 // ---------------------------------------------------------------------------
00146 inline XMLExcepts::Codes XMLException::getCode() const
00147 {
00148     return fCode;
00149 }
00150 
00151 inline const XMLCh* XMLException::getMessage() const
00152 {
00153     return fMsg;
00154 }
00155 
00156 inline const char* XMLException::getSrcFile() const
00157 {
00158     if (!fSrcFile)
00159         return "";
00160     return fSrcFile;
00161 }
00162 
00163 inline unsigned int XMLException::getSrcLine() const
00164 {
00165     return fSrcLine;
00166 }
00167 
00168 inline XMLErrorReporter::ErrTypes XMLException::getErrorType() const
00169 {
00170    if ((fCode >= XMLExcepts::W_LowBounds) && (fCode <= XMLExcepts::W_HighBounds))
00171        return XMLErrorReporter::ErrType_Warning;
00172    else if ((fCode >= XMLExcepts::F_LowBounds) && (fCode <= XMLExcepts::F_HighBounds))
00173         return XMLErrorReporter::ErrType_Fatal;
00174    else if ((fCode >= XMLExcepts::E_LowBounds) && (fCode <= XMLExcepts::E_HighBounds))
00175         return XMLErrorReporter::ErrType_Error;
00176    return XMLErrorReporter::ErrTypes_Unknown;
00177 }
00178 
00179 // ---------------------------------------------------------------------------
00180 //  This macro is used to create derived classes. They are all identical
00181 //  except the name of the exception, so it crazy to type them in over and
00182 //  over.
00183 // ---------------------------------------------------------------------------
00184 #define MakeXMLException(theType, expKeyword) \
00185 class expKeyword theType : public XMLException \
00186 { \
00187 public: \
00188  \
00189     theType(const   char* const         srcFile \
00190             , const unsigned int        srcLine \
00191             , const XMLExcepts::Codes toThrow \
00192             , MemoryManager*            memoryManager = 0) : \
00193         XMLException(srcFile, srcLine, memoryManager) \
00194     { \
00195         loadExceptText(toThrow); \
00196     } \
00197  \
00198     theType(const theType& toCopy) : \
00199  \
00200         XMLException(toCopy) \
00201     { \
00202     } \
00203   \
00204     theType(const   char* const         srcFile \
00205             , const unsigned int        srcLine \
00206             , const XMLExcepts::Codes   toThrow \
00207             , const XMLCh* const        text1 \
00208             , const XMLCh* const        text2 = 0 \
00209             , const XMLCh* const        text3 = 0 \
00210             , const XMLCh* const        text4 = 0 \
00211             , MemoryManager*            memoryManager = 0) : \
00212         XMLException(srcFile, srcLine, memoryManager) \
00213     { \
00214         loadExceptText(toThrow, text1, text2, text3, text4); \
00215     } \
00216  \
00217     theType(const   char* const         srcFile \
00218             , const unsigned int        srcLine \
00219             , const XMLExcepts::Codes   toThrow \
00220             , const char* const         text1 \
00221             , const char* const         text2 = 0 \
00222             , const char* const         text3 = 0 \
00223             , const char* const         text4 = 0 \
00224             , MemoryManager*            memoryManager = 0) : \
00225         XMLException(srcFile, srcLine, memoryManager) \
00226     { \
00227         loadExceptText(toThrow, text1, text2, text3, text4); \
00228     } \
00229  \
00230     virtual ~theType() {} \
00231  \
00232     theType& operator=(const theType& toAssign) \
00233     { \
00234         XMLException::operator=(toAssign); \
00235         return *this; \
00236     } \
00237  \
00238     virtual XMLException* duplicate() const \
00239     { \
00240         return new (fMemoryManager) theType(*this); \
00241     } \
00242  \
00243     virtual const XMLCh* getType() const \
00244     { \
00245         return XMLUni::fg##theType##_Name; \
00246     } \
00247  \
00248 private : \
00249     theType(); \
00250 };
00251 
00252 
00253 
00254 // ---------------------------------------------------------------------------
00255 //  This macros is used to actually throw an exception. It is used in order
00256 //  to make sure that source code line/col info is stored correctly, and to
00257 //  give flexibility for other stuff in the future.
00258 // ---------------------------------------------------------------------------
00259 
00260 #define ThrowXML(type,code) throw type(__FILE__, __LINE__, code)
00261 
00262 #define ThrowXML1(type,code,p1) throw type(__FILE__, __LINE__, code, p1)
00263 
00264 #define ThrowXML2(type,code,p1,p2) throw type(__FILE__, __LINE__, code, p1, p2)
00265 
00266 #define ThrowXML3(type,code,p1,p2,p3) throw type(__FILE__, __LINE__, code, p1, p2, p3)
00267 
00268 #define ThrowXML4(type,code,p1,p2,p3,p4) throw type(__FILE__, __LINE__, code, p1, p2, p3, p4)
00269 
00270 #define ThrowXMLwithMemMgr(type,code,memMgr) throw type(__FILE__, __LINE__, code, memMgr)
00271 
00272 #define ThrowXMLwithMemMgr1(type,code,p1,memMgr) throw type(__FILE__, __LINE__, code, p1, 0, 0, 0, memMgr)
00273 
00274 #define ThrowXMLwithMemMgr2(type,code,p1,p2,memMgr) throw type(__FILE__, __LINE__, code, p1, p2, 0, 0, memMgr)
00275 
00276 #define ThrowXMLwithMemMgr3(type,code,p1,p2,p3,memMgr) throw type(__FILE__, __LINE__, code, p1, p2, p3, 0, memMgr)
00277 
00278 #define ThrowXMLwithMemMgr4(type,code,p1,p2,p3,p4,memMgr) throw type(__FILE__, __LINE__, code, p1, p2, p3, p4, memMgr)
00279 
00280 XERCES_CPP_NAMESPACE_END
00281 
00282 #endif

Generated on Thu Feb 19 11:35:44 2009 for Xerces-C++ by  doxygen 1.5.4