From 96079c05b72f63ab043efdaacab35c06fb2d62fd Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sun, 19 Jun 2022 12:03:51 +0000 Subject: [PATCH] remove use of some calls to deprecated cursor dispose() git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1902061 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xwpf/usermodel/XWPFFooter.java | 10 +- .../poi/xwpf/usermodel/XWPFParagraph.java | 81 +++++++-------- .../apache/poi/xwpf/usermodel/XWPFRun.java | 98 +++++++++---------- .../poifs/crypt/dsig/TestSignatureInfo.java | 28 +++--- .../poi/xssf/streaming/TestSXSSFCell.java | 36 +++---- .../poi/xwpf/usermodel/TestXWPFTableCell.java | 6 +- 6 files changed, 122 insertions(+), 137 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFFooter.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFFooter.java index 90f7bc1f0e..ba077227a0 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFFooter.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFFooter.java @@ -47,8 +47,7 @@ public class XWPFFooter extends XWPFHeaderFooter { 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 XWPFHeaderFooter { } } - } finally { - cursor.dispose(); } } @@ -98,8 +95,7 @@ public class XWPFFooter extends XWPFHeaderFooter { 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 XWPFHeaderFooter { bodyElements.add(c); } } - } finally { - cursor.dispose(); } } catch (Exception e) { throw new POIXMLException(e); diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java index 2077b857ff..2b08c4ef6b 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java @@ -72,8 +72,7 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para // 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 IBodyElement, IRunBody, ISDTContents, Para } } - } finally { - c.dispose(); } } } @@ -1610,28 +1607,28 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para 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; + 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; } - 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 IBodyElement, IRunBody, ISDTContents, Para * 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 IBodyElement, IRunBody, ISDTContents, Para 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 IBodyElement, IRunBody, ISDTContents, Para candCharPos = 0; } } - } finally { - c.dispose(); } } return null; @@ -1765,10 +1759,9 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para // 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 IBodyElement, IRunBody, ISDTContents, Para // 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; diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java index 0a77b32d0f..abd499f3f0 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java @@ -155,10 +155,10 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { 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 ISDTContents, IRunElement, CharacterRun { // 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 ISDTContents, IRunElement, CharacterRun { // 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 ISDTContents, IRunElement, CharacterRun { * @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) { diff --git a/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java b/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java index 91cda44fd7..831170d992 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java +++ b/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java @@ -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); diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFCell.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFCell.java index a1377a3b5d..2670a6e291 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFCell.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/streaming/TestSXSSFCell.java @@ -73,24 +73,24 @@ class TestSXSSFCell extends BaseTestXCell { "\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); - - // 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); - - 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(); + 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 + try (XSSFWorkbook xwb = (XSSFWorkbook) _testDataProvider.writeOutAndReadBack(swb)) { + XSSFCell xCell = xwb.getSheetAt(0).getRow(0).getCell(0); + + 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 + "\"" ); + } + } + } } } diff --git a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java index 830f706c83..669b3e0992 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java @@ -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()); -- 2.39.5