1 /* ==================================================================== 2 * The Apache Software License, Version 1.1 3 * 4 * Copyright (c) 2002 The Apache Software Foundation. All rights 5 * reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * 3. The end-user documentation included with the redistribution, 20 * if any, must include the following acknowledgment: 21 * "This product includes software developed by the 22 * Apache Software Foundation (http://www.apache.org/)." 23 * Alternately, this acknowledgment may appear in the software itself, 24 * if and wherever such third-party acknowledgments normally appear. 25 * 26 * 4. The names "Apache" and "Apache Software Foundation" and 27 * "Apache POI" must not be used to endorse or promote products 28 * derived from this software without prior written permission. For 29 * written permission, please contact apache@apache.org. 30 * 31 * 5. Products derived from this software may not be called "Apache", 32 * "Apache POI", nor may "Apache" appear in their name, without 33 * prior written permission of the Apache Software Foundation. 34 * 35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * ==================================================================== 48 * 49 * This software consists of voluntary contributions made by many 50 * individuals on behalf of the Apache Software Foundation. For more 51 * information on the Apache Software Foundation, please see 52 * <http://www.apache.org/>. 53 */ 54 55 package org.apache.poi.hssf.record; 56 57 import org.apache.poi.hssf.record.formula.Ptg; 58 import org.apache.poi.util.LittleEndian; 59 60 import java.util.Stack; 61 import java.util.Iterator; 62 63 /** 64 * Not implemented yet. May commit it anyway just so people can see 65 * where I'm heading. 66 * 67 * @author Glen Stampoultzis (glens at apache.org) 68 */ 69 public class LinkedDataFormulaField 70 implements CustomField 71 { 72 Stack formulaTokens = new Stack(); 73 74 public int getSize() 75 { 76 int size = 0; 77 for ( Iterator iterator = formulaTokens.iterator(); iterator.hasNext(); ) 78 { 79 Ptg token = (Ptg) iterator.next(); 80 size += token.getSize(); 81 } 82 return size + 2; 83 } 84 85 public int fillField( byte[] data, short size, int offset ) 86 { 87 short tokenSize = LittleEndian.getShort(data, offset); 88 formulaTokens = getParsedExpressionTokens(data, size, offset + 2); 89 90 return tokenSize + 2; 91 } 92 93 public void toString( StringBuffer buffer ) 94 { 95 for ( int k = 0; k < formulaTokens.size(); k++ ) 96 { 97 buffer.append( "Formula " ) 98 .append( k ) 99 .append( "=" ) 100 .append( formulaTokens.get( k ).toString() ) 101 .append( "\n" ) 102 .append( ( (Ptg) formulaTokens.get( k ) ).toDebugString() ) 103 .append( "\n" ); 104 } 105 } 106 107 public String toString() 108 { 109 StringBuffer b = new StringBuffer(); 110 toString( b ); 111 return b.toString(); 112 } 113 114 public int serializeField( int offset, byte[] data ) 115 { 116 int size = getSize(); 117 LittleEndian.putShort(data, offset, (short)(size - 2)); 118 int pos = offset + 2; 119 for ( Iterator iterator = formulaTokens.iterator(); iterator.hasNext(); ) 120 { 121 Ptg ptg = (Ptg) iterator.next(); 122 ptg.writeBytes(data, pos); 123 pos += ptg.getSize(); 124 } 125 return size; 126 } 127 128 public Object clone() 129 { 130 try 131 { 132 // todo: clone tokens? or are they immutable? 133 return super.clone(); 134 } 135 catch ( CloneNotSupportedException e ) 136 { 137 // should not happen 138 return null; 139 } 140 } 141 142 private Stack getParsedExpressionTokens( byte[] data, short size, 143 int offset ) 144 { 145 Stack stack = new Stack(); 146 int pos = offset; 147 148 while ( pos < size ) 149 { 150 Ptg ptg = Ptg.createPtg( data, pos ); 151 pos += ptg.getSize(); 152 stack.push( ptg ); 153 } 154 return stack; 155 } 156 157 public void setFormulaTokens( Stack formulaTokens ) 158 { 159 this.formulaTokens = (Stack) formulaTokens.clone(); 160 } 161 162 public Stack getFormulaTokens() 163 { 164 return (Stack)this.formulaTokens.clone(); 165 } 166 167 } 168