You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2022/06/19 12:03:51 UTC

svn commit: r1902061 - in /poi/trunk/poi-ooxml/src: main/java/org/apache/poi/xwpf/usermodel/ test/java/org/apache/poi/poifs/crypt/dsig/ test/java/org/apache/poi/xssf/streaming/ test/java/org/apache/poi/xwpf/usermodel/

Author: fanningpj
Date: Sun Jun 19 12:03:51 2022
New Revision: 1902061

URL: http://svn.apache.org/viewvc?rev=1902061&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/XWPFFooter.java
    poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
    poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
    poi/trunk/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java
    poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFCell.java
    poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java

Modified: poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFFooter.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFFooter.java?rev=1902061&r1=1902060&r2=1902061&view=diff
==============================================================================
--- poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFFooter.java (original)
+++ poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFFooter.java Sun Jun 19 12:03:51 2022
@@ -47,8 +47,7 @@ public class XWPFFooter extends XWPFHead
 
     public XWPFFooter(XWPFDocument doc, CTHdrFtr hdrFtr) throws IOException {
         super(doc, hdrFtr);
-        XmlCursor cursor = headerFooter.newCursor();
-        try {
+        try (XmlCursor cursor = headerFooter.newCursor()) {
             cursor.selectPath("./*");
             while (cursor.toNextSelection()) {
                 XmlObject o = cursor.getObject();
@@ -64,8 +63,6 @@ public class XWPFFooter extends XWPFHead
                 }
 
             }
-        } finally {
-            cursor.dispose();
         }
     }
 
@@ -98,8 +95,7 @@ public class XWPFFooter extends XWPFHead
             headerFooter = ftrDocument.getFtr();
             // 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();
@@ -118,8 +114,6 @@ public class XWPFFooter extends XWPFHead
                         bodyElements.add(c);
                     }
                 }
-            } finally {
-                cursor.dispose();
             }
         } catch (Exception e) {
             throw new POIXMLException(e);

Modified: poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java?rev=1902061&r1=1902060&r2=1902061&view=diff
==============================================================================
--- poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java (original)
+++ poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java Sun Jun 19 12:03:51 2022
@@ -72,8 +72,7 @@ public class XWPFParagraph implements IB
 
             // Check for bits that only apply when attached to a core document
             // TODO Make this nicer by tracking the XWPFFootnotes directly
-            XmlCursor c = r.newCursor();
-            try {
+            try (XmlCursor c = r.newCursor()) {
                 c.selectPath("child::*");
                 while (c.toNextSelection()) {
                     XmlObject o = c.getObject();
@@ -100,8 +99,6 @@ public class XWPFParagraph implements IB
 
                     }
                 }
-            } finally {
-                c.dispose();
             }
         }
     }
@@ -1610,28 +1607,28 @@ public class XWPFParagraph implements IB
         if (pos >= 0 && pos < runs.size()) {
             XWPFRun run = runs.get(pos);
             CTR ctr = run.getCTR();
-            XmlCursor newCursor = ctr.newCursor();
-            if (!isCursorInParagraph(newCursor)) {
-                // look up correct position for CTP -> XXX -> R array
-                newCursor.toParent();
-            }
-            if (isCursorInParagraph(newCursor)) {
-                // provide a new run
-                T newRun = provider.apply(newCursor);
-
-                // To update the iruns, find where we're going
-                // in the normal runs, and go in there
-                int iPos = iruns.size();
-                int oldAt = iruns.indexOf(run);
-                if (oldAt != -1) {
-                    iPos = oldAt;
-                }
-                iruns.add(iPos, newRun);
-                // Runs itself is easy to update
-                runs.add(pos, newRun);
-                return newRun;
+            try (XmlCursor newCursor = ctr.newCursor()) {
+                if (!isCursorInParagraph(newCursor)) {
+                    // look up correct position for CTP -> XXX -> R array
+                    newCursor.toParent();
+                }
+                if (isCursorInParagraph(newCursor)) {
+                    // provide a new run
+                    T newRun = provider.apply(newCursor);
+
+                    // To update the iruns, find where we're going
+                    // in the normal runs, and go in there
+                    int iPos = iruns.size();
+                    int oldAt = iruns.indexOf(run);
+                    if (oldAt != -1) {
+                        iPos = oldAt;
+                    }
+                    iruns.add(iPos, newRun);
+                    // Runs itself is easy to update
+                    runs.add(pos, newRun);
+                    return newRun;
+                }
             }
-            newCursor.dispose();
         }
         return null;
     }
@@ -1640,11 +1637,10 @@ public class XWPFParagraph implements IB
      * verifies that cursor is on the right position
      */
     private boolean isCursorInParagraph(XmlCursor cursor) {
-        XmlCursor verify = cursor.newCursor();
-        verify.toParent();
-        boolean result = (verify.getObject() == this.paragraph);
-        verify.dispose();
-        return result;
+        try (XmlCursor verify = cursor.newCursor()) {
+            verify.toParent();
+            return  (verify.getObject() == this.paragraph);
+        }
     }
 
     /**
@@ -1663,9 +1659,9 @@ public class XWPFParagraph implements IB
         for (int runPos = startRun; runPos < rArray.length; runPos++) {
             int beginTextPos = 0, beginCharPos = 0, textPos = 0, charPos;
             CTR ctRun = rArray[runPos];
-            XmlCursor c = ctRun.newCursor();
-            c.selectPath("./*");
-            try {
+
+            try (XmlCursor c = ctRun.newCursor()) {
+                c.selectPath("./*");
                 while (c.toNextSelection()) {
                     XmlObject o = c.getObject();
                     if (o instanceof CTText) {
@@ -1711,8 +1707,6 @@ public class XWPFParagraph implements IB
                         candCharPos = 0;
                     }
                 }
-            } finally {
-                c.dispose();
             }
         }
         return null;
@@ -1765,10 +1759,9 @@ public class XWPFParagraph implements IB
             // CTP -> CTHyperlink -> R array
             if (run instanceof XWPFHyperlinkRun
                     && isTheOnlyCTHyperlinkInRuns((XWPFHyperlinkRun) run)) {
-                XmlCursor c = ((XWPFHyperlinkRun) run).getCTHyperlink()
-                        .newCursor();
-                c.removeXml();
-                c.dispose();
+                try (XmlCursor c = ((XWPFHyperlinkRun) run).getCTHyperlink().newCursor()) {
+                    c.removeXml();
+                }
                 runs.remove(pos);
                 iruns.remove(run);
                 return true;
@@ -1776,16 +1769,16 @@ public class XWPFParagraph implements IB
             // CTP -> CTField -> R array
             if (run instanceof XWPFFieldRun
                     && isTheOnlyCTFieldInRuns((XWPFFieldRun) run)) {
-                XmlCursor c = ((XWPFFieldRun) run).getCTField().newCursor();
-                c.removeXml();
-                c.dispose();
+                try (XmlCursor c = ((XWPFFieldRun) run).getCTField().newCursor()) {
+                    c.removeXml();
+                }
                 runs.remove(pos);
                 iruns.remove(run);
                 return true;
             }
-            XmlCursor c = run.getCTR().newCursor();
-            c.removeXml();
-            c.dispose();
+            try (XmlCursor c = run.getCTR().newCursor()) {
+                c.removeXml();
+            }
             runs.remove(pos);
             iruns.remove(run);
             return true;

Modified: poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java?rev=1902061&r1=1902060&r2=1902061&view=diff
==============================================================================
--- poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java (original)
+++ poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java Sun Jun 19 12:03:51 2022
@@ -155,10 +155,10 @@ public class XWPFRun implements ISDTCont
         String text = xs.getStringValue();
         if (text != null && text.length() >= 1
                 && (Character.isWhitespace(text.charAt(0)) || Character.isWhitespace(text.charAt(text.length()-1)))) {
-            XmlCursor c = xs.newCursor();
-            c.toNextToken();
-            c.insertAttributeWithValue(new QName("http://www.w3.org/XML/1998/namespace", "space"), "preserve");
-            c.dispose();
+            try (XmlCursor c = xs.newCursor()) {
+                c.toNextToken();
+                c.insertAttributeWithValue(new QName("http://www.w3.org/XML/1998/namespace", "space"), "preserve");
+            }
         }
     }
 
@@ -1279,19 +1279,18 @@ public class XWPFRun implements ISDTCont
 
         // Grab the text and tabs of the text run
         // Do so in a way that preserves the ordering
-        XmlCursor c = run.newCursor();
-        c.selectPath("./*");
-        while (c.toNextSelection()) {
-            XmlObject o = c.getObject();
-            if (o instanceof CTRuby) {
-                handleRuby(o, text, false);
-                continue;
+        try (XmlCursor c = run.newCursor()) {
+            c.selectPath("./*");
+            while (c.toNextSelection()) {
+                XmlObject o = c.getObject();
+                if (o instanceof CTRuby) {
+                    handleRuby(o, text, false);
+                    continue;
+                }
+                _getText(o, text);
             }
-            _getText(o, text);
         }
-        c.dispose();
         return text.toString();
-
     }
 
     /**
@@ -1302,19 +1301,19 @@ public class XWPFRun implements ISDTCont
 
         // Grab the text and tabs of the text run
         // Do so in a way that preserves the ordering
-        XmlCursor c = run.newCursor();
-        c.selectPath("./*");
-        while (c.toNextSelection()) {
-            XmlObject o = c.getObject();
-            if (o instanceof CTRuby) {
-                handleRuby(o, text, true);
+        try (XmlCursor c = run.newCursor()) {
+            c.selectPath("./*");
+            while (c.toNextSelection()) {
+                XmlObject o = c.getObject();
+                if (o instanceof CTRuby) {
+                    handleRuby(o, text, true);
+                }
+            }
+            // Any picture text?
+            if (pictureText != null && pictureText.length() > 0) {
+                text.append("\n").append(pictureText).append("\n");
             }
         }
-        // Any picture text?
-        if (pictureText != null && pictureText.length() > 0) {
-            text.append("\n").append(pictureText).append("\n");
-        }
-        c.dispose();
         return text.toString();
     }
 
@@ -1324,34 +1323,33 @@ public class XWPFRun implements ISDTCont
      * @param extractPhonetic extract the phonetic (rt) component or the base component
      */
     private void handleRuby(XmlObject rubyObj, StringBuilder text, boolean extractPhonetic) {
-        XmlCursor c = rubyObj.newCursor();
-
-        //according to the spec, a ruby object
-        //has the phonetic (rt) first, then the actual text (base)
-        //second.
-
-        c.selectPath(".//*");
-        boolean inRT = false;
-        boolean inBase = false;
-        while (c.toNextSelection()) {
-            XmlObject o = c.getObject();
-            if (o instanceof CTRubyContent) {
-                String tagName = o.getDomNode().getNodeName();
-                if ("w:rt".equals(tagName)) {
-                    inRT = true;
-                } else if ("w:rubyBase".equals(tagName)) {
-                    inRT = false;
-                    inBase = true;
-                }
-            } else {
-                if (extractPhonetic && inRT) {
-                    _getText(o, text);
-                } else if (!extractPhonetic && inBase) {
-                    _getText(o, text);
+        try (XmlCursor c = rubyObj.newCursor()) {
+            //according to the spec, a ruby object
+            //has the phonetic (rt) first, then the actual text (base)
+            //second.
+
+            c.selectPath(".//*");
+            boolean inRT = false;
+            boolean inBase = false;
+            while (c.toNextSelection()) {
+                XmlObject o = c.getObject();
+                if (o instanceof CTRubyContent) {
+                    String tagName = o.getDomNode().getNodeName();
+                    if ("w:rt".equals(tagName)) {
+                        inRT = true;
+                    } else if ("w:rubyBase".equals(tagName)) {
+                        inRT = false;
+                        inBase = true;
+                    }
+                } else {
+                    if (extractPhonetic && inRT) {
+                        _getText(o, text);
+                    } else if (!extractPhonetic && inBase) {
+                        _getText(o, text);
+                    }
                 }
             }
         }
-        c.dispose();
     }
 
     private void _getText(XmlObject o, StringBuilder text) {

Modified: poi/trunk/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java?rev=1902061&r1=1902060&r2=1902061&view=diff
==============================================================================
--- poi/trunk/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java (original)
+++ poi/trunk/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java Sun Jun 19 12:03:51 2022
@@ -944,21 +944,21 @@ class TestSignatureInfo {
             boolean found = false;
             for (SignaturePart sp : si.getSignatureParts()) {
                 for (ObjectType ot : sp.getSignatureDocument().getSignature().getObjectArray()) {
-                    XmlCursor xc = ot.newCursor();
-                    if (xc.toChild(SignatureFacet.XADES_132_NS, "QualifyingProperties")) {
-                        QualifyingPropertiesType qpt = (QualifyingPropertiesType) xc.getObject();
-                        assertTrue(qpt.isSetUnsignedProperties());
-                        UnsignedPropertiesType up = qpt.getUnsignedProperties();
-                        assertTrue(up.isSetUnsignedSignatureProperties());
-                        UnsignedSignaturePropertiesType ups = up.getUnsignedSignatureProperties();
-                        assertEquals(1, ups.sizeOfSignatureTimeStampArray());
-                        XAdESTimeStampType ts = ups.getSignatureTimeStampArray(0);
-                        assertEquals(1, ts.sizeOfEncapsulatedTimeStampArray());
-                        EncapsulatedPKIDataType ets = ts.getEncapsulatedTimeStampArray(0);
-                        assertFalse(ets.getStringValue().isEmpty());
-                        found = true;
+                    try (XmlCursor xc = ot.newCursor()) {
+                        if (xc.toChild(SignatureFacet.XADES_132_NS, "QualifyingProperties")) {
+                            QualifyingPropertiesType qpt = (QualifyingPropertiesType) xc.getObject();
+                            assertTrue(qpt.isSetUnsignedProperties());
+                            UnsignedPropertiesType up = qpt.getUnsignedProperties();
+                            assertTrue(up.isSetUnsignedSignatureProperties());
+                            UnsignedSignaturePropertiesType ups = up.getUnsignedSignatureProperties();
+                            assertEquals(1, ups.sizeOfSignatureTimeStampArray());
+                            XAdESTimeStampType ts = ups.getSignatureTimeStampArray(0);
+                            assertEquals(1, ts.sizeOfEncapsulatedTimeStampArray());
+                            EncapsulatedPKIDataType ets = ts.getEncapsulatedTimeStampArray(0);
+                            assertFalse(ets.getStringValue().isEmpty());
+                            found = true;
+                        }
                     }
-                    xc.dispose();
                 }
             }
             assertTrue(found);

Modified: poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFCell.java?rev=1902061&r1=1902060&r2=1902061&view=diff
==============================================================================
--- poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFCell.java (original)
+++ poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFCell.java Sun Jun 19 12:03:51 2022
@@ -73,24 +73,24 @@ class TestSXSSFCell extends BaseTestXCel
                 "\n\nPOI \n",
         };
         for (String str : samplesWithSpaces) {
-            Workbook swb = _testDataProvider.createWorkbook();
-            Cell sCell = swb.createSheet().createRow(0).createCell(0);
-            sCell.setCellValue(str);
-            assertEquals(sCell.getStringCellValue(), str);
+            try (Workbook swb = _testDataProvider.createWorkbook()) {
+                Cell sCell = swb.createSheet().createRow(0).createCell(0);
+                sCell.setCellValue(str);
+                assertEquals(sCell.getStringCellValue(), str);
 
-            // read back as XSSF and check that xml:spaces="preserve" is set
-            XSSFWorkbook xwb = (XSSFWorkbook) _testDataProvider.writeOutAndReadBack(swb);
-            XSSFCell xCell = xwb.getSheetAt(0).getRow(0).getCell(0);
+                // read back as XSSF and check that xml:spaces="preserve" is set
+                try (XSSFWorkbook xwb = (XSSFWorkbook) _testDataProvider.writeOutAndReadBack(swb)) {
+                    XSSFCell xCell = xwb.getSheetAt(0).getRow(0).getCell(0);
 
-            CTRst is = xCell.getCTCell().getIs();
-            assertNotNull(is);
-            XmlCursor c = is.newCursor();
-            c.toNextToken();
-            String t = c.getAttributeText(new QName("http://www.w3.org/XML/1998/namespace", "space"));
-            c.dispose();
-            assertEquals( "preserve", t, "expected xml:spaces=\"preserve\" \"" + str + "\"" );
-            xwb.close();
-            swb.close();
+                    CTRst is = xCell.getCTCell().getIs();
+                    assertNotNull(is);
+                    try (XmlCursor c = is.newCursor()) {
+                        c.toNextToken();
+                        String t = c.getAttributeText(new QName("http://www.w3.org/XML/1998/namespace", "space"));
+                        assertEquals( "preserve", t, "expected xml:spaces=\"preserve\" \"" + str + "\"" );
+                    }
+                }
+            }
         }
     }
 

Modified: poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java?rev=1902061&r1=1902060&r2=1902061&view=diff
==============================================================================
--- poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java (original)
+++ poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java Sun Jun 19 12:03:51 2022
@@ -222,9 +222,9 @@ class TestXWPFTableCell {
 
          // cell have at least one paragraph by default
         XWPFParagraph p0 = cell.getParagraphArray(0);
-        XmlCursor newCursor = p0.getCTP().newCursor();
-        cell.insertNewTbl(newCursor);
-        newCursor.dispose();
+        try (XmlCursor newCursor = p0.getCTP().newCursor()) {
+            cell.insertNewTbl(newCursor);
+        }
 
         assertEquals(1, cell.getTables().size());
         assertEquals(2, cell.getBodyElements().size());



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org