You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by jo...@apache.org on 2008/10/30 21:17:06 UTC

svn commit: r709235 [2/2] - in /poi/trunk/src: documentation/content/xdocs/ java/org/apache/poi/hssf/record/ java/org/apache/poi/hssf/record/aggregates/ java/org/apache/poi/hssf/record/cf/ java/org/apache/poi/hssf/usermodel/ java/org/apache/poi/hssf/ut...

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/cf/BorderFormatting.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/cf/BorderFormatting.java?rev=709235&r1=709234&r2=709235&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/cf/BorderFormatting.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/cf/BorderFormatting.java Thu Oct 30 13:17:04 2008
@@ -1,4 +1,3 @@
-
 /* ====================================================================
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
@@ -15,129 +14,54 @@
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
-        
 
-/*
- * FontFormatting.java
- *
- * Created on January 22, 2008, 10:05 PM
- */
 package org.apache.poi.hssf.record.cf;
 
-import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.util.BitField;
 import org.apache.poi.util.BitFieldFactory;
 import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianInput;
+import org.apache.poi.util.LittleEndianOutput;
 
 /**
  * Border Formatting Block of the Conditional Formatting Rule Record.
- * 
+ *
  * @author Dmitriy Kumshayev
  */
+public final class BorderFormatting {
 
-public class BorderFormatting 
-{
-	
-    /**
-     * No border
-     */
-
+    /** No border */
     public final static short    BORDER_NONE                = 0x0;
-
-    /**
-     * Thin border
-     */
-
+    /** Thin border */
     public final static short    BORDER_THIN                = 0x1;
-
-    /**
-     * Medium border
-     */
-
+    /** Medium border */
     public final static short    BORDER_MEDIUM              = 0x2;
-
-    /**
-     * dash border
-     */
-
+    /** dash border */
     public final static short    BORDER_DASHED              = 0x3;
-
-    /**
-     * dot border
-     */
-
-    public final static short    BORDER_HAIR              = 0x4;
-
-    /**
-     * Thick border
-     */
-
+    /** dot border */
+    public final static short    BORDER_HAIR                = 0x4;
+    /** Thick border */
     public final static short    BORDER_THICK               = 0x5;
-
-    /**
-     * double-line border
-     */
-
+    /** double-line border */
     public final static short    BORDER_DOUBLE              = 0x6;
-
-    /**
-     * hair-line border
-     */
-
-    public final static short    BORDER_DOTTED                = 0x7;
-
-    /**
-     * Medium dashed border
-     */
-
+    /** hair-line border */
+    public final static short    BORDER_DOTTED              = 0x7;
+    /** Medium dashed border */
     public final static short    BORDER_MEDIUM_DASHED       = 0x8;
-
-    /**
-     * dash-dot border
-     */
-
+    /** dash-dot border */
     public final static short    BORDER_DASH_DOT            = 0x9;
-
-    /**
-     * medium dash-dot border
-     */
-
+    /** medium dash-dot border */
     public final static short    BORDER_MEDIUM_DASH_DOT     = 0xA;
-
-    /**
-     * dash-dot-dot border
-     */
-
+    /** dash-dot-dot border */
     public final static short    BORDER_DASH_DOT_DOT        = 0xB;
-
-    /**
-     * medium dash-dot-dot border
-     */
-
+    /** medium dash-dot-dot border */
     public final static short    BORDER_MEDIUM_DASH_DOT_DOT = 0xC;
-
-    /**
-     * slanted dash-dot border
-     */
-
+    /** slanted dash-dot border */
     public final static short    BORDER_SLANTED_DASH_DOT    = 0xD;
-	
-    public BorderFormatting()
-    {
-        field_13_border_styles1	= (short)0;
-        field_14_border_styles2	= (short)0;
-    }
-    
-    /** Creates new FontFormatting */
-    public BorderFormatting(RecordInputStream in)
-	{
-        field_13_border_styles1	= in.readInt();
-        field_14_border_styles2	= in.readInt();
-	}
-    
+
     // BORDER FORMATTING BLOCK
     // For Border Line Style codes see HSSFCellStyle.BORDER_XXXXXX
-    private int 			 field_13_border_styles1;
+    private int              field_13_border_styles1;
     private static final BitField  bordLeftLineStyle  = BitFieldFactory.getInstance(0x0000000F);
     private static final BitField  bordRightLineStyle = BitFieldFactory.getInstance(0x000000F0);
     private static final BitField  bordTopLineStyle   = BitFieldFactory.getInstance(0x00000F00);
@@ -147,12 +71,25 @@
     private static final BitField  bordTlBrLineOnOff  = BitFieldFactory.getInstance(0x40000000);
     private static final BitField  bordBlTrtLineOnOff = BitFieldFactory.getInstance(0x80000000);
 
-    private int 			 field_14_border_styles2;
+    private int              field_14_border_styles2;
     private static final BitField  bordTopLineColor   = BitFieldFactory.getInstance(0x0000007F);
     private static final BitField  bordBottomLineColor= BitFieldFactory.getInstance(0x00003f80);
     private static final BitField  bordDiagLineColor  = BitFieldFactory.getInstance(0x001FC000);
     private static final BitField  bordDiagLineStyle  = BitFieldFactory.getInstance(0x01E00000);
 
+
+    public BorderFormatting() {
+        field_13_border_styles1    = 0;
+        field_14_border_styles2    = 0;
+    }
+
+    /** Creates new FontFormatting */
+    public BorderFormatting(LittleEndianInput in) {
+        field_13_border_styles1    = in.readInt();
+        field_14_border_styles2    = in.readInt();
+    }
+
+
     /**
      * set the type of border to use for the left border of the cell
      * @param border type
@@ -171,10 +108,8 @@
      * @see #BORDER_MEDIUM_DASH_DOT_DOT
      * @see #BORDER_SLANTED_DASH_DOT
      */
-
-    public void setBorderLeft(short border)
-    {
-    	field_13_border_styles1 = bordLeftLineStyle.setValue(field_13_border_styles1, border);
+    public void setBorderLeft(int border) {
+        field_13_border_styles1 = bordLeftLineStyle.setValue(field_13_border_styles1, border);
     }
 
     /**
@@ -195,10 +130,8 @@
      * @see #BORDER_MEDIUM_DASH_DOT_DOT
      * @see #BORDER_SLANTED_DASH_DOT
      */
-
-    public short getBorderLeft()
-    {
-    	return (short)bordLeftLineStyle.getValue(field_13_border_styles1);
+    public int getBorderLeft() {
+        return bordLeftLineStyle.getValue(field_13_border_styles1);
     }
 
     /**
@@ -219,10 +152,8 @@
      * @see #BORDER_MEDIUM_DASH_DOT_DOT
      * @see #BORDER_SLANTED_DASH_DOT
      */
-
-    public void setBorderRight(short border)
-    {
-    	field_13_border_styles1 = bordRightLineStyle.setValue(field_13_border_styles1, border);
+    public void setBorderRight(int border) {
+        field_13_border_styles1 = bordRightLineStyle.setValue(field_13_border_styles1, border);
     }
 
     /**
@@ -243,10 +174,8 @@
      * @see #BORDER_MEDIUM_DASH_DOT_DOT
      * @see #BORDER_SLANTED_DASH_DOT
      */
-
-    public short getBorderRight()
-    {
-    	return (short)bordRightLineStyle.getValue(field_13_border_styles1);
+    public int getBorderRight() {
+        return bordRightLineStyle.getValue(field_13_border_styles1);
     }
 
     /**
@@ -267,10 +196,8 @@
      * @see #BORDER_MEDIUM_DASH_DOT_DOT
      * @see #BORDER_SLANTED_DASH_DOT
      */
-
-    public void setBorderTop(short border)
-    {
-    	field_13_border_styles1 = bordTopLineStyle.setValue(field_13_border_styles1, border);
+    public void setBorderTop(int border) {
+        field_13_border_styles1 = bordTopLineStyle.setValue(field_13_border_styles1, border);
     }
 
     /**
@@ -291,10 +218,8 @@
      * @see #BORDER_MEDIUM_DASH_DOT_DOT
      * @see #BORDER_SLANTED_DASH_DOT
      */
-
-    public short getBorderTop()
-    {
-    	return (short)bordTopLineStyle.getValue(field_13_border_styles1);
+    public int getBorderTop() {
+        return bordTopLineStyle.getValue(field_13_border_styles1);
     }
 
     /**
@@ -315,10 +240,8 @@
      * @see #BORDER_MEDIUM_DASH_DOT_DOT
      * @see #BORDER_SLANTED_DASH_DOT
      */
-
-    public void setBorderBottom(short border)
-    {
-    	field_13_border_styles1 = bordBottomLineStyle.setValue(field_13_border_styles1, border);
+    public void setBorderBottom(int border) {
+        field_13_border_styles1 = bordBottomLineStyle.setValue(field_13_border_styles1, border);
     }
 
     /**
@@ -339,11 +262,10 @@
      * @see #BORDER_MEDIUM_DASH_DOT_DOT
      * @see #BORDER_SLANTED_DASH_DOT
      */
-    public short getBorderBottom()
-    {
-    	return (short)bordBottomLineStyle.getValue(field_13_border_styles1);
+    public int getBorderBottom() {
+        return bordBottomLineStyle.getValue(field_13_border_styles1);
     }
-    
+
     /**
      * set the type of border to use for the diagonal border of the cell
      * @param border type
@@ -362,10 +284,8 @@
      * @see #BORDER_MEDIUM_DASH_DOT_DOT
      * @see #BORDER_SLANTED_DASH_DOT
      */
-
-    public void setBorderDiagonal(short border)
-    {
-    	field_14_border_styles2 = bordDiagLineStyle.setValue(field_14_border_styles2, border);
+    public void setBorderDiagonal(int border) {
+        field_14_border_styles2 = bordDiagLineStyle.setValue(field_14_border_styles2, border);
     }
 
     /**
@@ -386,18 +306,16 @@
      * @see #BORDER_MEDIUM_DASH_DOT_DOT
      * @see #BORDER_SLANTED_DASH_DOT
      */
-    public short getBorderDiagonal()
-    {
-    	return (short)bordDiagLineStyle.getValue(field_14_border_styles2);
+    public int getBorderDiagonal() {
+        return bordDiagLineStyle.getValue(field_14_border_styles2);
     }
 
     /**
      * set the color to use for the left border
      * @param color The index of the color definition
      */
-    public void setLeftBorderColor(short color)
-    {
-    	field_13_border_styles1 = bordLeftLineColor.setValue(field_13_border_styles1, color);
+    public void setLeftBorderColor(int color) {
+        field_13_border_styles1 = bordLeftLineColor.setValue(field_13_border_styles1, color);
     }
 
     /**
@@ -405,18 +323,16 @@
      * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
      * @param color The index of the color definition
      */
-    public short getLeftBorderColor()
-    {
-    	return (short)bordLeftLineColor.getValue(field_13_border_styles1);
+    public int getLeftBorderColor() {
+        return bordLeftLineColor.getValue(field_13_border_styles1);
     }
 
     /**
      * set the color to use for the right border
      * @param color The index of the color definition
      */
-    public void setRightBorderColor(short color)
-    {
-    	field_13_border_styles1 = bordRightLineColor.setValue(field_13_border_styles1, color);
+    public void setRightBorderColor(int color) {
+        field_13_border_styles1 = bordRightLineColor.setValue(field_13_border_styles1, color);
     }
 
     /**
@@ -424,18 +340,16 @@
      * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
      * @param color The index of the color definition
      */
-    public short getRightBorderColor()
-    {
-    	return (short)bordRightLineColor.getValue(field_13_border_styles1);
+    public int getRightBorderColor() {
+        return bordRightLineColor.getValue(field_13_border_styles1);
     }
 
     /**
      * set the color to use for the top border
      * @param color The index of the color definition
      */
-    public void setTopBorderColor(short color)
-    {
-    	field_14_border_styles2 = bordTopLineColor.setValue(field_14_border_styles2, color);
+    public void setTopBorderColor(int color) {
+        field_14_border_styles2 = bordTopLineColor.setValue(field_14_border_styles2, color);
     }
 
     /**
@@ -443,18 +357,17 @@
      * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
      * @param color The index of the color definition
      */
-    public short getTopBorderColor()
-    {
-    	return (short)bordTopLineColor.getValue(field_14_border_styles2);
+    public int getTopBorderColor() {
+        return bordTopLineColor.getValue(field_14_border_styles2);
     }
 
     /**
      * set the color to use for the bottom border
      * @param color The index of the color definition
      */
-    public void setBottomBorderColor(short color)
+    public void setBottomBorderColor(int color)
     {
-    	field_14_border_styles2 = bordBottomLineColor.setValue(field_14_border_styles2, color);
+        field_14_border_styles2 = bordBottomLineColor.setValue(field_14_border_styles2, color);
     }
 
     /**
@@ -462,18 +375,16 @@
      * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
      * @param color The index of the color definition
      */
-    public short getBottomBorderColor()
-    {
-    	return (short)bordBottomLineColor.getValue(field_14_border_styles2);
+    public int getBottomBorderColor() {
+        return bordBottomLineColor.getValue(field_14_border_styles2);
     }
-    
+
     /**
      * set the color to use for the diagonal borders
      * @param color The index of the color definition
      */
-    public void setDiagonalBorderColor(short color)
-    {
-    	field_14_border_styles2 = bordDiagLineColor.setValue(field_14_border_styles2, color);
+    public void setDiagonalBorderColor(int color) {
+        field_14_border_styles2 = bordDiagLineColor.setValue(field_14_border_styles2, color);
     }
 
     /**
@@ -481,50 +392,44 @@
      * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
      * @param color The index of the color definition
      */
-    public short getDiagonalBorderColor()
-    {
-    	return (short)bordDiagLineColor.getValue(field_14_border_styles2);
+    public int getDiagonalBorderColor() {
+        return bordDiagLineColor.getValue(field_14_border_styles2);
     }
 
     /**
      * Of/off bottom left to top right line
-     *   
-     * @param on - if true - on, otherwise off
+     *
+     * @param on - if <code>true</code> - on, otherwise off
      */
-    public void setForwardDiagonalOn(boolean on)
-    {
-    	field_13_border_styles1 = bordBlTrtLineOnOff.setBoolean(field_13_border_styles1, on);
+    public void setForwardDiagonalOn(boolean on) {
+        field_13_border_styles1 = bordBlTrtLineOnOff.setBoolean(field_13_border_styles1, on);
     }
 
     /**
      * Of/off top left to bottom right line
-     *   
-     * @param on - if true - on, otherwise off
+     *
+     * @param on - if <code>true</code> - on, otherwise off
      */
-    public void setBackwardDiagonalOn(boolean on)
-    {
-    	field_13_border_styles1 = bordTlBrLineOnOff.setBoolean(field_13_border_styles1, on);
+    public void setBackwardDiagonalOn(boolean on) {
+        field_13_border_styles1 = bordTlBrLineOnOff.setBoolean(field_13_border_styles1, on);
     }
-    
+
     /**
-     * @return true if forward diagonal is on
+     * @return <code>true</code> if forward diagonal is on
      */
-    public boolean isForwardDiagonalOn()
-    {
-    	return bordBlTrtLineOnOff.isSet(field_13_border_styles1);
+    public boolean isForwardDiagonalOn() {
+        return bordBlTrtLineOnOff.isSet(field_13_border_styles1);
     }
 
     /**
-     * @return true if backward diagonal is on
+     * @return <code>true</code> if backward diagonal is on
      */
-    public boolean isBackwardDiagonalOn()
-    {
-    	return bordTlBrLineOnOff.isSet(field_13_border_styles1);
+    public boolean isBackwardDiagonalOn() {
+        return bordTlBrLineOnOff.isSet(field_13_border_styles1);
     }
-    
-    
-    public String toString()
-    {
+
+
+    public String toString() {
         StringBuffer buffer = new StringBuffer();
         buffer.append("    [Border Formatting]\n");
         buffer.append("          .lftln     = ").append(Integer.toHexString(getBorderLeft())).append("\n");
@@ -540,21 +445,21 @@
         buffer.append("    [/Border Formatting]\n");
         return buffer.toString();
     }
-    
-    public Object clone() 
-    {
+
+    public Object clone() {
       BorderFormatting rec = new BorderFormatting();
-      rec.field_13_border_styles1 = field_13_border_styles1; 
-      rec.field_14_border_styles2 = field_14_border_styles2; 
+      rec.field_13_border_styles1 = field_13_border_styles1;
+      rec.field_14_border_styles2 = field_14_border_styles2;
       return rec;
     }
-    
-    public int serialize(int offset, byte [] data)
-    {
-    	LittleEndian.putInt(data, offset, field_13_border_styles1);
-    	offset += 4;
-    	LittleEndian.putInt(data, offset, field_14_border_styles2);
-    	offset += 4;
-    	return 8;
+
+    public int serialize(int offset, byte [] data) {
+        LittleEndian.putInt(data, offset+0, field_13_border_styles1);
+        LittleEndian.putInt(data, offset+4, field_14_border_styles2);
+        return 8;
+    }
+    public void serialize(LittleEndianOutput out) {
+        out.writeInt(field_13_border_styles1);
+        out.writeInt(field_14_border_styles2);
     }
 }

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/cf/PatternFormatting.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/cf/PatternFormatting.java?rev=709235&r1=709234&r2=709235&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/cf/PatternFormatting.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/cf/PatternFormatting.java Thu Oct 30 13:17:04 2008
@@ -1,4 +1,3 @@
-
 /* ====================================================================
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
@@ -15,28 +14,20 @@
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
-        
 
-/*
- * FontFormatting.java
- *
- * Created on January 22, 2008, 10:05 PM
- */
 package org.apache.poi.hssf.record.cf;
 
-import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.util.BitField;
 import org.apache.poi.util.BitFieldFactory;
-import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianInput;
+import org.apache.poi.util.LittleEndianOutput;
 
 /**
  * Pattern Formatting Block of the Conditional Formatting Rule Record.
  * 
  * @author Dmitriy Kumshayev
  */
-
-public class PatternFormatting implements Cloneable
-{
+public final class PatternFormatting implements Cloneable {
     /**  No background */
     public final static short     NO_FILL             = 0  ;
     /**  Solidly filled */
@@ -75,29 +66,29 @@
     public final static short     LESS_DOTS           = 17 ;
     /**  Least Dots */
     public final static short     LEAST_DOTS          = 18 ;
-	
-    public PatternFormatting()
-    {
-        field_15_pattern_style	= (short)0;
-        field_16_pattern_color_indexes	= (short)0;
-    }
     
-    /** Creates new FontFormatting */
-    public PatternFormatting(RecordInputStream in)
-	{
-    	field_15_pattern_style	= in.readShort();
-        field_16_pattern_color_indexes	= in.readShort();
-	}
     
     // PATTERN FORMATING BLOCK
     // For Pattern Styles see constants at HSSFCellStyle (from NO_FILL to LEAST_DOTS)
-    private short 			 field_15_pattern_style;
+    private int              field_15_pattern_style;
     private static final BitField  fillPatternStyle = BitFieldFactory.getInstance(0xFC00);
     
-    private short 			 field_16_pattern_color_indexes;
-    private static final BitField  patternColorIndex = BitFieldFactory.getInstance(0x007F);		 
-    private static final BitField  patternBackgroundColorIndex = BitFieldFactory.getInstance(0x3F80);		 
+    private int              field_16_pattern_color_indexes;
+    private static final BitField  patternColorIndex = BitFieldFactory.getInstance(0x007F);         
+    private static final BitField  patternBackgroundColorIndex = BitFieldFactory.getInstance(0x3F80);         
 
+    
+    public PatternFormatting() {
+        field_15_pattern_style    = 0;
+        field_16_pattern_color_indexes    = 0;
+    }
+    
+    /** Creates new FontFormatting */
+    public PatternFormatting(LittleEndianInput in) {
+        field_15_pattern_style    = in.readUShort();
+        field_16_pattern_color_indexes    = in.readUShort();
+    }
+    
     /**
      * setting fill pattern
      *
@@ -121,63 +112,48 @@
      *
      * @param fp  fill pattern 
      */
-    public void setFillPattern(short fp)
-    {
-    	field_15_pattern_style = fillPatternStyle.setShortValue(field_15_pattern_style, fp);
+    public void setFillPattern(int fp) {
+        field_15_pattern_style = fillPatternStyle.setValue(field_15_pattern_style, fp);
     }
 
     /**
-     * get the fill pattern 
      * @return fill pattern
      */
-
-    public short getFillPattern()
-    {
-        return fillPatternStyle.getShortValue(field_15_pattern_style);
+    public int getFillPattern() {
+        return fillPatternStyle.getValue(field_15_pattern_style);
     }
     
     /**
      * set the background fill color.
-     *
-     * @param bg  color
      */
-
-    public void setFillBackgroundColor(short bg)
-    {    	
-    	field_16_pattern_color_indexes = patternBackgroundColorIndex.setShortValue(field_16_pattern_color_indexes,bg);
+    public void setFillBackgroundColor(int bg) {        
+        field_16_pattern_color_indexes = patternBackgroundColorIndex.setValue(field_16_pattern_color_indexes,bg);
     }
 
     /**
-     * get the background fill color
      * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
-     * @return fill color
+     * @return get the background fill color
      */
-    public short getFillBackgroundColor()
-    {
-    	return patternBackgroundColorIndex.getShortValue(field_16_pattern_color_indexes);
+    public int getFillBackgroundColor() {
+        return patternBackgroundColorIndex.getValue(field_16_pattern_color_indexes);
     }
 
     /**
      * set the foreground fill color
-     * @param bg  color
      */
-    public void setFillForegroundColor(short fg)
-    {
-    	field_16_pattern_color_indexes = patternColorIndex.setShortValue(field_16_pattern_color_indexes,fg);
+    public void setFillForegroundColor(int fg) {
+        field_16_pattern_color_indexes = patternColorIndex.setValue(field_16_pattern_color_indexes,fg);
     }
 
     /**
-     * get the foreground fill color
      * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
-     * @return fill color
+     * @return get the foreground fill color
      */
-    public short getFillForegroundColor()
-    {
-    	return patternColorIndex.getShortValue(field_16_pattern_color_indexes);
+    public int getFillForegroundColor() {
+        return patternColorIndex.getValue(field_16_pattern_color_indexes);
     }
     
-    public String toString()
-    {
+    public String toString() {
         StringBuffer buffer = new StringBuffer();
         buffer.append("    [Pattern Formatting]\n");
         buffer.append("          .fillpattern= ").append(Integer.toHexString(getFillPattern())).append("\n");
@@ -187,20 +163,15 @@
         return buffer.toString();
     }
     
-    public Object clone() 
-    {
+    public Object clone()  {
       PatternFormatting rec = new PatternFormatting();
       rec.field_15_pattern_style = field_15_pattern_style;
       rec.field_16_pattern_color_indexes = field_16_pattern_color_indexes; 
       return rec;
     }
-    
-    public int serialize(int offset, byte [] data)
-    {
-    	LittleEndian.putShort(data, offset, field_15_pattern_style);
-    	offset += 2;
-    	LittleEndian.putShort(data, offset, field_16_pattern_color_indexes);
-    	offset += 2;
-    	return 4;
+
+    public void serialize(LittleEndianOutput out) {
+        out.writeShort(field_15_pattern_style);
+        out.writeShort(field_16_pattern_color_indexes);
     }
 }

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFBorderFormatting.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFBorderFormatting.java?rev=709235&r1=709234&r2=709235&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFBorderFormatting.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFBorderFormatting.java Thu Oct 30 13:17:04 2008
@@ -75,52 +75,52 @@
 
 	public short getBorderBottom()
 	{
-		return borderFormatting.getBorderBottom();
+		return (short)borderFormatting.getBorderBottom();
 	}
 
 	public short getBorderDiagonal()
 	{
-		return borderFormatting.getBorderDiagonal();
+		return (short)borderFormatting.getBorderDiagonal();
 	}
 
 	public short getBorderLeft()
 	{
-		return borderFormatting.getBorderLeft();
+		return (short)borderFormatting.getBorderLeft();
 	}
 
 	public short getBorderRight()
 	{
-		return borderFormatting.getBorderRight();
+		return (short)borderFormatting.getBorderRight();
 	}
 
 	public short getBorderTop()
 	{
-		return borderFormatting.getBorderTop();
+		return (short)borderFormatting.getBorderTop();
 	}
 
 	public short getBottomBorderColor()
 	{
-		return borderFormatting.getBottomBorderColor();
+		return (short)borderFormatting.getBottomBorderColor();
 	}
 
 	public short getDiagonalBorderColor()
 	{
-		return borderFormatting.getDiagonalBorderColor();
+		return (short)borderFormatting.getDiagonalBorderColor();
 	}
 
 	public short getLeftBorderColor()
 	{
-		return borderFormatting.getLeftBorderColor();
+		return (short)borderFormatting.getLeftBorderColor();
 	}
 
 	public short getRightBorderColor()
 	{
-		return borderFormatting.getRightBorderColor();
+		return (short)borderFormatting.getRightBorderColor();
 	}
 
 	public short getTopBorderColor()
 	{
-		return borderFormatting.getTopBorderColor();
+		return (short)borderFormatting.getTopBorderColor();
 	}
 
 	public boolean isBackwardDiagonalOn()

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java?rev=709235&r1=709234&r2=709235&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java Thu Oct 30 13:17:04 2008
@@ -265,9 +265,8 @@
      * @see #CELL_TYPE_BOOLEAN
      * @see #CELL_TYPE_ERROR
      */
-
-    public void setCellType(int cellType)
-    {
+    public void setCellType(int cellType) {
+        notifyFormulaChanging();
         int row=record.getRow();
         short col=record.getColumn();
         short styleIndex=record.getXFIndex();
@@ -533,13 +532,14 @@
      * @param value  value to set the cell to.  For formulas we'll set the formula
      * string, for String cells we'll set its value.  For other types we will
      * change the cell to a string cell and set its value.
-     * If value is null then we will change the cell to a Blank cell.
+     * If value is <code>null</code> then we will change the cell to a Blank cell.
      */
     public void setCellValue(HSSFRichTextString value) {
         int row=record.getRow();
         short col=record.getColumn();
         short styleIndex=record.getXFIndex();
         if (value == null) {
+            notifyFormulaChanging();
             setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);
             return;
         }
@@ -577,25 +577,35 @@
         short styleIndex=record.getXFIndex();
 
         if (formula==null) {
+            notifyFormulaChanging();
             setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);
             return;
         }
         setCellType(CELL_TYPE_FORMULA, false, row, col, styleIndex);
-        FormulaRecordAggregate rec = (FormulaRecordAggregate) record;
-        FormulaRecord frec = rec.getFormulaRecord();
+        FormulaRecordAggregate agg = (FormulaRecordAggregate) record;
+        FormulaRecord frec = agg.getFormulaRecord();
         frec.setOptions((short) 2);
         frec.setValue(0);
 
         //only set to default if there is no extended format index already set
-        if (rec.getXFIndex() == (short)0) {
-            rec.setXFIndex((short) 0x0f);
+        if (agg.getXFIndex() == (short)0) {
+            agg.setXFIndex((short) 0x0f);
         }
         Ptg[] ptgs = HSSFFormulaParser.parse(formula, book);
-        frec.setParsedExpression(ptgs);
+        agg.setParsedExpression(ptgs);
+    }
+    /**
+     * Should be called any time that a formula could potentially be deleted.
+     * Does nothing if this cell currently does not hold a formula
+     */
+    private void notifyFormulaChanging() {
+        if (record instanceof FormulaRecordAggregate) {
+            ((FormulaRecordAggregate)record).notifyFormulaChanging();
+        }
     }
 
     public String getCellFormula() {
-        return HSSFFormulaParser.toFormulaString(book, ((FormulaRecordAggregate)record).getFormulaRecord().getParsedExpression());
+        return HSSFFormulaParser.toFormulaString(book, ((FormulaRecordAggregate)record).getFormulaTokens());
     }
 
     /**

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java?rev=709235&r1=709234&r2=709235&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java Thu Oct 30 13:17:04 2008
@@ -19,7 +19,6 @@
 
 import org.apache.poi.hssf.model.HSSFFormulaParser;
 import org.apache.poi.hssf.model.Workbook;
-import org.apache.poi.hssf.record.FormulaRecord;
 import org.apache.poi.hssf.record.NameRecord;
 import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
 import org.apache.poi.hssf.record.formula.NamePtg;
@@ -121,8 +120,8 @@
 			// to make sure that all formulas POI can evaluate can also be parsed.
 			return HSSFFormulaParser.parse(cell.getCellFormula(), _uBook);
 		}
-		FormulaRecord fr = ((FormulaRecordAggregate) cell.getCellValueRecord()).getFormulaRecord();
-		return fr.getParsedExpression();
+		FormulaRecordAggregate fra = (FormulaRecordAggregate) cell.getCellValueRecord();
+		return fra.getFormulaTokens();
 	}
 
 	private static final class Name implements EvaluationName {

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFPatternFormatting.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFPatternFormatting.java?rev=709235&r1=709234&r2=709235&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFPatternFormatting.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFPatternFormatting.java Thu Oct 30 13:17:04 2008
@@ -87,7 +87,7 @@
 	 */
 	public short getFillBackgroundColor()
 	{
-		return patternFormatting.getFillBackgroundColor();
+		return (short)patternFormatting.getFillBackgroundColor();
 	}
 
 	/**
@@ -96,7 +96,7 @@
 	 */
 	public short getFillForegroundColor()
 	{
-		return patternFormatting.getFillForegroundColor();
+		return (short)patternFormatting.getFillForegroundColor();
 	}
 
 	/**
@@ -105,7 +105,7 @@
 	 */
 	public short getFillPattern()
 	{
-		return patternFormatting.getFillPattern();
+		return (short)patternFormatting.getFillPattern();
 	}
 
 	/**

Modified: poi/trunk/src/java/org/apache/poi/hssf/util/CellRangeAddress.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/util/CellRangeAddress.java?rev=709235&r1=709234&r2=709235&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/util/CellRangeAddress.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/util/CellRangeAddress.java Thu Oct 30 13:17:04 2008
@@ -19,6 +19,8 @@
 import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.hssf.record.SelectionRecord;
 import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianByteArrayOutputStream;
+import org.apache.poi.util.LittleEndianOutput;
 
 /**
  * See OOO documentation: excelfileformat.pdf sec 2.5.14 - 'Cell Range Address'<p/>
@@ -48,13 +50,19 @@
 		return in.readUShort();
 	}
 
+	/**
+	 * @deprecated use {@link #serialize(LittleEndianOutput)}
+	 */
 	public int serialize(int offset, byte[] data) {
-		LittleEndian.putUShort(data, offset + 0, getFirstRow());
-		LittleEndian.putUShort(data, offset + 2, getLastRow());
-		LittleEndian.putUShort(data, offset + 4, getFirstColumn());
-		LittleEndian.putUShort(data, offset + 6, getLastColumn());
+		serialize(new LittleEndianByteArrayOutputStream(data, offset, ENCODED_SIZE));
 		return ENCODED_SIZE;
 	}
+	public void serialize(LittleEndianOutput out) {
+		out.writeShort(getFirstRow());
+		out.writeShort(getLastRow());
+		out.writeShort(getFirstColumn());
+		out.writeShort(getLastColumn());
+	}
 	
 	public CellRangeAddress copy() {
 		return new CellRangeAddress(getFirstRow(), getLastRow(), getFirstColumn(), getLastColumn());

Modified: poi/trunk/src/java/org/apache/poi/hssf/util/CellRangeAddress8Bit.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/util/CellRangeAddress8Bit.java?rev=709235&r1=709234&r2=709235&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/util/CellRangeAddress8Bit.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/util/CellRangeAddress8Bit.java Thu Oct 30 13:17:04 2008
@@ -16,8 +16,9 @@
 
 package org.apache.poi.hssf.util;
 
-import org.apache.poi.hssf.record.RecordInputStream;
-import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianByteArrayOutputStream;
+import org.apache.poi.util.LittleEndianInput;
+import org.apache.poi.util.LittleEndianOutput;
 
 /**
  * See OOO documentation: excelfileformat.pdf sec 2.5.14 - 'Cell Range Address'<p/>
@@ -34,25 +35,31 @@
 		super(firstRow, lastRow, firstCol, lastCol);
 	}
 
-	public CellRangeAddress8Bit(RecordInputStream in) {
+	public CellRangeAddress8Bit(LittleEndianInput in) {
 		super(readUShortAndCheck(in), in.readUShort(), in.readUByte(), in.readUByte());
 	}
 
-	private static int readUShortAndCheck(RecordInputStream in) {
-		if (in.remaining() < ENCODED_SIZE) {
+	private static int readUShortAndCheck(LittleEndianInput in) {
+		if (in.available() < ENCODED_SIZE) {
 			// Ran out of data
 			throw new RuntimeException("Ran out of data reading CellRangeAddress");
 		}
 		return in.readUShort();
 	}
 
+	/**
+	 * @deprecated use {@link #serialize(LittleEndianOutput)}
+	 */
 	public int serialize(int offset, byte[] data) {
-		LittleEndian.putUShort(data, offset + 0, getFirstRow());
-		LittleEndian.putUShort(data, offset + 2, getLastRow());
-		LittleEndian.putByte(data, offset + 4, getFirstColumn());
-		LittleEndian.putByte(data, offset + 5, getLastColumn());
+		serialize(new LittleEndianByteArrayOutputStream(data, offset, ENCODED_SIZE));
 		return ENCODED_SIZE;
 	}
+	public void serialize(LittleEndianOutput out) {
+		out.writeShort(getFirstRow());
+		out.writeShort(getLastRow());
+		out.writeByte(getFirstColumn());
+		out.writeByte(getLastColumn());
+	}
 	
 	public CellRangeAddress8Bit copy() {
 		return new CellRangeAddress8Bit(getFirstRow(), getLastRow(), getFirstColumn(), getLastColumn());

Modified: poi/trunk/src/java/org/apache/poi/hssf/util/CellRangeAddressList.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/util/CellRangeAddressList.java?rev=709235&r1=709234&r2=709235&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/util/CellRangeAddressList.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/util/CellRangeAddressList.java Thu Oct 30 13:17:04 2008
@@ -21,6 +21,8 @@
 
 import org.apache.poi.hssf.record.RecordInputStream;
 import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianByteArrayOutputStream;
+import org.apache.poi.util.LittleEndianOutput;
 
 /**
  * Implementation of the cell range address lists,like is described
@@ -113,15 +115,17 @@
 	}
 
 	public int serialize(int offset, byte[] data) {
-		int pos = 2;
-
+		int totalSize = getSize();
+		serialize(new LittleEndianByteArrayOutputStream(data, offset, totalSize));
+		return totalSize;
+	}
+	public void serialize(LittleEndianOutput out) {
 		int nItems = _list.size();
-		LittleEndian.putUShort(data, offset, nItems);
+		out.writeShort(nItems);
 		for (int k = 0; k < nItems; k++) {
 			CellRangeAddress region = (CellRangeAddress) _list.get(k);
-			pos += region.serialize(offset + pos, data);
+			region.serialize(out);
 		}
-		return getSize();
 	}
 
 	public int getSize() {

Added: poi/trunk/src/java/org/apache/poi/ss/formula/Formula.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/Formula.java?rev=709235&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/Formula.java (added)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/Formula.java Thu Oct 30 13:17:04 2008
@@ -0,0 +1,133 @@
+package org.apache.poi.ss.formula;
+
+import org.apache.poi.hssf.record.formula.Ptg;
+import org.apache.poi.util.LittleEndianByteArrayInputStream;
+import org.apache.poi.util.LittleEndianInput;
+import org.apache.poi.util.LittleEndianOutput;
+
+public class Formula {
+
+	private static final byte[] EMPTY_BYTE_ARRAY = { };
+	private final byte[] _byteEncoding;
+	private final int _encodedTokenLen;
+	
+	private Formula(byte[] byteEncoding, int encodedTokenLen) {
+		_byteEncoding = byteEncoding;
+		_encodedTokenLen = encodedTokenLen;
+		if (false) { // set to true to eagerly check Ptg decoding 
+    		LittleEndianByteArrayInputStream in = new LittleEndianByteArrayInputStream(byteEncoding);
+    		Ptg.readTokens(encodedTokenLen, in);
+    		int nUnusedBytes = _byteEncoding.length - in.getReadIndex();
+    		if (nUnusedBytes > 0) {
+    			// TODO - this seems to occur when IntersectionPtg is present
+        		// This example file "IntersectionPtg.xls"
+        		// used by test: TestIntersectionPtg.testReading()
+        		// has 10 bytes unused at the end of the formula
+    			// 10 extra bytes are just 0x01 and 0x00
+    			System.out.println(nUnusedBytes + " unused bytes at end of formula");
+    		}
+		}
+	}
+	/**
+	 * Convenience method for {@link #read(int, LittleEndianInput, int)}
+	 */
+	public static Formula read(int encodedTokenLen, LittleEndianInput in) {
+		return read(encodedTokenLen, in, encodedTokenLen);
+	}
+	/**
+	 * When there are no array constants present, <tt>encodedTokenLen</tt>==<tt>totalEncodedLen</tt>
+	 * @param encodedTokenLen number of bytes in the stream taken by the plain formula tokens
+	 * @param totalEncodedLen the total number of bytes in the formula (includes trailing encoding
+	 * for array constants, but does not include 2 bytes for initial <tt>ushort encodedTokenLen</tt> field.
+	 * @return A new formula object as read from the stream.  Possibly empty, never <code>null</code>.
+	 */
+	public static Formula read(int encodedTokenLen, LittleEndianInput in, int totalEncodedLen) {
+		byte[] byteEncoding = new byte[totalEncodedLen];
+		in.readFully(byteEncoding);
+		return new Formula(byteEncoding, encodedTokenLen);
+	}
+	
+	public Ptg[] getTokens() {
+		LittleEndianInput in = new LittleEndianByteArrayInputStream(_byteEncoding);
+		return Ptg.readTokens(_encodedTokenLen, in);
+	}
+	/**
+	 * Writes  The formula encoding is includes:
+	 * <ul>
+	 * <li>ushort tokenDataLen</li>
+	 * <li>tokenData</li>
+	 * <li>arrayConstantData (if present)</li>
+	 * </ul>
+	 */
+	public void serialize(LittleEndianOutput out) {
+		out.writeShort(_encodedTokenLen);
+		out.write(_byteEncoding);
+	}
+
+	public void serializeTokens(LittleEndianOutput out) {
+		out.write(_byteEncoding, 0, _encodedTokenLen);
+	}
+	public void serializeArrayConstantData(LittleEndianOutput out) {
+		int len = _byteEncoding.length-_encodedTokenLen;
+		out.write(_byteEncoding, _encodedTokenLen, len);
+	}
+	
+	
+	/**
+	 * @return total formula encoding length.  The formula encoding includes:
+	 * <ul>
+	 * <li>ushort tokenDataLen</li>
+	 * <li>tokenData</li>
+	 * <li>arrayConstantData (optional)</li>
+	 * </ul>
+	 * Note - this value is different to <tt>tokenDataLength</tt>
+	 */
+	public int getEncodedSize() {
+		return 2 + _byteEncoding.length;
+	}
+	/**
+	 * This method is often used when the formula length does not appear immediately before
+	 * the encoded token data.
+	 * 
+	 * @return the encoded length of the plain formula tokens.  This does <em>not</em> include
+	 * the leading ushort field, nor any trailing array constant data.
+	 */
+	public int getEncodedTokenSize() {
+		return _encodedTokenLen;
+	}
+	
+	/**
+	 * Creates a {@link Formula} object from a supplied {@link Ptg} array. 
+	 * Handles <code>null</code>s OK.
+	 * @param ptgs may be <code>null</code>
+	 * @return Never <code>null</code> (Possibly empty if the supplied <tt>ptgs</tt> is <code>null</code>)
+	 */
+	public static Formula create(Ptg[] ptgs) {
+		if (ptgs == null) {
+			return new Formula(EMPTY_BYTE_ARRAY, 0);
+		}
+		int totalSize = Ptg.getEncodedSize(ptgs);
+		byte[] encodedData = new byte[totalSize];
+		Ptg.serializePtgs(ptgs, encodedData, 0);
+		int encodedTokenLen = Ptg.getEncodedSizeWithoutArrayData(ptgs);
+		return new Formula(encodedData, encodedTokenLen);
+	}
+	/**
+	 * Gets the {@link Ptg} array from the supplied {@link Formula}. 
+	 * Handles <code>null</code>s OK.
+	 * 
+	 * @param formula may be <code>null</code>
+	 * @return possibly <code>null</code> (if the supplied <tt>formula</tt> is <code>null</code>)
+	 */
+	public static Ptg[] getTokens(Formula formula) {
+		if (formula == null) {
+			return null;
+		}
+		return formula.getTokens();
+	}
+	
+	public Formula copy() {
+		// OK to return this for the moment because currently immutable
+		return this;
+	}
+}

Modified: poi/trunk/src/java/org/apache/poi/util/LittleEndianByteArrayOutputStream.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/util/LittleEndianByteArrayOutputStream.java?rev=709235&r1=709234&r2=709235&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/util/LittleEndianByteArrayOutputStream.java (original)
+++ poi/trunk/src/java/org/apache/poi/util/LittleEndianByteArrayOutputStream.java Thu Oct 30 13:17:04 2008
@@ -81,6 +81,11 @@
 		System.arraycopy(b, 0, _buf, _writeIndex, len);
 		_writeIndex += len;
 	}
+	public void write(byte[] b, int offset, int len) {
+		checkPosition(len);
+		System.arraycopy(b, offset, _buf, _writeIndex, len);
+		_writeIndex += len;
+	}
 	public int getWriteIndex() {
 		return _writeIndex;
 	}

Modified: poi/trunk/src/java/org/apache/poi/util/LittleEndianOutput.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/util/LittleEndianOutput.java?rev=709235&r1=709234&r2=709235&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/util/LittleEndianOutput.java (original)
+++ poi/trunk/src/java/org/apache/poi/util/LittleEndianOutput.java Thu Oct 30 13:17:04 2008
@@ -26,5 +26,6 @@
 	void writeInt(int v);
 	void writeLong(long v);
 	void writeDouble(double v);
-	void write(byte[] data);
+	void write(byte[] b);
+	void write(byte[] b, int offset, int len);
 }

Modified: poi/trunk/src/java/org/apache/poi/util/LittleEndianOutputStream.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/util/LittleEndianOutputStream.java?rev=709235&r1=709234&r2=709235&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/util/LittleEndianOutputStream.java (original)
+++ poi/trunk/src/java/org/apache/poi/util/LittleEndianOutputStream.java Thu Oct 30 13:17:04 2008
@@ -80,4 +80,12 @@
 			throw new RuntimeException(e);
 		}
 	}
+	public void write(byte[] b, int off, int len) {
+		// suppress IOException for interface method
+		try {
+			super.write(b, off, len);
+		} catch (IOException e) {
+			throw new RuntimeException(e);
+		}
+	}
 }

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/TestFormulaRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/TestFormulaRecord.java?rev=709235&r1=709234&r2=709235&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/TestFormulaRecord.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/TestFormulaRecord.java Thu Oct 30 13:17:04 2008
@@ -17,8 +17,6 @@
 
 package org.apache.poi.hssf.record;
 
-import java.io.ByteArrayInputStream;
-
 import junit.framework.TestCase;
 
 import org.apache.poi.hssf.record.formula.AttrPtg;
@@ -152,4 +150,22 @@
 		FuncVarPtg choose = (FuncVarPtg)ptgs[8];
 		assertEquals("CHOOSE", choose.getName());
 	}
+	
+	public void testReserialize() {
+		FormulaRecord formulaRecord = new FormulaRecord();
+		formulaRecord.setRow(1);
+		formulaRecord.setColumn((short) 1);
+		formulaRecord.setParsedExpression(new Ptg[] { new RefPtg("B$5"), });
+		formulaRecord.setValue(3.3);
+		byte[] ser = formulaRecord.serialize();
+		assertEquals(31, ser.length);
+
+		RecordInputStream in = TestcaseRecordInputStream.create(ser);
+		FormulaRecord fr2 = new FormulaRecord(in);
+		assertEquals(3.3, fr2.getValue(), 0.0);
+		Ptg[] ptgs = fr2.getParsedExpression();
+		assertEquals(1, ptgs.length);
+		RefPtg rp = (RefPtg) ptgs[0];
+		assertEquals("B$5", rp.toFormulaString());
+	}
 }

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/TestNameRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/TestNameRecord.java?rev=709235&r1=709234&r2=709235&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/TestNameRecord.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/TestNameRecord.java Thu Oct 30 13:17:04 2008
@@ -17,46 +17,42 @@
 
 package org.apache.poi.hssf.record;
 
+import org.apache.poi.util.HexRead;
+
 import junit.framework.TestCase;
 
 /**
  * Tests the NameRecord serializes/deserializes correctly
- *
+ * 
  * @author Danny Mui (dmui at apache dot org)
  */
 public final class TestNameRecord extends TestCase {
 
-    /**
-     * Makes sure that additional name information is parsed properly such as menu/description
-     */
-    public void testFillExtras()
-    {
-
-        byte[] examples = {
-            (byte) 0x88, (byte) 0x03, (byte) 0x67, (byte) 0x06,
-            (byte) 0x07, (byte) 0x00, (byte) 0x00, (byte) 0x00,
-            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x23,
-            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x4D,
-            (byte) 0x61, (byte) 0x63, (byte) 0x72, (byte) 0x6F,
-            (byte) 0x31, (byte) 0x3A, (byte) 0x01, (byte) 0x00,
-            (byte) 0x00, (byte) 0x00, (byte) 0x11, (byte) 0x00,
-            (byte) 0x00, (byte) 0x4D, (byte) 0x61, (byte) 0x63,
-            (byte) 0x72, (byte) 0x6F, (byte) 0x20, (byte) 0x72,
-            (byte) 0x65, (byte) 0x63, (byte) 0x6F, (byte) 0x72,
-            (byte) 0x64, (byte) 0x65, (byte) 0x64, (byte) 0x20,
-            (byte) 0x32, (byte) 0x37, (byte) 0x2D, (byte) 0x53,
-            (byte) 0x65, (byte) 0x70, (byte) 0x2D, (byte) 0x39,
-            (byte) 0x33, (byte) 0x20, (byte) 0x62, (byte) 0x79,
-            (byte) 0x20, (byte) 0x41, (byte) 0x4C, (byte) 0x4C,
-            (byte) 0x57, (byte) 0x4F, (byte) 0x52
-        };
-
-
-        NameRecord name = new NameRecord(TestcaseRecordInputStream.create(NameRecord.sid, examples));
-        String description = name.getDescriptionText();
-        assertNotNull( description );
-        assertTrue( "text contains ALLWOR", description.indexOf( "ALLWOR" ) > 0 );
-    }
+	/**
+	 * Makes sure that additional name information is parsed properly such as menu/description
+	 */
+	public void testFillExtras() {
+
+		byte[] examples = HexRead.readFromString(""
+				+ "88 03 67 06 07 00 00 00 00 00 00 23 00 00 00 4D "
+				+ "61 63 72 6F 31 3A 01 00 00 00 11 00 00 4D 61 63 "
+				+ "72 6F 20 72 65 63 6F 72 64 65 64 20 32 37 2D 53 "
+				+ "65 70 2D 39 33 20 62 79 20 41 4C 4C 57 4F 52");
+
+		NameRecord name = new NameRecord(TestcaseRecordInputStream.create(NameRecord.sid, examples));
+		String description = name.getDescriptionText();
+		assertNotNull(description);
+		assertTrue(description.endsWith("Macro recorded 27-Sep-93 by ALLWOR"));
+	}
+
+	public void testReserialize() {
+		byte[] data = HexRead
+				.readFromString(""
+						+ "20 00 00 01 0B 00 00 00 01 00 00 00 00 00 00 06 3B 00 00 00 00 02 00 00 00 09 00]");
+		RecordInputStream in = TestcaseRecordInputStream.create(NameRecord.sid, data);
+		NameRecord nr = new NameRecord(in);
+		assertEquals(0x0020, nr.getOptionFlag());
+		byte[] data2 = nr.serialize();
+		TestcaseRecordInputStream.confirmRecordEncoding(NameRecord.sid, data, data2);
+	}
 }
-
-

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/TestSharedFormulaRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/TestSharedFormulaRecord.java?rev=709235&r1=709234&r2=709235&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/TestSharedFormulaRecord.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/TestSharedFormulaRecord.java Thu Oct 30 13:17:04 2008
@@ -21,8 +21,15 @@
 import junit.framework.ComparisonFailure;
 import junit.framework.TestCase;
 
+import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.hssf.record.formula.Ptg;
 import org.apache.poi.hssf.record.formula.RefPtg;
+import org.apache.poi.hssf.usermodel.HSSFCell;
+import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.hssf.usermodel.RecordInspector;
+import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.CellValue;
 import org.apache.poi.util.LittleEndianInput;
 
 /**
@@ -31,6 +38,10 @@
 public final class TestSharedFormulaRecord extends TestCase {
 
 	/**
+	 * A sample spreadsheet known to have one sheet with 4 shared formula ranges
+	 */
+	private static final String SHARED_FORMULA_TEST_XLS = "SharedFormulaTest.xls";
+	/**
 	 * Binary data for an encoded formula.  Taken from attachment 22062 (bugzilla 45123/45421).
 	 * The shared formula is in Sheet1!C6:C21, with text "SUMPRODUCT(--(End_Acct=$C6),--(End_Bal))"
 	 * This data is found at offset 0x1A4A (within the shared formula record).
@@ -86,4 +97,113 @@
 			}
 		}
 	}
+	
+	/**
+	 * Make sure that POI preserves {@link SharedFormulaRecord}s
+	 */
+	public void testPreserveOnReserialize() {
+		HSSFWorkbook wb;
+		HSSFSheet sheet;
+		HSSFCell cellB32769;
+		HSSFCell cellC32769;
+		
+		// Reading directly from XLS file
+		wb = HSSFTestDataSamples.openSampleWorkbook(SHARED_FORMULA_TEST_XLS);
+		sheet = wb.getSheetAt(0);
+		cellB32769 = sheet.getRow(32768).getCell(1);
+		cellC32769 = sheet.getRow(32768).getCell(2);
+		// check reading of formulas which are shared (two cells from a 1R x 8C range) 
+		assertEquals("B32770*2", cellB32769.getCellFormula()); 
+		assertEquals("C32770*2", cellC32769.getCellFormula());
+		confirmCellEvaluation(wb, cellB32769, 4);
+		confirmCellEvaluation(wb, cellC32769, 6);
+		// Confirm this example really does have SharedFormulas.
+		// there are 3 others besides the one at A32769:H32769
+		assertEquals(4, countSharedFormulas(sheet)); 
+		
+		
+		// Re-serialize and check again
+		wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+		sheet = wb.getSheetAt(0);
+		cellB32769 = sheet.getRow(32768).getCell(1);
+		cellC32769 = sheet.getRow(32768).getCell(2);
+		assertEquals("B32770*2", cellB32769.getCellFormula());
+		confirmCellEvaluation(wb, cellB32769, 4);
+		assertEquals(4, countSharedFormulas(sheet));
+	}
+	
+	public void testUnshareFormulaDueToChangeFormula() {
+		HSSFWorkbook wb;
+		HSSFSheet sheet;
+		HSSFCell cellB32769;
+		HSSFCell cellC32769;
+		
+		wb = HSSFTestDataSamples.openSampleWorkbook(SHARED_FORMULA_TEST_XLS);
+		sheet = wb.getSheetAt(0);
+		cellB32769 = sheet.getRow(32768).getCell(1);
+		cellC32769 = sheet.getRow(32768).getCell(2);
+		
+		// Updating cell formula, causing it to become unshared
+		cellB32769.setCellFormula("1+1");
+		confirmCellEvaluation(wb, cellB32769, 2);
+		// currently (Oct 2008) POI handles this by exploding the whole shared formula group
+		assertEquals(3, countSharedFormulas(sheet)); // one less now
+		// check that nearby cell of the same group still has the same formula
+		assertEquals("C32770*2", cellC32769.getCellFormula());
+		confirmCellEvaluation(wb, cellC32769, 6);
+	}
+	public void testUnshareFormulaDueToDelete() {
+		HSSFWorkbook wb;
+		HSSFSheet sheet;
+		HSSFCell cell;
+		final int ROW_IX = 2;
+
+		// changing shared formula cell to blank
+		wb = HSSFTestDataSamples.openSampleWorkbook(SHARED_FORMULA_TEST_XLS);
+		sheet = wb.getSheetAt(0);
+	
+		assertEquals("A$1*2", sheet.getRow(ROW_IX).getCell(1).getCellFormula());
+		cell = sheet.getRow(ROW_IX).getCell(1);
+		cell.setCellType(HSSFCell.CELL_TYPE_BLANK);
+		assertEquals(3, countSharedFormulas(sheet));
+		
+		wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+		sheet = wb.getSheetAt(0);
+		assertEquals("A$1*2", sheet.getRow(ROW_IX+1).getCell(1).getCellFormula());
+		
+		// deleting shared formula cell
+		wb = HSSFTestDataSamples.openSampleWorkbook(SHARED_FORMULA_TEST_XLS);
+		sheet = wb.getSheetAt(0);
+	
+		assertEquals("A$1*2", sheet.getRow(ROW_IX).getCell(1).getCellFormula());
+		cell = sheet.getRow(ROW_IX).getCell(1);
+		sheet.getRow(ROW_IX).removeCell(cell);
+		assertEquals(3, countSharedFormulas(sheet));
+		
+		wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+		sheet = wb.getSheetAt(0);
+		assertEquals("A$1*2", sheet.getRow(ROW_IX+1).getCell(1).getCellFormula());
+	}
+
+	private static void confirmCellEvaluation(HSSFWorkbook wb, HSSFCell cell, double expectedValue) {
+		HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+		CellValue cv = fe.evaluate(cell);
+		assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cv.getCellType());
+		assertEquals(expectedValue, cv.getNumberValue(), 0.0);
+	}
+
+	/**
+	 * @return the number of {@link SharedFormulaRecord}s encoded for the specified sheet
+	 */
+	private static int countSharedFormulas(HSSFSheet sheet) {
+		Record[] records = RecordInspector.getRecords(sheet, 0);
+		int count = 0;
+		for (int i = 0; i < records.length; i++) {
+			Record rec = records[i];
+			if(rec instanceof SharedFormulaRecord) {
+				count++;
+			}
+		}
+		return count;
+	}
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org