View | Details | Raw Unified | Return to bug 45252
Collapse All | Expand All

(-)./src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java (-2 / +30 lines)
Lines 333-339 Link Here
333
    _doc.getCharacterTable().adjustForInsert(_charStart, adjustedLength);
335
    _doc.getCharacterTable().adjustForInsert(_charStart, adjustedLength);
334
    _doc.getParagraphTable().adjustForInsert(_parStart, adjustedLength);
336
    _doc.getParagraphTable().adjustForInsert(_parStart, adjustedLength);
335
    _doc.getSectionTable().adjustForInsert(_sectionStart, adjustedLength);
337
    _doc.getSectionTable().adjustForInsert(_sectionStart, adjustedLength);
336
    adjustForInsert(text.length());
338
	adjustForInsert(adjustedLength);
337
339
338
	// update the FIB.CCPText field
340
	// update the FIB.CCPText field
339
	adjustFIB(text.length());
341
	adjustFIB(text.length());
Lines 656-663 Link Here
656
            );
658
            );
657
    }
659
    }
658
660
661
	// this Range isn't a proper parent of the subRange() so we'll have to keep
662
	// track of an updated endOffset on our own
663
	int previousEndOffset = subRange.getEndOffset();
664
659
    subRange.insertBefore(pValue);
665
    subRange.insertBefore(pValue);
660
666
667
	if (subRange.getEndOffset() != previousEndOffset)
668
		_end += (subRange.getEndOffset() - previousEndOffset);
669
661
    // re-create the sub-range so we can delete it
670
    // re-create the sub-range so we can delete it
662
    subRange = new Range(
671
    subRange = new Range(
663
            (absPlaceHolderIndex + pValue.length()),
672
            (absPlaceHolderIndex + pValue.length()),
Lines 671-680 Link Here
671
					  (pValue.length() * 2)), getDocument()
680
					  (pValue.length() * 2)), getDocument()
672
            );
681
            );
673
682
683
	// deletes are automagically propagated
674
    subRange.delete();
684
    subRange.delete();
675
  }
685
  }
676
686
677
  /**
687
  /**
688
   * Replace (all instances of) a piece of text with another...
689
   *
690
   * @param pPlaceHolder    The text to be replaced (e.g., "${organization}")
691
   * @param pValue          The replacement text (e.g., "Apache Software Foundation")
692
   */
693
  public void replaceText(String pPlaceHolder, String pValue)
694
  {
695
		boolean keepLooking = true;
696
		while (keepLooking) {
697
698
			String text = text();
699
			int offset = text.indexOf(pPlaceHolder);
700
			if (offset >= 0)
701
				replaceText(pPlaceHolder, pValue, offset);
702
			else
703
				keepLooking = false;
704
		}
705
  }
706
707
  /**
678
   * Gets the character run at index. The index is relative to this range.
708
   * Gets the character run at index. The index is relative to this range.
679
   *
709
   *
680
   * @param index The index of the character run to get.
710
   * @param index The index of the character run to get.
Lines 915-921 Link Here
915
945
916
  /**
946
  /**
917
   * adjust this range after an insert happens.
947
   * adjust this range after an insert happens.
918
   * @param length the length to adjust for
948
   * @param length the length to adjust for (expected to be a count of code-points, not necessarily chars)
919
   */
949
   */
920
  private void adjustForInsert(int length)
950
  private void adjustForInsert(int length)
921
  {
951
  {
(-)./src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java (-6 / +37 lines)
Lines 39-46 Link Here
39
		"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";
39
		"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";
40
	private String searchText = "${organization}";
40
	private String searchText = "${organization}";
41
	private String replacementText = "Apache Software Foundation";
41
	private String replacementText = "Apache Software Foundation";
42
	private String expectedText =
42
	private String expectedText2 =
43
		"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";
43
		"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";
44
	private String expectedText3 = "Thank you, Apache Software Foundation!\r";
44
45
45
	private String illustrativeDocFile;
46
	private String illustrativeDocFile;
46
47
Lines 84-90 Link Here
84
	/**
85
	/**
85
	 * Test that we can replace text in our Range with Unicode text.
86
	 * Test that we can replace text in our Range with Unicode text.
86
	 */
87
	 */
87
	public void testRangeReplacement() throws Exception {
88
	public void testRangeReplacementOne() throws Exception {
88
89
89
		HWPFDocument daDoc = new HWPFDocument(new FileInputStream(illustrativeDocFile));
90
		HWPFDocument daDoc = new HWPFDocument(new FileInputStream(illustrativeDocFile));
90
91
Lines 104-119 Link Here
104
105
105
		para.replaceText(searchText, replacementText, offset);
106
		para.replaceText(searchText, replacementText, offset);
106
107
107
		// we need to let the model re-calculate the Range before we evaluate it
108
		range = daDoc.getRange();
109
110
		assertEquals(1, range.numSections());
108
		assertEquals(1, range.numSections());
111
		section = range.getSection(0);
109
		section = range.getSection(0);
112
110
111
		assertEquals(4, section.numParagraphs());
112
		para = section.getParagraph(2);
113
114
		text = para.text();
115
		assertEquals(expectedText2, text);
116
	}
117
118
	/**
119
	 * Test that we can replace text in our Range with Unicode text.
120
	 */
121
	public void testRangeReplacementAll() throws Exception {
122
123
		HWPFDocument daDoc = new HWPFDocument(new FileInputStream(illustrativeDocFile));
124
125
		Range range = daDoc.getRange();
126
		assertEquals(1, range.numSections());
127
128
		Section section = range.getSection(0);
113
		assertEquals(5, section.numParagraphs());
129
		assertEquals(5, section.numParagraphs());
130
131
		Paragraph para = section.getParagraph(2);
132
133
		String text = para.text();
134
		assertEquals(originalText, text);
135
136
		range.replaceText(searchText, replacementText);
137
138
		assertEquals(1, range.numSections());
139
		section = range.getSection(0);
140
		assertEquals(5, section.numParagraphs());
141
114
		para = section.getParagraph(2);
142
		para = section.getParagraph(2);
143
		text = para.text();
144
		assertEquals(expectedText2, text);
115
145
146
		para = section.getParagraph(3);
116
		text = para.text();
147
		text = para.text();
117
		assertEquals(expectedText, text);
148
		assertEquals(expectedText3, text);
118
	}
149
	}
119
}
150
}

Return to bug 45252