From 7cf39af9a25934669c19bf3f5bc50829aa94210c Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 16 Jun 2008 12:49:55 +0000 Subject: [PATCH] Unit test from bug #45001, and new replaceText method (no test yet) git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@668143 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hwpf/usermodel/Range.java | 44 +++++++ .../poi/hwpf/data/testRangeInsertion.doc | Bin 0 -> 104448 bytes .../hwpf/usermodel/TestRangeInsertion.java | 120 ++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 src/scratchpad/testcases/org/apache/poi/hwpf/data/testRangeInsertion.doc create mode 100644 src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeInsertion.java diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java index 85592a92a..6324cd86a 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java @@ -632,6 +632,50 @@ public class Range return (ListEntry)insertAfter(props, styleIndex); } + /** + * 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 HWPFDocument in which the placeholder was found + * @param pStartOffset The offset or index where the CharacterRun begins + * @param pPlaceHolderIndex The offset or index of the placeholder, + * relative to the CharacterRun where + * pPlaceHolder was found + */ + protected void replaceText(String pPlaceHolder, String pValue, + int pStartOffset, int pPlaceHolderIndex, HWPFDocument pDocument) { + int absPlaceHolderIndex = pStartOffset + pPlaceHolderIndex; + Range subRange = new Range( + absPlaceHolderIndex, + (absPlaceHolderIndex + pPlaceHolder.length()), pDocument + ); + if (subRange.usesUnicode()) { + absPlaceHolderIndex = pStartOffset + (pPlaceHolderIndex * 2); + subRange = new Range( + absPlaceHolderIndex, + (absPlaceHolderIndex + (pPlaceHolder.length() * 2)), + pDocument + ); + } + + subRange.insertBefore(pValue); + + // re-create the sub-range so we can delete it + subRange = new Range( + (absPlaceHolderIndex + pValue.length()), + (absPlaceHolderIndex + pPlaceHolder.length() + pValue.length()), + pDocument + ); + if (subRange.usesUnicode()) + subRange = new Range( + (absPlaceHolderIndex + (pValue.length() * 2)), + (absPlaceHolderIndex + (pPlaceHolder.length() * 2) + + (pValue.length() * 2)), pDocument + ); + + subRange.delete(); + } /** * Gets the character run at index. The index is relative to this range. diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/data/testRangeInsertion.doc b/src/scratchpad/testcases/org/apache/poi/hwpf/data/testRangeInsertion.doc new file mode 100644 index 0000000000000000000000000000000000000000..322431c27b6a1f003993949cbf1cfbabb08e8976 GIT binary patch literal 104448 zcmeI0X>eU-9f$uXTa((Bkc3)FkV}a)v@~?3t$>!$mNXdB(y}OJxlK}1XcD|lq2N{p z+(8x8gDNu)sO?{uDX z-hFw_$@{*y&;9<$7w>y`=3lJnI?YOL-(ZzhB+^R=|F#gXHuHD@`vwOG3&}eP5Y=^v z7}%R1r@zB8-d#EPJO33_LKRGgx4{&c3e(^S;1jSTp$2N94vvE9@OGF1N5f1w2I}Eh zcn2H@88{wh!3od+?}QU!HZ;OXFbC$s$uJL^;1rk-3t%BEg2k`|PKBkg43@*Y;50ZL z&VYBrdte2e3C(a8tb|pt8qS6mSOcwa4!jrI;9O{jwXhD(gY~ch&W8)&LbwRt2k(cA zVIy1um%?RmIb>lIbU-I`!DhGux?u}k30J{Z=z(77gR9{Kkb`Z|58L4yxE6N6b#Og= z5IzJShL6BU;bU+fE%i||!_0em=E53eq`ao{#UwxVG}V&pMVj(sl~Mou4UFZ=^OOFA ze7yMYe-i1h_20kuaj5_O`yBl5-=A-VD_|z2Ebu;;imDQ3X0|)L+iG9G+qQJ%`nL6L z?$2DumLZ21~;Ab`q<47b|L(m2qphLi$&|puy%e1dFVBjI7?sGvsvB@DlW;ny?d^89e?*&~~)y&c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p-r1V>P+_yngy0PuI5X7O3CxjK7V)9G1Yh6S!r`m ze)XZ1q?H$?Z6s|51bM1?dfk`yj+{>RYj&N-uG|;y>Z`&%FlX0|tL^DlZiD&D=TK1< zx!4`GvU<42IN;mv+9-#0g#LTjiQ8?HT{&FwQPXO$StGf_kJEu zo@S%-T~v0sdn5g`MVdF4w*N_#{bRp>Ef$}oSMn7kPs?FuirdTy%)g#?8}dIn%B{HP d@%dObf&Sa*zdyy?(iRC{eEycE&c4KV@;~hb(hvXu literal 0 HcmV?d00001 diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeInsertion.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeInsertion.java new file mode 100644 index 000000000..0ac3ff0aa --- /dev/null +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeInsertion.java @@ -0,0 +1,120 @@ + +/* ==================================================================== + 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.insertBefore() works even if the Range contains a + * CharacterRun that uses Unicode characters. + */ +public class TestRangeInsertion extends TestCase { + + // u201c and u201d are "smart-quotes" + private String originalText = + "It is used to confirm that text insertion 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.\r"; + private String textToInsert = "Look at me! I'm cool! "; + private int insertionPoint = 244; + + private String illustrativeDocFile; + + protected void setUp() throws Exception { + + String dirname = System.getProperty("HWPF.testdata.path"); + + illustrativeDocFile = dirname + "/testRangeInsertion.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(3, section.numParagraphs()); + Paragraph para = section.getParagraph(2); + + assertEquals(3, para.numCharacterRuns()); + String text = para.getCharacterRun(0).text() + para.getCharacterRun(1).text() + + para.getCharacterRun(2).text(); + + assertEquals(originalText, text); + } + + /** + * Test that we can insert text in our CharacterRun with Unicode text. + */ + public void testRangeInsertion() throws Exception { + + HWPFDocument daDoc = new HWPFDocument(new FileInputStream(illustrativeDocFile)); + + /* + Range range = daDoc.getRange(); + Section section = range.getSection(0); + Paragraph para = section.getParagraph(2); + String text = para.getCharacterRun(0).text() + para.getCharacterRun(1).text() + + para.getCharacterRun(2).text(); + + System.out.println(text); + */ + + Range range = new Range(insertionPoint, (insertionPoint + 2), daDoc); + range.insertBefore(textToInsert); + + // we need to let the model re-calculate the Range before we evaluate it + range = daDoc.getRange(); + + assertEquals(1, range.numSections()); + Section section = range.getSection(0); + + assertEquals(3, section.numParagraphs()); + Paragraph para = section.getParagraph(2); + + assertEquals(3, para.numCharacterRuns()); + String text = para.getCharacterRun(0).text() + para.getCharacterRun(1).text() + + para.getCharacterRun(2).text(); + + // System.out.println(text); + + assertEquals((textToInsert + originalText), text); + + } +}