01: /* ====================================================================
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: 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: package org.apache.poi.hssf.record.formula;
19:
20: import org.apache.poi.util.LittleEndian;
21: import org.apache.poi.hssf.model.Workbook;
22: import org.apache.poi.hssf.record.NameRecord;
23: import org.apache.poi.hssf.record.RecordInputStream;
24:
25: /**
26: *
27: * @author andy
28: * @author Jason Height (jheight at chariot dot net dot au)
29: */
30:
31: public class NamePtg extends Ptg {
32: public final static short sid = 0x23;
33: private final static int SIZE = 5;
34: private short field_1_label_index;
35: private short field_2_zero; // reserved must be 0
36: boolean xtra = false;
37:
38: private NamePtg() {
39: //Required for clone methods
40: }
41:
42: /** Creates new NamePtg */
43:
44: public NamePtg(String name, Workbook book) {
45: final short n = (short) (book.getNumNames() + 1);
46: NameRecord rec;
47: for (short i = 1; i < n; i++) {
48: rec = book.getNameRecord(i - 1);
49: if (name.equals(rec.getNameText())) {
50: field_1_label_index = i;
51: return;
52: }
53: }
54: rec = new NameRecord();
55: rec.setNameText(name);
56: rec.setNameTextLength((byte) name.length());
57: book.addName(rec);
58: field_1_label_index = n;
59: }
60:
61: /** Creates new NamePtg */
62:
63: public NamePtg(RecordInputStream in) {
64: //field_1_ixti = LittleEndian.getShort(data, offset);
65: field_1_label_index = in.readShort();
66: field_2_zero = in.readShort();
67: //if (data[offset+6]==0) xtra=true;
68: }
69:
70: public void writeBytes(byte[] array, int offset) {
71: array[offset + 0] = (byte) (sid + ptgClass);
72: LittleEndian.putShort(array, offset + 1, field_1_label_index);
73: LittleEndian.putShort(array, offset + 3, field_2_zero);
74: }
75:
76: public int getSize() {
77: return SIZE;
78: }
79:
80: public String toFormulaString(Workbook book) {
81: NameRecord rec = book.getNameRecord(field_1_label_index - 1);
82: return rec.getNameText();
83: }
84:
85: public byte getDefaultOperandClass() {
86: return Ptg.CLASS_REF;
87: }
88:
89: public Object clone() {
90: NamePtg ptg = new NamePtg();
91: ptg.field_1_label_index = field_1_label_index;
92: ptg.field_2_zero = field_2_zero;
93: return ptg;
94: }
95: }
|