From 619d59e9fdd3745560fc7497c8a951b7650bd44b Mon Sep 17 00:00:00 2001
From: PeiYong Zhang <peiyongz@apache.org>
Date: Thu, 1 Apr 2004 22:00:27 +0000
Subject: [PATCH] DOMException to resolve message if not provided, (de)allocat
 memory for message using MemoryManager provided/defaulted

git-svn-id: https://svn.apache.org/repos/asf/xerces/c/trunk@175847 13f79535-47bb-0310-9956-ffa450edef68
---
 src/xercesc/dom/DOMException.cpp      | 53 +++++++++++++++++++++------
 src/xercesc/dom/DOMException.hpp      | 39 ++++++++++++++++++--
 src/xercesc/dom/DOMRangeException.cpp |  6 ++-
 src/xercesc/dom/DOMRangeException.hpp |  8 +++-
 4 files changed, 88 insertions(+), 18 deletions(-)

diff --git a/src/xercesc/dom/DOMException.cpp b/src/xercesc/dom/DOMException.cpp
index 979b80788..d12ff158d 100644
--- a/src/xercesc/dom/DOMException.cpp
+++ b/src/xercesc/dom/DOMException.cpp
@@ -58,33 +58,62 @@
  * $Id$
  */
 
+#include <xercesc/dom/DOMImplementation.hpp>
+#include <xercesc/util/XMLString.hpp>
+#include <xercesc/framework/MemoryManager.hpp>
+
 #include "DOMException.hpp"
 
 XERCES_CPP_NAMESPACE_BEGIN
 
+// ---------------------------------------------------------------------------
+//  Destructor and Constructor
+// ---------------------------------------------------------------------------
+DOMException::~DOMException()
+{
+    if (msg && fMsgOwned)
+        fMemoryManager->deallocate((void*)msg);
+}
 
 DOMException::DOMException()
-:  code((ExceptionCode) 0)
-,  msg(0)
+:code((ExceptionCode) 0)
+,msg(0)
+,fMemoryManager(0)
+,fMsgOwned(false)
 {      
 }
 
-
-DOMException::DOMException(short exCode, const XMLCh *message)
-:  code ((ExceptionCode) exCode)
-,  msg(message)
+DOMException::DOMException(      short                 exCode
+                         , const XMLCh*                message
+                         ,       MemoryManager* const  memoryManager)
+:code((ExceptionCode) exCode)
+,msg(message)
+,fMemoryManager(memoryManager)
+,fMsgOwned(false)
 {  
-}
+    if (!message)
+    {
+        const unsigned int msgSize = 2047;
+        XMLCh errText[msgSize + 1];
+        fMsgOwned = true;
 
+        // load the text
+        msg = XMLString::replicate
+             ( 
+              DOMImplementation::loadDOMExceptionMsg(code, errText, msgSize) ? errText : XMLUni::fgDefErrMsg
+            , fMemoryManager
+             );
 
-DOMException::DOMException(const DOMException &other)
-:  code(other.code)
-,  msg(other.msg)
-{
+    }
 }
 
-DOMException::~DOMException()
+DOMException::DOMException(const DOMException &other)
+:code(other.code)
+,msg(0)
+,fMemoryManager(other.fMemoryManager)
+,fMsgOwned(other.fMsgOwned)
 {
+    msg = other.fMsgOwned? XMLString::replicate(other.msg, other.fMemoryManager) : other.msg;
 }
 
 XERCES_CPP_NAMESPACE_END
diff --git a/src/xercesc/dom/DOMException.hpp b/src/xercesc/dom/DOMException.hpp
index a09ed4665..d0b3f1126 100644
--- a/src/xercesc/dom/DOMException.hpp
+++ b/src/xercesc/dom/DOMException.hpp
@@ -62,6 +62,7 @@
  */
 
 #include <xercesc/util/XercesDefs.hpp>
+#include <xercesc/util/PlatformUtils.hpp>
 
 XERCES_CPP_NAMESPACE_BEGIN
 
@@ -85,6 +86,8 @@ XERCES_CPP_NAMESPACE_BEGIN
  * @since DOM Level 1
  */
 
+class MemoryManager;
+
 class CDOM_EXPORT DOMException  {
 public:
     // -----------------------------------------------------------------------
@@ -101,10 +104,15 @@ public:
     /**
       * Constructor which takes an error code and a message.
       *
-      * @param code The error code which indicates the exception
-      * @param message The string containing the error message
+      * @param code           The error code which indicates the exception
+      * @param message        The string containing the error message
+      * @param memoryManager  The memory manager used to (de)allocate memory
       */
-    DOMException(short code, const XMLCh *message);
+    DOMException(      
+                       short                 code
+               , const XMLCh*                message
+               ,       MemoryManager* const  memoryManager = XMLPlatformUtils::fgMemoryManager
+                );
 
     /**
       * Copy constructor.
@@ -227,6 +235,11 @@ public:
         };
     //@}
 
+    // -----------------------------------------------------------------------
+    //  Getter
+    // -----------------------------------------------------------------------
+    inline const XMLCh* getMessage()    const;
+
     // -----------------------------------------------------------------------
     //  Class Types
     // -----------------------------------------------------------------------
@@ -247,6 +260,21 @@ public:
     const XMLCh *msg;
     //@}
 
+protected:
+
+    MemoryManager*  fMemoryManager;
+
+private:
+
+	 /**
+	  * A boolean value.  
+      *   If the message is provided by the applications, it is not 
+      *   adopted.
+      *   If the message is resolved by the DOM implementation, it is
+      *   owned.
+	  */
+    bool            fMsgOwned;
+
 private:
     // -----------------------------------------------------------------------
     // Unimplemented constructors and operators
@@ -254,6 +282,11 @@ private:
     DOMException & operator = (const DOMException &);
 };
 
+inline const XMLCh* DOMException::getMessage() const
+{
+    return msg;
+}
+
 XERCES_CPP_NAMESPACE_END
 
 #endif
diff --git a/src/xercesc/dom/DOMRangeException.cpp b/src/xercesc/dom/DOMRangeException.cpp
index 90ccdbc3a..2fa8bc40a 100644
--- a/src/xercesc/dom/DOMRangeException.cpp
+++ b/src/xercesc/dom/DOMRangeException.cpp
@@ -70,8 +70,10 @@ DOMRangeException::DOMRangeException()
 }
 
 
-DOMRangeException::DOMRangeException(RangeExceptionCode exCode, const XMLCh* message)
-: DOMException(exCode, message)
+DOMRangeException::DOMRangeException(      RangeExceptionCode         exCode
+                                   , const XMLCh*                     message
+                                   ,       MemoryManager*      const  memoryManager)
+: DOMException(exCode, message, memoryManager)
 , code(exCode)
 {
 }
diff --git a/src/xercesc/dom/DOMRangeException.hpp b/src/xercesc/dom/DOMRangeException.hpp
index ac37001ea..96a424a3c 100644
--- a/src/xercesc/dom/DOMRangeException.hpp
+++ b/src/xercesc/dom/DOMRangeException.hpp
@@ -71,6 +71,7 @@ XERCES_CPP_NAMESPACE_BEGIN
  * <p>See also the <a href='http://www.w3.org/TR/2000/REC-DOM-Level-2-Traversal-Range-20001113'>Document Object Model (DOM) Level 2 Traversal and Range Specification</a>.
  * @since DOM Level 2
  */
+
 class CDOM_EXPORT DOMRangeException  : public DOMException {
 public:
     // -----------------------------------------------------------------------
@@ -114,8 +115,13 @@ public:
       *
       * @param code The error code which indicates the exception
       * @param message The string containing the error message
+      * @param memoryManager  The memory manager used to (de)allocate memory
       */
-    DOMRangeException(RangeExceptionCode code, const XMLCh* message);
+    DOMRangeException(
+                            RangeExceptionCode       code
+                    , const XMLCh*                   message
+                    ,       MemoryManager*     const memoryManager
+                     );
 
     /**
       * Copy constructor.
-- 
GitLab