KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > FooterRecord


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.hssf.record;
20
21 import org.apache.poi.util.LittleEndian;
22 import org.apache.poi.util.StringUtil;
23
24 /**
25  * Title: Footer Record <P>
26  * Description: Specifies the footer for a sheet<P>
27  * REFERENCE: PG 317 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
28  * @author Andrew C. Oliver (acoliver at apache dot org)
29  * @author Shawn Laubach (slaubach at apache dot org) Modified 3/14/02
30  * @author Jason Height (jheight at chariot dot net dot au)
31  * @version 2.0-pre
32  */

33
34 public class FooterRecord
35     extends Record
36 {
37     public final static short sid = 0x15;
38     private byte field_1_footer_len;
39     private String JavaDoc field_2_footer;
40
41     public FooterRecord()
42     {
43     }
44
45     /**
46      * Constructs a FooterRecord record and sets its fields appropriately.
47      *
48      * @param id id must be 0x15 or an exception will be throw upon validation
49      * @param size the size of the data area of the record
50      * @param data data of the record (should not contain sid/len)
51      */

52
53     public FooterRecord(short id, short size, byte [] data)
54     {
55         super(id, size, data);
56     }
57
58     /**
59      * Constructs a FooterRecord record and sets its fields appropriately.
60      *
61      * @param id id must be 0x15 or an exception will be throw upon validation
62      * @param size the size of the data area of the record
63      * @param data data of the record (should not contain sid/len)
64      * @param offset of the record's data
65      */

66
67     public FooterRecord(short id, short size, byte [] data, int offset)
68     {
69         super(id, size, data, offset);
70     }
71
72     protected void validateSid(short id)
73     {
74         if (id != sid)
75         {
76             throw new RecordFormatException("NOT A FooterRECORD");
77         }
78     }
79
80     protected void fillFields(byte [] data, short size, int offset)
81     {
82         if (size > 0)
83         {
84             field_1_footer_len = data[ 0 + offset ];
85             field_2_footer = StringUtil.getFromCompressedUnicode(data, 3 + offset, // [Shawn] Changed 1 to 3 for offset of string
86
LittleEndian.ubyteToInt( field_1_footer_len) );
87         }
88     }
89
90     /**
91      * set the length of the footer string
92      *
93      * @param len length of the footer string
94      * @see #setFooter(String)
95      */

96
97     public void setFooterLength(byte len)
98     {
99         field_1_footer_len = len;
100     }
101
102     /**
103      * set the footer string
104      *
105      * @param footer string to display
106      * @see #setFooterLength(byte)
107      */

108
109     public void setFooter(String JavaDoc footer)
110     {
111         field_2_footer = footer;
112     }
113
114     /**
115      * get the length of the footer string
116      *
117      * @return length of the footer string
118      * @see #getFooter()
119      */

120
121     public short getFooterLength()
122     {
123         return (short)(0xFF & field_1_footer_len); // [Shawn] Fixed needing unsigned byte
124
}
125
126     /**
127      * get the footer string
128      *
129      * @return footer string to display
130      * @see #getFooterLength()
131      */

132
133     public String JavaDoc getFooter()
134     {
135         return field_2_footer;
136     }
137
138     public String JavaDoc toString()
139     {
140         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
141
142         buffer.append("[FOOTER]\n");
143         buffer.append(" .footerlen = ")
144             .append(Integer.toHexString(getFooterLength())).append("\n");
145         buffer.append(" .footer = ").append(getFooter())
146             .append("\n");
147         buffer.append("[/FOOTER]\n");
148         return buffer.toString();
149     }
150
151     public int serialize(int offset, byte [] data)
152     {
153         int len = 4;
154
155         if (getFooterLength() > 0)
156         {
157             len+=3; // [Shawn] Fixed for two null bytes in the length
158
}
159         LittleEndian.putShort(data, 0 + offset, sid);
160         LittleEndian.putShort(data, 2 + offset,
161                               ( short ) ((len - 4) + getFooterLength()));
162         if (getFooterLength() > 0)
163         {
164             data[ 4 + offset ] = (byte)getFooterLength();
165             StringUtil.putCompressedUnicode(getFooter(), data, 7 + offset); // [Shawn] Place the string in the correct offset
166
}
167         return getRecordSize();
168     }
169
170     public int getRecordSize()
171     {
172         int retval = 4;
173
174         if (getFooterLength() > 0)
175         {
176             retval+=3; // [Shawn] Fixed for two null bytes in the length
177
}
178         return retval + getFooterLength();
179     }
180
181     public short getSid()
182     {
183         return this.sid;
184     }
185
186     public Object JavaDoc clone() {
187       FooterRecord rec = new FooterRecord();
188       rec.field_1_footer_len = field_1_footer_len;
189       rec.field_2_footer = field_2_footer;
190       return rec;
191     }
192 }
193
Popular Tags