Author: maxcom
Date: Mon Jul 26 14:04:27 2010
New Revision: 979286

URL: http://svn.apache.org/viewvc?rev=979286&view=rev
Log:
More fixes for auto-saved documents

Added:
    poi/trunk/test-data/document/o_kurs.doc   (with props)
Modified:
    
poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/CHPFormattedDiskPage.java
    poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/CHPX.java
    
poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/CharIndexTranslator.java
    poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/PicturesTable.java
    poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java
    poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java
    
poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestCHPBinTable.java
    
poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestPictures.java

Modified: 
poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/CHPFormattedDiskPage.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/CHPFormattedDiskPage.java?rev=979286&r1=979285&r2=979286&view=diff
==============================================================================
--- 
poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/CHPFormattedDiskPage.java
 (original)
+++ 
poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/CHPFormattedDiskPage.java
 Mon Jul 26 14:04:27 2010
@@ -62,7 +62,12 @@ public final class CHPFormattedDiskPage 
       {
        int startAt = getStart(x);
                int endAt = getEnd(x);
-               _chpxList.add(new CHPX(startAt, endAt, tpt, getGrpprl(x)));
+
+        if (!tpt.isIndexInTable(startAt) && !tpt.isIndexInTable(endAt)) {
+            _chpxList.add(null);
+        } else {
+                   _chpxList.add(new CHPX(startAt, endAt, tpt, getGrpprl(x)));
+        }
       }
     }
 

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/CHPX.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/CHPX.java?rev=979286&r1=979285&r2=979286&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/CHPX.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/CHPX.java Mon Jul 26 
14:04:27 2010
@@ -36,12 +36,12 @@ public final class CHPX extends ByteProp
 
   public CHPX(int fcStart, int fcEnd, CharIndexTranslator translator, byte[] 
grpprl)
   {
-    super(fcStart, fcEnd, translator, new SprmBuffer(grpprl));
+    super(fcStart, translator.lookIndexBackward(fcEnd), translator, new 
SprmBuffer(grpprl));
   }
 
   public CHPX(int fcStart, int fcEnd, CharIndexTranslator translator, 
SprmBuffer buf)
   {
-    super(fcStart, fcEnd, translator ,buf);
+    super(fcStart, translator.lookIndexBackward(fcEnd), translator ,buf);
   }
 
 

Modified: 
poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/CharIndexTranslator.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/CharIndexTranslator.java?rev=979286&r1=979285&r2=979286&view=diff
==============================================================================
--- 
poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/CharIndexTranslator.java 
(original)
+++ 
poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/CharIndexTranslator.java 
Mon Jul 26 14:04:27 2010
@@ -18,9 +18,9 @@
 package org.apache.poi.hwpf.model;
 
 public interface CharIndexTranslator {
-
     /**
      * Calculates the char index of the given byte index.
+     * Look forward if index is not in table
      *
      * @param bytePos The character offset to check 
      * @return the char index
@@ -28,13 +28,28 @@ public interface CharIndexTranslator {
     int getCharIndex(int bytePos);
 
     /**
-     * Is the text at the given byte offset unicode, or plain old ascii? In a
-     * very evil fashion, you have to actually know this to make sense of
-     * character and paragraph properties :(
+     * Check if index is in table
+     *
+     * @param bytePos
+     * @return true if index in table, false if not
+     */
+
+    boolean isIndexInTable(int bytePos);
+
+    /**
+     * Return first index >= bytePos that is in table
+     *
+     * @param bytePos
+     * @return
+     */
+    public int lookIndexForward(int bytePos);
+
+    /**
+     * Return last index <= bytePos that is in table
      *
-     * @param bytePos The character offset to check about
-     * @return true if the text at the given byte offset is unicode
+     * @param bytePos
+     * @return
      */
-    boolean isUnicodeAtByteOffset(int bytePos);
+    public int lookIndexBackward(int bytePos);
 
 }

Modified: 
poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/PicturesTable.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/PicturesTable.java?rev=979286&r1=979285&r2=979286&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/PicturesTable.java 
(original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/PicturesTable.java 
Mon Jul 26 14:04:27 2010
@@ -196,7 +196,11 @@ public final class PicturesTable
     Range range = _document.getOverallRange();
     for (int i = 0; i < range.numCharacterRuns(); i++) {
        CharacterRun run = range.getCharacterRun(i);
-       String text = run.text();
+
+        if (run==null) {
+            continue;
+        }
+
        Picture picture = extractPicture(run, false);
        if (picture != null) {
                pictures.add(picture);

Modified: 
poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java?rev=979286&r1=979285&r2=979286&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java 
(original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java 
Mon Jul 26 14:04:27 2010
@@ -33,7 +33,7 @@ import java.util.List;
  *
  * @author Ryan Ackley
  */
-public final class TextPieceTable implements CharIndexTranslator {
+public class TextPieceTable implements CharIndexTranslator {
        protected ArrayList<TextPiece> _textPieces = new ArrayList<TextPiece>();
     protected ArrayList<TextPiece> _textPiecesFCOrder = new 
ArrayList<TextPiece>();
        // int _multiple;
@@ -118,51 +118,6 @@ public final class TextPieceTable implem
         Collections.sort(_textPiecesFCOrder, new FCComparator());
     }
 
-       /**
-        * Is the text at the given Character offset unicode, or plain old 
ascii? In
-        * a very evil fashion, you have to actually know this to make sense of
-        * character and paragraph properties :(
-        *
-        * @param cp
-        *            The character offset to check about
-        */
-       public boolean isUnicodeAtCharOffset(int cp) {
-               boolean lastWas = false;
-
-               for(TextPiece tp : _textPieces) {
-                       // If the text piece covers the character, all good
-                       if (tp.getStart() <= cp && tp.getEnd() >= cp) {
-                               return tp.isUnicode();
-                       }
-                       // Otherwise keep track for the last one
-                       lastWas = tp.isUnicode();
-               }
-
-               // If they ask off the end, just go with the last one...
-               return lastWas;
-       }
-
-       public boolean isUnicodeAtByteOffset(int bytePos) {
-               boolean lastWas = false;
-
-        for(TextPiece tp : _textPieces) {
-                       int curByte = tp.getPieceDescriptor().getFilePosition();
-                       int pieceEnd = curByte + tp.bytesLength();
-
-                       // If the text piece covers the character, all good
-                       if (curByte <= bytePos && pieceEnd > bytePos) {
-                               return tp.isUnicode();
-                       }
-                       // Otherwise keep track for the last one
-                       lastWas = tp.isUnicode();
-                       // Move along
-                       curByte = pieceEnd;
-               }
-
-               // If they ask off the end, just go with the last one...
-               return lastWas;
-       }
-
        public byte[] writeTo(HWPFOutputStream docStream) throws IOException {
 
                PlexOfCps textPlex = new 
PlexOfCps(PieceDescriptor.getSizeInBytes());
@@ -245,19 +200,7 @@ public final class TextPieceTable implem
     public int getCharIndex(int bytePos) {
         int charCount = 0;
 
-        for(TextPiece tp : _textPiecesFCOrder) {
-                       int pieceStart = 
tp.getPieceDescriptor().getFilePosition();
-
-            if (bytePos > pieceStart + tp.bytesLength()) {
-                continue;
-            }
-
-                       if (pieceStart > bytePos) {
-                               bytePos = pieceStart;
-                       }
-
-            break;
-        }
+        bytePos = lookIndexForward(bytePos);
 
         for(TextPiece tp : _textPieces) {
             int pieceStart = tp.getPieceDescriptor().getFilePosition();
@@ -287,6 +230,62 @@ public final class TextPieceTable implem
         return charCount;
     }
 
+    public int lookIndexForward(int bytePos) {
+        for(TextPiece tp : _textPiecesFCOrder) {
+                       int pieceStart = 
tp.getPieceDescriptor().getFilePosition();
+
+            if (bytePos > pieceStart + tp.bytesLength()) {
+                continue;
+            }
+
+                       if (pieceStart > bytePos) {
+                               bytePos = pieceStart;
+                       }
+
+            break;
+        }
+        return bytePos;
+    }
+
+    public int lookIndexBackward(int bytePos) {
+        int lastEnd = 0;
+
+        for(TextPiece tp : _textPiecesFCOrder) {
+                       int pieceStart = 
tp.getPieceDescriptor().getFilePosition();
+
+            if (bytePos > pieceStart + tp.bytesLength()) {
+                lastEnd = pieceStart + tp.bytesLength();
+                continue;
+            }
+
+                       if (pieceStart > bytePos) {
+                               bytePos = lastEnd;
+                       }
+
+            break;
+        }
+
+        return bytePos;
+    }
+
+    public boolean isIndexInTable(int bytePos) {
+        for(TextPiece tp : _textPiecesFCOrder) {
+                       int pieceStart = 
tp.getPieceDescriptor().getFilePosition();
+
+            if (bytePos > pieceStart + tp.bytesLength()) {
+                continue;
+            }
+
+                       if (pieceStart > bytePos) {
+                               return false;
+                       }
+
+            return true;
+        }
+
+        return false;
+    }
+
     private static class FCComparator implements Comparator<TextPiece> {
         public int compare(TextPiece textPiece, TextPiece textPiece1) {
             if 
(textPiece.getPieceDescriptor().fc>textPiece1.getPieceDescriptor().fc) {

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=979286&r1=979285&r2=979286&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 Mon 
Jul 26 14:04:27 2010
@@ -784,6 +784,10 @@ public class Range { // TODO -instantiab
        public CharacterRun getCharacterRun(int index) {
                initCharacterRuns();
                CHPX chpx = _characters.get(index + _charStart);
+        
+        if (chpx == null) {
+            return null;
+        }
 
                int[] point = findRange(_paragraphs, _parStart, 
Math.max(chpx.getStart(), _start), chpx
                                .getEnd());
@@ -963,7 +967,7 @@ public class Range { // TODO -instantiab
                int x = min;
                PropertyNode node = (PropertyNode) rpl.get(x);
 
-               while (node.getEnd() <= start && x < rpl.size() - 1) {
+               while (node==null || (node.getEnd() <= start && x < rpl.size() 
- 1)) {
                        x++;
                        node = (PropertyNode) rpl.get(x);
                }
@@ -978,7 +982,7 @@ public class Range { // TODO -instantiab
 
                int y = x;
                node = (PropertyNode) rpl.get(y);
-               while (node.getEnd() < end && y < rpl.size() - 1) {
+               while (node==null || (node.getEnd() < end && y < rpl.size() - 
1)) {
                        y++;
                        node = (PropertyNode) rpl.get(y);
                }

Modified: 
poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestCHPBinTable.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestCHPBinTable.java?rev=979286&r1=979285&r2=979286&view=diff
==============================================================================
--- 
poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestCHPBinTable.java
 (original)
+++ 
poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestCHPBinTable.java
 Mon Jul 26 14:04:27 2010
@@ -31,7 +31,12 @@ public final class TestCHPBinTable
   private CHPBinTable _cHPBinTable = null;
   private HWPFDocFixture _hWPFDocFixture;
 
-  private TextPieceTable fakeTPT = new TextPieceTable();
+  private TextPieceTable fakeTPT = new TextPieceTable() {
+      @Override
+      public boolean isIndexInTable(int bytePos) {
+          return true;
+      }
+  };
 
   public void testReadWrite()
     throws Exception

Modified: 
poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestPictures.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestPictures.java?rev=979286&r1=979285&r2=979286&view=diff
==============================================================================
--- 
poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestPictures.java
 (original)
+++ 
poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestPictures.java
 Mon Jul 26 14:04:27 2010
@@ -157,4 +157,10 @@ public final class TestPictures extends 
             doc.getPicturesTable().getAllPictures(); // just check that we do 
not throw Exception
     }
 
+    public void testFastSaved2() {
+            HWPFDocument doc = 
HWPFTestDataSamples.openSampleFile("o_kurs.doc");
+
+            doc.getPicturesTable().getAllPictures(); // just check that we do 
not throw Exception
+    }
+
 }

Added: poi/trunk/test-data/document/o_kurs.doc
URL: 
http://svn.apache.org/viewvc/poi/trunk/test-data/document/o_kurs.doc?rev=979286&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/document/o_kurs.doc
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to