Dispose XmlCursor

Replacing a method argument (XmlCursor) with a local instance and then updating it, doesn't make sense

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1744002 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2016-05-16 09:54:01 +00:00
parent 12425ac107
commit 84be4acea9
8 changed files with 100 additions and 80 deletions

View File

@ -667,11 +667,9 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
private boolean isCursorInBody(XmlCursor cursor) {
XmlCursor verify = cursor.newCursor();
verify.toParent();
try {
return (verify.getObject() == this.ctDocument.getBody());
} finally {
verify.dispose();
}
boolean result = (verify.getObject() == this.ctDocument.getBody());
verify.dispose();
return result;
}
private int getPosOfBodyElement(IBodyElement needle) {

View File

@ -269,8 +269,10 @@ public class XWPFFootnote implements Iterable<XWPFParagraph>, IBody {
i++;
}
bodyElements.add(i, newT);
cursor = t.newCursor();
XmlCursor c2 = t.newCursor();
cursor.toCursor(c2);
cursor.toEndToken();
c2.dispose();
return newT;
}
return null;
@ -283,7 +285,7 @@ public class XWPFFootnote implements Iterable<XWPFParagraph>, IBody {
* @return the inserted paragraph
* @see org.apache.poi.xwpf.usermodel.IBody#insertNewParagraph(XmlCursor cursor)
*/
public XWPFParagraph insertNewParagraph(XmlCursor cursor) {
public XWPFParagraph insertNewParagraph(final XmlCursor cursor) {
if (isCursorInFtn(cursor)) {
String uri = CTP.type.getName().getNamespaceURI();
String localPart = "p";
@ -302,15 +304,19 @@ public class XWPFFootnote implements Iterable<XWPFParagraph>, IBody {
paragraphs.add(pos, newP);
}
int i = 0;
cursor.toCursor(p.newCursor());
XmlCursor p2 = p.newCursor();
cursor.toCursor(p2);
p2.dispose();
while (cursor.toPrevSibling()) {
o = cursor.getObject();
if (o instanceof CTP || o instanceof CTTbl)
i++;
}
bodyElements.add(i, newP);
cursor.toCursor(p.newCursor());
p2 = p.newCursor();
cursor.toCursor(p2);
cursor.toEndToken();
p2.dispose();
return newP;
}
return null;

View File

@ -355,15 +355,19 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
paragraphs.add(pos, newP);
}
int i = 0;
cursor.toCursor(p.newCursor());
XmlCursor p2 = p.newCursor();
cursor.toCursor(p2);
p2.dispose();
while (cursor.toPrevSibling()) {
o = cursor.getObject();
if (o instanceof CTP || o instanceof CTTbl)
i++;
}
bodyElements.add(i, newP);
cursor.toCursor(p.newCursor());
p2 = p.newCursor();
cursor.toCursor(p2);
cursor.toEndToken();
p2.dispose();
return newP;
}
return null;
@ -374,7 +378,7 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
* @param cursor
* @return the inserted table
*/
public XWPFTable insertNewTbl(XmlCursor cursor) {
public XWPFTable insertNewTbl(final XmlCursor cursor) {
if (isCursorInHdrF(cursor)) {
String uri = CTTbl.type.getName().getNamespaceURI();
String localPart = "tbl";
@ -394,15 +398,19 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
tables.add(pos, newT);
}
int i = 0;
cursor = t.newCursor();
while (cursor.toPrevSibling()) {
o = cursor.getObject();
if (o instanceof CTP || o instanceof CTTbl)
XmlCursor cursor2 = t.newCursor();
while (cursor2.toPrevSibling()) {
o = cursor2.getObject();
if (o instanceof CTP || o instanceof CTTbl) {
i++;
}
}
cursor2.dispose();
bodyElements.add(i, newT);
cursor = t.newCursor();
cursor2 = t.newCursor();
cursor.toCursor(cursor2);
cursor.toEndToken();
cursor2.dispose();
return newT;
}
return null;
@ -416,10 +424,9 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
private boolean isCursorInHdrF(XmlCursor cursor) {
XmlCursor verify = cursor.newCursor();
verify.toParent();
if (verify.getObject() == this.headerFooter) {
return true;
}
return false;
boolean result = (verify.getObject() == this.headerFooter);
verify.dispose();
return result;
}
@ -492,6 +499,7 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
cursor.toParent();
XmlObject o = cursor.getObject();
if (!(o instanceof CTRow)) {
cursor.dispose();
return null;
}
CTRow row = (CTRow) o;

View File

@ -1396,58 +1396,59 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para
int beginRunPos = 0, candCharPos = 0;
boolean newList = false;
@SuppressWarnings("deprecation")
CTR[] rArray = paragraph.getRArray();
for (int runPos = startRun; runPos < rArray.length; runPos++) {
int beginTextPos = 0, beginCharPos = 0, textPos = 0, charPos = 0;
CTR ctRun = rArray[runPos];
XmlCursor c = ctRun.newCursor();
c.selectPath("./*");
while (c.toNextSelection()) {
XmlObject o = c.getObject();
if (o instanceof CTText) {
if (textPos >= startText) {
String candidate = ((CTText) o).getStringValue();
if (runPos == startRun)
charPos = startChar;
else
charPos = 0;
for (; charPos < candidate.length(); charPos++) {
if ((candidate.charAt(charPos) == searched.charAt(0)) && (candCharPos == 0)) {
beginTextPos = textPos;
beginCharPos = charPos;
beginRunPos = runPos;
newList = true;
}
if (candidate.charAt(charPos) == searched.charAt(candCharPos)) {
if (candCharPos + 1 < searched.length())
candCharPos++;
else if (newList) {
TextSegement segement = new TextSegement();
segement.setBeginRun(beginRunPos);
segement.setBeginText(beginTextPos);
segement.setBeginChar(beginCharPos);
segement.setEndRun(runPos);
segement.setEndText(textPos);
segement.setEndChar(charPos);
return segement;
try {
while (c.toNextSelection()) {
XmlObject o = c.getObject();
if (o instanceof CTText) {
if (textPos >= startText) {
String candidate = ((CTText) o).getStringValue();
if (runPos == startRun)
charPos = startChar;
else
charPos = 0;
for (; charPos < candidate.length(); charPos++) {
if ((candidate.charAt(charPos) == searched.charAt(0)) && (candCharPos == 0)) {
beginTextPos = textPos;
beginCharPos = charPos;
beginRunPos = runPos;
newList = true;
}
if (candidate.charAt(charPos) == searched.charAt(candCharPos)) {
if (candCharPos + 1 < searched.length())
candCharPos++;
else if (newList) {
TextSegement segement = new TextSegement();
segement.setBeginRun(beginRunPos);
segement.setBeginText(beginTextPos);
segement.setBeginChar(beginCharPos);
segement.setEndRun(runPos);
segement.setEndText(textPos);
segement.setEndChar(charPos);
return segement;
}
} else {
candCharPos = 0;
}
} else {
candCharPos = 0;
}
}
}
textPos++;
} else if (o instanceof CTProofErr) {
c.removeXml();
} else if (o instanceof CTRPr) ;
//do nothing
else
candCharPos = 0;
textPos++;
} else if (o instanceof CTProofErr) {
c.removeXml();
} else if (o instanceof CTRPr) ;
//do nothing
else
candCharPos = 0;
}
} finally {
c.dispose();
}
c.dispose();
}
return null;
}
@ -1465,10 +1466,8 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para
int textEnd = segment.getEndText();
int charEnd = segment.getEndChar();
StringBuilder out = new StringBuilder();
@SuppressWarnings("deprecation")
CTR[] rArray = paragraph.getRArray();
for (int i = runBegin; i <= runEnd; i++) {
@SuppressWarnings("deprecation")
CTText[] tArray = rArray[i].getTArray();
int startText = 0, endText = tArray.length - 1;
if (i == runBegin)

View File

@ -76,7 +76,8 @@ public class XWPFSDTContent implements ISDTContent {
runs.add(run);
bodyElements.add(run);
}
}
}
cursor.dispose();
}
public String getText() {

View File

@ -84,7 +84,8 @@ public class XWPFSDTContentCell implements ISDTContent {
depth--;
}
}
text = sb.toString();
text = sb.toString();
cursor.dispose();
}

View File

@ -258,7 +258,7 @@ public class XWPFTableCell implements IBody, ICell {
* @param cursor
* @return the inserted paragraph
*/
public XWPFParagraph insertNewParagraph(XmlCursor cursor) {
public XWPFParagraph insertNewParagraph(final XmlCursor cursor) {
if (!isCursorInTableCell(cursor)) {
return null;
}
@ -280,19 +280,23 @@ public class XWPFTableCell implements IBody, ICell {
paragraphs.add(pos, newP);
}
int i = 0;
cursor.toCursor(p.newCursor());
XmlCursor p2 = p.newCursor();
cursor.toCursor(p2);
p2.dispose();
while (cursor.toPrevSibling()) {
o = cursor.getObject();
if (o instanceof CTP || o instanceof CTTbl)
i++;
}
bodyElements.add(i, newP);
cursor.toCursor(p.newCursor());
p2 = p.newCursor();
cursor.toCursor(p2);
p2.dispose();
cursor.toEndToken();
return newP;
}
public XWPFTable insertNewTbl(XmlCursor cursor) {
public XWPFTable insertNewTbl(final XmlCursor cursor) {
if (isCursorInTableCell(cursor)) {
String uri = CTTbl.type.getName().getNamespaceURI();
String localPart = "tbl";
@ -312,15 +316,18 @@ public class XWPFTableCell implements IBody, ICell {
tables.add(pos, newT);
}
int i = 0;
cursor = t.newCursor();
while (cursor.toPrevSibling()) {
o = cursor.getObject();
XmlCursor cursor2 = t.newCursor();
while (cursor2.toPrevSibling()) {
o = cursor2.getObject();
if (o instanceof CTP || o instanceof CTTbl)
i++;
}
cursor2.dispose();
bodyElements.add(i, newT);
cursor = t.newCursor();
cursor2 = t.newCursor();
cursor.toCursor(cursor2);
cursor.toEndToken();
cursor2.dispose();
return newT;
}
return null;
@ -332,10 +339,9 @@ public class XWPFTableCell implements IBody, ICell {
private boolean isCursorInTableCell(XmlCursor cursor) {
XmlCursor verify = cursor.newCursor();
verify.toParent();
if (verify.getObject() == this.ctTc) {
return true;
}
return false;
boolean result = (verify.getObject() == this.ctTc);
verify.dispose();
return result;
}
/**

View File

@ -146,7 +146,8 @@ public class XWPFTableRow {
} else if (o instanceof CTSdtCell) {
cells.add(new XWPFSDTCell((CTSdtCell) o, this, table.getBody()));
}
}
}
cursor.dispose();
return cells;
}