]> source.dussan.org Git - poi.git/commitdiff
Bug 51504 - avoid NPE when DefaultRowHeight or DefaultColumnWidth records are missing
authorYegor Kozlov <yegor@apache.org>
Fri, 15 Jul 2011 09:18:09 +0000 (09:18 +0000)
committerYegor Kozlov <yegor@apache.org>
Fri, 15 Jul 2011 09:18:09 +0000 (09:18 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1147049 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/model/InternalSheet.java
src/java/org/apache/poi/hssf/record/DefaultColWidthRecord.java
src/java/org/apache/poi/hssf/record/DefaultRowHeightRecord.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java

index 58738599ddd7cf50d300ed16376bf8cd4aec4c0d..da2b6b8423f391b3f72ae7f0a0cd401b09e99a2c 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta4" date="2011-??-??">
+           <action dev="poi-developers" type="fix">51504 - avoid NPE when DefaultRowHeight or DefaultColumnWidth records are missing</action>
            <action dev="poi-developers" type="fix">51502 - Correct Subtotal function javadoc entry</action>
            <action dev="poi-developers" type="add">Support for hyperlinks in SXSSF</action>
            <action dev="poi-developers" type="fix">49933 - Word 6/95 documents with sections cause ArrayIndexOutOfBoundsException</action>
index 0ad87845d2d618067f2259633ca054e12c12ab63..a6a427262fec1ebb0e3a5bd28b1430c290a7d3ef 100644 (file)
@@ -65,12 +65,12 @@ import org.apache.poi.hssf.record.aggregates.DataValidityTable;
 import org.apache.poi.hssf.record.aggregates.MergedCellsTable;
 import org.apache.poi.hssf.record.aggregates.PageSettingsBlock;
 import org.apache.poi.hssf.record.aggregates.RecordAggregate;
-import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate;
-import org.apache.poi.hssf.record.aggregates.WorksheetProtectionBlock;
 import org.apache.poi.hssf.record.aggregates.RecordAggregate.PositionTrackingVisitor;
 import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor;
-import org.apache.poi.ss.formula.FormulaShifter;
+import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate;
+import org.apache.poi.hssf.record.aggregates.WorksheetProtectionBlock;
 import org.apache.poi.hssf.util.PaneInformation;
+import org.apache.poi.ss.formula.FormulaShifter;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.util.Internal;
 import org.apache.poi.util.POILogFactory;
@@ -110,8 +110,8 @@ public final class InternalSheet {
     protected PrintGridlinesRecord       printGridlines    =     null;
     protected GridsetRecord              gridset           =     null;
     private   GutsRecord                 _gutsRecord;
-    protected DefaultColWidthRecord      defaultcolwidth   =     null;
-    protected DefaultRowHeightRecord     defaultrowheight  =     null;
+    protected DefaultColWidthRecord      defaultcolwidth   =     new DefaultColWidthRecord();
+    protected DefaultRowHeightRecord     defaultrowheight  =     new DefaultRowHeightRecord();
     private PageSettingsBlock _psBlock;
 
     /**
@@ -272,7 +272,7 @@ public final class InternalSheet {
                 records.add(rec);
                 continue;
             }
-            
+
             if (recSid == EOFRecord.sid) {
                 records.add(rec);
                 break;
@@ -723,7 +723,7 @@ public final class InternalSheet {
     public void removeRow(RowRecord row) {
         _rowsAggregate.removeRow(row);
     }
-    
+
     /**
      * Get all the value records (from LOC). Records will be returned from the first
      *  record (starting at LOC) which is a value record.
@@ -753,7 +753,7 @@ public final class InternalSheet {
      * subsequent calls will return values in (physical) sequence or NULL when you get to the end.
      *
      * @return Array of CellValueRecordInterface representing the remaining value records
-     * @deprecated use {@link #getValueIterator()} instead
+     * @deprecated use {@link #getCellValueIterator()} instead
      */
     @Deprecated
     public CellValueRecordInterface[] getValueRecords() {
@@ -934,7 +934,7 @@ public final class InternalSheet {
         DefaultRowHeightRecord retval = new DefaultRowHeightRecord();
 
         retval.setOptionFlags(( short ) 0);
-        retval.setRowHeight(( short ) 0xff);
+        retval.setRowHeight(DefaultRowHeightRecord.DEFAULT_ROW_HEIGHT);
         return retval;
     }
 
@@ -955,7 +955,7 @@ public final class InternalSheet {
       */
     private static DefaultColWidthRecord createDefaultColWidth() {
         DefaultColWidthRecord retval = new DefaultColWidthRecord();
-        retval.setColWidth(( short ) 8);
+        retval.setColWidth(DefaultColWidthRecord.DEFAULT_COLUMN_WIDTH);
         return retval;
     }
 
index e8fe98cc8bf1f7c9a53f5d781bc51b8446364fa1..6e3160ff6205d848ec09d9afe286b873ce8568e1 100644 (file)
@@ -32,8 +32,14 @@ public final class DefaultColWidthRecord extends StandardRecord {
     public final static short sid = 0x0055;
     private int             field_1_col_width;
 
+    /**
+     *  The default column width is 8 characters
+     */
+    public final static int DEFAULT_COLUMN_WIDTH = 0x0008;
+
     public DefaultColWidthRecord()
     {
+        field_1_col_width = DEFAULT_COLUMN_WIDTH;
     }
 
     public DefaultColWidthRecord(RecordInputStream in)
index eacebacfcb436c1b1370f26eaa5a1034b2ceaf38..0d015e09ab60ba551f3bbbf842f03cfeb8264df2 100644 (file)
@@ -15,7 +15,7 @@
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
-        
+
 
 package org.apache.poi.hssf.record;
 
@@ -38,8 +38,15 @@ public final class DefaultRowHeightRecord
     private short             field_1_option_flags;
     private short             field_2_row_height;
 
+    /**
+     * The default row height for empty rows is 255 twips (255 / 20 == 12.75 points)
+     */
+    public static final short DEFAULT_ROW_HEIGHT = 0xFF;
+
     public DefaultRowHeightRecord()
     {
+        field_1_option_flags = 0x0000;
+        field_2_row_height = DEFAULT_ROW_HEIGHT;
     }
 
     public DefaultRowHeightRecord(RecordInputStream in)
index 1ce0b1d22757a99fe4a48079e667730250136e36..85a4cb3b0acfabd44c704a87c86cab8cc03c9f57 100644 (file)
@@ -736,6 +736,28 @@ public final class TestHSSFSheet extends BaseTestSheet {
         assertEquals(40000, sh.getColumnWidth(0));
     }
 
+
+    public void testDefaultColumnWidth() {
+        HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook( "12843-1.xls" );
+        HSSFSheet sheet = wb.getSheetAt( 7 );
+        // shall not be NPE
+        assertEquals(8, sheet.getDefaultColumnWidth());
+        assertEquals(8*256, sheet.getColumnWidth(0));
+
+        assertEquals(0xFF, sheet.getDefaultRowHeight());
+
+        wb = HSSFTestDataSamples.openSampleWorkbook( "34775.xls" );
+        // second and third sheets miss DefaultColWidthRecord
+        for(int i = 1; i <= 2; i++){
+            int dw = wb.getSheetAt( i ).getDefaultColumnWidth();
+            assertEquals(8, dw);
+            int cw = wb.getSheetAt( i ).getColumnWidth(0);
+            assertEquals(8*256, cw);
+
+            assertEquals(0xFF, sheet.getDefaultRowHeight());
+        }
+    }
+
     /**
      * Some utilities write Excel files without the ROW records.
      * Excel, ooo, and google docs are OK with this.