remove use of some calls to deprecated cursor dispose()

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1902057 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-06-19 11:02:01 +00:00
parent 4b2913afd3
commit b7261c1621
31 changed files with 255 additions and 464 deletions

View File

@ -109,9 +109,8 @@ public final class XPathHelper {
public static <T extends XmlObject> T selectProperty(XmlObject startObject, Class<T> resultClass, XSLFShape.ReparseFactory<T> factory, QName[]... path)
throws XmlException {
XmlObject xo = startObject;
XmlCursor cur = xo.newCursor();
XmlCursor innerCur = null;
try {
try (XmlCursor cur = startObject.newCursor()) {
innerCur = selectProperty(cur, path, 0, factory != null, false);
if (innerCur == null) {
return null;
@ -133,9 +132,8 @@ public final class XPathHelper {
return (T)xo;
} finally {
cur.dispose();
if (innerCur != null) {
innerCur.dispose();
innerCur.close();
}
}
}
@ -187,22 +185,17 @@ public final class XPathHelper {
for (int i=0; i<choices; i++) {
// TODO: check [Requires] attribute of [Choice] element, if we can handle the content
AlternateContentDocument.AlternateContent.Choice choice = alterCont.getChoiceArray(i);
XmlCursor cCur = choice.newCursor();
XmlCursor innerCur = null;
try {
try (XmlCursor cCur = choice.newCursor()) {
String requiresNS = cCur.namespaceForPrefix(choice.getRequires());
if (MAC_DML_NS.equalsIgnoreCase(requiresNS)) {
// Mac DML usually contains PDFs ...
continue;
}
innerCur = selectProperty(cCur, path, offset, reparseAlternate, true);
if (innerCur != null) {
if (innerCur != null && innerCur != cCur) {
return innerCur;
}
} finally {
if (innerCur != cCur) {
cCur.dispose();
}
}
}
@ -217,7 +210,7 @@ public final class XPathHelper {
return innerCur;
} finally {
if (innerCur != fCur) {
fCur.dispose();
fCur.close();
}
}
}

View File

@ -195,10 +195,10 @@ public abstract class SignatureLine {
}
public void setSignatureShape(CTSignatureLine signatureLine) {
XmlCursor cur = signatureLine.newCursor();
cur.toParent();
this.signatureShape = (CTShape)cur.getObject();
cur.dispose();
try (XmlCursor cur = signatureLine.newCursor()) {
cur.toParent();
this.signatureShape = (CTShape)cur.getObject();
}
}
public void updateSignatureConfig(SignatureConfig config) throws IOException {
@ -231,12 +231,9 @@ public abstract class SignatureLine {
setSuggestedSigner(signatureLine.getSuggestedsigner());
setSuggestedSigner2(signatureLine.getSuggestedsigner2());
setSuggestedSignerEmail(signatureLine.getSuggestedsigneremail());
XmlCursor cur = signatureLine.newCursor();
try {
try (XmlCursor cur = signatureLine.newCursor()) {
// the signinginstructions are actually qualified, but our schema version is too old
setSigningInstructions(cur.getAttributeText(new QName(MS_OFFICE_URN, "signinginstructions")));
} finally {
cur.dispose();
}
}
@ -260,14 +257,14 @@ public abstract class SignatureLine {
CTGroup grp = CTGroup.Factory.newInstance();
grp.addNewShape();
XmlCursor contCur = signatureContainer.newCursor();
contCur.toEndToken();
XmlCursor otherC = grp.newCursor();
otherC.copyXmlContents(contCur);
otherC.dispose();
contCur.toPrevSibling();
signatureShape = (CTShape)contCur.getObject();
contCur.dispose();
try (XmlCursor contCur = signatureContainer.newCursor()) {
contCur.toEndToken();
try (XmlCursor otherC = grp.newCursor()) {
otherC.copyXmlContents(contCur);
}
contCur.toPrevSibling();
signatureShape = (CTShape)contCur.getObject();
}
signatureShape.setAlt("Microsoft Office Signature Line...");
signatureShape.setStyle("width:191.95pt;height:96.05pt");
@ -298,9 +295,9 @@ public abstract class SignatureLine {
xsl.setProvid("{00000000-0000-0000-0000-000000000000}");
xsl.setExt(STExt.EDIT);
xsl.setSigninginstructionsset(STTrueFalse.T);
XmlCursor cur = xsl.newCursor();
cur.setAttributeText(new QName(MS_OFFICE_URN, "signinginstructions"), signingInstructions);
cur.dispose();
try (XmlCursor cur = xsl.newCursor()) {
cur.setAttributeText(new QName(MS_OFFICE_URN, "signinginstructions"), signingInstructions);
}
} catch (IOException | InvalidFormatException e) {
// shouldn't happen ...
throw new POIXMLException("Can't generate signature line image", e);

View File

@ -351,21 +351,20 @@ public class XAdESSignatureFacet implements SignatureFacet {
}
protected static void insertXChild(XmlObject root, XmlObject child) {
XmlCursor rootCursor = root.newCursor();
rootCursor.toEndToken();
XmlCursor childCursor = child.newCursor();
childCursor.toNextToken();
childCursor.moveXml(rootCursor);
childCursor.dispose();
rootCursor.dispose();
try (XmlCursor rootCursor = root.newCursor()) {
rootCursor.toEndToken();
try (XmlCursor childCursor = child.newCursor()) {
childCursor.toNextToken();
childCursor.moveXml(rootCursor);
}
}
}
/**
* Workaround for Document.importNode, which causes SIGSEGV in JDK14 (Ubuntu)
*/
private static Element importNode(Document document, XmlObject xo) {
XmlCursor cur = xo.newCursor();
try {
try (XmlCursor cur = xo.newCursor()) {
QName elName = cur.getName();
Element lastNode = document.createElementNS(elName.getNamespaceURI(), elName.getLocalPart());
while (cur.hasNextToken()) {
@ -415,8 +414,6 @@ public class XAdESSignatureFacet implements SignatureFacet {
}
}
return lastNode;
} finally {
cur.dispose();
}
}
}

View File

@ -56,8 +56,7 @@ public class XSLFPath implements PathIf {
@Override
public Path2D.Double getPath(Context ctx) {
Path2D.Double path2D = new Path2D.Double();
XmlCursor cur = pathXml.newCursor();
try {
try (XmlCursor cur = pathXml.newCursor()) {
for (boolean hasNext = cur.toFirstChild(); hasNext; hasNext = cur.toNextSibling()) {
XmlObject xo = cur.getObject();
PathCommand pc;
@ -78,8 +77,6 @@ public class XSLFPath implements PathIf {
}
pc.execute(path2D, ctx);
}
} finally {
cur.dispose();
}
return path2D;
}

View File

@ -138,13 +138,10 @@ public final class XSLFChart extends XDDFChart {
xfrm.setOff(off);
CTGraphicalObjectData gr = frame.addNewGraphic().addNewGraphicData();
XmlCursor grCur = gr.newCursor();
try {
try (XmlCursor grCur = gr.newCursor()) {
grCur.toNextToken();
grCur.beginElement(new QName(CHART_URI, "chart"));
grCur.insertAttributeWithValue("id", PackageRelationshipTypes.CORE_PROPERTIES_ECMA376_NS, rID);
} finally {
grCur.dispose();
}
gr.setUri(CHART_URI);

View File

@ -141,9 +141,8 @@ public class XSLFColor {
return _phClr == null ? null : toColor(_phClr, theme);
}
final XmlCursor cur = obj.newCursor();
Color color = null;
try {
try (XmlCursor cur = obj.newCursor()) {
XmlObject ch;
for (int idx=0; color == null && (ch = nextObject(obj, cur, idx)) != null; idx++) {
if (ch instanceof CTHslColor) {
@ -162,8 +161,6 @@ public class XSLFColor {
throw new IllegalArgumentException("Unexpected color choice: " + ch.getClass());
}
}
} finally {
cur.dispose();
}
return color;
}
@ -254,8 +251,7 @@ public class XSLFColor {
if (obj == null) {
continue;
}
XmlCursor cur = obj.newCursor();
try {
try (XmlCursor cur = obj.newCursor()) {
if (!(
cur.toChild(XSLFRelation.NS_DRAWINGML, elem) ||
(cur.toFirstChild() && cur.toChild(XSLFRelation.NS_DRAWINGML, elem))
@ -266,8 +262,6 @@ public class XSLFColor {
if (str != null && !"".equals(str)) {
return Integer.parseInt(str);
}
} finally {
cur.dispose();
}
}
return -1;

View File

@ -143,9 +143,8 @@ public class XSLFFreeformShape extends XSLFAutoShape
}
final CTCustomGeometry2D geom = ((CTShapeProperties)xo).getCustGeom();
for(CTPath2D spPath : geom.getPathLst().getPathArray()){
XmlCursor cursor = spPath.newCursor();
try {
for(CTPath2D spPath : geom.getPathLst().getPathArray()) {
try (XmlCursor cursor = spPath.newCursor()) {
if (cursor.toFirstChild()) {
do {
final XmlObject ch = cursor.getObject();
@ -164,8 +163,6 @@ public class XSLFFreeformShape extends XSLFAutoShape
}
} while (cursor.toNextSibling());
}
} finally {
cursor.dispose();
}
}

View File

@ -190,12 +190,9 @@ public class XSLFGraphicFrame extends XSLFShape implements GraphicalFrame<XSLFSh
String xpath = "declare namespace c='" + DRAWINGML_CHART_URI + "' c:chart";
XmlObject[] obj = getGraphicalData().selectPath(xpath);
if (obj != null && obj.length == 1) {
XmlCursor c = obj[0].newCursor();
try {
try (XmlCursor c = obj[0].newCursor()) {
QName idQualifiedName = new QName(CORE_PROPERTIES_ECMA376_NS, "id");
id = c.getAttributeText(idQualifiedName);
} finally {
c.dispose();
}
}
if (id == null) {
@ -230,8 +227,7 @@ public class XSLFGraphicFrame extends XSLFShape implements GraphicalFrame<XSLFSh
String xpath = "declare namespace c='" + DRAWINGML_CHART_URI + "' c:chart";
XmlObject[] obj = objData.selectPath(xpath);
if (obj != null && obj.length == 1) {
XmlCursor c = obj[0].newCursor();
try {
try (XmlCursor c = obj[0].newCursor()) {
// duplicate chart with embedded workbook
QName idQualifiedName = new QName(CORE_PROPERTIES_ECMA376_NS, "id");
String id = c.getAttributeText(idQualifiedName);
@ -254,8 +250,6 @@ public class XSLFGraphicFrame extends XSLFShape implements GraphicalFrame<XSLFSh
}
} catch (InvalidFormatException | IOException e) {
throw new POIXMLException(e);
} finally {
c.dispose();
}
}
}
@ -265,10 +259,8 @@ public class XSLFGraphicFrame extends XSLFShape implements GraphicalFrame<XSLFSh
String xpath = "declare namespace dgm='" + DRAWINGML_DIAGRAM_URI + "' $this//dgm:relIds";
XmlObject[] obj = objData.selectPath(xpath);
if(obj != null && obj.length == 1) {
XmlCursor c = obj[0].newCursor();
XSLFSheet sheet = srcShape.getSheet();
try {
try (XmlCursor c = obj[0].newCursor()) {
String dm = c.getAttributeText(new QName(CORE_PROPERTIES_ECMA376_NS, "dm"));
PackageRelationship dmRel = sheet.getPackagePart().getRelationship(dm);
PackagePart dmPart = sheet.getPackagePart().getRelatedPart(dmRel);
@ -291,8 +283,6 @@ public class XSLFGraphicFrame extends XSLFShape implements GraphicalFrame<XSLFSh
} catch (InvalidFormatException e){
throw new POIXMLException(e);
} finally {
c.dispose();
}
}
}

View File

@ -250,8 +250,7 @@ public class XSLFObjectShape extends XSLFGraphicFrame implements ObjectShape<XSL
frame.addNewXfrm();
CTGraphicalObjectData gr = frame.addNewGraphic().addNewGraphicData();
gr.setUri(OLE_URI);
XmlCursor grCur = gr.newCursor();
try {
try (XmlCursor grCur = gr.newCursor()) {
grCur.toEndToken();
grCur.beginElement(new QName(PML_NS, "oleObj"));
grCur.insertElement(new QName(PML_NS, "embed"));
@ -281,15 +280,10 @@ public class XSLFObjectShape extends XSLFGraphicFrame implements ObjectShape<XSL
spPr.addNewPrstGeom().setPrst(STShapeType.RECT);
XmlCursor picCur = grpShp.newCursor();
try {
try (XmlCursor picCur = grpShp.newCursor()) {
picCur.toStartDoc();
picCur.moveXmlContents(grCur);
} finally {
picCur.dispose();
}
} finally {
grCur.dispose();
}
return frame;

View File

@ -240,14 +240,11 @@ public class XSLFPictureShape extends XSLFSimpleShape
if (bitmapId == -1) {
extBitmap = extLst.addNewExt();
extBitmap.setUri(BITMAP_URI);
XmlCursor cur = extBitmap.newCursor();
try {
try (XmlCursor cur = extBitmap.newCursor()) {
cur.toEndToken();
cur.beginElement(new QName(MS_DML_NS, "useLocalDpi", "a14"));
cur.insertNamespace("a14", MS_DML_NS);
cur.insertAttributeWithValue("val", "0");
} finally {
cur.dispose();
}
}
@ -263,14 +260,11 @@ public class XSLFPictureShape extends XSLFSimpleShape
CTOfficeArtExtension svgBitmap = extLst.addNewExt();
svgBitmap.setUri(SVG_URI);
XmlCursor cur = svgBitmap.newCursor();
try {
try (XmlCursor cur = svgBitmap.newCursor()) {
cur.toEndToken();
cur.beginElement(new QName(MS_SVG_NS, "svgBlip", "asvg"));
cur.insertNamespace("asvg", MS_SVG_NS);
cur.insertAttributeWithValue(EMBED_TAG, svgRelId);
} finally {
cur.dispose();
}
}
@ -337,14 +331,11 @@ public class XSLFPictureShape extends XSLFSimpleShape
int size = extLst.sizeOfExtArray();
for (int i = 0; i < size; i++) {
XmlCursor cur = extLst.getExtArray(i).newCursor();
try {
try (XmlCursor cur = extLst.getExtArray(i).newCursor()) {
if (cur.toChild(MS_SVG_NS, "svgBlip")) {
String svgRelId = cur.getAttributeText(EMBED_TAG);
return (svgRelId != null) ? (XSLFPictureData) getSheet().getRelationById(svgRelId) : null;
}
} finally {
cur.dispose();
}
}
return null;
@ -431,13 +422,10 @@ public class XSLFPictureShape extends XSLFSimpleShape
String xpath = "declare namespace a14='"+ MS_DML_NS +"' $this//a14:imgProps/a14:imgLayer";
XmlObject[] obj = ext.selectPath(xpath);
if(obj != null && obj.length == 1) {
XmlCursor c = obj[0].newCursor();
try {
try (XmlCursor c = obj[0].newCursor()) {
String id = c.getAttributeText(EMBED_TAG);
String newId = getSheet().importBlip(id, p.getSheet());
c.setAttributeText(EMBED_TAG, newId);
} finally {
c.dispose();
}
}
}

View File

@ -1154,12 +1154,9 @@ import org.openxmlformats.schemas.presentationml.x2006.main.CTBackgroundProperti
@Override
public boolean isLineStyle() {
XmlCursor cur = props.newCursor();
try {
try (XmlCursor cur = props.newCursor()) {
String name = cur.getName().getLocalPart();
return "lnRef".equals(name);
} finally {
cur.dispose();
}
}
}

View File

@ -256,16 +256,13 @@ public abstract class XSLFShape implements Shape<XSLFShape,XSLFTextParagraph> {
@SuppressWarnings({"unchecked", "WeakerAccess", "unused", "SameParameterValue"})
protected <T extends XmlObject> T getChild(Class<T> childClass, String namespace, String nodename) {
T child = null;
XmlCursor cur = getXmlObject().newCursor();
try {
try (XmlCursor cur = getXmlObject().newCursor()) {
if (cur.toChild(namespace, nodename)) {
child = (T)cur.getObject();
}
if (cur.toChild(XSLFRelation.NS_DRAWINGML, nodename)) {
child = (T)cur.getObject();
}
} finally {
cur.dispose();
}
return child;
}
@ -461,13 +458,10 @@ public abstract class XSLFShape implements Shape<XSLFShape,XSLFTextParagraph> {
return null;
}
XSLFFillProperties fp = null;
XmlCursor cur = styleLst.newCursor();
try {
try (XmlCursor cur = styleLst.newCursor()) {
if (cur.toChild(Math.toIntExact(childIdx))) {
fp = XSLFPropertiesDelegate.getFillDelegate(cur.getObject());
}
} finally {
cur.dispose();
}
CTSchemeColor phClr = fillRef.getSchemeClr();

View File

@ -140,8 +140,7 @@ implements XSLFShapeContainer, Sheet<XSLFShape,XSLFTextParagraph> {
final XSLFSheet sheet = (parent instanceof XSLFSheet) ? (XSLFSheet)parent : ((XSLFShape)parent).getSheet();
List<XSLFShape> shapes = new ArrayList<>();
XmlCursor cur = spTree.newCursor();
try {
try (XmlCursor cur = spTree.newCursor()) {
for (boolean b = cur.toFirstChild(); b; b = cur.toNextSibling()) {
XmlObject ch = cur.getObject();
if(ch instanceof CTShape){
@ -174,8 +173,6 @@ implements XSLFShapeContainer, Sheet<XSLFShape,XSLFTextParagraph> {
cur.pop();
}
}
} finally {
cur.dispose();
}
for (final XSLFShape s : shapes) {

View File

@ -63,8 +63,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
super(shape, sheet);
CTGraphicalObjectData god = shape.getGraphic().getGraphicData();
XmlCursor xc = god.newCursor();
try {
try (XmlCursor xc = god.newCursor()) {
if (!xc.toChild(XSLFRelation.NS_DRAWINGML, "tbl")) {
throw new IllegalStateException("a:tbl element was not found in\n " + god);
}
@ -81,8 +80,6 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
throw new IllegalStateException(errStr);
}
_table = (CTTable)xo;
} finally {
xc.dispose();
}
_rows = new ArrayList<>(_table.sizeOfTrArray());
@ -260,22 +257,16 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
frame.addNewXfrm();
CTGraphicalObjectData gr = frame.addNewGraphic().addNewGraphicData();
XmlCursor grCur = gr.newCursor();
try {
try (XmlCursor grCur = gr.newCursor()) {
grCur.toNextToken();
grCur.beginElement(new QName(XSLFRelation.NS_DRAWINGML, "tbl"));
CTTable tbl = CTTable.Factory.newInstance();
tbl.addNewTblPr();
tbl.addNewTblGrid();
XmlCursor tblCur = tbl.newCursor();
try {
try (XmlCursor tblCur = tbl.newCursor()) {
tblCur.moveXmlContents(grCur);
} finally {
tblCur.dispose();
}
} finally {
grCur.dispose();
}
gr.setUri(TABLE_URI);
return frame;

View File

@ -66,8 +66,7 @@ public class XSLFTextParagraph implements TextParagraph<XSLFShape,XSLFTextParagr
_runs = new ArrayList<>();
_shape = shape;
XmlCursor c = _p.newCursor();
try {
try (XmlCursor c = _p.newCursor()) {
if (c.toFirstChild()) {
do {
XmlObject r = c.getObject();
@ -78,8 +77,6 @@ public class XSLFTextParagraph implements TextParagraph<XSLFShape,XSLFTextParagr
}
} while (c.toNextSibling());
}
} finally {
c.dispose();
}
}
@ -751,8 +748,7 @@ public class XSLFTextParagraph implements TextParagraph<XSLFShape,XSLFTextParagr
for (XSLFSheet m = masterSheet; m != null; m = (XSLFSheet)m.getMasterSheet()) {
masterSheet = m;
XmlObject xo = masterSheet.getXmlObject();
XmlCursor cur = xo.newCursor();
try {
try (XmlCursor cur = xo.newCursor()) {
cur.push();
if ((cur.toChild(nsPML, "txStyles") && cur.toChild(nsPML, defaultStyleSelector)) ||
(cur.pop() && cur.toChild(nsPML, "notesStyle"))) {
@ -765,8 +761,6 @@ public class XSLFTextParagraph implements TextParagraph<XSLFShape,XSLFTextParagr
level--;
}
}
} finally {
cur.dispose();
}
}
@ -805,17 +799,11 @@ public class XSLFTextParagraph implements TextParagraph<XSLFShape,XSLFTextParagr
thisP.removeFld(i-1);
}
XmlCursor thisC = thisP.newCursor();
try {
try (XmlCursor thisC = thisP.newCursor()) {
thisC.toEndToken();
XmlCursor otherC = otherP.newCursor();
try {
try (XmlCursor otherC = otherP.newCursor()) {
otherC.copyXmlContents(thisC);
} finally {
otherC.dispose();
}
} finally {
thisC.dispose();
}
for (XSLFTextRun tr : other.getTextRuns()) {

View File

@ -462,8 +462,7 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
ole1.setId(olePR.getId());
CTTwoCellAnchor ctAnchor;
final XmlCursor cur1 = ole1.newCursor();
try {
try (XmlCursor cur1 = ole1.newCursor()) {
cur1.toEndToken();
cur1.beginElement("objectPr", XSSFRelation.NS_SPREADSHEETML);
cur1.insertAttributeWithValue("id", PackageRelationshipTypes.CORE_PROPERTIES_ECMA376_NS, imgSheetPR.getId());
@ -473,11 +472,8 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
ctAnchor = createTwoCellAnchor((XSSFClientAnchor) anchor);
final XmlCursor cur2 = ctAnchor.newCursor();
try {
try (XmlCursor cur2 = ctAnchor.newCursor()) {
cur2.copyXmlContents(cur1);
} finally {
cur2.dispose();
}
cur1.toParent();
@ -485,8 +481,6 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
cur1.setName(new QName(XSSFRelation.NS_SPREADSHEETML, "from"));
cur1.toNextSibling();
cur1.setName(new QName(XSSFRelation.NS_SPREADSHEETML, "to"));
} finally {
cur1.dispose();
}
// add a new shape and link OLE & image part
@ -503,12 +497,9 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
cNvPr.setId(shapeId);
cNvPr.setName("Object " + shapeId);
XmlCursor extCur = cNvPr.getExtLst().getExtArray(0).newCursor();
try {
try (XmlCursor extCur = cNvPr.getExtLst().getExtArray(0).newCursor()) {
extCur.toFirstChild();
extCur.setAttributeText(new QName("spid"), "_x0000_s" + shapeId);
} finally {
extCur.dispose();
}
XSSFObjectData shape = new XSSFObjectData(this, ctShape);
@ -595,13 +586,10 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
*/
public List<XSSFShape> getShapes() {
List<XSSFShape> lst = new ArrayList<>();
XmlCursor cur = drawing.newCursor();
try {
try (XmlCursor cur = drawing.newCursor()) {
if (cur.toFirstChild()) {
addShapes(cur, lst);
}
} finally {
cur.dispose();
}
return lst;
}
@ -611,87 +599,76 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
*/
public List<XSSFShape> getShapes(XSSFShapeGroup groupshape) {
List<XSSFShape> lst = new ArrayList<>();
XmlCursor cur = groupshape.getCTGroupShape().newCursor();
try {
try (XmlCursor cur = groupshape.getCTGroupShape().newCursor()) {
addShapes(cur, lst);
} finally {
cur.dispose();
}
return lst;
}
private void addShapes(XmlCursor cur, List<XSSFShape> lst) {
try {
do {
cur.push();
if (cur.toFirstChild()) {
do {
XmlObject obj = cur.getObject();
do {
cur.push();
if (cur.toFirstChild()) {
do {
XmlObject obj = cur.getObject();
XSSFShape shape;
if (obj instanceof CTMarker) {
// ignore anchor elements
continue;
} else if (obj instanceof CTPicture) {
shape = new XSSFPicture(this, (CTPicture) obj);
} else if (obj instanceof CTConnector) {
shape = new XSSFConnector(this, (CTConnector) obj);
} else if (obj instanceof CTShape) {
shape = hasOleLink(obj) ? new XSSFObjectData(this, (CTShape) obj)
: new XSSFSimpleShape(this, (CTShape) obj);
} else if (obj instanceof CTGraphicalObjectFrame) {
shape = new XSSFGraphicFrame(this, (CTGraphicalObjectFrame) obj);
} else if (obj instanceof CTGroupShape) {
shape = new XSSFShapeGroup(this, (CTGroupShape) obj);
} else if (obj instanceof XmlAnyTypeImpl) {
LOG.atWarn().log("trying to parse AlternateContent, this unlinks the returned Shapes from the underlying xml content, so those shapes can't be used to modify the drawing, i.e. modifications will be ignored!");
XSSFShape shape;
if (obj instanceof CTMarker) {
// ignore anchor elements
continue;
} else if (obj instanceof CTPicture) {
shape = new XSSFPicture(this, (CTPicture) obj);
} else if (obj instanceof CTConnector) {
shape = new XSSFConnector(this, (CTConnector) obj);
} else if (obj instanceof CTShape) {
shape = hasOleLink(obj) ? new XSSFObjectData(this, (CTShape) obj)
: new XSSFSimpleShape(this, (CTShape) obj);
} else if (obj instanceof CTGraphicalObjectFrame) {
shape = new XSSFGraphicFrame(this, (CTGraphicalObjectFrame) obj);
} else if (obj instanceof CTGroupShape) {
shape = new XSSFShapeGroup(this, (CTGroupShape) obj);
} else if (obj instanceof XmlAnyTypeImpl) {
LOG.atWarn().log("trying to parse AlternateContent, this unlinks the returned Shapes from the underlying xml content, so those shapes can't be used to modify the drawing, i.e. modifications will be ignored!");
// XmlAnyTypeImpl is returned for AlternateContent
// parts, which might contain a CTDrawing
cur.push();
cur.toFirstChild();
XmlCursor cur2 = null;
try {
// need to parse AlternateContent again,
// otherwise the child elements aren't typed,
// but also XmlAnyTypes
CTDrawing alterWS = CTDrawing.Factory.parse(cur.newXMLStreamReader());
cur2 = alterWS.newCursor();
// XmlAnyTypeImpl is returned for AlternateContent
// parts, which might contain a CTDrawing
cur.push();
cur.toFirstChild();
try {
// need to parse AlternateContent again,
// otherwise the child elements aren't typed,
// but also XmlAnyTypes
CTDrawing alterWS = CTDrawing.Factory.parse(cur.newXMLStreamReader());
try (XmlCursor cur2 = alterWS.newCursor()) {
if (cur2.toFirstChild()) {
addShapes(cur2, lst);
}
} catch (XmlException e) {
LOG.atWarn().withThrowable(e).log("unable to parse CTDrawing in alternate content.");
} finally {
if (cur2 != null) {
cur2.dispose();
}
cur.pop();
}
continue;
} else {
// ignore anything else
continue;
} catch (XmlException e) {
LOG.atWarn().withThrowable(e).log("unable to parse CTDrawing in alternate content.");
} finally {
cur.pop();
}
continue;
} else {
// ignore anything else
continue;
}
assert (shape != null);
shape.anchor = getAnchorFromParent(obj);
lst.add(shape);
assert (shape != null);
shape.anchor = getAnchorFromParent(obj);
lst.add(shape);
} while (cur.toNextSibling());
}
cur.pop();
} while (cur.toNextSibling());
} finally {
cur.dispose();
}
} while (cur.toNextSibling());
}
cur.pop();
} while (cur.toNextSibling());
}
private boolean hasOleLink(XmlObject shape) {
QName uriName = new QName(null, "uri");
String xquery = "declare namespace a='" + XSSFRelation.NS_DRAWINGML + "' .//a:extLst/a:ext";
XmlCursor cur = shape.newCursor();
try {
try (XmlCursor cur = shape.newCursor()) {
cur.selectPath(xquery);
while (cur.toNextSelection()) {
String uri = cur.getAttributeText(uriName);
@ -699,8 +676,6 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
return true;
}
}
} finally {
cur.dispose();
}
return false;
}
@ -709,13 +684,10 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
XSSFAnchor anchor = null;
XmlObject parentXbean = null;
XmlCursor cursor = obj.newCursor();
try {
try (XmlCursor cursor = obj.newCursor()) {
if (cursor.toParent()) {
parentXbean = cursor.getObject();
}
} finally {
cursor.dispose();
}
if (parentXbean != null) {
if (parentXbean instanceof CTTwoCellAnchor) {

View File

@ -192,13 +192,10 @@ public final class XSSFGraphicFrame extends XSSFShape {
private void appendChartElement(CTGraphicalObjectData data, String id) {
String r_namespaceUri = STRelationshipId.type.getName().getNamespaceURI();
String c_namespaceUri = XSSFDrawing.NAMESPACE_C;
XmlCursor cursor = data.newCursor();
try {
try (XmlCursor cursor = data.newCursor()) {
cursor.toNextToken();
cursor.beginElement(new QName(c_namespaceUri, "chart", "c"));
cursor.insertAttributeWithValue(new QName(r_namespaceUri, "id", "r"), id);
} finally {
cursor.dispose();
}
data.setUri(c_namespaceUri);
}

View File

@ -82,14 +82,11 @@ public class XSSFObjectData extends XSSFSimpleShape implements ObjectData {
// https://msdn.microsoft.com/en-us/library/dd911027(v=office.12).aspx
CTOfficeArtExtension ext = extLst.addNewExt();
ext.setUri("{63B3BB69-23CF-44E3-9099-C40C66FF867C}");
XmlCursor cur = ext.newCursor();
try {
try (XmlCursor cur = ext.newCursor()) {
cur.toEndToken();
cur.beginElement(new QName(drawNS, "compatExt", "a14"));
cur.insertNamespace("a14", drawNS);
cur.insertAttributeWithValue("spid", "_x0000_s1");
} finally {
cur.dispose();
}
nv.addNewCNvSpPr();
@ -188,15 +185,12 @@ public class XSSFObjectData extends XSSFSimpleShape implements ObjectData {
@Override
public XSSFPictureData getPictureData() {
XmlCursor cur = getOleObject().newCursor();
try {
try (XmlCursor cur = getOleObject().newCursor()) {
if (cur.toChild(XSSFRelation.NS_SPREADSHEETML, "objectPr")) {
String blipId = cur.getAttributeText(new QName(PackageRelationshipTypes.CORE_PROPERTIES_ECMA376_NS, "id"));
return (XSSFPictureData)getSheet().getRelationById(blipId);
}
return null;
} finally {
cur.dispose();
}
}

View File

@ -489,12 +489,9 @@ public class XSSFRichTextString implements RichTextString {
char firstChar = text.charAt(0);
char lastChar = text.charAt(text.length() - 1);
if(Character.isWhitespace(firstChar) || Character.isWhitespace(lastChar)) {
XmlCursor c = xs.newCursor();
try {
try (XmlCursor c = xs.newCursor()) {
c.toNextToken();
c.insertAttributeWithValue(new QName("http://www.w3.org/XML/1998/namespace", "space"), "preserve");
} finally {
c.dispose();
}
}
}

View File

@ -4725,8 +4725,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet, OoxmlSheetEx
// we use a XmlCursor here to handle oleObject with-/out AlternateContent wrappers
String xquery = "declare namespace p='"+XSSFRelation.NS_SPREADSHEETML+"' .//p:oleObject";
XmlCursor cur = getCTWorksheet().getOleObjects().newCursor();
try {
try (XmlCursor cur = getCTWorksheet().getOleObjects().newCursor()) {
cur.selectPath(xquery);
CTOleObject coo = null;
while (cur.toNextSelection()) {
@ -4765,8 +4764,6 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet, OoxmlSheetEx
}
}
return coo;
} finally {
cur.dispose();
}
}

View File

@ -59,8 +59,7 @@ public class XSSFTableStyle implements TableStyle {
List<CTDxf> dxfList = new ArrayList<>();
// CT* classes don't handle "mc:AlternateContent" elements, so get the Dxf instances manually
final XmlCursor cur = dxfs.newCursor();
try {
try (XmlCursor cur = dxfs.newCursor()) {
// sometimes there are namespaces sometimes not.
String xquery = "declare namespace x='"+XSSFRelation.NS_SPREADSHEETML+"' .//x:dxf | .//dxf";
cur.selectPath(xquery);
@ -82,8 +81,6 @@ public class XSSFTableStyle implements TableStyle {
}
}
}
} finally {
cur.dispose();
}
for (CTTableStyleElement element : tableStyle.getTableStyleElementList()) {

View File

@ -150,9 +150,7 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart {
" xmlns=\""+NS_SPREADSHEETML+"\"", "")
, xopt);
XmlCursor cur = root.getXml().newCursor();
try {
try (XmlCursor cur = root.getXml().newCursor()) {
for (boolean found = cur.toFirstChild(); found; found = cur.toNextSibling()) {
XmlObject xo = cur.getObject();
if (xo instanceof CTShapetype) {
@ -168,21 +166,16 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart {
}
}
}
} finally {
cur.dispose();
}
}
protected List<XmlObject> getItems(){
List<XmlObject> items = new ArrayList<>();
XmlCursor cur = root.getXml().newCursor();
try {
try (XmlCursor cur = root.getXml().newCursor()) {
for (boolean found = cur.toFirstChild(); found; found = cur.toNextSibling()) {
items.add(cur.getObject());
}
} finally {
cur.dispose();
}
return items;
@ -207,8 +200,7 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart {
*/
private void newDrawing(){
root = XmlDocument.Factory.newInstance();
final XmlCursor xml = root.addNewXml().newCursor();
try {
try (final XmlCursor xml = root.addNewXml().newCursor()) {
ShapelayoutDocument layDoc = ShapelayoutDocument.Factory.newInstance();
CTShapeLayout layout = layDoc.addNewShapelayout();
layout.setExt(STExt.EDIT);
@ -217,11 +209,8 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart {
idmap.setData("1");
xml.toEndToken();
final XmlCursor layCur = layDoc.newCursor();
try {
try (XmlCursor layCur = layDoc.newCursor()) {
layCur.copyXmlContents(xml);
} finally {
layCur.dispose();
}
CTGroup grp = CTGroup.Factory.newInstance();
@ -237,16 +226,10 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart {
path.setConnecttype(STConnectType.RECT);
xml.toEndToken();
final XmlCursor grpCur = grp.newCursor();
try {
try (XmlCursor grpCur = grp.newCursor()) {
grpCur.copyXmlContents(xml);
} finally {
grpCur.dispose();
}
} finally {
xml.dispose();
}
}
/**
@ -278,19 +261,13 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart {
cldata.addNewRow().setBigIntegerValue(BigInteger.valueOf(0));
cldata.addNewColumn().setBigIntegerValue(BigInteger.valueOf(0));
XmlCursor xml = root.getXml().newCursor();
try {
try (final XmlCursor xml = root.getXml().newCursor()){
xml.toEndToken();
XmlCursor grpCur = grp.newCursor();
try {
try (final XmlCursor grpCur = grp.newCursor()){
grpCur.copyXmlContents(xml);
xml.toPrevSibling();
shape = (CTShape)xml.getObject();
} finally {
grpCur.dispose();
}
} finally {
xml.dispose();
}
return shape;
@ -302,16 +279,13 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart {
* @return the comment shape or <code>null</code>
*/
public CTShape findCommentShape(int row, int col){
XmlCursor cur = root.getXml().newCursor();
try {
try (final XmlCursor cur = root.getXml().newCursor()){
for (boolean found = cur.toFirstChild(); found; found = cur.toNextSibling()) {
XmlObject itm = cur.getObject();
if (matchCommentShape(itm, row, col)) {
return (CTShape)itm;
}
}
} finally {
cur.dispose();
}
return null;
}
@ -337,8 +311,7 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart {
}
protected boolean removeCommentShape(int row, int col){
XmlCursor cur = root.getXml().newCursor();
try {
try (final XmlCursor cur = root.getXml().newCursor()) {
for (boolean found = cur.toFirstChild(); found; found = cur.toNextSibling()) {
XmlObject itm = cur.getObject();
if (matchCommentShape(itm, row, col)) {
@ -346,8 +319,6 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart {
return true;
}
}
} finally {
cur.dispose();
}
return false;
}

View File

@ -49,8 +49,7 @@ public final class XSSFPasswordHelper {
* @param prefix the prefix of the password attributes, may be null
*/
public static void setPassword(XmlObject xobj, String password, HashAlgorithm hashAlgo, String prefix) {
XmlCursor cur = xobj.newCursor();
try {
try (final XmlCursor cur = xobj.newCursor()) {
if (password == null) {
cur.removeAttribute(getAttrName(prefix, "password"));
cur.removeAttribute(getAttrName(prefix, "algorithmName"));
@ -84,8 +83,6 @@ public final class XSSFPasswordHelper {
cur.insertAttributeWithValue(getAttrName(prefix, "saltValue"), enc64.encodeToString(salt));
cur.insertAttributeWithValue(getAttrName(prefix, "spinCount"), ""+spinCount);
}
} finally {
cur.dispose();
}
}
@ -103,9 +100,8 @@ public final class XSSFPasswordHelper {
public static boolean validatePassword(XmlObject xobj, String password, String prefix) {
// TODO: is "velvetSweatshop" the default password?
if (password == null) return false;
XmlCursor cur = xobj.newCursor();
try {
try (final XmlCursor cur = xobj.newCursor()) {
String xorHashVal = cur.getAttributeText(getAttrName(prefix, "password"));
String algoName = cur.getAttributeText(getAttrName(prefix, "algorithmName"));
String hashVal = cur.getAttributeText(getAttrName(prefix, "hashValue"));
@ -129,8 +125,6 @@ public final class XSSFPasswordHelper {
byte[] hash2 = CryptoFunctions.hashPassword(password, hashAlgo, salt, spinCnt, false);
return Arrays.equals(hash1, hash2);
}
} finally {
cur.dispose();
}
}

View File

@ -46,25 +46,25 @@ public class XWPFComment implements IBody {
}
protected void init() {
XmlCursor cursor = ctComment.newCursor();
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject o = cursor.getObject();
if (o instanceof CTP) {
XWPFParagraph p = new XWPFParagraph((CTP) o, this);
bodyElements.add(p);
paragraphs.add(p);
} else if (o instanceof CTTbl) {
XWPFTable t = new XWPFTable((CTTbl) o, this);
bodyElements.add(t);
tables.add(t);
} else if (o instanceof CTSdtBlock) {
XWPFSDT c = new XWPFSDT((CTSdtBlock) o, this);
bodyElements.add(c);
}
try (XmlCursor cursor = ctComment.newCursor()) {
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject o = cursor.getObject();
if (o instanceof CTP) {
XWPFParagraph p = new XWPFParagraph((CTP) o, this);
bodyElements.add(p);
paragraphs.add(p);
} else if (o instanceof CTTbl) {
XWPFTable t = new XWPFTable((CTTbl) o, this);
bodyElements.add(t);
tables.add(t);
} else if (o instanceof CTSdtBlock) {
XWPFSDT c = new XWPFSDT((CTSdtBlock) o, this);
bodyElements.add(c);
}
}
}
cursor.dispose();
}
/**
@ -174,30 +174,29 @@ public class XWPFComment implements IBody {
paragraphs.add(pos, newP);
}
int i = 0;
XmlCursor p2 = p.newCursor();
cursor.toCursor(p2);
p2.dispose();
try (XmlCursor p2 = p.newCursor()) {
cursor.toCursor(p2);
}
while (cursor.toPrevSibling()) {
o = cursor.getObject();
if (o instanceof CTP || o instanceof CTTbl)
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;
}
private boolean isCursorInCmt(XmlCursor cursor) {
XmlCursor verify = cursor.newCursor();
verify.toParent();
boolean result = (verify.getObject() == this.ctComment);
verify.dispose();
return result;
try (XmlCursor verify = cursor.newCursor()) {
verify.toParent();
return (verify.getObject() == this.ctComment);
}
}
@Override
@ -221,19 +220,19 @@ public class XWPFComment implements IBody {
tables.add(pos, newT);
}
int i = 0;
XmlCursor cursor2 = t.newCursor();
while (cursor2.toPrevSibling()) {
o = cursor2.getObject();
if (o instanceof CTP || o instanceof CTTbl) {
i++;
try (XmlCursor cursor2 = t.newCursor()) {
while (cursor2.toPrevSibling()) {
o = cursor2.getObject();
if (o instanceof CTP || o instanceof CTTbl) {
i++;
}
}
}
cursor2.dispose();
bodyElements.add(i, newT);
cursor2 = t.newCursor();
cursor.toCursor(cursor2);
cursor.toEndToken();
cursor2.dispose();
try (XmlCursor cursor2 = t.newCursor()) {
cursor.toCursor(cursor2);
cursor.toEndToken();
}
return newT;
}
return null;
@ -257,19 +256,15 @@ public class XWPFComment implements IBody {
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)) {
cursor.dispose();
return null;
}
row = (CTRow) o;
cursor.toParent();
o = cursor.getObject();
} finally {
cursor.dispose();
}
if (!(o instanceof CTTbl)) {
return null;
@ -314,9 +309,9 @@ public class XWPFComment implements IBody {
public void removeParagraph(XWPFParagraph paragraph) {
if (paragraphs.contains(paragraph)) {
CTP ctP = paragraph.getCTP();
XmlCursor c = ctP.newCursor();
c.removeXml();
c.dispose();
try (XmlCursor c = ctP.newCursor()) {
c.removeXml();
}
paragraphs.remove(paragraph);
bodyElements.remove(paragraph);
}
@ -325,17 +320,16 @@ public class XWPFComment implements IBody {
public void removeTable(XWPFTable table) {
if (tables.contains(table)) {
CTTbl ctTbl = table.getCTTbl();
XmlCursor c = ctTbl.newCursor();
c.removeXml();
c.dispose();
try (XmlCursor c = ctTbl.newCursor()) {
c.removeXml();
}
tables.remove(table);
bodyElements.remove(table);
}
}
public XWPFTable createTable(int rows, int cols) {
XWPFTable table = new XWPFTable(ctComment.addNewTbl(), this, rows,
cols);
XWPFTable table = new XWPFTable(ctComment.addNewTbl(), this, rows, cols);
tables.add(table);
bodyElements.add(table);
return table;

View File

@ -554,8 +554,7 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
public XWPFTableCell getTableCell(CTTc cell) {
XmlObject o;
CTRow row;
final XmlCursor cursor = cell.newCursor();
try {
try (XmlCursor cursor = cell.newCursor()) {
cursor.toParent();
o = cursor.getObject();
if (!(o instanceof CTRow)) {
@ -564,8 +563,6 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
row = (CTRow) o;
cursor.toParent();
o = cursor.getObject();
} finally {
cursor.dispose();
}
if (!(o instanceof CTTbl)) {
return null;

View File

@ -113,8 +113,7 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para
*/
@SuppressWarnings("deprecation")
private void buildRunsInOrderFromXml(XmlObject object) {
XmlCursor c = object.newCursor();
try {
try (XmlCursor c = object.newCursor()) {
c.selectPath("child::*");
while (c.toNextSelection()) {
XmlObject o = c.getObject();
@ -166,8 +165,6 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para
}
}
}
} finally {
c.dispose();
}
}

View File

@ -43,48 +43,48 @@ public class XWPFSDTContent implements ISDTContent {
if (sdtRun == null) {
return;
}
XmlCursor cursor = sdtRun.newCursor();
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject o = cursor.getObject();
if (o instanceof CTR) {
XWPFRun run = new XWPFRun((CTR) o, parent);
bodyElements.add(run);
} else if (o instanceof CTSdtRun) {
XWPFSDT c = new XWPFSDT(((CTSdtRun) o), part);
bodyElements.add(c);
try (final XmlCursor cursor = sdtRun.newCursor()) {
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject o = cursor.getObject();
if (o instanceof CTR) {
XWPFRun run = new XWPFRun((CTR) o, parent);
bodyElements.add(run);
} else if (o instanceof CTSdtRun) {
XWPFSDT c = new XWPFSDT(((CTSdtRun) o), part);
bodyElements.add(c);
}
}
}
cursor.dispose();
}
public XWPFSDTContent(CTSdtContentBlock block, IBody part, IRunBody parent) {
if (block == null) {
return;
}
XmlCursor cursor = block.newCursor();
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject o = cursor.getObject();
if (o instanceof CTP) {
XWPFParagraph p = new XWPFParagraph((CTP) o, part);
bodyElements.add(p);
// paragraphs.add(p);
} else if (o instanceof CTTbl) {
XWPFTable t = new XWPFTable((CTTbl) o, part);
bodyElements.add(t);
// tables.add(t);
} else if (o instanceof CTSdtBlock) {
XWPFSDT c = new XWPFSDT(((CTSdtBlock) o), part);
bodyElements.add(c);
// contentControls.add(c);
} else if (o instanceof CTR) {
XWPFRun run = new XWPFRun((CTR) o, parent);
// runs.add(run);
bodyElements.add(run);
try (final XmlCursor cursor = block.newCursor()) {
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject o = cursor.getObject();
if (o instanceof CTP) {
XWPFParagraph p = new XWPFParagraph((CTP) o, part);
bodyElements.add(p);
// paragraphs.add(p);
} else if (o instanceof CTTbl) {
XWPFTable t = new XWPFTable((CTTbl) o, part);
bodyElements.add(t);
// tables.add(t);
} else if (o instanceof CTSdtBlock) {
XWPFSDT c = new XWPFSDT(((CTSdtBlock) o), part);
bodyElements.add(c);
// contentControls.add(c);
} else if (o instanceof CTR) {
XWPFRun run = new XWPFRun((CTR) o, parent);
// runs.add(run);
bodyElements.add(run);
}
}
}
cursor.dispose();
}
@Override

View File

@ -50,49 +50,47 @@ public class XWPFSDTContentCell implements ISDTContent {
return;
}
StringBuilder sb = new StringBuilder();
XmlCursor cursor = sdtContentCell.newCursor();
try (final XmlCursor cursor = sdtContentCell.newCursor()) {
//keep track of the following,
//and add "\n" only before the start of a body
//element if it is not the first body element.
//keep track of the following,
//and add "\n" only before the start of a body
//element if it is not the first body element.
//index of cell in row
int tcCnt = 0;
//count of body objects
int iBodyCnt = 0;
int depth = 1;
//index of cell in row
int tcCnt = 0;
//count of body objects
int iBodyCnt = 0;
int depth = 1;
while (cursor.hasNextToken() && depth > 0) {
TokenType t = cursor.toNextToken();
if (t.isText()) {
sb.append(cursor.getTextValue());
} else if (isStartToken(cursor, "tr")) {
tcCnt = 0;
iBodyCnt = 0;
} else if (isStartToken(cursor, "tc")) {
if (tcCnt++ > 0) {
sb.append("\t");
while (cursor.hasNextToken() && depth > 0) {
TokenType t = cursor.toNextToken();
if (t.isText()) {
sb.append(cursor.getTextValue());
} else if (isStartToken(cursor, "tr")) {
tcCnt = 0;
iBodyCnt = 0;
} else if (isStartToken(cursor, "tc")) {
if (tcCnt++ > 0) {
sb.append("\t");
}
iBodyCnt = 0;
} else if (isStartToken(cursor, "p") ||
isStartToken(cursor, "tbl") ||
isStartToken(cursor, "sdt")) {
if (iBodyCnt > 0) {
sb.append("\n");
}
iBodyCnt++;
}
iBodyCnt = 0;
} else if (isStartToken(cursor, "p") ||
isStartToken(cursor, "tbl") ||
isStartToken(cursor, "sdt")) {
if (iBodyCnt > 0) {
sb.append("\n");
if (cursor.isStart()) {
depth++;
} else if (cursor.isEnd()) {
depth--;
}
iBodyCnt++;
}
if (cursor.isStart()) {
depth++;
} else if (cursor.isEnd()) {
depth--;
}
text = sb.toString();
}
text = sb.toString();
cursor.dispose();
}
private boolean isStartToken(XmlCursor cursor, String string) {
if (!cursor.isStart()) {
return false;

View File

@ -86,8 +86,7 @@ public class XWPFTableCell implements IBody, ICell {
paragraphs = new ArrayList<>();
tables = new ArrayList<>();
XmlCursor cursor = ctTc.newCursor();
try {
try (XmlCursor cursor = ctTc.newCursor()) {
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject o = cursor.getObject();
@ -110,8 +109,6 @@ public class XWPFTableCell implements IBody, ICell {
bodyElements.add(c);
}
}
} finally {
cursor.dispose();
}
}
@ -279,11 +276,8 @@ public class XWPFTableCell implements IBody, ICell {
paragraphs.add(pos, newP);
}
int i = 0;
final XmlCursor p2 = p.newCursor();
try {
try (XmlCursor p2 = p.newCursor()) {
cursor.toCursor(p2);
} finally {
p2.dispose();
}
while (cursor.toPrevSibling()) {
o = cursor.getObject();
@ -291,11 +285,8 @@ public class XWPFTableCell implements IBody, ICell {
i++;
}
bodyElements.add(i, newP);
final XmlCursor p3 = p.newCursor();
try {
try (XmlCursor p3 = p.newCursor()) {
cursor.toCursor(p3);
} finally {
p3.dispose();
}
cursor.toEndToken();
return newP;
@ -322,23 +313,17 @@ public class XWPFTableCell implements IBody, ICell {
tables.add(pos, newT);
}
int i = 0;
final XmlCursor cursor2 = t.newCursor();
try {
try (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 (XmlCursor cursor3 = t.newCursor()) {
cursor.toCursor(cursor3);
cursor.toEndToken();
} finally {
cursor3.dispose();
}
return newT;
}
@ -349,13 +334,10 @@ public class XWPFTableCell implements IBody, ICell {
* verifies that cursor is on the right position
*/
private boolean isCursorInTableCell(XmlCursor cursor) {
XmlCursor verify = cursor.newCursor();
try {
try (XmlCursor verify = cursor.newCursor()) {
verify.toParent();
boolean result = (verify.getObject() == this.ctTc);
return result;
} finally {
verify.dispose();
}
}
@ -495,8 +477,7 @@ public class XWPFTableCell implements IBody, ICell {
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)) {
@ -505,8 +486,6 @@ public class XWPFTableCell implements IBody, ICell {
row = (CTRow) o;
cursor.toParent();
o = cursor.getObject();
} finally {
cursor.dispose();
}
if (!(o instanceof CTTbl)) {
return null;

View File

@ -180,8 +180,7 @@ public class XWPFTableRow {
List<ICell> cells = new ArrayList<>();
//Can't use ctRow.getTcList because that only gets table cells
//Can't use ctRow.getSdtList because that only gets sdts that are at cell level
XmlCursor cursor = ctRow.newCursor();
try {
try (XmlCursor cursor = ctRow.newCursor()) {
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject o = cursor.getObject();
@ -191,8 +190,6 @@ public class XWPFTableRow {
cells.add(new XWPFSDTCell((CTSdtCell) o, this, table.getBody()));
}
}
} finally {
cursor.dispose();
}
return cells;
}

View File

@ -84,13 +84,12 @@ class TestXSLFExamples {
ext.setUri("{DAA4B4D4-6D71-4841-9C94-3DE7FCFB9230}");
String p14Ns = "http://schemas.microsoft.com/office/powerpoint/2010/main";
XmlCursor cur = ext.newCursor();
cur.toEndToken();
cur.beginElement(new QName(p14Ns, "media", "p14"));
cur.insertNamespace("p14", p14Ns);
cur.insertAttributeWithValue(new QName(CORE_PROPERTIES_ECMA376_NS, "link"), prsEmbed1.getId());
cur.dispose();
try (XmlCursor cur = ext.newCursor()) {
cur.toEndToken();
cur.beginElement(new QName(p14Ns, "media", "p14"));
cur.insertNamespace("p14", p14Ns);
cur.insertAttributeWithValue(new QName(CORE_PROPERTIES_ECMA376_NS, "link"), prsEmbed1.getId());
}
CTSlide xslide = slide1.getXmlObject();
CTTimeNodeList ctnl;