unoxml/inc/node.hxx        |    6 ++++
 unoxml/source/dom/node.cxx |   67 +++++++++++++++++++++++----------------------
 2 files changed, 41 insertions(+), 32 deletions(-)

New commits:
commit a1c6d24aac530d7374cf0fdc5c62570d86e30996
Author:     Hannah Meeks <[email protected]>
AuthorDate: Sat Jun 18 16:06:44 2022 +0100
Commit:     Noel Grandin <[email protected]>
CommitDate: Mon Jun 20 18:53:27 2022 +0200

    Simplify checks with functions.
    
    Change-Id: I55fbab5d8184a743ace3cfec884b0364536ee6fa
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/136088
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <[email protected]>

diff --git a/unoxml/inc/node.hxx b/unoxml/inc/node.hxx
index e6974271f83a..852bcd1ae972 100644
--- a/unoxml/inc/node.hxx
+++ b/unoxml/inc/node.hxx
@@ -113,6 +113,12 @@ namespace DOM
 
         void dispatchSubtreeModified();
 
+        void checkNoParent(css::uno::Reference< css::xml::dom::XNode >const& 
xNode);
+
+        static void checkNoParent(const xmlNodePtr pNode);
+
+        void checkSameOwner(css::uno::Reference< css::xml::dom::XNode >const& 
xNode);
+
     public:
 
         virtual ~CNode() override;
diff --git a/unoxml/source/dom/node.cxx b/unoxml/source/dom/node.cxx
index 235af0bc5e0d..f5bd99aef475 100644
--- a/unoxml/source/dom/node.cxx
+++ b/unoxml/source/dom/node.cxx
@@ -268,6 +268,28 @@ namespace DOM
         return false;
     }
 
+    void CNode::checkNoParent(Reference<XNode>const& xNode){
+        if (xNode->getParentNode() != Reference<XNode>(this)){
+            DOMException e;
+            e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR;
+            throw e;
+        }
+    }
+    void CNode::checkNoParent(const xmlNodePtr pNode){
+        if (pNode->parent != nullptr){
+            DOMException e;
+            e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR;
+            throw e;
+        }
+    }
+    void CNode::checkSameOwner(Reference<XNode>const& xNode){
+        if (xNode->getOwnerDocument() != getOwnerDocument()) {
+            DOMException e;
+            e.Code = DOMExceptionType_WRONG_DOCUMENT_ERR;
+            throw e;
+        }
+    }
+
     /**
     Adds the node newChild to the end of the list of children of this node.
     */
@@ -296,11 +318,8 @@ namespace DOM
             e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR;
             throw e;
         }
-        if (cur->parent != nullptr) {
-            DOMException e;
-            e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR;
-            throw e;
-        }
+        checkNoParent(cur);
+
         if (!IsChildTypeAllowed(pNewChild->m_aNodeType)) {
             DOMException e;
             e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR;
@@ -610,11 +629,8 @@ namespace DOM
     {
         if (!newChild.is() || !refChild.is()) { throw RuntimeException(); }
 
-        if (newChild->getOwnerDocument() != getOwnerDocument()) {
-            DOMException e;
-            e.Code = DOMExceptionType_WRONG_DOCUMENT_ERR;
-            throw e;
-        }
+        checkSameOwner(newChild);
+
         if (refChild->getParentNode() != Reference< XNode >(this)) {
             DOMException e;
             e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR;
@@ -636,12 +652,8 @@ namespace DOM
             throw e;
         }
         // already has parent
-        if (pNewChild->parent != nullptr)
-        {
-            DOMException e;
-            e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR;
-            throw e;
-        }
+        checkNoParent(pNewChild);
+
         if (!IsChildTypeAllowed(pNewNode->m_aNodeType)) {
             DOMException e;
             e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR;
@@ -714,11 +726,8 @@ namespace DOM
             throw RuntimeException();
         }
 
-        if (xOldChild->getOwnerDocument() != getOwnerDocument()) {
-            DOMException e;
-            e.Code = DOMExceptionType_WRONG_DOCUMENT_ERR;
-            throw e;
-        }
+        checkSameOwner(xOldChild);
+
         if (xOldChild->getParentNode() != Reference< XNode >(this)) {
             DOMException e;
             e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR;
@@ -784,16 +793,13 @@ namespace DOM
     Reference< XNode > SAL_CALL CNode::replaceChild(
             Reference< XNode > const& xNewChild,
             Reference< XNode > const& xOldChild)
-    {
+     {
         if (!xOldChild.is() || !xNewChild.is()) {
             throw RuntimeException();
         }
 
-        if (xNewChild->getOwnerDocument() != getOwnerDocument()) {
-            DOMException e;
-            e.Code = DOMExceptionType_WRONG_DOCUMENT_ERR;
-            throw e;
-        }
+        checkSameOwner(xNewChild);
+
         if (xOldChild->getParentNode() != Reference< XNode >(this)) {
             DOMException e;
             e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR;
@@ -817,11 +823,8 @@ namespace DOM
             throw e;
         }
         // already has parent
-        if (pNew->parent != nullptr) {
-            DOMException e;
-            e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR;
-            throw e;
-        }
+        checkNoParent(pNew);
+
         if (!IsChildTypeAllowed(pNewNode->m_aNodeType)) {
             DOMException e;
             e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR;

Reply via email to