]> source.dussan.org Git - poi.git/commitdiff
Get most of the hwpf tests passing again
authorNick Burch <nick@apache.org>
Sat, 9 Aug 2008 19:56:37 +0000 (19:56 +0000)
committerNick Burch <nick@apache.org>
Sat, 9 Aug 2008 19:56:37 +0000 (19:56 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@684322 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hwpf/extractor/WordExtractor.java
src/scratchpad/src/org/apache/poi/hwpf/model/SectionTable.java
src/scratchpad/src/org/apache/poi/hwpf/model/TextPiece.java
src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java
src/scratchpad/testcases/org/apache/poi/hwpf/extractor/TestDifferentRoutes.java
src/scratchpad/testcases/org/apache/poi/hwpf/model/TestTextPieceTable.java
src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeProperties.java [new file with mode: 0644]

index 85009459d75f932d284dd182798482dea6908d38..d73aa337c0752484d70efbadd269d968bae1e35a 100644 (file)
@@ -128,7 +128,7 @@ public class WordExtractor extends POIOLE2TextExtractor {
                TextPiece piece = (TextPiece) textPieces.next();
 
                String encoding = "Cp1252";
-               if (piece.usesUnicode()) {
+               if (piece.isUnicode()) {
                        encoding = "UTF-16LE";
                }
                try {
index 251246327623d03e5687ce5726c923977c169587..7b9c23325fc78cf603cd603367c3e79fa7e849d2 100644 (file)
@@ -104,7 +104,6 @@ public class SectionTable
       }
       int FC = TP.getPieceDescriptor().getFilePosition();
       int offset = CP - TP.getCP();
-      if(TP.usesUnicode()) offset*=2;
       FC = FC+offset-((TextPiece)_text.get(0)).getPieceDescriptor().getFilePosition();
       return FC;
     }
@@ -120,12 +119,12 @@ public class SectionTable
 
        if (fc <= piece.getEnd())
        {
-         cp += ((fc - piece.getStart())/ (piece.usesUnicode() ? 2 : 1));
+         cp += (fc - piece.getStart());
          break;
        }
        else
        {
-         cp += ((piece.getEnd() - piece.getStart())/ (piece.usesUnicode() ? 2 : 1));
+         cp += (piece.getEnd() - piece.getStart());
        }
      }
      return cp;
index f326e59b88001be5237ed1b069216f61cbb4a863..351c06ae7426fa700664f83ca7696f46503a588e 100644 (file)
@@ -73,9 +73,9 @@ public class TextPiece extends PropertyNode implements Comparable
   }
   
   /**
-   * @return If this text piece uses unicode
+   * @return If this text piece is unicode
    */
-   public boolean usesUnicode()
+   public boolean isUnicode()
    {
       return _usesUnicode;
    }
index 9838f9ef21102d16b3d7584bc409d2ca8f93f3bc..c37e9be98ac129cd041a866b7e62f7d0f8470283 100644 (file)
@@ -239,7 +239,7 @@ public class Range
        for (int i = _textStart; i < _textEnd; i++)
        {
          TextPiece piece = (TextPiece)_text.get(i);
-         if (piece.usesUnicode())
+         if (piece.isUnicode())
                  return true;
        }
 
@@ -332,8 +332,6 @@ public class Range
     // Since this is the first item in our list, it is safe to assume that
     // _start >= tp.getStart()
     int insertIndex = _start - tp.getStart();
-       if (tp.usesUnicode())
-               insertIndex /= 2;
     sb.insert(insertIndex, text);
 
     int adjustedLength = _doc.getTextTable().adjustForInsert(_textStart, text.length());
index ae8c8d6b26bc98542efb9d13e5cfe9a2ebbb0035..786fc807dba3dd01da420e8455c9813192e5129e 100644 (file)
@@ -87,7 +87,7 @@ public class TestDifferentRoutes extends TestCase {
                TextPiece piece = (TextPiece) textPieces.next();
 
                String encoding = "Cp1252";
-               if (piece.usesUnicode()) {
+               if (piece.isUnicode()) {
                        encoding = "UTF-16LE";
                }
                String text = new String(piece.getRawBytes(), encoding);
index eef4f0d5eb33866577a7a114a26e0c3339d71394..4595c4ce0e4796deed56be44e1301b30a18c03da 100644 (file)
@@ -118,9 +118,9 @@ public class TestTextPieceTable extends TestCase {
                TextPiece tpB = (TextPiece)tbl.getTextPieces().get(1);
                TextPiece tpC = (TextPiece)tbl.getTextPieces().get(2);
                
-               assertTrue(tpA.usesUnicode());
-               assertTrue(tpB.usesUnicode());
-               assertTrue(tpC.usesUnicode());
+               assertTrue(tpA.isUnicode());
+               assertTrue(tpB.isUnicode());
+               assertTrue(tpC.isUnicode());
                
                assertEquals(256, tpA.characterLength());
                assertEquals(256, tpB.characterLength());
@@ -147,9 +147,9 @@ public class TestTextPieceTable extends TestCase {
                tpB = (TextPiece)tbl.getTextPieces().get(1);
                tpC = (TextPiece)tbl.getTextPieces().get(2);
                
-               assertTrue(tpA.usesUnicode());
-               assertTrue(tpB.usesUnicode());
-               assertTrue(tpC.usesUnicode());
+               assertTrue(tpA.isUnicode());
+               assertTrue(tpB.isUnicode());
+               assertTrue(tpC.isUnicode());
                
                assertEquals(256, tpA.characterLength());
                assertEquals(256, tpB.characterLength());
diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeProperties.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeProperties.java
new file mode 100644 (file)
index 0000000..6c841e8
--- /dev/null
@@ -0,0 +1,82 @@
+/* ====================================================================
+   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.File;
+import java.io.FileInputStream;
+
+import org.apache.poi.hwpf.HWPFDocFixture;
+import org.apache.poi.hwpf.HWPFDocument;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests to ensure that our ranges end up with
+ *  the right text in them, and the right font/styling
+ *  properties applied to them.
+ */
+public class TestRangeProperties extends TestCase {
+       private static final char page_break = (char)12;
+       
+       private static final String u_page_1 =
+               "This is a fairly simple word document, over two pages, with headers and footers.\r" +
+               "The trick with this one is that it contains some Unicode based strings in it.\r" +
+               "Firstly, some currency symbols:\r" +
+               "\tGBP - \u00a3\r" +
+        "\tEUR - \u20ac\r" +
+        "Now, we\u2019ll have some French text, in bold and big:\r" +
+        "\tMoli\u00e8re\r" +
+        "And some normal French text:\r" +
+        "\tL'Avare ou l'\u00c9cole du mensonge\r" +
+        "That\u2019s it for page one\r"
+       ;
+       private static final String u_page_2 =
+               "This is page two. Les Pr\u00e9cieuses ridicules. The end.\r"
+       ;
+       
+       private HWPFDocument u;
+       // TODO - a non unicode document too
+       
+       private String dirname;
+       
+       protected void setUp() throws Exception {
+               dirname = System.getProperty("HWPF.testdata.path");
+               u = new HWPFDocument(
+                               new FileInputStream(new File(dirname, "HeaderFooterUnicode.doc"))
+               );
+       }
+
+       public void testUnicodeTextParagraphs() throws Exception {
+               Range r = u.getRange();
+               assertEquals(
+                               u_page_1 +
+                               page_break + "\r" + 
+                               u_page_2,
+                               r.text()
+               );
+               
+               assertEquals(
+                               5,
+                               r.numParagraphs()
+               );
+               
+               System.out.println(r.getParagraph(2).text());
+       }
+       public void testUnicodeStyling() throws Exception {
+               
+       }
+}