You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2008/06/19 13:47:48 UTC

svn commit: r669456 - in /poi/trunk/src: documentation/content/xdocs/ scratchpad/src/org/apache/poi/hwpf/usermodel/ scratchpad/testcases/org/apache/poi/hwpf/data/ scratchpad/testcases/org/apache/poi/hwpf/usermodel/

Author: nick
Date: Thu Jun 19 04:47:48 2008
New Revision: 669456

URL: http://svn.apache.org/viewvc?rev=669456&view=rev
Log:
Improved HWPF Range.replaceText, from N. Hira in bug #45001

Added:
    poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/data/testRangeReplacement.doc   (with props)
    poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java   (with props)
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

Modified: poi/trunk/src/documentation/content/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/changes.xml?rev=669456&r1=669455&r2=669456&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/changes.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/changes.xml Thu Jun 19 04:47:48 2008
@@ -37,6 +37,7 @@
 
 		<!-- Don't forget to update status.xml too! -->
         <release version="3.1-final" date="2008-06-??">
+           <action dev="POI-DEVELOPERS" type="fix">45001 - Improved HWPF Range.replaceText()</action>
            <action dev="POI-DEVELOPERS" type="fix">44692 - Fixed HSSFPicture.resize() to properly resize pictures if the underlying columns/rows have modified size</action>
            <action dev="POI-DEVELOPERS" type="add">Support custom image renderers in HSLF</action>
            <action dev="POI-DEVELOPERS" type="fix">Correctly increment the reference count of a blip when a picture is inserted</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=669456&r1=669455&r2=669456&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Thu Jun 19 04:47:48 2008
@@ -34,6 +34,7 @@
 	<!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.1-final" date="2008-06-??">
+           <action dev="POI-DEVELOPERS" type="fix">45001 - Improved HWPF Range.replaceText()</action>
            <action dev="POI-DEVELOPERS" type="fix">44692 - Fixed HSSFPicture.resize() to properly resize pictures if the underlying columns/rows have modified size</action>
            <action dev="POI-DEVELOPERS" type="add">Support custom image renderers in HSLF</action>
            <action dev="POI-DEVELOPERS" type="fix">Correctly increment the reference count of a blip when a picture is inserted</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=669456&r1=669455&r2=669456&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 Thu Jun 19 04:47:48 2008
@@ -635,27 +635,24 @@
   /**
    * Replace (one instance of) a piece of text with another...
    *
-   * @param pPlaceHolder    The text to be replaced (e.g., "${company}")
-   * @param pValue          The replacement text (e.g., "Cognocys, Inc.")
-   * @param pDocument       The <code>HWPFDocument</code> in which the placeholder was found
-   * @param pStartOffset    The offset or index where the <code>CharacterRun</code> begins
-   * @param pPlaceHolderIndex   The offset or index of the placeholder, 
-   *  relative to the <code>CharacterRun</code> where 
-   *  <code>pPlaceHolder</code> was found
-   */
-  protected void replaceText(String pPlaceHolder, String pValue, 
-        int pStartOffset, int pPlaceHolderIndex, HWPFDocument pDocument) {
-    int absPlaceHolderIndex = pStartOffset + pPlaceHolderIndex;
+   * @param pPlaceHolder    The text to be replaced (e.g., "${organization}")
+   * @param pValue          The replacement text (e.g., "Apache Software Foundation")
+   * @param pOffset         The offset or index where the text to be replaced begins
+   *                        (relative to/within this <code>Range</code>)
+   */
+  public void replaceText(String pPlaceHolder, String pValue, int pOffset)
+  {
+	int absPlaceHolderIndex = getStartOffset() + pOffset;
     Range subRange = new Range(
                 absPlaceHolderIndex, 
-                (absPlaceHolderIndex + pPlaceHolder.length()), pDocument
+				(absPlaceHolderIndex + pPlaceHolder.length()), getDocument()
     );
     if (subRange.usesUnicode()) {
-            absPlaceHolderIndex = pStartOffset + (pPlaceHolderIndex * 2);
+			absPlaceHolderIndex = getStartOffset() + (pOffset * 2);
             subRange = new Range(
                       absPlaceHolderIndex, 
                       (absPlaceHolderIndex + (pPlaceHolder.length() * 2)), 
-                      pDocument
+					  getDocument()
             );
     }
 
@@ -665,13 +662,13 @@
     subRange = new Range(
             (absPlaceHolderIndex + pValue.length()),
             (absPlaceHolderIndex + pPlaceHolder.length() + pValue.length()), 
-            pDocument
+			getDocument()
     );
     if (subRange.usesUnicode())
             subRange = new Range(
                       (absPlaceHolderIndex + (pValue.length() * 2)),
                       (absPlaceHolderIndex + (pPlaceHolder.length() * 2) + 
-                      (pValue.length() * 2)), pDocument
+					  (pValue.length() * 2)), getDocument()
             );
 
     subRange.delete();
@@ -942,4 +939,9 @@
 
 		return _end;
 	}
+
+	protected HWPFDocument getDocument() {
+
+		return _doc;
+	}
 }

Added: poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/data/testRangeReplacement.doc
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/data/testRangeReplacement.doc?rev=669456&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/data/testRangeReplacement.doc
------------------------------------------------------------------------------
    svn:mime-type = application/msword

Added: 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=669456&view=auto
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java (added)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java Thu Jun 19 04:47:48 2008
@@ -0,0 +1,119 @@
+
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+	   http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.hwpf.usermodel;
+
+import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.util.List;
+
+import org.apache.poi.hwpf.HWPFDocument;
+import org.apache.poi.hwpf.model.PicturesTable;
+import org.apache.poi.hwpf.usermodel.Picture;
+
+import junit.framework.TestCase;
+
+/**
+ *	Test to see if Range.replaceText() works even if the Range contains a
+ *	CharacterRun that uses Unicode characters.
+ */
+public class TestRangeReplacement extends TestCase {
+
+	// u201c and u201d are "smart-quotes"
+	private String originalText =
+		"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 =
+		"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 illustrativeDocFile;
+
+	protected void setUp() throws Exception {
+
+		String dirname = System.getProperty("HWPF.testdata.path");
+
+		illustrativeDocFile = dirname + "/testRangeReplacement.doc";
+	}
+
+	/**
+	 * Test just opening the files
+	 */
+	public void testOpen() throws Exception {
+
+		HWPFDocument docA = new HWPFDocument(new FileInputStream(illustrativeDocFile));
+	}
+
+	/**
+	 * Test (more "confirm" than test) that we have the general structure that we expect to have.
+	 */
+	public void testDocStructure() 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);
+
+		assertEquals(5, para.numCharacterRuns());
+		String text = para.getCharacterRun(0).text() + para.getCharacterRun(1).text() +
+			para.getCharacterRun(2).text() + para.getCharacterRun(3).text() + para.getCharacterRun(4).text();
+
+		assertEquals(originalText, text);
+	}
+
+	/**
+	 * Test that we can replace text in our Range with Unicode text.
+	 */
+	public void testRangeReplacement() 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);
+
+		int offset = text.indexOf(searchText);
+		assertEquals(181, offset);
+
+		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(5, section.numParagraphs());
+		para = section.getParagraph(2);
+
+		text = para.text();
+		assertEquals(expectedText, text);
+	}
+}

Propchange: poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeReplacement.java
------------------------------------------------------------------------------
    svn:eol-style = native



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