Author: fanningpj
Date: Sun Jun 19 13:37:14 2022
New Revision: 1902065

URL: http://svn.apache.org/viewvc?rev=1902065&view=rev
Log:
remove use of some calls to deprecated cursor dispose()

Modified:
    
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFAbstractFootnoteEndnote.java
    
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
    
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java
    
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java

Modified: 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFAbstractFootnoteEndnote.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFAbstractFootnoteEndnote.java?rev=1902065&r1=1902064&r2=1902065&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFAbstractFootnoteEndnote.java
 (original)
+++ 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFAbstractFootnoteEndnote.java
 Sun Jun 19 13:37:14 2022
@@ -72,8 +72,7 @@ public abstract class XWPFAbstractFootno
     }
 
     protected void init() {
-        XmlCursor cursor = ctFtnEdn.newCursor();
-        try {
+        try (XmlCursor cursor = ctFtnEdn.newCursor()) {
             //copied from XWPFDocument...should centralize this code
             //to avoid duplication
             cursor.selectPath("./*");
@@ -93,8 +92,6 @@ public abstract class XWPFAbstractFootno
                 }
 
             }
-        } finally {
-            cursor.dispose();
         }
     }
 
@@ -251,8 +248,7 @@ public abstract class XWPFAbstractFootno
     public XWPFTableCell getTableCell(CTTc cell) {
         XmlObject o;
         CTRow row;
-        final XmlCursor cursor = cell.newCursor();
-        try {
+        try (final XmlCursor cursor = cell.newCursor()) {
             cursor.toParent();
             o = cursor.getObject();
             if (!(o instanceof CTRow)) {
@@ -261,8 +257,6 @@ public abstract class XWPFAbstractFootno
             row = (CTRow) o;
             cursor.toParent();
             o = cursor.getObject();
-        } finally {
-            cursor.dispose();
         }
         if (!(o instanceof CTTbl)) {
             return null;
@@ -285,13 +279,9 @@ public abstract class XWPFAbstractFootno
      * @return true if the cursor is within a CTFtnEdn element.
      */
     private boolean isCursorInFtn(XmlCursor cursor) {
-        XmlCursor verify = cursor.newCursor();
-        try {
+        try (XmlCursor verify = cursor.newCursor()) {
             verify.toParent();
             return  (verify.getObject() == this.ctFtnEdn);
-        } finally {
-            verify.dispose();
-
         }
     }
 
@@ -329,19 +319,17 @@ public abstract class XWPFAbstractFootno
                 tables.add(pos, newT);
             }
             int i = 0;
-            cursor = t.newCursor();
-            while (cursor.toPrevSibling()) {
-                o = cursor.getObject();
-                if (o instanceof CTP || o instanceof CTTbl)
-                    i++;
+            try (XmlCursor cursor1 = t.newCursor()) {
+                while (cursor1.toPrevSibling()) {
+                    o = cursor1.getObject();
+                    if (o instanceof CTP || o instanceof CTTbl)
+                        i++;
+                }
+                bodyElements.add(i, newT);
             }
-            bodyElements.add(i, newT);
-            XmlCursor c2 = t.newCursor();
-            try {
+            try (XmlCursor c2 = t.newCursor()) {
                 cursor.toCursor(c2);
                 cursor.toEndToken();
-            } finally {
-                c2.dispose();
             }
             return newT;
         }
@@ -373,11 +361,8 @@ public abstract class XWPFAbstractFootno
                 paragraphs.add(pos, newP);
             }
             int i = 0;
-            XmlCursor p2 = p.newCursor();
-            try {
+            try (XmlCursor p2 = p.newCursor()) {
                 cursor.toCursor(p2);
-            } finally {
-                p2.dispose();
             }
             while (cursor.toPrevSibling()) {
                 o = cursor.getObject();
@@ -385,10 +370,10 @@ public abstract class XWPFAbstractFootno
                     i++;
             }
             bodyElements.add(i, newP);
-            p2 = p.newCursor();
-            cursor.toCursor(p2);
-            cursor.toEndToken();
-            p2.dispose();
+            try (XmlCursor p2 = p.newCursor()) {
+                cursor.toCursor(p2);
+                cursor.toEndToken();
+            }
             return newP;
         }
         return null;

Modified: 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java?rev=1902065&r1=1902064&r2=1902065&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
 (original)
+++ 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
 Sun Jun 19 13:37:14 2022
@@ -201,34 +201,33 @@ public class XWPFDocument extends POIXML
 
             // parse the document with cursor and add
             // the XmlObject to its lists
-            XmlCursor docCursor = ctDocument.newCursor();
-            docCursor.selectPath("./*");
-            while (docCursor.toNextSelection()) {
-                XmlObject o = docCursor.getObject();
-                if (o instanceof CTBody) {
-                    XmlCursor bodyCursor = o.newCursor();
-                    bodyCursor.selectPath("./*");
-                    while (bodyCursor.toNextSelection()) {
-                        XmlObject bodyObj = bodyCursor.getObject();
-                        if (bodyObj instanceof CTP) {
-                            XWPFParagraph p = new XWPFParagraph((CTP) bodyObj,
-                                    this);
-                            bodyElements.add(p);
-                            paragraphs.add(p);
-                        } else if (bodyObj instanceof CTTbl) {
-                            XWPFTable t = new XWPFTable((CTTbl) bodyObj, this);
-                            bodyElements.add(t);
-                            tables.add(t);
-                        } else if (bodyObj instanceof CTSdtBlock) {
-                            XWPFSDT c = new XWPFSDT((CTSdtBlock) bodyObj, 
this);
-                            bodyElements.add(c);
-                            contentControls.add(c);
+            try (XmlCursor docCursor = ctDocument.newCursor()) {
+                docCursor.selectPath("./*");
+                while (docCursor.toNextSelection()) {
+                    XmlObject o = docCursor.getObject();
+                    if (o instanceof CTBody) {
+                        try (XmlCursor bodyCursor = o.newCursor()) {
+                            bodyCursor.selectPath("./*");
+                            while (bodyCursor.toNextSelection()) {
+                                XmlObject bodyObj = bodyCursor.getObject();
+                                if (bodyObj instanceof CTP) {
+                                    XWPFParagraph p = new XWPFParagraph((CTP) 
bodyObj, this);
+                                    bodyElements.add(p);
+                                    paragraphs.add(p);
+                                } else if (bodyObj instanceof CTTbl) {
+                                    XWPFTable t = new XWPFTable((CTTbl) 
bodyObj, this);
+                                    bodyElements.add(t);
+                                    tables.add(t);
+                                } else if (bodyObj instanceof CTSdtBlock) {
+                                    XWPFSDT c = new XWPFSDT((CTSdtBlock) 
bodyObj, this);
+                                    bodyElements.add(c);
+                                    contentControls.add(c);
+                                }
+                            }
                         }
                     }
-                    bodyCursor.dispose();
                 }
             }
-            docCursor.dispose();
             // Sort out headers and footers
             if (doc.getDocument().getBody().getSectPr() != null) {
                 headerFooterPolicy = new XWPFHeaderFooterPolicy(this);
@@ -724,8 +723,7 @@ public class XWPFDocument extends POIXML
              * create a new cursor, that points to the START token of the just
              * inserted paragraph
              */
-            XmlCursor newParaPos = p.newCursor();
-            try {
+            try (XmlCursor newParaPos = p.newCursor()) {
                 /*
                  * Calculate the paragraphs index in the list of all body
                  * elements
@@ -742,8 +740,6 @@ public class XWPFDocument extends POIXML
                 cursor.toCursor(newParaPos);
                 cursor.toEndToken();
                 return newP;
-            } finally {
-                newParaPos.dispose();
             }
         }
         return null;
@@ -769,8 +765,7 @@ public class XWPFDocument extends POIXML
                 tables.add(pos, newT);
             }
             int i = 0;
-            XmlCursor tableCursor = t.newCursor();
-            try {
+            try (XmlCursor tableCursor = t.newCursor()) {
                 cursor.toCursor(tableCursor);
                 while (cursor.toPrevSibling()) {
                     o = cursor.getObject();
@@ -782,8 +777,6 @@ public class XWPFDocument extends POIXML
                 cursor.toCursor(tableCursor);
                 cursor.toEndToken();
                 return newT;
-            } finally {
-                tableCursor.dispose();
             }
         }
         return null;
@@ -793,11 +786,10 @@ public class XWPFDocument extends POIXML
      * verifies that cursor is on the right position
      */
     private boolean isCursorInBody(XmlCursor cursor) {
-        XmlCursor verify = cursor.newCursor();
-        verify.toParent();
-        boolean result = (verify.getObject() == this.ctDocument.getBody());
-        verify.dispose();
-        return result;
+        try (XmlCursor verify = cursor.newCursor()) {
+            verify.toParent();
+            return (verify.getObject() == this.ctDocument.getBody());
+        }
     }
 
     private int getPosOfBodyElement(IBodyElement needle) {
@@ -1661,8 +1653,7 @@ public class XWPFDocument extends POIXML
     public XWPFTableCell getTableCell(CTTc cell) {
         XmlObject o;
         CTRow row;
-        final XmlCursor cursor = cell.newCursor();
-        try {
+        try (final XmlCursor cursor = cell.newCursor()) {
             cursor.toParent();
             o = cursor.getObject();
             if (!(o instanceof CTRow)) {
@@ -1671,8 +1662,6 @@ public class XWPFDocument extends POIXML
             row = (CTRow) o;
             cursor.toParent();
             o = cursor.getObject();
-        } finally {
-            cursor.dispose();
         }
         if (!(o instanceof CTTbl)) {
             return null;

Modified: 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java?rev=1902065&r1=1902064&r2=1902065&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java 
(original)
+++ 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java 
Sun Jun 19 13:37:14 2022
@@ -55,8 +55,7 @@ public class XWPFHeader extends XWPFHead
 
     public XWPFHeader(XWPFDocument doc, CTHdrFtr hdrFtr) {
         super(doc, hdrFtr);
-        XmlCursor cursor = headerFooter.newCursor();
-        try {
+        try (XmlCursor cursor = headerFooter.newCursor()) {
             cursor.selectPath("./*");
             while (cursor.toNextSelection()) {
                 XmlObject o = cursor.getObject();
@@ -69,8 +68,6 @@ public class XWPFHeader extends XWPFHead
                     tables.add(t);
                 }
             }
-        } finally {
-            cursor.dispose();
         }
     }
 
@@ -99,26 +96,26 @@ public class XWPFHeader extends XWPFHead
             headerFooter = hdrDocument.getHdr();
             // parse the document with cursor and add
             // the XmlObject to its lists
-            XmlCursor cursor = headerFooter.newCursor();
-            cursor.selectPath("./*");
-            while (cursor.toNextSelection()) {
-                XmlObject o = cursor.getObject();
-                if (o instanceof CTP) {
-                    XWPFParagraph p = new XWPFParagraph((CTP) o, this);
-                    paragraphs.add(p);
-                    bodyElements.add(p);
-                }
-                if (o instanceof CTTbl) {
-                    XWPFTable t = new XWPFTable((CTTbl) o, this);
-                    tables.add(t);
-                    bodyElements.add(t);
-                }
-                if (o instanceof CTSdtBlock) {
-                    XWPFSDT c = new XWPFSDT((CTSdtBlock) o, this);
-                    bodyElements.add(c);
+            try (XmlCursor cursor = headerFooter.newCursor()) {
+                cursor.selectPath("./*");
+                while (cursor.toNextSelection()) {
+                    XmlObject o = cursor.getObject();
+                    if (o instanceof CTP) {
+                        XWPFParagraph p = new XWPFParagraph((CTP) o, this);
+                        paragraphs.add(p);
+                        bodyElements.add(p);
+                    }
+                    if (o instanceof CTTbl) {
+                        XWPFTable t = new XWPFTable((CTTbl) o, this);
+                        tables.add(t);
+                        bodyElements.add(t);
+                    }
+                    if (o instanceof CTSdtBlock) {
+                        XWPFSDT c = new XWPFSDT((CTSdtBlock) o, this);
+                        bodyElements.add(c);
+                    }
                 }
             }
-            cursor.dispose();
         } catch (XmlException e) {
             throw new POIXMLException(e);
         }

Modified: 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java?rev=1902065&r1=1902064&r2=1902065&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java
 (original)
+++ 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java
 Sun Jun 19 13:37:14 2022
@@ -330,11 +330,8 @@ public abstract class XWPFHeaderFooter e
     public void removeParagraph(XWPFParagraph paragraph) {
         if (paragraphs.contains(paragraph)) {
             CTP ctP = paragraph.getCTP();
-            XmlCursor c = ctP.newCursor();
-            try {
+            try (XmlCursor c = ctP.newCursor()) {
                 c.removeXml();
-            } finally {
-                c.dispose();
             }
             paragraphs.remove(paragraph);
             bodyElements.remove(paragraph);
@@ -349,11 +346,8 @@ public abstract class XWPFHeaderFooter e
     public void removeTable(XWPFTable table) {
         if (tables.contains(table)) {
             CTTbl ctTbl = table.getCTTbl();
-            XmlCursor c = ctTbl.newCursor();
-            try {
+            try (XmlCursor c = ctTbl.newCursor()) {
                 c.removeXml();
-            } finally {
-                c.dispose();
             }
             tables.remove(table);
             bodyElements.remove(table);
@@ -364,11 +358,8 @@ public abstract class XWPFHeaderFooter e
      * Clears all paragraphs and tables from this header / footer
      */
     public void clearHeaderFooter() {
-       XmlCursor c = headerFooter.newCursor();
-       try {
+       try (XmlCursor c = headerFooter.newCursor()) {
            c.removeXmlContents();
-       } finally {
-           c.dispose();
        }
        paragraphs.clear();
        tables.clear();
@@ -400,11 +391,8 @@ public abstract class XWPFHeaderFooter e
                 paragraphs.add(pos, newP);
             }
             int i = 0;
-            final XmlCursor p2 = p.newCursor();
-            try {
+            try (final XmlCursor p2 = p.newCursor()) {
                 cursor.toCursor(p2);
-            } finally {
-                p2.dispose();
             }
             while (cursor.toPrevSibling()) {
                 o = cursor.getObject();
@@ -412,12 +400,9 @@ public abstract class XWPFHeaderFooter e
                     i++;
             }
             bodyElements.add(i, newP);
-            final XmlCursor p3 = p.newCursor();
-            try {
+            try(final XmlCursor p3 = p.newCursor()) {
                 cursor.toCursor(p3);
                 cursor.toEndToken();
-            } finally {
-                p3.dispose();
             }
             return newP;
         }
@@ -449,24 +434,18 @@ public abstract class XWPFHeaderFooter e
                 tables.add(pos, newT);
             }
             int i = 0;
-            final XmlCursor cursor2 = t.newCursor();
-            try {
+            try (final XmlCursor cursor2 = t.newCursor()) {
                 while (cursor2.toPrevSibling()) {
                     o = cursor2.getObject();
                     if (o instanceof CTP || o instanceof CTTbl) {
                         i++;
                     }
                 }
-            } finally {
-                cursor2.dispose();
             }
             bodyElements.add(i, newT);
-            final XmlCursor cursor3 = t.newCursor();
-            try {
+            try(final XmlCursor cursor3 = t.newCursor()) {
                 cursor.toCursor(cursor3);
                 cursor.toEndToken();
-            } finally {
-                cursor3.dispose();
             }
             return newT;
         }
@@ -477,13 +456,10 @@ public abstract class XWPFHeaderFooter e
      * verifies that cursor is on the right position
      */
     private boolean isCursorInHdrF(XmlCursor cursor) {
-        XmlCursor verify = cursor.newCursor();
-        try {
+        try (XmlCursor verify = cursor.newCursor()) {
             verify.toParent();
             boolean result = (verify.getObject() == this.headerFooter);
             return result;
-        } finally {
-            verify.dispose();
         }
     }
 
@@ -526,8 +502,7 @@ public abstract class XWPFHeaderFooter e
         tables = new ArrayList<>();
         // parse the document with cursor and add
         // the XmlObject to its lists
-        XmlCursor cursor = headerFooter.newCursor();
-        try {
+        try (XmlCursor cursor = headerFooter.newCursor()) {
             cursor.selectPath("./*");
             while (cursor.toNextSelection()) {
                 XmlObject o = cursor.getObject();
@@ -542,8 +517,6 @@ public abstract class XWPFHeaderFooter e
                     bodyElements.add(t);
                 }
             }
-        } finally {
-            cursor.dispose();
         }
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to