001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.record.formula;
019:
020: import org.apache.poi.util.LittleEndian;
021: import org.apache.poi.hssf.record.RecordInputStream;
022:
023: /**
024: * @author aviks
025: * @author Jason Height (jheight at chariot dot net dot au)
026: * @author Danny Mui (dmui at apache dot org) (Leftover handling)
027: */
028: public class FuncPtg extends AbstractFunctionPtg {
029:
030: public final static byte sid = 0x21;
031: public final static int SIZE = 3;
032: private int numParams = 0;
033:
034: /**
035: * FuncPtgs are defined to be 4 bytes but the actual FuncPtg uses only 2 bytes.
036: * If we have leftOvers that are read from the file we should serialize them back out.
037: * <p>
038: * If the leftovers are removed, a prompt "Warning: Data may have been lost occurs in Excel"
039: */
040: //protected byte[] leftOvers = null;
041: private FuncPtg() {
042: //Required for clone methods
043: }
044:
045: /**Creates new function pointer from a byte array
046: * usually called while reading an excel file.
047: */
048: public FuncPtg(RecordInputStream in) {
049: //field_1_num_args = data[ offset + 0 ];
050: field_2_fnc_index = in.readShort();
051:
052: /*
053: if (data.length - offset > 2) { //save left overs if there are any
054: leftOvers = new byte[2];
055: System.arraycopy(data, offset+1, leftOvers, 0, leftOvers.length);
056: }
057: */
058: try {
059: numParams = ((Integer) functionData[field_2_fnc_index][2])
060: .intValue();
061: } catch (NullPointerException npe) {
062: numParams = 0;
063: }
064:
065: }
066:
067: public void writeBytes(byte[] array, int offset) {
068: array[offset + 0] = (byte) (sid + ptgClass);
069: //array[offset+1]=field_1_num_args;
070: LittleEndian.putShort(array, offset + 1, field_2_fnc_index);
071: /**if (leftOvers != null) {
072: System.arraycopy(leftOvers, 0, array, offset+2, leftOvers.length);
073: }**/
074: }
075:
076: public int getNumberOfOperands() {
077: return numParams;
078: }
079:
080: public Object clone() {
081: FuncPtg ptg = new FuncPtg();
082: //ptg.field_1_num_args = field_1_num_args;
083: ptg.field_2_fnc_index = field_2_fnc_index;
084: ptg.setClass(ptgClass);
085: return ptg;
086: }
087:
088: public int getSize() {
089: return SIZE;
090: }
091:
092: public String toString() {
093: StringBuffer buffer = new StringBuffer();
094: buffer.append("<FunctionPtg>").append("\n").append(
095: " numArgs(internal)=").append(this .numParams).append(
096: "\n").append(" name =").append(
097: lookupName(field_2_fnc_index)).append("\n").append(
098: " field_2_fnc_index=").append(field_2_fnc_index)
099: .append("\n").append("</FunctionPtg>");
100: return buffer.toString();
101: }
102: }
|