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.hssf.model.Workbook;
021: import org.apache.poi.hssf.record.RecordInputStream;
022:
023: import org.apache.poi.util.LittleEndian;
024: import org.apache.poi.util.BitField;
025: import org.apache.poi.util.BitFieldFactory;
026:
027: /**
028: * "Special Attributes"
029: * This seems to be a Misc Stuff and Junk record. One function it serves is
030: * in SUM functions (i.e. SUM(A1:A3) causes an area PTG then an ATTR with the SUM option set)
031: * @author andy
032: * @author Jason Height (jheight at chariot dot net dot au)
033: */
034:
035: public class AttrPtg extends OperationPtg {
036: public final static byte sid = 0x19;
037: private final static int SIZE = 4;
038: private byte field_1_options;
039: private short field_2_data;
040: private BitField semiVolatile = BitFieldFactory.getInstance(0x01);
041: private BitField optiIf = BitFieldFactory.getInstance(0x02);
042: private BitField optiChoose = BitFieldFactory.getInstance(0x04);
043: private BitField optGoto = BitFieldFactory.getInstance(0x08);
044: private BitField sum = BitFieldFactory.getInstance(0x10);
045: private BitField baxcel = BitFieldFactory.getInstance(0x20);
046: private BitField space = BitFieldFactory.getInstance(0x40);
047:
048: public AttrPtg() {
049: }
050:
051: public AttrPtg(RecordInputStream in) {
052: field_1_options = in.readByte();
053: field_2_data = in.readShort();
054: }
055:
056: public void setOptions(byte options) {
057: field_1_options = options;
058: }
059:
060: public byte getOptions() {
061: return field_1_options;
062: }
063:
064: public boolean isSemiVolatile() {
065: return semiVolatile.isSet(getOptions());
066: }
067:
068: public boolean isOptimizedIf() {
069: return optiIf.isSet(getOptions());
070: }
071:
072: public boolean isOptimizedChoose() {
073: return optiChoose.isSet(getOptions());
074: }
075:
076: // lets hope no one uses this anymore
077: public boolean isGoto() {
078: return optGoto.isSet(getOptions());
079: }
080:
081: public boolean isSum() {
082: return sum.isSet(getOptions());
083: }
084:
085: public void setSum(boolean bsum) {
086: field_1_options = sum.setByteBoolean(field_1_options, bsum);
087: }
088:
089: public void setOptimizedIf(boolean bif) {
090: field_1_options = optiIf.setByteBoolean(field_1_options, bif);
091: }
092:
093: /**
094: * Flags this ptg as a goto/jump
095: * @param isGoto
096: */
097: public void setGoto(boolean isGoto) {
098: field_1_options = optGoto.setByteBoolean(field_1_options,
099: isGoto);
100: }
101:
102: // lets hope no one uses this anymore
103: public boolean isBaxcel() {
104: return baxcel.isSet(getOptions());
105: }
106:
107: // biff3&4 only shouldn't happen anymore
108: public boolean isSpace() {
109: return space.isSet(getOptions());
110: }
111:
112: public void setData(short data) {
113: field_2_data = data;
114: }
115:
116: public short getData() {
117: return field_2_data;
118: }
119:
120: public String toString() {
121: StringBuffer buffer = new StringBuffer();
122:
123: buffer.append("AttrPtg\n");
124: buffer.append("options=").append(field_1_options).append("\n");
125: buffer.append("data =").append(field_2_data).append("\n");
126: buffer.append("semi =").append(isSemiVolatile()).append("\n");
127: buffer.append("optimif=").append(isOptimizedIf()).append("\n");
128: buffer.append("optchos=").append(isOptimizedChoose()).append(
129: "\n");
130: buffer.append("isGoto =").append(isGoto()).append("\n");
131: buffer.append("isSum =").append(isSum()).append("\n");
132: buffer.append("isBaxce=").append(isBaxcel()).append("\n");
133: buffer.append("isSpace=").append(isSpace()).append("\n");
134: return buffer.toString();
135: }
136:
137: public void writeBytes(byte[] array, int offset) {
138: array[offset] = sid;
139: array[offset + 1] = field_1_options;
140: LittleEndian.putShort(array, offset + 2, field_2_data);
141: }
142:
143: public int getSize() {
144: return SIZE;
145: }
146:
147: public String toFormulaString(String[] operands) {
148: if (space.isSet(field_1_options)) {
149: return operands[0];
150: } else if (optiIf.isSet(field_1_options)) {
151: return toFormulaString((Workbook) null) + "(" + operands[0]
152: + ")";
153: } else if (optGoto.isSet(field_1_options)) {
154: return toFormulaString((Workbook) null) + operands[0]; //goto isn't a real formula element should not show up
155: } else {
156: return toFormulaString((Workbook) null) + "(" + operands[0]
157: + ")";
158: }
159: }
160:
161: public int getNumberOfOperands() {
162: return 1;
163: }
164:
165: public int getType() {
166: return -1;
167: }
168:
169: public String toFormulaString(Workbook book) {
170: if (semiVolatile.isSet(field_1_options)) {
171: return "ATTR(semiVolatile)";
172: }
173: if (optiIf.isSet(field_1_options)) {
174: return "IF";
175: }
176: if (optiChoose.isSet(field_1_options)) {
177: return "CHOOSE";
178: }
179: if (optGoto.isSet(field_1_options)) {
180: return "";
181: }
182: if (sum.isSet(field_1_options)) {
183: return "SUM";
184: }
185: if (baxcel.isSet(field_1_options)) {
186: return "ATTR(baxcel)";
187: }
188: if (space.isSet(field_1_options)) {
189: return "";
190: }
191: return "UNKNOWN ATTRIBUTE";
192: }
193:
194: public byte getDefaultOperandClass() {
195: return Ptg.CLASS_VALUE;
196: }
197:
198: public Object clone() {
199: AttrPtg ptg = new AttrPtg();
200: ptg.field_1_options = field_1_options;
201: ptg.field_2_data = field_2_data;
202: return ptg;
203: }
204: }
|