1 2 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 ; 25 import java.util.Iterator ; 26 27 33 public class LinkedDataFormulaField 34 implements CustomField 35 { 36 Stack formulaTokens = new Stack (); 37 38 public int getSize() 39 { 40 int size = 0; 41 for ( Iterator 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 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 toString() 72 { 73 StringBuffer b = new StringBuffer (); 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 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 clone() 93 { 94 try 95 { 96 return super.clone(); 98 } 99 catch ( CloneNotSupportedException e ) 100 { 101 return null; 103 } 104 } 105 106 private Stack getParsedExpressionTokens( byte[] data, short size, 107 int offset ) 108 { 109 Stack stack = new Stack (); 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 formulaTokens ) 122 { 123 this.formulaTokens = (Stack ) formulaTokens.clone(); 124 } 125 126 public Stack getFormulaTokens() 127 { 128 return (Stack )this.formulaTokens.clone(); 129 } 130 131 } 132 | Popular Tags |