KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 /**
24  * Title: SharedFormulaRecord
25  * Description: Primarily used as an excel optimization so that multiple similar formulas
26  * are not written out too many times. We should recognize this record and
27  * serialize as is since this is used when reading templates.
28  * <p>
29  * Note: the documentation says that the SID is BC where biffviewer reports 4BC. The hex dump shows
30  * that the two byte sid representation to be 'BC 04' that is consistent with the other high byte
31  * record types.
32  * @author Danny Mui at apache dot org
33  */

34
35 public class SharedFormulaRecord
36     extends Record
37 {
38      public final static short sid = 0x4BC;
39     private short size = 0;
40     private byte[] thedata = null;
41     int offset = 0;
42
43     public SharedFormulaRecord()
44     {
45     }
46
47     /**
48      * construct the sharedformula record, save all the information
49      * @param id id of the record -not validated, just stored for serialization
50      * @param size size of the data
51      * @param data the data
52      */

53
54     public SharedFormulaRecord(short id, short size, byte [] data)
55     {
56           super(id, size, data);
57           
58           this.fillFields(data, size, 0);
59     }
60
61     /**
62      * spit the record out AS IS. no interperatation or identification
63      */

64
65     public int serialize(int offset, byte [] data)
66     {
67         if (thedata == null)
68         {
69             thedata = new byte[ 0 ];
70         }
71         LittleEndian.putShort(data, 0 + offset, sid);
72         LittleEndian.putShort(data, 2 + offset, ( short ) (thedata.length));
73         if (thedata.length > 0)
74         {
75             System.arraycopy(thedata, 0, data, 4 + offset, thedata.length);
76         }
77         return getRecordSize();
78     }
79
80     public int getRecordSize()
81     {
82         int retval = 4;
83
84         if (thedata != null)
85         {
86             retval += thedata.length;
87         }
88         return retval;
89     }
90
91
92     protected void validateSid(short id)
93     {
94         if (id != this.sid)
95         {
96             throw new RecordFormatException("Not a valid SharedFormula");
97         }
98         
99     }
100
101     /**
102      * print a sort of string representation ([SHARED FORMULA RECORD] id = x [/SHARED FORMULA RECORD])
103      */

104
105     public String JavaDoc toString()
106     {
107         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
108
109         buffer.append("[SHARED FORMULA RECORD:" + Integer.toHexString(sid) + "]\n");
110         buffer.append(" .id = ").append(Integer.toHexString(sid))
111             .append("\n");
112         buffer.append("[/SHARED FORMULA RECORD]\n");
113         return buffer.toString();
114     }
115
116     public short getSid()
117     {
118         return this.sid;
119     }
120
121      /**
122       * Shared formulas are to treated like unknown records, and as a result d
123       */

124     protected void fillFields(byte [] data, short size, int offset)
125     {
126         thedata = new byte[size];
127         System.arraycopy(data, 0, thedata, 0, size);
128
129     }
130
131     /**
132      * Mirroring formula records so it is registered in the ValueRecordsAggregate
133      */

134     public boolean isInValueSection()
135     {
136          return true;
137     }
138
139
140      /**
141       * Register it in the ValueRecordsAggregate so it can go into the FormulaRecordAggregate
142       */

143      public boolean isValue() {
144         return true;
145      }
146
147     public Object JavaDoc clone() {
148       SharedFormulaRecord rec = new SharedFormulaRecord();
149       rec.offset = offset;
150       rec.size = size;
151       rec.thedata = thedata;
152       return rec;
153     }
154 }
155
Popular Tags