# 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 673960 2008-07-04 08:50:12Z borisk $
    00020  */
    00021
    00022 #if !defined(XERCESC_INCLUDE_GUARD_XMLEXCEPTION_HPP)
    00023 #define XERCESC_INCLUDE_GUARD_XMLEXCEPTION_HPP
    00024
    00025 #include <xercesc/util/XercesDefs.hpp>
    00026 #include <xercesc/util/XMemory.hpp>
    00027 #include <xercesc/util/XMLExceptMsgs.hpp>
    00028 #include <xercesc/util/XMLUni.hpp>
    00029 #include <xercesc/framework/XMLErrorReporter.hpp>
    00030
    00031 XERCES_CPP_NAMESPACE_BEGIN
    00032
    00033 // ---------------------------------------------------------------------------
    00034 //  This is the base class from which all the XML parser exceptions are
    00035 //  derived. The virtual interface is very simple and most of the functionality
    00036 //  is in this class.
    00037 //
    00038 //  Because all derivatives are EXACTLY the same except for the static
    00039 //  string that is used to hold the name of the class, a macro is provided
    00040 //  below via which they are all created.
    00041 // ---------------------------------------------------------------------------
    00042 class XMLUTIL_EXPORT XMLException : public XMemory
    00043 {
    00044 public:
    00045     // -----------------------------------------------------------------------
    00046     //  Virtual Destructor
    00047     // -----------------------------------------------------------------------
    00048     virtual ~XMLException();
    00049
    00050
    00051     // -----------------------------------------------------------------------
    00052     //  The XML exception virtual interface
    00053     // -----------------------------------------------------------------------
    00054     virtual const XMLCh* getType() const = 0;
    00055
    00056
    00057     // -----------------------------------------------------------------------
    00058     //  Getter methods
    00059     // -----------------------------------------------------------------------
    00060     XMLExcepts::Codes getCode() const;
    00061     const XMLCh* getMessage() const;
    00062     const char* getSrcFile() const;
    00063     XMLFileLoc getSrcLine() const;
    00064     XMLErrorReporter::ErrTypes getErrorType() const;
    00065
    00066
    00067     // -----------------------------------------------------------------------
    00068     //  Setter methods
    00069     // -----------------------------------------------------------------------
    00070     void setPosition(const char* const file, const XMLFileLoc line);
    00071
    00072
    00073     // -----------------------------------------------------------------------
    00074     //  Hidden constructors and operators
    00075     //
    00076     //  NOTE:   Technically, these should be protected, since this is a
    00077     //          base class that is never used directly. However, VC++ 6.0 will
    00078     //          fail to catch via a reference to base class if the ctors are
    00079     //          not public!! This seems to have been caused by the install
    00080     //          of IE 5.0.
    00081     // -----------------------------------------------------------------------
    00082     XMLException();
    00083     XMLException(const char* const srcFile, const XMLFileLoc srcLine, MemoryManager* const memoryManager = 0);
    00084     XMLException(const XMLException& toCopy);
    00085     XMLException& operator=(const XMLException& toAssign);
    00086
    00087 protected :
    00088     // -----------------------------------------------------------------------
    00089     //  Protected methods
    00090     // -----------------------------------------------------------------------
    00091     void loadExceptText
    00092     (
    00093         const   XMLExcepts::Codes toLoad
    00094     );
    00095     void loadExceptText
    00096     (
    00097         const   XMLExcepts::Codes toLoad
    00098         , const XMLCh* const        text1
    00099         , const XMLCh* const        text2 = 0
    00100         , const XMLCh* const        text3 = 0
    00101         , const XMLCh* const        text4 = 0
    00102     );
    00103     void loadExceptText
    00104     (
    00105         const   XMLExcepts::Codes toLoad
    00106         , const char* const         text1
    00107         , const char* const         text2 = 0
    00108         , const char* const         text3 = 0
    00109         , const char* const         text4 = 0
    00110     );
    00111
    00112
    00113 private :
    00114     // -----------------------------------------------------------------------
    00115     //  Data members
    00116     //
    00117     //  fCode
    00118     //      The error code that this exception represents.
    00119     //
    00120     //  fSrcFile
    00121     //  fSrcLine
    00122     //      These are the file and line information from the source where the
    00123     //      exception was thrown from.
    00124     //
    00125     //  fMsg
    00126     //      The loaded message text for this exception.
    00127     // -----------------------------------------------------------------------
    00128     XMLExcepts::Codes       fCode;
    00129     char*                   fSrcFile;
    00130     XMLFileLoc              fSrcLine;
    00131     XMLCh*                  fMsg;
    00132
    00133 protected:
    00134     MemoryManager*          fMemoryManager;
    00135 };
    00136
    00137 // ---------------------------------------------------------------------------
    00138 //  XMLException: Getter methods
    00139 // ---------------------------------------------------------------------------
    00140 inline XMLExcepts::Codes XMLException::getCode() const
    00141 {
    00142     return fCode;
    00143 }
    00144
    00145 inline const XMLCh* XMLException::getMessage() const
    00146 {
    00147     return fMsg;
    00148 }
    00149
    00150 inline const char* XMLException::getSrcFile() const
    00151 {
    00152     if (!fSrcFile)
    00153         return "";
    00154     return fSrcFile;
    00155 }
    00156
    00157 inline XMLFileLoc XMLException::getSrcLine() const
    00158 {
    00159     return fSrcLine;
    00160 }
    00161
    00162 inline XMLErrorReporter::ErrTypes XMLException::getErrorType() const
    00163 {
    00164    if ((fCode >= XMLExcepts::W_LowBounds) && (fCode <= XMLExcepts::W_HighBounds))
    00165        return XMLErrorReporter::ErrType_Warning;
    00166    else if ((fCode >= XMLExcepts::F_LowBounds) && (fCode <= XMLExcepts::F_HighBounds))
    00167         return XMLErrorReporter::ErrType_Fatal;
    00168    else if ((fCode >= XMLExcepts::E_LowBounds) && (fCode <= XMLExcepts::E_HighBounds))
    00169         return XMLErrorReporter::ErrType_Error;
    00170    return XMLErrorReporter::ErrTypes_Unknown;
    00171 }
    00172
    00173 // ---------------------------------------------------------------------------
    00174 //  This macro is used to create derived classes. They are all identical
    00175 //  except the name of the exception, so it crazy to type them in over and
    00176 //  over.
    00177 // ---------------------------------------------------------------------------
    00178 #define MakeXMLException(theType, expKeyword) \
    00179 class expKeyword theType : public XMLException \
    00180 { \
    00181 public: \
    00182  \
    00183     theType(const   char* const         srcFile \
    00184             , const XMLFileLoc          srcLine \
    00185             , const XMLExcepts::Codes toThrow \
    00186             , MemoryManager*            memoryManager = 0) : \
    00187         XMLException(srcFile, srcLine, memoryManager) \
    00188     { \
    00189         loadExceptText(toThrow); \
    00190     } \
    00191  \
    00192     theType(const theType& toCopy) : \
    00193  \
    00194         XMLException(toCopy) \
    00195     { \
    00196     } \
    00197   \
    00198     theType(const   char* const         srcFile \
    00199             , const XMLFileLoc          srcLine \
    00200             , const XMLExcepts::Codes   toThrow \
    00201             , const XMLCh* const        text1 \
    00202             , const XMLCh* const        text2 = 0 \
    00203             , const XMLCh* const        text3 = 0 \
    00204             , const XMLCh* const        text4 = 0 \
    00205             , MemoryManager*            memoryManager = 0) : \
    00206         XMLException(srcFile, srcLine, memoryManager) \
    00207     { \
    00208         loadExceptText(toThrow, text1, text2, text3, text4); \
    00209     } \
    00210  \
    00211     theType(const   char* const         srcFile \
    00212             , const XMLFileLoc          srcLine \
    00213             , const XMLExcepts::Codes   toThrow \
    00214             , const char* const         text1 \
    00215             , const char* const         text2 = 0 \
    00216             , const char* const         text3 = 0 \
    00217             , const char* const         text4 = 0 \
    00218             , MemoryManager*            memoryManager = 0) : \
    00219         XMLException(srcFile, srcLine, memoryManager) \
    00220     { \
    00221         loadExceptText(toThrow, text1, text2, text3, text4); \
    00222     } \
    00223  \
    00224     virtual ~theType() {} \
    00225  \
    00226     theType& operator=(const theType& toAssign) \
    00227     { \
    00228         XMLException::operator=(toAssign); \
    00229         return *this; \
    00230     } \
    00231  \
    00232     virtual XMLException* duplicate() const \
    00233     { \
    00234         return new (fMemoryManager) theType(*this); \
    00235     } \
    00236  \
    00237     virtual const XMLCh* getType() const \
    00238     { \
    00239         return XMLUni::fg##theType##_Name; \
    00240     } \
    00241  \
    00242 private : \
    00243     theType(); \
    00244 };
    00245
    00246
    00247
    00248 // ---------------------------------------------------------------------------
    00249 //  This macros is used to actually throw an exception. It is used in order
    00250 //  to make sure that source code line/col info is stored correctly, and to
    00251 //  give flexibility for other stuff in the future.
    00252 // ---------------------------------------------------------------------------
    00253
    00254 #define ThrowXML(type,code) throw type(__FILE__, __LINE__, code)
    00255
    00256 #define ThrowXML1(type,code,p1) throw type(__FILE__, __LINE__, code, p1)
    00257
    00258 #define ThrowXML2(type,code,p1,p2) throw type(__FILE__, __LINE__, code, p1, p2)
    00259
    00260 #define ThrowXML3(type,code,p1,p2,p3) throw type(__FILE__, __LINE__, code, p1, p2, p3)
    00261
    00262 #define ThrowXML4(type,code,p1,p2,p3,p4) throw type(__FILE__, __LINE__, code, p1, p2, p3, p4)
    00263
    00264 #define ThrowXMLwithMemMgr(type,code,memMgr) throw type(__FILE__, __LINE__, code, memMgr)
    00265
    00266 #define ThrowXMLwithMemMgr1(type,code,p1,memMgr) throw type(__FILE__, __LINE__, code, p1, 0, 0, 0, memMgr)
    00267
    00268 #define ThrowXMLwithMemMgr2(type,code,p1,p2,memMgr) throw type(__FILE__, __LINE__, code, p1, p2, 0, 0, memMgr)
    00269
    00270 #define ThrowXMLwithMemMgr3(type,code,p1,p2,p3,memMgr) throw type(__FILE__, __LINE__, code, p1, p2, p3, 0, memMgr)
    00271
    00272 #define ThrowXMLwithMemMgr4(type,code,p1,p2,p3,p4,memMgr) throw type(__FILE__, __LINE__, code, p1, p2, p3, p4, memMgr)
    00273
    00274 XERCES_CPP_NAMESPACE_END
    00275
    00276 #endif