Author: maxcom
Date: Mon Jul  5 12:56:02 2010
New Revision: 960587

URL: http://svn.apache.org/viewvc?rev=960587&view=rev
Log:
HWPF: Improve reading of auto-saved ("complex") document

Modified:
    poi/trunk/src/documentation/content/xdocs/status.xml
    poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFOldDocument.java
    poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=960587&r1=960586&r2=960587&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Mon Jul  5 12:56:02 
2010
@@ -39,6 +39,7 @@
            <action dev="POI-DEVELOPERS" type="add">49508 - Allow the addition 
of paragraphs to XWPF Table Cells</action>
            <action dev="POI-DEVELOPERS" type="fix">49446 - Don't consider 
17.16.23 field codes as properly part of the paragraph's text</action>
            <action dev="POI-DEVELOPERS" type="fix">XSLFSlideShow shouldn't 
break on .thmx (theme) files. Support for them is still very limited 
though</action>
+           <action dev="POI-DEVELOPERS" type="fix">HWPF: Improve reading of 
auto-saved ("complex") documents</action>
         </release>
         <release version="3.7-beta1" date="2010-06-20">
            <action dev="POI-DEVELOPERS" type="fix">49432 - Lazy caching of 
XSSFComment CTComment objects by reference, to make repeated comment searching 
faster</action>

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFOldDocument.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFOldDocument.java?rev=960587&r1=960586&r2=960587&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFOldDocument.java 
(original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFOldDocument.java Mon 
Jul  5 12:56:02 2010
@@ -86,7 +86,7 @@ public class HWPFOldDocument extends HWP
             TextPiece tp = new TextPiece(
                     0, textData.length, textData, pd, 0
             );
-            tpt.getTextPieces().add(tp);
+            tpt.add(tp);
             
             text.append(tp.getStringBuffer());
         }

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=960587&r1=960586&r2=960587&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  5 12:56:02 2010
@@ -17,14 +17,15 @@
 
 package org.apache.poi.hwpf.model;
 
+import org.apache.poi.hwpf.model.io.HWPFOutputStream;
+import org.apache.poi.poifs.common.POIFSConstants;
+
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 
-import org.apache.poi.hwpf.model.io.HWPFOutputStream;
-import org.apache.poi.poifs.common.POIFSConstants;
-
 /**
  * The piece table for matching up character positions to bits of text. This
  * mostly works in bytes, but the TextPieces themselves work in characters. 
This
@@ -34,6 +35,7 @@ import org.apache.poi.poifs.common.POIFS
  */
 public final class TextPieceTable implements CharIndexTranslator {
        protected ArrayList<TextPiece> _textPieces = new ArrayList<TextPiece>();
+    protected ArrayList<TextPiece> _textPiecesFCOrder = new 
ArrayList<TextPiece>();
        // int _multiple;
        int _cpMin;
 
@@ -96,11 +98,9 @@ public final class TextPieceTable implem
 
                // In the interest of our sanity, now sort the text pieces
                // into order, if they're not already
-               TextPiece[] tp = _textPieces.toArray(new 
TextPiece[_textPieces.size()]);
-               Arrays.sort(tp);
-               for (int i = 0; i < tp.length; i++) {
-                       _textPieces.set(i, tp[i]);
-               }
+        Collections.sort(_textPieces);
+        _textPiecesFCOrder = new ArrayList<TextPiece>(_textPieces);
+        Collections.sort(_textPiecesFCOrder, new FCComparator());
        }
 
        public int getCpMin() {
@@ -111,6 +111,13 @@ public final class TextPieceTable implem
                return _textPieces;
        }
 
+    public void add(TextPiece piece) {
+        _textPieces.add(piece);
+        _textPiecesFCOrder.add(piece);
+        Collections.sort(_textPieces);
+        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
@@ -238,7 +245,7 @@ public final class TextPieceTable implem
        public int getCharIndex(int bytePos) {
                int charCount = 0;
 
-        for(TextPiece tp : _textPieces) {
+        for(TextPiece tp : _textPiecesFCOrder) {
                        int pieceStart = 
tp.getPieceDescriptor().getFilePosition();
                        if (pieceStart >= bytePos) {
                                break;
@@ -259,4 +266,15 @@ public final class TextPieceTable implem
                return charCount;
        }
 
+    private static class FCComparator implements Comparator<TextPiece> {
+        public int compare(TextPiece textPiece, TextPiece textPiece1) {
+            if 
(textPiece.getPieceDescriptor().fc>textPiece1.getPieceDescriptor().fc) {
+                return 1;
+            } else if 
(textPiece.getPieceDescriptor().fc<textPiece1.getPieceDescriptor().fc) {
+                return -1;
+            } else {
+                return 0;
+            }
+        }
+    }
 }



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

Reply via email to