poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r672570 - in /poi/trunk/src: documentation/content/xdocs/changes.xml documentation/content/xdocs/status.xml scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java
Date Sat, 28 Jun 2008 18:58:23 GMT
Author: nick
Date: Sat Jun 28 11:58:23 2008
New Revision: 672570

URL: http://svn.apache.org/viewvc?rev=672570&view=rev
Log:
Patch from N.Hira from bug #45252 - Improvement for HWPF Range.replaceText()

Modified:
    poi/trunk/src/documentation/content/xdocs/changes.xml
    poi/trunk/src/documentation/content/xdocs/status.xml
    poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java
    poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java

Modified: poi/trunk/src/documentation/content/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/changes.xml?rev=672570&r1=672569&r2=672570&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/changes.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/changes.xml Sat Jun 28 11:58:23 2008
@@ -37,6 +37,7 @@
 
 		<!-- Don't forget to update status.xml too! -->
         <release version="3.1.1-alpha1" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">45252 - Improvement for HWPF Range.replaceText()</action>
            <action dev="POI-DEVELOPERS" type="fix">45001 - Further fix for HWPF Range.delete()
and unicode characters</action>
            <action dev="POI-DEVELOPERS" type="add">45175 - Support for variable length
operands in org.apache.poi.hwpf.sprm.SprmOperation</action>
            <action dev="POI-DEVELOPERS" type="fix">Avoid spurious missing lines with
the MissingRecordAware event code, and odd files that contain RowRecords in the middle of
the cell Records.</action>

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=672570&r1=672569&r2=672570&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Sat Jun 28 11:58:23 2008
@@ -34,6 +34,7 @@
 	<!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.1.1-alpha1" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">45252 - Improvement for HWPF Range.replaceText()</action>
            <action dev="POI-DEVELOPERS" type="fix">45001 - Further fix for HWPF Range.delete()
and unicode characters</action>
            <action dev="POI-DEVELOPERS" type="add">45175 - Support for variable length
operands in org.apache.poi.hwpf.sprm.SprmOperation</action>
            <action dev="POI-DEVELOPERS" type="fix">Avoid spurious missing lines with
the MissingRecordAware event code, and odd files that contain RowRecords in the middle of
the cell Records.</action>

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java?rev=672570&r1=672569&r2=672570&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java Sat Jun 28 11:58:23
2008
@@ -333,7 +333,7 @@
     _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());
@@ -656,8 +656,15 @@
             );
     }
 
+	// 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()),
@@ -671,10 +678,31 @@
 					  (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.
    *
    * @param index The index of the character run to get.
@@ -915,7 +943,7 @@
 
   /**
    * 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)
   {

Modified: poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java?rev=672570&r1=672569&r2=672570&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java
(original)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java
Sat Jun 28 11:58:23 2008
@@ -39,8 +39,9 @@
 		"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;
 
@@ -84,7 +85,7 @@
 	/**
 	 * 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));
 
@@ -104,16 +105,46 @@
 
 		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);
 	}
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message