_doc.getCharacterTable().adjustForInsert(_charStart, adjustedLength);
_doc.getParagraphTable().adjustForInsert(_parStart, adjustedLength);
_doc.getSectionTable().adjustForInsert(_sectionStart, adjustedLength);
- adjustForInsert(text.length());
+ adjustForInsert(adjustedLength);
// update the FIB.CCPText field
adjustFIB(text.length());
);
}
+ // this Range isn't a proper parent of the subRange() so we'll have to keep
+ // track of an updated endOffset on our own
+ int previousEndOffset = subRange.getEndOffset();
+
subRange.insertBefore(pValue);
+ if (subRange.getEndOffset() != previousEndOffset)
+ _end += (subRange.getEndOffset() - previousEndOffset);
+
// re-create the sub-range so we can delete it
subRange = new Range(
(absPlaceHolderIndex + pValue.length()),
(pValue.length() * 2)), getDocument()
);
+ // deletes are automagically propagated
subRange.delete();
}
+ /**
+ * Replace (all instances of) a piece of text with another...
+ *
+ * @param pPlaceHolder The text to be replaced (e.g., "${organization}")
+ * @param pValue The replacement text (e.g., "Apache Software Foundation")
+ */
+ public void replaceText(String pPlaceHolder, String pValue)
+ {
+ boolean keepLooking = true;
+ while (keepLooking) {
+
+ String text = text();
+ int offset = text.indexOf(pPlaceHolder);
+ if (offset >= 0)
+ replaceText(pPlaceHolder, pValue, offset);
+ else
+ keepLooking = false;
+ }
+ }
+
/**
* Gets the character run at index. The index is relative to this range.
*
/**
* adjust this range after an insert happens.
- * @param length the length to adjust for
+ * @param length the length to adjust for (expected to be a count of code-points, not necessarily chars)
*/
private void adjustForInsert(int length)
{
"It is used to confirm that text replacement works even if Unicode characters (such as \u201c\u2014\u201d (U+2014), \u201c\u2e8e\u201d (U+2E8E), or \u201c\u2714\u201d (U+2714)) are present. Everybody should be thankful to the ${organization} and all the POI contributors for their assistance in this matter.\r";
private String searchText = "${organization}";
private String replacementText = "Apache Software Foundation";
- private String expectedText =
+ private String expectedText2 =
"It is used to confirm that text replacement works even if Unicode characters (such as \u201c\u2014\u201d (U+2014), \u201c\u2e8e\u201d (U+2E8E), or \u201c\u2714\u201d (U+2714)) are present. Everybody should be thankful to the Apache Software Foundation and all the POI contributors for their assistance in this matter.\r";
+ private String expectedText3 = "Thank you, Apache Software Foundation!\r";
private String illustrativeDocFile;
/**
* Test that we can replace text in our Range with Unicode text.
*/
- public void testRangeReplacement() throws Exception {
+ public void testRangeReplacementOne() throws Exception {
HWPFDocument daDoc = new HWPFDocument(new FileInputStream(illustrativeDocFile));
para.replaceText(searchText, replacementText, offset);
- // we need to let the model re-calculate the Range before we evaluate it
- range = daDoc.getRange();
-
assertEquals(1, range.numSections());
section = range.getSection(0);
+ assertEquals(4, section.numParagraphs());
+ para = section.getParagraph(2);
+
+ text = para.text();
+ assertEquals(expectedText2, text);
+ }
+
+ /**
+ * Test that we can replace text in our Range with Unicode text.
+ */
+ public void testRangeReplacementAll() throws Exception {
+
+ HWPFDocument daDoc = new HWPFDocument(new FileInputStream(illustrativeDocFile));
+
+ Range range = daDoc.getRange();
+ assertEquals(1, range.numSections());
+
+ Section section = range.getSection(0);
+ assertEquals(5, section.numParagraphs());
+
+ Paragraph para = section.getParagraph(2);
+
+ String text = para.text();
+ assertEquals(originalText, text);
+
+ range.replaceText(searchText, replacementText);
+
+ assertEquals(1, range.numSections());
+ section = range.getSection(0);
assertEquals(5, section.numParagraphs());
+
para = section.getParagraph(2);
+ text = para.text();
+ assertEquals(expectedText2, text);
+ para = section.getParagraph(3);
text = para.text();
- assertEquals(expectedText, text);
+ assertEquals(expectedText3, text);
}
}