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();
}
}
- } finally {
- cursor.dispose();
}
}
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();
bodyElements.add(c);
}
}
- } finally {
- cursor.dispose();
}
} catch (Exception e) {
throw new POIXMLException(e);
// 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();
}
}
- } finally {
- c.dispose();
}
}
}
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;
}
* 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);
+ }
}
/**
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) {
candCharPos = 0;
}
}
- } finally {
- c.dispose();
}
}
return null;
// 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;
// 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;
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");
+ }
}
}
// 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();
-
}
/**
// 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();
}
* @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) {
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);
"\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 + "\"" );
+ }
+ }
+ }
}
}
// 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());