poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r718810 [1/2] - in /poi/trunk/src: java/org/apache/poi/hssf/model/ java/org/apache/poi/hssf/record/ java/org/apache/poi/hssf/record/aggregates/ java/org/apache/poi/hssf/usermodel/ scratchpad/src/org/apache/poi/hssf/usermodel/ testcases/org/...
Date Wed, 19 Nov 2008 00:06:07 GMT
Author: josh
Date: Tue Nov 18 16:06:06 2008
New Revision: 718810

URL: http://svn.apache.org/viewvc?rev=718810&view=rev
Log:
Refactoring Record.serialize methods

Added:
    poi/trunk/src/java/org/apache/poi/hssf/record/HeaderFooterBase.java
Removed:
    poi/trunk/src/java/org/apache/poi/hssf/record/ExtSSTInfoSubRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/LinkedDataFormulaField.java
Modified:
    poi/trunk/src/java/org/apache/poi/hssf/model/Workbook.java
    poi/trunk/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/CFHeaderRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/ChartTitleFormatRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/DBCellRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/DVALRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/DrawingRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/ExtSSTRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/ExternSheetRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/FileSharingRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/FontRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/FooterRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/FormatRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/HeaderRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/IndexRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/LinkedDataRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/MergeCellsRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/MulBlankRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/MulRKRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/PageBreakRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/PaletteRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/RKRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/RecalcIdRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/SelectionRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/SeriesListRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/StyleRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/TabIdRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/UnknownRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/WriteAccessRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/aggregates/PageSettingsBlock.java
    poi/trunk/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFFooter.java
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFHeader.java
    poi/trunk/src/scratchpad/src/org/apache/poi/hssf/usermodel/HSSFChart.java
    poi/trunk/src/testcases/org/apache/poi/hssf/record/TestFontRecord.java
    poi/trunk/src/testcases/org/apache/poi/hssf/record/TestLinkedDataRecord.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/model/Workbook.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/model/Workbook.java?rev=718810&r1=718809&r2=718810&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/model/Workbook.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/model/Workbook.java Tue Nov 18 16:06:06 2008
@@ -95,7 +95,7 @@
 
     protected int              numxfs      = 0;   // hold the number of extended format records
     protected int              numfonts    = 0;   // hold the number of font records
-    private short              maxformatid  = -1;  // holds the max format id
+    private int                maxformatid  = -1;  // holds the max format id
     private boolean            uses1904datewindowing  = false;  // whether 1904 date windowing is being used
     private DrawingManager2    drawingManager;
     private List               escherBSERecords = new ArrayList();  // EscherBSERecord
@@ -1106,15 +1106,8 @@
      * @see org.apache.poi.hssf.record.Record
      * @return record containing a TabIdRecord
      */
-
-    protected Record createTabId() {
-        TabIdRecord retval     = new TabIdRecord();
-        short[]     tabidarray = {
-            0
-        };
-
-        retval.setTabIdArray(tabidarray);
-        return retval;
+    private static TabIdRecord createTabId() {
+        return new TabIdRecord();
     }
 
     /**
@@ -1334,7 +1327,6 @@
      * @see org.apache.poi.hssf.record.Record
      * @return record containing a FontRecord
      */
-
     protected Record createFont() {
         FontRecord retval = new FontRecord();
 
@@ -1342,7 +1334,6 @@
         retval.setAttributes(( short ) 0x0);
         retval.setColorPaletteIndex(( short ) 0x7fff);
         retval.setBoldWeight(( short ) 0x190);
-        retval.setFontNameLength(( byte ) 5);
         retval.setFontName("Arial");
         return retval;
     }
@@ -1355,66 +1346,21 @@
      * @see org.apache.poi.hssf.record.FormatRecord
      * @see org.apache.poi.hssf.record.Record
      */
-
-    protected Record createFormat(int id) {   // we'll need multiple editions for
-        FormatRecord retval = new FormatRecord();   // the differnt formats
+    private static FormatRecord createFormat(int id) {  
+    	// we'll need multiple editions for
+        // the different formats
 
         switch (id) {
-
-            case 0 :
-                retval.setIndexCode(( short ) 5);
-                retval.setFormatStringLength(( byte ) 0x17);
-                retval.setFormatString("\"$\"#,##0_);\\(\"$\"#,##0\\)");
-                break;
-
-            case 1 :
-                retval.setIndexCode(( short ) 6);
-                retval.setFormatStringLength(( byte ) 0x1c);
-                retval.setFormatString("\"$\"#,##0_);[Red]\\(\"$\"#,##0\\)");
-                break;
-
-            case 2 :
-                retval.setIndexCode(( short ) 7);
-                retval.setFormatStringLength(( byte ) 0x1d);
-                retval.setFormatString("\"$\"#,##0.00_);\\(\"$\"#,##0.00\\)");
-                break;
-
-            case 3 :
-                retval.setIndexCode(( short ) 8);
-                retval.setFormatStringLength(( byte ) 0x22);
-                retval.setFormatString(
-                "\"$\"#,##0.00_);[Red]\\(\"$\"#,##0.00\\)");
-                break;
-
-            case 4 :
-                retval.setIndexCode(( short ) 0x2a);
-                retval.setFormatStringLength(( byte ) 0x32);
-                retval.setFormatString(
-                "_(\"$\"* #,##0_);_(\"$\"* \\(#,##0\\);_(\"$\"* \"-\"_);_(@_)");
-                break;
-
-            case 5 :
-                retval.setIndexCode(( short ) 0x29);
-                retval.setFormatStringLength(( byte ) 0x29);
-                retval.setFormatString(
-                "_(* #,##0_);_(* \\(#,##0\\);_(* \"-\"_);_(@_)");
-                break;
-
-            case 6 :
-                retval.setIndexCode(( short ) 0x2c);
-                retval.setFormatStringLength(( byte ) 0x3a);
-                retval.setFormatString(
-                "_(\"$\"* #,##0.00_);_(\"$\"* \\(#,##0.00\\);_(\"$\"* \"-\"??_);_(@_)");
-                break;
-
-            case 7 :
-                retval.setIndexCode(( short ) 0x2b);
-                retval.setFormatStringLength(( byte ) 0x31);
-                retval.setFormatString(
-                "_(* #,##0.00_);_(* \\(#,##0.00\\);_(* \"-\"??_);_(@_)");
-                break;
+            case 0: return new FormatRecord(5, "\"$\"#,##0_);\\(\"$\"#,##0\\)");
+            case 1: return new FormatRecord(6, "\"$\"#,##0_);[Red]\\(\"$\"#,##0\\)");
+            case 2: return new FormatRecord(7, "\"$\"#,##0.00_);\\(\"$\"#,##0.00\\)");
+            case 3: return new FormatRecord(8, "\"$\"#,##0.00_);[Red]\\(\"$\"#,##0.00\\)");
+            case 4: return new FormatRecord(0x2a, "_(\"$\"* #,##0_);_(\"$\"* \\(#,##0\\);_(\"$\"* \"-\"_);_(@_)");
+            case 5: return new FormatRecord(0x29, "_(* #,##0_);_(* \\(#,##0\\);_(* \"-\"_);_(@_)");
+            case 6: return new FormatRecord(0x2c, "_(\"$\"* #,##0.00_);_(\"$\"* \\(#,##0.00\\);_(\"$\"* \"-\"??_);_(@_)");
+            case 7: return new FormatRecord(0x2b, "_(* #,##0.00_);_(* \\(#,##0.00\\);_(* \"-\"??_);_(@_)");
         }
-        return retval;
+        throw new  IllegalArgumentException("Unexpected id " + id);
     }
 
     /**
@@ -2061,12 +2007,12 @@
     for (iterator = formats.iterator(); iterator.hasNext();) {
         FormatRecord r = (FormatRecord)iterator.next();
         if (r.getFormatString().equals(format)) {
-        return r.getIndexCode();
+        return (short)r.getIndexCode();
         }
     }
 
     if (createIfNotFound) {
-        return createFormat(format);
+        return (short)createFormat(format);
     }
 
     return -1;
@@ -2082,21 +2028,15 @@
 
     /**
      * Creates a FormatRecord, inserts it, and returns the index code.
-     * @param format the format string
+     * @param formatString the format string
      * @return the index code of the format record.
      * @see org.apache.poi.hssf.record.FormatRecord
      * @see org.apache.poi.hssf.record.Record
      */
-    public short createFormat( String format )
-    {
-//        ++xfpos;    //These are to ensure that positions are updated properly
-//        ++palettepos;
-//        ++bspos;
-        FormatRecord rec = new FormatRecord();
-        maxformatid = maxformatid >= (short) 0xa4 ? (short) ( maxformatid + 1 ) : (short) 0xa4; //Starting value from M$ empiracle study.
-        rec.setIndexCode( maxformatid );
-        rec.setFormatStringLength( (byte) format.length() );
-        rec.setFormatString( format );
+    public int createFormat(String formatString) {
+
+        maxformatid = maxformatid >= 0xa4 ? maxformatid + 1 : 0xa4; //Starting value from M$ empircal study.
+        FormatRecord rec = new FormatRecord(maxformatid, formatString);
 
         int pos = 0;
         while ( pos < records.size() && records.get( pos ).getSid() != FormatRecord.sid )

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java?rev=718810&r1=718809&r2=718810&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java Tue Nov 18 16:06:06 2008
@@ -24,20 +24,19 @@
 import org.apache.poi.util.BitField;
 import org.apache.poi.util.BitFieldFactory;
 import org.apache.poi.util.HexDump;
-import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianOutput;
 import org.apache.poi.util.StringUtil;
 
 /**
- * Title:        Bound Sheet Record (aka BundleSheet) <P>
- * Description:  Defines a sheet within a workbook.  Basically stores the sheetname
+ * Title:        Bound Sheet Record (aka BundleSheet) (0x0085)<P>
+ * Description:  Defines a sheet within a workbook.  Basically stores the sheet name
  *               and tells where the Beginning of file record is within the HSSF
  *               file. <P>
  * REFERENCE:  PG 291 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
  * @author Andrew C. Oliver (acoliver at apache dot org)
  * @author Sergei Kozello (sergeikozello at mail.ru)
- * @version 2.0-pre
  */
-public final class BoundSheetRecord extends Record {
+public final class BoundSheetRecord extends StandardRecord {
 	public final static short sid = 0x0085;
 
 	private static final BitField hiddenFlag = BitFieldFactory.getInstance(0x01);
@@ -160,23 +159,19 @@
 		return 8 + field_5_sheetname.length() * (isMultibyte() ? 2 : 1);
 	}
 
-	public int serialize(int offset, byte[] data) {
-		int dataSize = getDataSize();
-		LittleEndian.putUShort(data, 0 + offset, sid);
-		LittleEndian.putUShort(data, 2 + offset, dataSize);
-		LittleEndian.putInt(data, 4 + offset, getPositionOfBof());
-		LittleEndian.putUShort(data, 8 + offset, field_2_option_flags);
+	public void serialize(LittleEndianOutput out) {
+		out.writeInt(getPositionOfBof());
+		out.writeShort(field_2_option_flags);
 
 		String name = field_5_sheetname;
-		LittleEndian.putByte(data, 10 + offset, name.length());
-		LittleEndian.putByte(data, 11 + offset, field_4_isMultibyteUnicode);
+		out.writeByte(name.length());
+		out.writeByte(field_4_isMultibyteUnicode);
 
 		if (isMultibyte()) {
-			StringUtil.putUnicodeLE(name, data, 12 + offset);
+			StringUtil.putUnicodeLE(name, out);
 		} else {
-			StringUtil.putCompressedUnicode(name, data, 12 + offset);
+			StringUtil.putCompressedUnicode(name, out);
 		}
-		return 4 + dataSize;
 	}
 
 	public short getSid() {
@@ -215,16 +210,14 @@
 	 * Converts a List of {@link BoundSheetRecord}s to an array and sorts by the position of their
 	 * BOFs.
 	 */
-	public static BoundSheetRecord[] orderByBofPosition(List boundSheetRecords) {
+	public static BoundSheetRecord[] orderByBofPosition(List<BoundSheetRecord> boundSheetRecords) {
 		BoundSheetRecord[] bsrs = new BoundSheetRecord[boundSheetRecords.size()];
 		boundSheetRecords.toArray(bsrs);
 		Arrays.sort(bsrs, BOFComparator);
 	 	return bsrs;
 	}
-	private static final Comparator BOFComparator = new Comparator() {
-		public int compare(Object bsr1, Object bsr2) {
-			return compare((BoundSheetRecord)bsr1, (BoundSheetRecord)bsr2);
-		}
+	private static final Comparator<BoundSheetRecord> BOFComparator = new Comparator<BoundSheetRecord>() {
+
 		public int compare(BoundSheetRecord bsr1, BoundSheetRecord bsr2) {
 			return bsr1.getPositionOfBof() - bsr2.getPositionOfBof();
 		}

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/CFHeaderRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/CFHeaderRecord.java?rev=718810&r1=718809&r2=718810&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/CFHeaderRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/CFHeaderRecord.java Tue Nov 18 16:06:06 2008
@@ -20,15 +20,15 @@
 import org.apache.poi.hssf.record.cf.CellRangeUtil;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.CellRangeAddressList;
-import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianOutput;
 
 /**
- * Conditional Formatting Header record CFHEADER (0x1B0)
+ * Conditional Formatting Header record CFHEADER (0x01B0)
  * 
  * @author Dmitriy Kumshayev
  */
-public final class CFHeaderRecord extends Record {
-	public static final short sid = 0x1B0;
+public final class CFHeaderRecord extends StandardRecord {
+	public static final short sid = 0x01B0;
 
 	private int field_1_numcf;
 	private int field_2_need_recalculation;
@@ -136,23 +136,15 @@
 			+ field_4_cell_ranges.getSize();
 	}
 	
-	/**
-	 * @return byte array containing instance data
-	 */
-	public int serialize(int offset, byte[] data) {
-		int dataSize = getDataSize();
-		
-		LittleEndian.putUShort(data, 0 + offset, sid);
-		LittleEndian.putUShort(data, 2 + offset, dataSize);
-		LittleEndian.putUShort(data, 4 + offset, field_1_numcf);
-		LittleEndian.putUShort(data, 6 + offset, field_2_need_recalculation);
-		field_3_enclosing_cell_range.serialize(8 + offset, data);
-		field_4_cell_ranges.serialize(16 + offset, data);
-		return 4 + dataSize;
+	public void serialize(LittleEndianOutput out) {
+
+		out.writeShort(field_1_numcf);
+		out.writeShort(field_2_need_recalculation);
+		field_3_enclosing_cell_range.serialize(out);
+		field_4_cell_ranges.serialize(out);
 	}
 
-	public short getSid()
-	{
+	public short getSid() {
 		return sid;
 	}
 

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/ChartTitleFormatRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/ChartTitleFormatRecord.java?rev=718810&r1=718809&r2=718810&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/ChartTitleFormatRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/ChartTitleFormatRecord.java Tue Nov 18 16:06:06 2008
@@ -20,76 +20,67 @@
  */
 package org.apache.poi.hssf.record;
 
-import org.apache.poi.util.LittleEndian;
-
-import java.util.ArrayList;
+import org.apache.poi.util.LittleEndianOutput;
 
 /**
+ * CHARTTITLEFORMAT (0x1050)<p/>
  * Describes the formatting runs associated with a chart title.
  */
-public class ChartTitleFormatRecord extends Record {
+public class ChartTitleFormatRecord extends StandardRecord {
 	public static final short sid = 0x1050;
 	
-	private int 		m_recs;
+	private CTFormat[] _formats;
 	
-	private class CTFormat {
-		private short m_offset;
-		private short m_fontIndex;
+	private static final class CTFormat {
+		public static final int ENCODED_SIZE=4;
+		private int _offset;
+		private int _fontIndex;
 		
 		protected CTFormat(short offset,short fontIdx){
-			m_offset = offset;
-			m_fontIndex = fontIdx;
+			_offset = offset;
+			_fontIndex = fontIdx;
 		}
 		
-		public short getOffset(){
-			return m_offset;
+		public CTFormat(RecordInputStream in) {
+			_offset = in.readShort();
+			_fontIndex = in.readShort();
 		}
-		public void setOffset(short newOff){
-			m_offset = newOff;
+
+		public int getOffset(){
+			return _offset;
 		}
-		public short getFontIndex() {
-			return m_fontIndex;
+		public void setOffset(int newOff){
+			_offset = newOff;
+		}
+		public int getFontIndex() {
+			return _fontIndex;
 		}
-	}
-	
-	private ArrayList m_formats;
 
-	public ChartTitleFormatRecord() {
-		super();
+		public void serialize(LittleEndianOutput out) {
+			out.writeShort(_offset);
+			out.writeShort(_fontIndex);
+		}
 	}
 
+
 	public ChartTitleFormatRecord(RecordInputStream in) {
-		m_recs = in.readUShort();
-		int idx;
-		CTFormat ctf;
-		if (m_formats == null){
-			m_formats = new ArrayList(m_recs);
-		}
-		for(idx=0;idx<m_recs;idx++) {
-			ctf = new CTFormat(in.readShort(),in.readShort());
-			m_formats.add(ctf);
+		int nRecs = in.readUShort();
+		_formats = new CTFormat[nRecs];
+
+		for(int i=0;i<nRecs;i++) {
+			_formats[i] = new CTFormat(in);
 		}
 	}
 
-	public int serialize(int offset, byte [] data)
-    {
-        LittleEndian.putShort(data, 0 + offset, sid);
-        LittleEndian.putShort(data, 2 + offset,
-                              ( short ) (getRecordSize() - 4));   
-        int idx;
-        CTFormat ctf;
-        LittleEndian.putShort(data, 4 + offset,(short)m_formats.size());
-        for(idx=0;idx<m_formats.size();idx++){
-        	ctf = (CTFormat)m_formats.get(idx);
-        	LittleEndian.putShort(data, 6 + (idx * 4) + offset, ctf.getOffset());
-        	LittleEndian.putShort(data, 8 + (idx * 4) + offset, ctf.getFontIndex());
+	public void serialize(LittleEndianOutput out) {
+        out.writeShort(_formats.length);
+        for(int i=0; i<_formats.length; i++){
+            _formats[i].serialize(out);
         }
-        
-        return getRecordSize();
     }
 
     protected int getDataSize() {
-        return 2 + (4 * m_formats.size());
+        return 2 + CTFormat.ENCODED_SIZE * _formats.length;
     }
     
 	public short getSid() {
@@ -97,33 +88,29 @@
 	}
 	
 	public int getFormatCount() {
-		return m_formats.size();
+		return _formats.length;
 	}
 	
-	public void modifyFormatRun(short oldPos,short newLen) {
-		short shift = (short)0;
-		for(int idx=0;idx < m_formats.size();idx++) {
-			CTFormat ctf = (CTFormat)m_formats.get(idx);
+	public void modifyFormatRun(short oldPos, short newLen) {
+		int shift = 0;
+		for(int i=0; i < _formats.length; i++) {
+			CTFormat ctf = _formats[i];
 			if (shift != 0) {
-				ctf.setOffset((short)(ctf.getOffset() + shift));
-			} else if ((oldPos == ctf.getOffset()) && (idx < (m_formats.size() - 1))){
-				CTFormat nextCTF = (CTFormat)m_formats.get(idx + 1);
-				shift = (short)(newLen - (nextCTF.getOffset() - ctf.getOffset()));
+				ctf.setOffset(ctf.getOffset() + shift);
+			} else if (oldPos == ctf.getOffset() && i < _formats.length - 1){
+				CTFormat nextCTF = _formats[i + 1];
+				shift = newLen - (nextCTF.getOffset() - ctf.getOffset());
 			} 
 		}
 	}
 	
-	public String toString()
-    {
+	public String toString() {
         StringBuffer buffer = new StringBuffer();
 
         buffer.append("[CHARTTITLEFORMAT]\n");
-        buffer.append("    .format_runs       = ").append(m_recs)
-            .append("\n");
-        int idx;
-        CTFormat ctf;
-        for(idx=0;idx<m_formats.size();idx++){
-        	ctf = (CTFormat)m_formats.get(idx);
+        buffer.append("    .format_runs       = ").append(_formats.length).append("\n");
+        for(int i=0; i<_formats.length; i++) {
+            CTFormat ctf = _formats[i];
         	buffer.append("       .char_offset= ").append(ctf.getOffset());
         	buffer.append(",.fontidx= ").append(ctf.getFontIndex());
             buffer.append("\n");

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/DBCellRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/DBCellRecord.java?rev=718810&r1=718809&r2=718810&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/DBCellRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/DBCellRecord.java Tue Nov 18 16:06:06 2008
@@ -17,29 +17,56 @@
 
 package org.apache.poi.hssf.record;
 
-import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.HexDump;
+import org.apache.poi.util.LittleEndianOutput;
 
 /**
- * Title:        DBCell Record
+ * Title:        DBCell Record (0x00D7)<p/>
  * Description:  Used by Excel and other MS apps to quickly find rows in the sheets.<P>
  * REFERENCE:  PG 299/440 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
  * @author Andrew C. Oliver (acoliver at apache dot org)
  * @author Jason Height
- * @version 2.0-pre
  */
-public final class DBCellRecord extends Record {
+public final class DBCellRecord extends StandardRecord {
+    public final static short sid = 0x00D7;
     public final static int BLOCK_SIZE = 32;
-    public final static short sid = 0xd7;
-    private int               field_1_row_offset;
-    private short[]           field_2_cell_offsets;
+    
+    public static final class Builder {
+        private short[] _cellOffsets;
+        private int _nCellOffsets;
+        public Builder() {
+        	_cellOffsets = new short[4];
+		}
+
+        public void addCellOffset(int cellRefOffset) {
+            if (_cellOffsets.length <= _nCellOffsets) {
+                short[] temp = new short[_nCellOffsets * 2];
+                System.arraycopy(_cellOffsets, 0, temp, 0, _nCellOffsets);
+                _cellOffsets = temp;
+            }
+            _cellOffsets[_nCellOffsets] = (short) cellRefOffset;
+            _nCellOffsets++;
+        }
+
+        public DBCellRecord build(int rowOffset) {
+            short[] cellOffsets = new short[_nCellOffsets];
+            System.arraycopy(_cellOffsets, 0, cellOffsets, 0, _nCellOffsets);
+            return new DBCellRecord(rowOffset, cellOffsets);
+        }
+    }
+    /**
+     * offset from the start of this DBCellRecord to the start of the first cell in
+     * the next DBCell block.
+     */
+    private final int     field_1_row_offset;
+    private final short[] field_2_cell_offsets;
 
-    public DBCellRecord()
-    {
-        field_2_cell_offsets = new short[0];
+    DBCellRecord(int rowOffset, short[]cellOffsets) {
+        field_1_row_offset = rowOffset;
+        field_2_cell_offsets = cellOffsets;
     }
 
-    public DBCellRecord(RecordInputStream in)
-    {
+    public DBCellRecord(RecordInputStream in) {
         field_1_row_offset   = in.readUShort();
         int size = in.remaining();        
         field_2_cell_offsets = new short[ size / 2 ];
@@ -50,101 +77,28 @@
         }
     }
 
-    /**
-     * sets offset from the start of this DBCellRecord to the start of the first cell in
-     * the next DBCell block.
-     *
-     * @param offset    offset to the start of the first cell in the next DBCell block
-     */
-    public void setRowOffset(int offset)
-    {
-        field_1_row_offset = offset;
-    }
-
-    // need short list impl.
-    public void addCellOffset(short offset)
-    {
-        if (field_2_cell_offsets == null)
-        {
-            field_2_cell_offsets = new short[ 1 ];
-        }
-        else
-        {
-            short[] temp = new short[ field_2_cell_offsets.length + 1 ];
-
-            System.arraycopy(field_2_cell_offsets, 0, temp, 0,
-                             field_2_cell_offsets.length);
-            field_2_cell_offsets = temp;
-        }
-        field_2_cell_offsets[ field_2_cell_offsets.length - 1 ] = offset;
-    }
-
-    /**
-     * gets offset from the start of this DBCellRecord to the start of the first cell in
-     * the next DBCell block.
-     *
-     * @return rowoffset to the start of the first cell in the next DBCell block
-     */
-    public int getRowOffset()
-    {
-        return field_1_row_offset;
-    }
-
-    /**
-     * return the cell offset in the array
-     *
-     * @param index of the cell offset to retrieve
-     * @return celloffset from the celloffset array
-     */
-    public short getCellOffsetAt(int index)
-    {
-        return field_2_cell_offsets[ index ];
-    }
 
-    /**
-     * get the number of cell offsets in the celloffset array
-     *
-     * @return number of cell offsets
-     */
-    public int getNumCellOffsets()
-    {
-        return field_2_cell_offsets.length;
-    }
-
-    public String toString()
-    {
+    public String toString() {
         StringBuffer buffer = new StringBuffer();
 
         buffer.append("[DBCELL]\n");
-        buffer.append("    .rowoffset       = ")
-            .append(Integer.toHexString(getRowOffset())).append("\n");
-        for (int k = 0; k < getNumCellOffsets(); k++)
-        {
-            buffer.append("    .cell_" + k + "          = ")
-                .append(Integer.toHexString(getCellOffsetAt(k))).append("\n");
+        buffer.append("    .rowoffset = ").append(HexDump.intToHex(field_1_row_offset)).append("\n");
+        for (int k = 0; k < field_2_cell_offsets.length; k++) {
+            buffer.append("    .cell_").append(k).append(" = ")
+                .append(HexDump.shortToHex(field_2_cell_offsets[ k ])).append("\n");
         }
         buffer.append("[/DBCELL]\n");
         return buffer.toString();
     }
 
-    public int serialize(int offset, byte [] data)
-    {
-        if (field_2_cell_offsets == null)
-        {
-            field_2_cell_offsets = new short[ 0 ];
-        }
-        LittleEndian.putShort(data, 0 + offset, sid);
-        LittleEndian.putShort(data, 2 + offset,
-                              (( short ) (4 + (getNumCellOffsets() * 2))));
-        LittleEndian.putInt(data, 4 + offset, getRowOffset());
-        for (int k = 0; k < getNumCellOffsets(); k++)
-        {
-            LittleEndian.putShort(data, 8 + 2*k + offset, getCellOffsetAt(k));
+    public void serialize(LittleEndianOutput out) {
+        out.writeInt(field_1_row_offset);
+        for (int k = 0; k < field_2_cell_offsets.length; k++) {
+            out.writeShort(field_2_cell_offsets[ k ]);
         }
-        return getRecordSize();
     }
     protected int getDataSize() {
-    	return 4 + (getNumCellOffsets() * 2);
+        return 4 + field_2_cell_offsets.length * 2;
     }
     
     /**
@@ -158,14 +112,12 @@
         return nBlocks * 8 + nRows * 2;
     }
 
-    public short getSid()
-    {
+    public short getSid() {
         return sid;
     }
 
     public Object clone() {
-        // TODO - make immutable.
-        // this should be safe because only the instantiating code mutates these objects
+        // safe because immutable
         return this;
     }
 }

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/DVALRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/DVALRecord.java?rev=718810&r1=718809&r2=718810&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/DVALRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/DVALRecord.java Tue Nov 18 16:06:06 2008
@@ -16,16 +16,15 @@
 
 package org.apache.poi.hssf.record;
 
-import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianOutput;
 
 /**
- * Title:        DATAVALIDATIONS Record<P>
+ * Title:        DATAVALIDATIONS Record (0x01B2)<p/>
  * Description:  used in data validation ;
  *               This record is the list header of all data validation records (0x01BE) in the current sheet.
  * @author Dragos Buleandra (dragos.buleandra@trade2b.ro)
  */
-public class DVALRecord extends Record
-{
+public final class DVALRecord extends StandardRecord {
 	public final static short sid = 0x01B2;
 
 	/** Options of the DVAL */
@@ -48,57 +47,51 @@
         field_5_dv_no = 0x00000000;
     }
 
-	public DVALRecord(RecordInputStream in)
-	{
-		this.field_1_options = in.readShort();
-		this.field_2_horiz_pos = in.readInt();
-		this.field_3_vert_pos = in.readInt();
-        this.field_cbo_id    = in.readInt(); 
-        this.field_5_dv_no   = in.readInt();
+	public DVALRecord(RecordInputStream in) {
+		field_1_options = in.readShort();
+		field_2_horiz_pos = in.readInt();
+		field_3_vert_pos = in.readInt();
+        field_cbo_id    = in.readInt(); 
+        field_5_dv_no   = in.readInt();
 	}
 
-
     /**
 	 * @param field_1_options the options of the dialog
 	 */
-	public void setOptions(short field_1_options) {
-		this.field_1_options = field_1_options;
+	public void setOptions(short options) {
+		field_1_options = options;
 	}
 
 	/**
 	 * @param field_2_horiz_pos the Horizontal position of the dialog
 	 */
-	public void setHorizontalPos(int field_2_horiz_pos) {
-		this.field_2_horiz_pos = field_2_horiz_pos;
+	public void setHorizontalPos(int horiz_pos) {
+		field_2_horiz_pos = horiz_pos;
 	}
 
 	/**
 	 * @param field_3_vert_pos the Vertical position of the dialog
 	 */
-	public void setVerticalPos(int field_3_vert_pos) {
-		this.field_3_vert_pos = field_3_vert_pos;
+	public void setVerticalPos(int vert_pos) {
+		field_3_vert_pos = vert_pos;
 	}
 
 	/**
      * set the object ID of the drop down arrow object for list boxes
      * @param cboID - Object ID
      */
-    public void setObjectID(int cboID)
-    {
-        this.field_cbo_id = cboID;
+    public void setObjectID(int cboID) {
+        field_cbo_id = cboID;
     }
 
     /**
      * Set the number of following DV records
      * @param dvNo - the DV records number
      */
-    public void setDVRecNo(int dvNo)
-    {
-        this.field_5_dv_no = dvNo;
+    public void setDVRecNo(int dvNo) {
+        field_5_dv_no = dvNo;
     }
 
-    
-    
     /**
 	 * @return the field_1_options
 	 */
@@ -123,64 +116,55 @@
 	/**
      * get Object ID of the drop down arrow object for list boxes
      */
-    public int getObjectID( )
-    {
-        return this.field_cbo_id;
+    public int getObjectID() {
+        return field_cbo_id;
     }
 
     /**
      * Get number of following DV records
      */
-    public int getDVRecNo( )
-    {
-        return this.field_5_dv_no;
+    public int getDVRecNo() {
+        return field_5_dv_no;
     }
 
 
-	public String toString()
-	{
+	public String toString() {
 		StringBuffer buffer = new StringBuffer();
 
 		buffer.append("[DVAL]\n");
-		buffer.append("    .options      = ").append(this.getOptions()).append('\n');
-		buffer.append("    .horizPos     = ").append(this.getHorizontalPos()).append('\n');
-		buffer.append("    .vertPos      = ").append(this.getVerticalPos()).append('\n');
-		buffer.append("    .comboObjectID   = ").append(Integer.toHexString(this.getObjectID())).append("\n");
-		buffer.append("    .DVRecordsNumber = ").append(Integer.toHexString(this.getDVRecNo())).append("\n");
+		buffer.append("    .options      = ").append(getOptions()).append('\n');
+		buffer.append("    .horizPos     = ").append(getHorizontalPos()).append('\n');
+		buffer.append("    .vertPos      = ").append(getVerticalPos()).append('\n');
+		buffer.append("    .comboObjectID   = ").append(Integer.toHexString(getObjectID())).append("\n");
+		buffer.append("    .DVRecordsNumber = ").append(Integer.toHexString(getDVRecNo())).append("\n");
 		buffer.append("[/DVAL]\n");
 		return buffer.toString();
 	}
 
-    public int serialize(int offset, byte [] data)
-    {
-        LittleEndian.putShort(data, 0 + offset, this.sid);
-        LittleEndian.putShort(data, 2 + offset, ( short)(this.getRecordSize()-4));
-		
-		LittleEndian.putShort(data, 4 + offset, this.getOptions());
-		LittleEndian.putInt(data, 6 + offset, this.getHorizontalPos());
-		LittleEndian.putInt(data, 10 + offset, this.getVerticalPos());
-        LittleEndian.putInt(data, 14 + offset, this.getObjectID());
-        LittleEndian.putInt(data, 18 + offset, this.getDVRecNo());
-        return getRecordSize();
+    public void serialize(LittleEndianOutput out) {
+ 		
+		out.writeShort(getOptions());
+		out.writeInt(getHorizontalPos());
+		out.writeInt(getVerticalPos());
+		out.writeInt(getObjectID());
+		out.writeInt(getDVRecNo());
     }
 
     protected int getDataSize() {
         return 18;
     }
 
-    public short getSid()
-    {
-        return this.sid;
+    public short getSid() {
+        return sid;
     }
 
-    public Object clone()
-    {
+    public Object clone() {
       DVALRecord rec = new DVALRecord();
       rec.field_1_options = field_1_options;
       rec.field_2_horiz_pos = field_2_horiz_pos;
       rec.field_3_vert_pos = field_3_vert_pos;
-      rec.field_cbo_id = this.field_cbo_id;
-      rec.field_5_dv_no = this.field_5_dv_no;
+      rec.field_cbo_id = field_cbo_id;
+      rec.field_5_dv_no = field_5_dv_no;
       return rec;
     }
 }

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/DrawingRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/DrawingRecord.java?rev=718810&r1=718809&r2=718810&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/DrawingRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/DrawingRecord.java Tue Nov 18 16:06:06 2008
@@ -17,9 +17,12 @@
 
 package org.apache.poi.hssf.record;
 
-import org.apache.poi.util.LittleEndian;
-
-public final class DrawingRecord extends Record {
+import org.apache.poi.util.LittleEndianOutput;
+/**
+ * DrawingRecord (0x00EC)<p/>
+ *
+ */
+public final class DrawingRecord extends StandardRecord {
     public static final short sid = 0x00EC;
 
 	private static final byte[] EMPTY_BYTE_ARRAY = { };
@@ -31,70 +34,50 @@
     	recordData = EMPTY_BYTE_ARRAY;
     }
 
-    public DrawingRecord( RecordInputStream in )
-    {
+    public DrawingRecord(RecordInputStream in) {
       recordData = in.readRemainder();
     }
 
-    public void processContinueRecord( byte[] record )
-    {
+    public void processContinueRecord(byte[] record) {
         //don't merge continue record with the drawing record, it must be serialized separately
         contd = record;
     }
 
-    public int serialize( int offset, byte[] data )
-    {
-        if (recordData == null)
-        {
-            recordData = new byte[ 0 ];
-        }
-        LittleEndian.putShort(data, 0 + offset, sid);
-        LittleEndian.putShort(data, 2 + offset, ( short ) (recordData.length));
-        if (recordData.length > 0)
-        {
-            System.arraycopy(recordData, 0, data, 4 + offset, recordData.length);
-        }
-        return getRecordSize();
+    public void serialize(LittleEndianOutput out) {
+        out.write(recordData);
     }
     protected int getDataSize() {
-        int retval = 0;
-
-        if (recordData != null) {
-            retval += recordData.length;
-        }
-        return retval;
+        return recordData.length;
     }
 
-    public short getSid()
-    {
+    public short getSid() {
         return sid;
     }
 
-    public byte[] getData()
-    {
+    public byte[] getData() {
         if(contd != null) {
             byte[] newBuffer = new byte[ recordData.length + contd.length ];
             System.arraycopy( recordData, 0, newBuffer, 0, recordData.length );
             System.arraycopy( contd, 0, newBuffer, recordData.length, contd.length);
             return newBuffer;
-        } else {
-            return recordData;
         }
+        return recordData;
     }
 
-    public void setData( byte[] thedata )
-    {
-        this.recordData = thedata;
+    public void setData(byte[] thedata) {
+    	if (thedata == null) {
+    		throw new IllegalArgumentException("data must not be null");
+    	}
+        recordData = thedata;
     }
 
     public Object clone() {
     	DrawingRecord rec = new DrawingRecord();
     	
-    	rec.recordData = new byte[ recordData.length ];
-    	System.arraycopy(recordData, 0, rec.recordData, 0, recordData.length);
+    	rec.recordData = recordData.clone();
     	if (contd != null) {
-	    	System.arraycopy(contd, 0, rec.contd, 0, contd.length);
-	    	rec.contd = new byte[ contd.length ];
+	    	// TODO - this code probably never executes
+	    	rec.contd = contd.clone();
     	}
     	
     	return rec;

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/ExtSSTRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/ExtSSTRecord.java?rev=718810&r1=718809&r2=718810&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/ExtSSTRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/ExtSSTRecord.java Tue Nov 18 16:06:06 2008
@@ -17,13 +17,10 @@
 
 package org.apache.poi.hssf.record;
 
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianOutput;
 
 /**
- * Title:        Extended Static String Table<P>
+ * Title:        Extended Static String Table (0x00FF)<p/>
  * Description: This record is used for a quick lookup into the SST record. This
  *              record breaks the SST table into a set of buckets. The offsets
  *              to these buckets within the SST record are kept as well as the
@@ -31,101 +28,108 @@
  * REFERENCE:  PG 313 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
  * @author Andrew C. Oliver (acoliver at apache dot org)
  * @author Jason Height (jheight at apache dot org)
- * @version 2.0-pre
+ *
  * @see org.apache.poi.hssf.record.ExtSSTInfoSubRecord
  */
-public final class ExtSSTRecord extends Record {
+public final class ExtSSTRecord extends StandardRecord {
     public final static short sid = 0x00FF;
     public static final int DEFAULT_BUCKET_SIZE = 8;
     //Can't seem to find this documented but from the biffviewer it is clear that
     //Excel only records the indexes for the first 128 buckets.
     public static final int MAX_BUCKETS = 128;
-    private short             field_1_strings_per_bucket = DEFAULT_BUCKET_SIZE;
-    private List         field_2_sst_info;
-
-
-    public ExtSSTRecord()
-    {
-        field_2_sst_info = new ArrayList();
-    }
-
-    public ExtSSTRecord(RecordInputStream in)
-    {
-        field_2_sst_info           = new ArrayList();
-        field_1_strings_per_bucket = in.readShort();
-        while (in.remaining() > 0) {
-            ExtSSTInfoSubRecord rec = new ExtSSTInfoSubRecord(in);
+    
+    
+    private static final class InfoSubRecord {
+    	public static final int ENCODED_SIZE = 8;
+        private int field_1_stream_pos;          // stream pointer to the SST record
+        private int field_2_bucket_sst_offset;   // don't really understand this yet.
+        /** unused - supposed to be zero */
+        private short field_3_zero;
+
+        /** Creates new ExtSSTInfoSubRecord */
+
+        public InfoSubRecord(int streamPos, int bucketSstOffset) {
+            field_1_stream_pos        = streamPos;
+            field_2_bucket_sst_offset = bucketSstOffset;
+        }
 
-            field_2_sst_info.add(rec);
+        public InfoSubRecord(RecordInputStream in)
+        {
+            field_1_stream_pos        = in.readInt();
+            field_2_bucket_sst_offset = in.readShort();
+            field_3_zero              = in.readShort();
         }
-    }
 
-    public void setNumStringsPerBucket(short numStrings)
-    {
-        field_1_strings_per_bucket = numStrings;
-    }
+        public int getStreamPos() {
+            return field_1_stream_pos;
+        }
 
-    public void addInfoRecord(ExtSSTInfoSubRecord rec)
-    {
-        field_2_sst_info.add(rec);
-    }
+        public int getBucketSSTOffset() {
+            return field_2_bucket_sst_offset;
+        }
 
-    public short getNumStringsPerBucket()
-    {
-        return field_1_strings_per_bucket;
+        public void serialize(LittleEndianOutput out) {
+            out.writeInt(field_1_stream_pos);
+            out.writeShort(field_2_bucket_sst_offset);
+            out.writeShort(field_3_zero);
+        }
     }
-
-    public int getNumInfoRecords()
-    {
-        return field_2_sst_info.size();
+    
+    
+    private short _stringsPerBucket;
+    private InfoSubRecord[] _sstInfos;
+
+
+    public ExtSSTRecord() {
+    	_stringsPerBucket = DEFAULT_BUCKET_SIZE;
+        _sstInfos = new InfoSubRecord[0];
+    }
+
+    public ExtSSTRecord(RecordInputStream in) {
+        _stringsPerBucket = in.readShort();
+        int nInfos = in.remaining() / InfoSubRecord.ENCODED_SIZE;
+        _sstInfos = new InfoSubRecord[nInfos];
+        for (int i = 0; i < _sstInfos.length; i++) {
+            _sstInfos[i] = new InfoSubRecord(in);
+        }
     }
 
-    public ExtSSTInfoSubRecord getInfoRecordAt(int elem)
-    {
-        return ( ExtSSTInfoSubRecord ) field_2_sst_info.get(elem);
+    public void setNumStringsPerBucket(short numStrings) {
+        _stringsPerBucket = numStrings;
     }
 
-    public String toString()
-    {
+    public String toString() {
         StringBuffer buffer = new StringBuffer();
 
         buffer.append("[EXTSST]\n");
         buffer.append("    .dsst           = ")
-            .append(Integer.toHexString(getNumStringsPerBucket()))
+            .append(Integer.toHexString(_stringsPerBucket))
             .append("\n");
-        buffer.append("    .numInfoRecords = ").append(getNumInfoRecords())
+        buffer.append("    .numInfoRecords = ").append(_sstInfos.length)
             .append("\n");
-        for (int k = 0; k < getNumInfoRecords(); k++)
+        for (int k = 0; k < _sstInfos.length; k++)
         {
             buffer.append("    .inforecord     = ").append(k).append("\n");
             buffer.append("    .streampos      = ")
                 .append(Integer
-                .toHexString(getInfoRecordAt(k).getStreamPos())).append("\n");
+                .toHexString(_sstInfos[k].getStreamPos())).append("\n");
             buffer.append("    .sstoffset      = ")
                 .append(Integer
-                .toHexString(getInfoRecordAt(k).getBucketSSTOffset()))
+                .toHexString(_sstInfos[k].getBucketSSTOffset()))
                     .append("\n");
         }
         buffer.append("[/EXTSST]\n");
         return buffer.toString();
     }
 
-    public int serialize(int offset, byte [] data)
-    {
-        LittleEndian.putShort(data, 0 + offset, sid);
-        LittleEndian.putShort(data, 2 + offset, (short)(getRecordSize() - 4));
-        LittleEndian.putShort(data, 4 + offset, field_1_strings_per_bucket);
-        int pos = 6;
-
-        for (int k = 0; k < getNumInfoRecords(); k++)
-        {
-            ExtSSTInfoSubRecord rec = getInfoRecordAt(k);
-            pos += rec.serialize(pos + offset, data);
+    public void serialize(LittleEndianOutput out) {
+        out.writeShort(_stringsPerBucket);
+        for (int k = 0; k < _sstInfos.length; k++) {
+            _sstInfos[k].serialize(out);
         }
-        return pos;
     }
     protected int getDataSize() {
-    	return 2 + 8*getNumInfoRecords();
+    	return 2 + InfoSubRecord.ENCODED_SIZE*_sstInfos.length;
     }
 
     public static final int getNumberOfInfoRecsForStrings(int numStrings) {
@@ -133,7 +137,7 @@
       if ((numStrings % DEFAULT_BUCKET_SIZE) != 0)
         infoRecs ++;
       //Excel seems to max out after 128 info records.
-      //This isnt really documented anywhere...
+      //This isn't really documented anywhere...
       if (infoRecs > MAX_BUCKETS)
         infoRecs = MAX_BUCKETS;
       return infoRecs;
@@ -141,24 +145,18 @@
 
     /** Given a number of strings (in the sst), returns the size of the extsst record*/
     public static final int getRecordSizeForStrings(int numStrings) {
-      return 4 + 2 + (getNumberOfInfoRecsForStrings(numStrings) * 8);
+        return 4 + 2 + getNumberOfInfoRecsForStrings(numStrings) * 8;
     }
 
-    public short getSid()
-    {
+    public short getSid() {
         return sid;
     }
 
-    public void setBucketOffsets( int[] bucketAbsoluteOffsets, int[] bucketRelativeOffsets )
-    {
-        this.field_2_sst_info = new ArrayList(bucketAbsoluteOffsets.length);
-        for ( int i = 0; i < bucketAbsoluteOffsets.length; i++ )
-        {
-            ExtSSTInfoSubRecord r = new ExtSSTInfoSubRecord();
-            r.setBucketRecordOffset((short)bucketRelativeOffsets[i]);
-            r.setStreamPos(bucketAbsoluteOffsets[i]);
-            field_2_sst_info.add(r);
+    public void setBucketOffsets(int[] bucketAbsoluteOffsets, int[] bucketRelativeOffsets) {
+    	// TODO - replace no-arg constructor with this logic
+        _sstInfos = new InfoSubRecord[bucketAbsoluteOffsets.length];
+        for (int i = 0; i < bucketAbsoluteOffsets.length; i++) {
+            _sstInfos[i] = new InfoSubRecord(bucketAbsoluteOffsets[i], bucketRelativeOffsets[i]);
         }
     }
-
 }

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/ExternSheetRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/ExternSheetRecord.java?rev=718810&r1=718809&r2=718810&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/ExternSheetRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/ExternSheetRecord.java Tue Nov 18 16:06:06 2008
@@ -20,7 +20,7 @@
 import java.util.ArrayList;
 import java.util.List;
 
-import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianOutput;
 
 /**
  * EXTERNSHEET (0x0017)<br/>
@@ -28,9 +28,9 @@
  * 
  * @author Libin Roman (Vista Portal LDT. Developer)
  */
-public class ExternSheetRecord extends Record {
+public class ExternSheetRecord extends StandardRecord {
 	public final static short sid = 0x0017;
-	private List _list;
+	private List<RefSubRecord> _list;
 	
 	private final class RefSubRecord {
 		public static final int ENCODED_SIZE = 6;
@@ -73,43 +73,27 @@
 			return buffer.toString();
 		}
 		
-		/**
-		 * called by the class that is responsible for writing this sucker.
-		 * Subclasses should implement this so that their data is passed back in a
-		 * byte array.
-		 *
-		 * @param offset to begin writing at
-		 * @param data byte array containing instance data
-		 * @return number of bytes written
-		 */
-		public void serialize(int offset, byte [] data) {
-			LittleEndian.putUShort(data, 0 + offset, _extBookIndex);
-			LittleEndian.putUShort(data, 2 + offset, _firstSheetIndex);
-			LittleEndian.putUShort(data, 4 + offset, _lastSheetIndex);
+		public void serialize(LittleEndianOutput out) {
+			out.writeShort(_extBookIndex);
+			out.writeShort(_firstSheetIndex);
+			out.writeShort(_lastSheetIndex);
 		}
 	}	
 	
 	
 	
 	public ExternSheetRecord() {
-		_list = new ArrayList();
+		_list = new ArrayList<RefSubRecord>();
 	}
-	
-	/**
-	 * called by the constructor, should set class level fields.  Should throw
-	 * runtime exception for bad/icomplete data.
-	 *
-	 * @param in the RecordInputstream to read the record from
-	 */
+
 	public ExternSheetRecord(RecordInputStream in) {
-		_list		   = new ArrayList();
+		_list = new ArrayList<RefSubRecord>();
 		
 		int nItems  = in.readShort();
 		
 		for (int i = 0 ; i < nItems ; ++i) {
 			RefSubRecord rec = new RefSubRecord(in);
-			
-			_list.add( rec);
+			_list.add(rec);
 		}
 	}
 	
@@ -157,35 +141,18 @@
 		return 2 + _list.size() * RefSubRecord.ENCODED_SIZE;
 	}
 	
-	/**
-	 * called by the class that is responsible for writing this sucker.
-	 * Subclasses should implement this so that their data is passed back in a
-	 * byte array.
-	 *
-	 * @param offset to begin writing at
-	 * @param data byte array containing instance data
-	 * @return number of bytes written
-	 */
-	public int serialize(int offset, byte [] data) {
-		int dataSize = getDataSize();
-		
+	public void serialize(LittleEndianOutput out) {
 		int nItems = _list.size();
 
-		LittleEndian.putShort(data, 0 + offset, sid);
-		LittleEndian.putUShort(data, 2 + offset, dataSize);
-		LittleEndian.putUShort(data, 4 + offset, nItems);
-		
-		int pos = 6 ;
+		out.writeShort(nItems);
 		
 		for (int i = 0; i < nItems; i++) {
-			getRef(i).serialize(offset + pos, data);
-			pos +=6;
+			getRef(i).serialize(out);
 		}
-		return dataSize + 4;
 	}
 
 	private RefSubRecord getRef(int i) {
-		return (RefSubRecord) _list.get(i);
+		return _list.get(i);
 	}
 	
 	/**

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/FileSharingRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/FileSharingRecord.java?rev=718810&r1=718809&r2=718810&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/FileSharingRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/FileSharingRecord.java Tue Nov 18 16:06:06 2008
@@ -17,19 +17,19 @@
 
 package org.apache.poi.hssf.record;
 
-import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianOutput;
 import org.apache.poi.util.StringUtil;
 
 /**
- * Title:        FILESHARING<P>
+ * Title:        FILESHARING (0x005B) <p/>
  * Description:  stores the encrypted readonly for a workbook (write protect) 
  * This functionality is accessed from the options dialog box available when performing 'Save As'.<p/>
  * REFERENCE:  PG 314 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<p/>
  * @author Andrew C. Oliver (acoliver at apache dot org)
  */
-public final class FileSharingRecord extends Record {
+public final class FileSharingRecord extends StandardRecord {
 
-    public final static short sid = 0x5b;
+    public final static short sid = 0x005B;
     private short             field_1_readonly;
     private short             field_2_password;
     private byte              field_3_username_unicode_options;
@@ -103,12 +103,6 @@
         return field_2_password;
     }
 
-    /**
-     * @return byte representing the length of the username field
-     */
-    public short getUsernameLength() {
-        return (short) field_3_username_value.length();
-    }
 
     /**
      * @return username of the user that created the file
@@ -139,22 +133,19 @@
         return buffer.toString();
     }
 
-    public int serialize(int offset, byte [] data) {
+    public void serialize(LittleEndianOutput out) {
         // TODO - junit
-        LittleEndian.putShort(data, 0 + offset, sid);
-        LittleEndian.putShort(data, 2 + offset, (short)(getRecordSize()-4));
-        LittleEndian.putShort(data, 4 + offset, getReadOnly());
-        LittleEndian.putShort(data, 6 + offset, getPassword());
-        LittleEndian.putShort(data, 8 + offset, getUsernameLength());
-        if(getUsernameLength() > 0) {
-            LittleEndian.putByte(data, 10 + offset, field_3_username_unicode_options);
-            StringUtil.putCompressedUnicode( getUsername(), data, 11 + offset );
+        out.writeShort(getReadOnly());
+        out.writeShort(getPassword());
+        out.writeShort(field_3_username_value.length());
+        if(field_3_username_value.length() > 0) {
+        	out.writeByte(field_3_username_unicode_options);
+            StringUtil.putCompressedUnicode(getUsername(), out);
         }
-        return getRecordSize();
     }
 
     protected int getDataSize() {
-        short nameLen = getUsernameLength();
+        int nameLen = field_3_username_value.length();
         if (nameLen < 1) {
             return 6;
         }

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/FontRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/FontRecord.java?rev=718810&r1=718809&r2=718810&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/FontRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/FontRecord.java Tue Nov 18 16:06:06 2008
@@ -17,18 +17,19 @@
 
 package org.apache.poi.hssf.record;
 
-import org.apache.poi.util.LittleEndian;
-import org.apache.poi.util.StringUtil;
 import org.apache.poi.util.BitField;
 import org.apache.poi.util.BitFieldFactory;
+import org.apache.poi.util.LittleEndianOutput;
+import org.apache.poi.util.StringUtil;
 
 /**
- * Title:        Font Record - descrbes a font in the workbook (index = 0-3,5-infinity - skip 4)<P>
+ * Title:        Font Record (0x0031) <p/>
+ * - describes a font in the workbook (index = 0-3,5-infinity - skip 4)<P>
  * Description:  An element in the Font Table<P>
  * REFERENCE:  PG 315 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
  * @author Andrew C. Oliver (acoliver at apache dot org)
  */
-public final class FontRecord extends Record {
+public final class FontRecord extends StandardRecord {
     public final static short     sid                 = 0x0031;                                                 // docs are wrong (0x231 Microsoft Support site article Q184647)
     public final static short     SS_NONE             = 0;
     public final static short     SS_SUPER            = 1;
@@ -42,16 +43,12 @@
     private short                 field_2_attributes;
 
     // 0 0x01 - Reserved bit must be 0
-    static final private BitField italic     =
-        BitFieldFactory.getInstance(0x02);                                   // is this font in italics
+    private static final BitField italic     = BitFieldFactory.getInstance(0x02); // is this font in italics
 
     // 2 0x04 - reserved bit must be 0
-    static final private BitField strikeout  =
-        BitFieldFactory.getInstance(0x08);                                   // is this font has a line through the center
-    static final private BitField macoutline = BitFieldFactory.getInstance(
-        0x10);                                                // some weird macintosh thing....but who understands those mac people anyhow
-    static final private BitField macshadow  = BitFieldFactory.getInstance(
-        0x20);                                                // some weird macintosh thing....but who understands those mac people anyhow
+    private static final BitField strikeout  =BitFieldFactory.getInstance(0x08);  // is this font has a line through the center
+    private static final BitField macoutline = BitFieldFactory.getInstance(0x10); // some weird macintosh thing....but who understands those mac people anyhow
+    private static final BitField macshadow  = BitFieldFactory.getInstance(0x20); // some weird macintosh thing....but who understands those mac people anyhow
 
     // 7-6 - reserved bits must be 0
     // the rest is unused
@@ -62,15 +59,13 @@
     private byte                  field_7_family;             // ?? defined by windows api logfont structure?
     private byte                  field_8_charset;            // ?? defined by windows api logfont structure?
     private byte                  field_9_zero = 0;           // must be 0
-    private byte                  field_10_font_name_len;     // length of the font name
+    /** possibly empty string never <code>null</code> */
     private String                field_11_font_name;         // whoa...the font name
 
-    public FontRecord()
-    {
+    public FontRecord() {
     }
 
-    public FontRecord(RecordInputStream in)
-    {
+    public FontRecord(RecordInputStream in) {
         field_1_font_height         = in.readShort();
         field_2_attributes          = in.readShort();
         field_3_color_palette_index = in.readShort();
@@ -80,17 +75,15 @@
         field_7_family              = in.readByte();
         field_8_charset             = in.readByte();
         field_9_zero                = in.readByte();
-        field_10_font_name_len      = in.readByte();
-        if (field_10_font_name_len > 0)
-        {
-            if (in.readByte() == 0)
-            {   // is compressed unicode
-                field_11_font_name = in.readCompressedUnicode(LittleEndian.ubyteToInt(field_10_font_name_len));
-            }
-            else
-            {   // is not compressed unicode
+        int field_10_font_name_len  = in.readUByte();
+        if (field_10_font_name_len > 0) {
+            if (in.readByte() == 0) {   // is compressed unicode
+                field_11_font_name = in.readCompressedUnicode(field_10_font_name_len);
+            } else {   // is not compressed unicode
                 field_11_font_name = in.readUnicodeLEString(field_10_font_name_len);
             }
+        } else {
+        	field_11_font_name = "";
         }
     }
 
@@ -244,17 +237,6 @@
         field_8_charset = charset;
     }
 
-    /**
-     * set the length of the fontname string
-     *
-     * @param len  length of the font name
-     * @see #setFontName(String)
-     */
-
-    public void setFontNameLength(byte len)
-    {
-        field_10_font_name_len = len;
-    }
 
     /**
      * set the name of the font
@@ -416,18 +398,6 @@
     }
 
     /**
-     * get the length of the fontname string
-     *
-     * @return length of the font name
-     * @see #getFontName()
-     */
-
-    public byte getFontNameLength()
-    {
-        return field_10_font_name_len;
-    }
-
-    /**
      * get the name of the font
      *
      * @return fn - name of the font (i.e. "Arial")
@@ -467,45 +437,46 @@
             .append(Integer.toHexString(getFamily())).append("\n");
         buffer.append("    .charset         = ")
             .append(Integer.toHexString(getCharset())).append("\n");
-        buffer.append("    .namelength      = ")
-            .append(Integer.toHexString(getFontNameLength())).append("\n");
         buffer.append("    .fontname        = ").append(getFontName())
             .append("\n");
         buffer.append("[/FONT]\n");
         return buffer.toString();
     }
 
-    public int serialize(int offset, byte [] data)
-    {
-        int realflen = getFontNameLength() * 2;
-
-        LittleEndian.putShort(data, 0 + offset, sid);
-        LittleEndian.putShort(
-            data, 2 + offset,
-            ( short ) (15 + realflen
-                       + 1));   // 19 - 4 (sid/len) + font name length = datasize
-
-        // undocumented single byte (1)
-        LittleEndian.putShort(data, 4 + offset, getFontHeight());
-        LittleEndian.putShort(data, 6 + offset, getAttributes());
-        LittleEndian.putShort(data, 8 + offset, getColorPaletteIndex());
-        LittleEndian.putShort(data, 10 + offset, getBoldWeight());
-        LittleEndian.putShort(data, 12 + offset, getSuperSubScript());
-        data[ 14 + offset ] = getUnderline();
-        data[ 15 + offset ] = getFamily();
-        data[ 16 + offset ] = getCharset();
-        data[ 17 + offset ] = field_9_zero;
-        data[ 18 + offset ] = getFontNameLength();
-        data[ 19 + offset ] = ( byte ) 1;
-        if (getFontName() != null) {
-           StringUtil.putUnicodeLE(getFontName(), data, 20 + offset);
-        }
-        return getRecordSize();
+    public void serialize(LittleEndianOutput out) {
+ 
+    	out.writeShort(getFontHeight());
+    	out.writeShort(getAttributes());
+    	out.writeShort(getColorPaletteIndex());
+    	out.writeShort(getBoldWeight());
+    	out.writeShort(getSuperSubScript());
+    	out.writeByte(getUnderline());
+    	out.writeByte(getFamily());
+    	out.writeByte(getCharset());
+    	out.writeByte(field_9_zero);
+    	int fontNameLen = field_11_font_name.length();
+		out.writeByte(fontNameLen);
+		if (fontNameLen > 0) {
+        	boolean hasMultibyte = StringUtil.hasMultibyte(field_11_font_name);
+        	out.writeByte(hasMultibyte ? 0x01 : 0x00);
+            if (hasMultibyte) {
+               StringUtil.putUnicodeLE(field_11_font_name, out);
+            } else {
+                StringUtil.putCompressedUnicode(field_11_font_name, out);
+            }
+		}
     }
     protected int getDataSize() {
-    	// Note - no matter the original, we always
-    	//  re-serialise the font name as unicode
-        return 16 + getFontNameLength() * 2;
+    	int size = 15; // 5 shorts + 5 bytes
+    	int fontNameLen = field_11_font_name.length();
+    	if (fontNameLen < 1) {
+    		// options byte is not encoded if no character data
+    		return size;
+    	}
+    	size ++; // options byte
+    	
+    	boolean hasMultibyte = StringUtil.hasMultibyte(field_11_font_name);
+    	return size + fontNameLen * (hasMultibyte ? 2 : 1);
     }
 
     public short getSid()
@@ -528,7 +499,6 @@
         field_7_family              = source.field_7_family;
         field_8_charset             = source.field_8_charset;
         field_9_zero                = source.field_9_zero;
-        field_10_font_name_len      = source.field_10_font_name_len;
         field_11_font_name          = source.field_11_font_name;
     }
 
@@ -548,7 +518,6 @@
 		result = prime * result + field_7_family;
 		result = prime * result + field_8_charset;
 		result = prime * result + field_9_zero;
-		result = prime * result + field_10_font_name_len;
 		return result;
 	}
 	
@@ -572,7 +541,6 @@
 		field_7_family              == other.field_7_family &&
 		field_8_charset             == other.field_8_charset &&
 		field_9_zero                == other.field_9_zero &&
-		field_10_font_name_len      == other.field_10_font_name_len &&
 		field_11_font_name.equals(other.field_11_font_name)
 		;
 	}

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/FooterRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/FooterRecord.java?rev=718810&r1=718809&r2=718810&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/FooterRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/FooterRecord.java Tue Nov 18 16:06:06 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,191 +14,61 @@
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
-        
 
 package org.apache.poi.hssf.record;
 
-import org.apache.poi.util.LittleEndian;
-import org.apache.poi.util.StringUtil;
-
 /**
- * Title:        Footer Record <P>
+ * Title:        Footer Record (0x0015) <p/>
  * Description:  Specifies the footer for a sheet<P>
- * REFERENCE:  PG 317 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
+ * REFERENCE:  PG 317 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<p/>
  * @author Andrew C. Oliver (acoliver at apache dot org)
  * @author Shawn Laubach (slaubach at apache dot org) Modified 3/14/02
  * @author Jason Height (jheight at chariot dot net dot au)
- * @version 2.0-pre
+ *
  */
+public final class FooterRecord extends HeaderFooterBase {
+    public final static short sid = 0x0015;
 
-public class FooterRecord
-    extends Record
-{
-    public final static short sid = 0x15;
-    private byte              field_1_footer_len;
-    private byte              field_2_reserved;
-    private byte              field_3_unicode_flag;
-    private String            field_4_footer;
-
-    public FooterRecord()
-    {
-    }
-
-    public FooterRecord(RecordInputStream in)
-    {
-        if (in.remaining() > 0)
-        {
-            field_1_footer_len = in.readByte();
-            /** These two fields are a bit odd. They are not documented*/
-            field_2_reserved = in.readByte();
-            field_3_unicode_flag = in.readByte();						// unicode
-    
-                         if(isMultibyte())
-                         {
-                field_4_footer = in.readUnicodeLEString(LittleEndian.ubyteToInt( field_1_footer_len));
-                         }
-                         else
-                         {
-                field_4_footer = in.readCompressedUnicode(LittleEndian.ubyteToInt( field_1_footer_len));
-                         }
-        }
-    }
-
-    /**
-     * see the unicode flag
-     *
-     * @return boolean flag
-     *  true:footer string has at least one multibyte character
-     */
-     public boolean isMultibyte() {
-         return ((field_3_unicode_flag & 0xFF) == 1);
+    public FooterRecord(String text) {
+    	super(text);
     }
 
+    public FooterRecord(RecordInputStream in) {
+		super(in);
+	}
 
-    /**
-     * set the length of the footer string
-     *
-     * @param len  length of the footer string
-     * @see #setFooter(String)
-     */
-
-    public void setFooterLength(byte len)
-    {
-        field_1_footer_len = len;
-    }
-
-    /**
+     /**
      * set the footer string
      *
      * @param footer string to display
-     * @see #setFooterLength(byte)
-     */
-
-    public void setFooter(String footer)
-    {
-        field_4_footer = footer;
-        field_3_unicode_flag = 
-            (byte) (StringUtil.hasMultibyte(field_4_footer) ? 1 : 0);
-        // Check it'll fit into the space in the record
-        
-        if(field_4_footer == null) return;
-        if(field_3_unicode_flag == 1) {
-        	if(field_4_footer.length() > 127) {
-        		throw new IllegalArgumentException("Footer string too long (limit is 127 for unicode strings)");
-        	}
-        } else {
-        	if(field_4_footer.length() > 255) {
-        		throw new IllegalArgumentException("Footer string too long (limit is 255 for non-unicode strings)");
-        	}
-        }
-    }
-
-    /**
-     * get the length of the footer string
-     *
-     * @return length of the footer string
-     * @see #getFooter()
      */
-
-    public short getFooterLength()
-    {
-        return (short)(0xFF & field_1_footer_len); // [Shawn] Fixed needing unsigned byte
+    public void setFooter(String footer) {
+        setText(footer);
     }
 
     /**
      * get the footer string
      *
      * @return footer string to display
-     * @see #getFooterLength()
      */
-
-    public String getFooter()
-    {
-        return field_4_footer;
+    public String getFooter() {
+        return getText();
     }
 
-    public String toString()
-    {
+    public String toString() {
         StringBuffer buffer = new StringBuffer();
 
         buffer.append("[FOOTER]\n");
-        buffer.append("    .footerlen      = ")
-            .append(Integer.toHexString(getFooterLength())).append("\n");
-        buffer.append("    .footer         = ").append(getFooter())
-            .append("\n");
+        buffer.append("    .footer = ").append(getText()).append("\n");
         buffer.append("[/FOOTER]\n");
         return buffer.toString();
     }
 
-    public int serialize(int offset, byte [] data)
-    {
-        int len = 4;
-
-        if (getFooterLength() > 0)
-        {
-            len+=3; // [Shawn] Fixed for two null bytes in the length
-        }
-        short bytelen = (short)(isMultibyte() ?
-            getFooterLength()*2 : getFooterLength() );
-        LittleEndian.putShort(data, 0 + offset, sid);
-        LittleEndian.putShort(data, 2 + offset,
-                              ( short ) ((len - 4) + bytelen ));
-        if (getFooterLength() > 0)
-        {
-            data[ 4 + offset ] = (byte)getFooterLength();
-            data[ 6 + offset ] = field_3_unicode_flag;
-            if(isMultibyte())
-            {
-                StringUtil.putUnicodeLE(getFooter(), data, 7 + offset);
-            }
-            else
-            {
-                StringUtil.putCompressedUnicode(getFooter(), data, 7 + offset); // [Shawn] Place the string in the correct offset
-            }
-        }
-        return getRecordSize();
-    }
-
-    protected int getDataSize() {
-        int retval = 0;
-
-        if (getFooterLength() > 0) {
-            retval+=3; // [Shawn] Fixed for two null bytes in the length
-        }
-        return retval + getFooterLength() * (isMultibyte() ? 2 : 1);
-    }
-
-    public short getSid()
-    {
+    public short getSid() {
         return sid;
     }
 
     public Object clone() {
-      FooterRecord rec = new FooterRecord();
-      rec.field_1_footer_len = field_1_footer_len;
-      rec.field_2_reserved = field_2_reserved;
-      rec.field_3_unicode_flag = field_3_unicode_flag;
-      rec.field_4_footer = field_4_footer;
-      return rec;
+        return new FooterRecord(getText());
     }
 }

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/FormatRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/FormatRecord.java?rev=718810&r1=718809&r2=718810&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/FormatRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/FormatRecord.java Tue Nov 18 16:06:06 2008
@@ -17,91 +17,41 @@
 
 package org.apache.poi.hssf.record;
 
-import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.HexDump;
+import org.apache.poi.util.LittleEndianOutput;
 import org.apache.poi.util.StringUtil;
 
 /**
- * Title:        Format Record<P>
- * Description:  describes a number format -- those goofy strings like $(#,###)<P>
+ * Title:        Format Record (0x041E) <p/>
+ * Description:  describes a number format -- those goofy strings like $(#,###)<p/>
  *
- * REFERENCE:  PG 317 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
+ * REFERENCE:  PG 317 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<p/>
  * @author Andrew C. Oliver (acoliver at apache dot org)
- * @author Shawn M. Laubach (slaubach at apache dot org)  
+ * @author Shawn M. Laubach (slaubach at apache dot org)
  */
-public final class FormatRecord extends Record {
+public final class FormatRecord extends StandardRecord {
     public final static short sid = 0x041E;
-    private short             field_1_index_code;
 
-    private short             field_3_unicode_len;      // unicode string length
-    private boolean          field_3_unicode_flag;     // it is not undocumented - it is unicode flag
-    private String            field_4_formatstring;
-
-    public FormatRecord()
-    {
-    }
-
-    public FormatRecord(RecordInputStream in)
-    {
-        field_1_index_code       = in.readShort();
-        field_3_unicode_len      = in.readShort();
-        field_3_unicode_flag     = ( in.readByte() & (byte)0x01 ) != 0;
-                                              
-      if ( field_3_unicode_flag  ) {
-          // unicode
-          field_4_formatstring = in.readUnicodeLEString( field_3_unicode_len );
-      }
-      else {
-          // not unicode
-          field_4_formatstring = in.readCompressedUnicode( field_3_unicode_len );
-      }
-    }
-
-    /**
-     * set the format index code (for built in formats)
-     *
-     * @param index  the format index code
-     * @see org.apache.poi.hssf.model.Workbook
-     */
-
-    public void setIndexCode(short index)
-    {
-        field_1_index_code = index;
-    }
-
-    /**
-     * set the format string length
-     *
-     * @param len  the length of the format string
-     * @see #setFormatString(String)
-     */
-    
-    public void setFormatStringLength(byte len)
-    {
-
-	field_3_unicode_len = len;
-    }
-
-    /**
-     * set whether the string is unicode
-     *
-     * @param unicode flag for whether string is unicode
-     */
+    private final int field_1_index_code;
+    private final boolean field_3_hasMultibyte;
+    private final String field_4_formatstring;
 
-    public void setUnicodeFlag(boolean unicode) {
-	field_3_unicode_flag = unicode;
+    public FormatRecord(int indexCode, String fs) {
+        field_1_index_code = indexCode;
+        field_4_formatstring = fs;
+        field_3_hasMultibyte = StringUtil.hasMultibyte(fs);
     }
 
-    /**
-     * set the format string
-     *
-     * @param fs  the format string
-     * @see #setFormatStringLength(byte)
-     */
-
-    public void setFormatString(String fs)
-    {
-        field_4_formatstring = fs;
-        setUnicodeFlag(StringUtil.hasMultibyte(fs));
+    public FormatRecord(RecordInputStream in) {
+        field_1_index_code = in.readShort();
+        int field_3_unicode_len = in.readUShort();
+        field_3_hasMultibyte = (in.readByte() & 0x01) != 0;
+
+        if (field_3_hasMultibyte) {
+            field_4_formatstring = in.readUnicodeLEString(field_3_unicode_len);
+        } else {
+            field_4_formatstring = in.readCompressedUnicode(field_3_unicode_len);
+        }
     }
 
     /**
@@ -110,95 +60,53 @@
      * @return the format index code
      * @see org.apache.poi.hssf.model.Workbook
      */
-
-    public short getIndexCode()
-    {
+    public int getIndexCode() {
         return field_1_index_code;
     }
 
     /**
-     * get the format string length
-     *
-     * @return the length of the format string
-     * @see #getFormatString()
-     */
-
-   /* public short getFormatStringLength()
-    {
-        return field_3_unicode_flag ? field_3_unicode_len : field_2_formatstring_len;
-    }*/
-
-    /**
-     * get whether the string is unicode
-     *
-     * @return flag for whether string is unicode
-     */
-
-    public boolean getUnicodeFlag() {
-	return field_3_unicode_flag;
-    }    
-
-    /**
      * get the format string
      *
      * @return the format string
      */
-
-    public String getFormatString()
-    {
+    public String getFormatString() {
         return field_4_formatstring;
     }
 
-    public String toString()
-    {
+    public String toString() {
         StringBuffer buffer = new StringBuffer();
 
         buffer.append("[FORMAT]\n");
-        buffer.append("    .indexcode       = ")
-            .append(Integer.toHexString(getIndexCode())).append("\n");
-        /*
-        buffer.append("    .formatstringlen = ")
-            .append(Integer.toHexString(getFormatStringLength()))
-            .append("\n");
-        */
-        buffer.append("    .unicode length  = ")
-            .append(Integer.toHexString(field_3_unicode_len)).append("\n");
-        buffer.append("    .isUnicode       = ")
-            .append( field_3_unicode_flag ).append("\n");
-        buffer.append("    .formatstring    = ").append(getFormatString())
-            .append("\n");
+        buffer.append("    .indexcode       = ").append(HexDump.shortToHex(getIndexCode())).append("\n");
+        buffer.append("    .isUnicode       = ").append(field_3_hasMultibyte ).append("\n");
+        buffer.append("    .formatstring    = ").append(getFormatString()).append("\n");
         buffer.append("[/FORMAT]\n");
         return buffer.toString();
     }
 
-    public int serialize(int offset, byte [] data)
-    {
-        LittleEndian.putShort(data, 0 + offset, sid);
-        LittleEndian.putShort(data, 2 + offset, (short)( 2 + 2 + 1 + ( (field_3_unicode_flag) 
-                                                                  ? 2 * field_3_unicode_len 
-                                                                  : field_3_unicode_len ) ) );
-                                                  // index + len + flag + format string length
-        LittleEndian.putShort(data, 4 + offset, getIndexCode());
-        LittleEndian.putShort(data, 6 + offset, field_3_unicode_len);
-        data[ 8 + offset ] = (byte)( (field_3_unicode_flag) ? 0x01 : 0x00 );
-
-      if ( field_3_unicode_flag ) {
-          // unicode
-          StringUtil.putUnicodeLE( getFormatString(), data, 9 + offset );
+    public void serialize(LittleEndianOutput out) {
+        String formatString = getFormatString();
+        out.writeShort(getIndexCode());
+        out.writeShort(formatString.length());
+        out.writeByte(field_3_hasMultibyte ? 0x01 : 0x00);
+
+      if ( field_3_hasMultibyte ) {
+          StringUtil.putUnicodeLE( formatString, out);
+      }  else {
+          StringUtil.putCompressedUnicode( formatString, out);
       }
-      else {
-          // not unicode
-          StringUtil.putCompressedUnicode( getFormatString(), data, 9 + offset );
-      }
-      
-        return getRecordSize();
     }
     protected int getDataSize() {
-        return 5 + field_3_unicode_len * (field_3_unicode_flag ? 2 : 1);
+        return 5 // 2 shorts + 1 byte
+            + getFormatString().length() * (field_3_hasMultibyte ? 2 : 1);
     }
 
-    public short getSid()
-    {
+    public short getSid() {
         return sid;
     }
+    @Override
+    public Object clone() {
+        // immutable
+        return this;
+    }
 }

Added: poi/trunk/src/java/org/apache/poi/hssf/record/HeaderFooterBase.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/HeaderFooterBase.java?rev=718810&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/HeaderFooterBase.java (added)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/HeaderFooterBase.java Tue Nov 18 16:06:06 2008
@@ -0,0 +1,109 @@
+/* ====================================================================
+   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.hssf.record;
+
+import org.apache.poi.util.LittleEndianOutput;
+import org.apache.poi.util.StringUtil;
+
+/**
+ * Common header/footer base class
+ * 
+ * @author Josh Micich
+ */
+abstract class HeaderFooterBase extends StandardRecord {
+	private boolean field_2_hasMultibyte;
+	private String field_3_text;
+
+	protected HeaderFooterBase(String text) {
+		setText(text);
+	}
+
+	protected HeaderFooterBase(RecordInputStream in) {
+		if (in.remaining() > 0) {
+			int field_1_footer_len = in.readShort();
+			field_2_hasMultibyte = in.readByte() != 0x00;
+
+			if (field_2_hasMultibyte) {
+				field_3_text = in.readUnicodeLEString(field_1_footer_len);
+			} else {
+				field_3_text = in.readCompressedUnicode(field_1_footer_len);
+			}
+		} else {
+			// Note - this is unusual: when the text is empty string, the whole record is empty (just the 4 byte BIFF header)
+			field_3_text = "";
+		}
+	}
+
+	/**
+	 * set the footer string
+	 * 
+	 * @param text string to display
+	 */
+	public final void setText(String text) {
+		if (text == null) {
+			throw new IllegalArgumentException("text must not be null");
+		}
+		field_2_hasMultibyte = StringUtil.hasMultibyte(text);
+		field_3_text = text;
+		
+		// Check it'll fit into the space in the record
+		if (field_2_hasMultibyte) {
+			if (field_3_text.length() > 127) {
+				throw new IllegalArgumentException(
+						"Footer string too long (limit is 127 for unicode strings)");
+			}
+		} else {
+			if (field_3_text.length() > 255) {
+				throw new IllegalArgumentException(
+						"Footer string too long (limit is 255 for non-unicode strings)");
+			}
+		}
+	}
+
+	/**
+	 * get the length of the footer string
+	 * 
+	 * @return length of the footer string
+	 */
+	private int getTextLength() {
+		return field_3_text.length();
+	}
+
+	public final String getText() {
+		return field_3_text;
+	}
+
+	public final void serialize(LittleEndianOutput out) {
+		if (getTextLength() > 0) {
+			out.writeShort(getTextLength());
+			out.writeByte(field_2_hasMultibyte ? 0x01 : 0x00);
+			if (field_2_hasMultibyte) {
+				StringUtil.putUnicodeLE(field_3_text, out);
+			} else {
+				StringUtil.putCompressedUnicode(field_3_text, out);
+			}
+		}
+	}
+
+	protected final int getDataSize() {
+		if (getTextLength() < 1) {
+			return 0;
+		}
+		return 3 + getTextLength() * (field_2_hasMultibyte ? 2 : 1);
+	}
+}

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/HeaderRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/HeaderRecord.java?rev=718810&r1=718809&r2=718810&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/HeaderRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/HeaderRecord.java Tue Nov 18 16:06:06 2008
@@ -17,8 +17,6 @@
 
 package org.apache.poi.hssf.record;
 
-import org.apache.poi.util.LittleEndian;
-import org.apache.poi.util.StringUtil;
 
 /**
  * Title:        Header Record<P>
@@ -28,172 +26,50 @@
  * @author Shawn Laubach (slaubach at apache dot org) Modified 3/14/02
  * @author Jason Height (jheight at chariot dot net dot au)
  */
-public final class HeaderRecord extends Record {
+public final class HeaderRecord extends HeaderFooterBase {
     public final static short sid = 0x0014;
-    private byte              field_1_header_len;
-    private byte              field_2_reserved;
-    private byte              field_3_unicode_flag;
-    private String            field_4_header;
-
-    public HeaderRecord()
-    {
-    }
-
-    public HeaderRecord(RecordInputStream in)
-    {
-        if (in.remaining() > 0)
-        {
-            field_1_header_len = in.readByte();
-            /** These two fields are a bit odd. They are not documented*/
-            field_2_reserved = in.readByte();
-            field_3_unicode_flag = in.readByte();						// unicode
-
-                         if(isMultibyte())
-                         {
-                field_4_header = in.readUnicodeLEString(LittleEndian.ubyteToInt( field_1_header_len));
-                         }
-                         else
-                         {
-                field_4_header = in.readCompressedUnicode(LittleEndian.ubyteToInt( field_1_header_len));
-                         }
-        }
-    }
 
-    /**
-     * see the unicode flag
-     *
-     * @return boolean flag
-     *  true:footer string has at least one multibyte character
-     */
-     public boolean isMultibyte() {
-         return ((field_3_unicode_flag & 0xFF) == 1);
+    public HeaderRecord(String text) {
+    	super(text);
     }
 
-    /**
-     * set the length of the header string
-     *
-     * @param len  length of the header string
-     * @see #setHeader(String)
-     */
-
-    public void setHeaderLength(byte len)
-    {
-        field_1_header_len = len;
+    public HeaderRecord(RecordInputStream in) {
+    	super(in);
     }
 
     /**
      * set the header string
      *
      * @param header string to display
-     * @see #setHeaderLength(byte)
      */
-
-    public void setHeader(String header)
-    {
-        field_4_header = header;
-        field_3_unicode_flag = 
-            (byte) (StringUtil.hasMultibyte(field_4_header) ? 1 : 0);
-        
-        // Check it'll fit into the space in the record
-        if(field_4_header == null) return;
-        if(field_3_unicode_flag == 1) {
-        	if(field_4_header.length() > 127) {
-        		throw new IllegalArgumentException("Header string too long (limit is 127 for unicode strings)");
-        	}
-        } else {
-        	if(field_4_header.length() > 255) {
-        		throw new IllegalArgumentException("Header string too long (limit is 255 for non-unicode strings)");
-        	}
-        }
-    }
-
-    /**
-     * get the length of the header string
-     *
-     * @return length of the header string
-     * @see #getHeader()
-     */
-
-    public short getHeaderLength()
-    {
-        return (short)(0xFF & field_1_header_len); // [Shawn] Fixed needing unsigned byte
+    public void setHeader(String header) {
+    	setText(header);
     }
 
     /**
      * get the header string
      *
      * @return header string to display
-     * @see #getHeaderLength()
      */
-
-    public String getHeader()
-    {
-        return field_4_header;
+    public String getHeader() {
+        return getText();
     }
 
-    public String toString()
-    {
+    public String toString() {
         StringBuffer buffer = new StringBuffer();
 
         buffer.append("[HEADER]\n");
-        buffer.append("    .length         = ").append(getHeaderLength())
-            .append("\n");
-        buffer.append("    .header         = ").append(getHeader())
-            .append("\n");
+        buffer.append("    .header = ").append(getText()).append("\n");
         buffer.append("[/HEADER]\n");
         return buffer.toString();
     }
 
-    public int serialize(int offset, byte [] data)
-    {
-        int len = 4;
-
-        if (getHeaderLength() != 0)
-        {
-            len+=3; // [Shawn] Fixed for two null bytes in the length
-        }
-        short bytelen = (short)(isMultibyte() ?
-            getHeaderLength()*2 : getHeaderLength() );
-        LittleEndian.putShort(data, 0 + offset, sid);
-        LittleEndian.putShort(data, 2 + offset,
-                              ( short ) ((len - 4) + bytelen));
-
-        if (getHeaderLength() > 0)
-        {
-            data[ 4 + offset ] = (byte)getHeaderLength();
-            data[ 6 + offset ] = field_3_unicode_flag;
-            if(isMultibyte())
-            {
-                StringUtil.putUnicodeLE(getHeader(), data, 7 + offset);
-            }
-            else
-            {
-                StringUtil.putCompressedUnicode(getHeader(), data, 7 + offset); // [Shawn] Place the string in the correct offset
-            }
-        }
-        return getRecordSize();
-    }
-
-    protected int getDataSize() {
-        int retval = 0;
-
-        if (getHeaderLength() != 0) {
-            retval+=3; // [Shawn] Fixed for two null bytes in the length
-        }
-       return retval + getHeaderLength() * (isMultibyte() ? 2 : 1);
-    }
 
-    public short getSid()
-    {
+    public short getSid() {
         return sid;
     }
 
     public Object clone() {
-      HeaderRecord rec = new HeaderRecord();
-      rec.field_1_header_len = field_1_header_len;
-      rec.field_2_reserved = field_2_reserved;
-      rec.field_3_unicode_flag = field_3_unicode_flag;
-      rec.field_4_header = field_4_header;
-      return rec;
+      return new HeaderRecord(getText());
     }
 }



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


Mime
View raw message