KickJava   Java API By Example, From Geeks To Geeks.

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


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.hssf.record.formula.Ptg;
22 import org.apache.poi.util.LittleEndian;
23
24 import java.util.Stack JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 /**
28  * Not implemented yet. May commit it anyway just so people can see
29  * where I'm heading.
30  *
31  * @author Glen Stampoultzis (glens at apache.org)
32  */

33 public class LinkedDataFormulaField
34         implements CustomField
35 {
36     Stack JavaDoc formulaTokens = new Stack JavaDoc();
37
38     public int getSize()
39     {
40         int size = 0;
41         for ( Iterator JavaDoc iterator = formulaTokens.iterator(); iterator.hasNext(); )
42         {
43             Ptg token = (Ptg) iterator.next();
44             size += token.getSize();
45         }
46         return size + 2;
47     }
48
49     public int fillField( byte[] data, short size, int offset )
50     {
51         short tokenSize = LittleEndian.getShort(data, offset);
52         formulaTokens = getParsedExpressionTokens(data, size, offset + 2);
53
54         return tokenSize + 2;
55     }
56
57     public void toString( StringBuffer JavaDoc buffer )
58     {
59         for ( int k = 0; k < formulaTokens.size(); k++ )
60         {
61             buffer.append( "Formula " )
62                     .append( k )
63                     .append( "=" )
64                     .append( formulaTokens.get( k ).toString() )
65                     .append( "\n" )
66                     .append( ( (Ptg) formulaTokens.get( k ) ).toDebugString() )
67                     .append( "\n" );
68         }
69     }
70
71     public String JavaDoc toString()
72     {
73         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
74         toString( b );
75         return b.toString();
76     }
77
78     public int serializeField( int offset, byte[] data )
79     {
80         int size = getSize();
81         LittleEndian.putShort(data, offset, (short)(size - 2));
82         int pos = offset + 2;
83         for ( Iterator JavaDoc iterator = formulaTokens.iterator(); iterator.hasNext(); )
84         {
85             Ptg ptg = (Ptg) iterator.next();
86             ptg.writeBytes(data, pos);
87             pos += ptg.getSize();
88         }
89         return size;
90     }
91
92     public Object JavaDoc clone()
93     {
94         try
95         {
96             // todo: clone tokens? or are they immutable?
97
return super.clone();
98         }
99         catch ( CloneNotSupportedException JavaDoc e )
100         {
101             // should not happen
102
return null;
103         }
104     }
105
106     private Stack JavaDoc getParsedExpressionTokens( byte[] data, short size,
107                                              int offset )
108     {
109         Stack JavaDoc stack = new Stack JavaDoc();
110         int pos = offset;
111
112         while ( pos < size )
113         {
114             Ptg ptg = Ptg.createPtg( data, pos );
115             pos += ptg.getSize();
116             stack.push( ptg );
117         }
118         return stack;
119     }
120
121     public void setFormulaTokens( Stack JavaDoc formulaTokens )
122     {
123         this.formulaTokens = (Stack JavaDoc) formulaTokens.clone();
124     }
125
126     public Stack JavaDoc getFormulaTokens()
127     {
128         return (Stack JavaDoc)this.formulaTokens.clone();
129     }
130
131 }
132
Popular Tags