2002-01-30 21:22:28 -05:00
|
|
|
/* ====================================================================
|
2006-12-22 14:18:16 -05:00
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
2004-04-09 09:05:39 -04:00
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==================================================================== */
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
package org.apache.poi.hssf.record.formula;
|
|
|
|
|
|
|
|
import org.apache.poi.util.LittleEndian;
|
2008-04-15 12:00:50 -04:00
|
|
|
import org.apache.poi.hssf.usermodel.HSSFName;
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
2003-08-31 02:16:57 -04:00
|
|
|
import org.apache.poi.hssf.record.NameRecord;
|
2005-08-18 03:06:44 -04:00
|
|
|
import org.apache.poi.hssf.record.RecordInputStream;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author andy
|
2002-09-04 20:26:28 -04:00
|
|
|
* @author Jason Height (jheight at chariot dot net dot au)
|
2002-01-30 21:22:28 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
public class NamePtg
|
|
|
|
extends Ptg
|
|
|
|
{
|
|
|
|
public final static short sid = 0x23;
|
2003-07-27 15:15:16 -04:00
|
|
|
private final static int SIZE = 5;
|
2008-03-04 11:53:32 -05:00
|
|
|
/** one-based index to defined name record */
|
2003-07-27 15:15:16 -04:00
|
|
|
private short field_1_label_index;
|
|
|
|
private short field_2_zero; // reserved must be 0
|
|
|
|
boolean xtra=false;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
2002-09-04 20:26:28 -04:00
|
|
|
|
|
|
|
private NamePtg() {
|
|
|
|
//Required for clone methods
|
|
|
|
}
|
|
|
|
|
2008-03-04 11:53:32 -05:00
|
|
|
/**
|
|
|
|
* Creates new NamePtg and sets its name index to that of the corresponding defined name record
|
|
|
|
* in the workbook. The search for the name record is case insensitive. If it is not found,
|
|
|
|
* it gets created.
|
|
|
|
*/
|
2008-04-15 12:00:50 -04:00
|
|
|
public NamePtg(String name, HSSFWorkbook book) {
|
2008-03-04 11:53:32 -05:00
|
|
|
field_1_label_index = (short)(1+getOrCreateNameRecord(book, name)); // convert to 1-based
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return zero based index of the found or newly created defined name record.
|
|
|
|
*/
|
2008-04-15 12:00:50 -04:00
|
|
|
private static final int getOrCreateNameRecord(HSSFWorkbook book, String name) {
|
|
|
|
// perhaps this logic belongs in Workbook?
|
|
|
|
int countNames = book.getNumberOfNames();
|
2008-03-04 11:53:32 -05:00
|
|
|
for (int i = 0; i < countNames; i++) {
|
2008-04-15 12:00:50 -04:00
|
|
|
if(name.equalsIgnoreCase( book.getNameName(i) )) {
|
2008-03-04 11:53:32 -05:00
|
|
|
return i;
|
2003-08-31 02:16:57 -04:00
|
|
|
}
|
|
|
|
}
|
2008-04-15 12:00:50 -04:00
|
|
|
|
|
|
|
HSSFName nameObj = book.createName();
|
|
|
|
nameObj.setNameName(name);
|
|
|
|
|
2008-03-04 11:53:32 -05:00
|
|
|
return countNames;
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Creates new NamePtg */
|
|
|
|
|
2005-08-18 03:06:44 -04:00
|
|
|
public NamePtg(RecordInputStream in)
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
2003-07-27 15:15:16 -04:00
|
|
|
//field_1_ixti = LittleEndian.getShort(data, offset);
|
2005-08-18 03:06:44 -04:00
|
|
|
field_1_label_index = in.readShort();
|
|
|
|
field_2_zero = in.readShort();
|
2003-07-27 15:15:16 -04:00
|
|
|
//if (data[offset+6]==0) xtra=true;
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
2008-03-04 11:53:32 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return zero based index to a defined name record in the LinkTable
|
|
|
|
*/
|
|
|
|
public int getIndex() {
|
|
|
|
return field_1_label_index-1; // convert to zero based
|
|
|
|
}
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
public void writeBytes(byte [] array, int offset)
|
|
|
|
{
|
2003-07-27 15:15:16 -04:00
|
|
|
array[offset+0]= (byte) (sid + ptgClass);
|
|
|
|
LittleEndian.putShort(array,offset+1,field_1_label_index);
|
|
|
|
LittleEndian.putShort(array,offset+3, field_2_zero);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getSize()
|
|
|
|
{
|
|
|
|
return SIZE;
|
|
|
|
}
|
|
|
|
|
2008-04-15 12:00:50 -04:00
|
|
|
public String toFormulaString(HSSFWorkbook book)
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
2008-04-15 12:00:50 -04:00
|
|
|
return book.getNameName(field_1_label_index - 1);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
2002-05-27 17:00:12 -04:00
|
|
|
|
2003-08-31 02:16:57 -04:00
|
|
|
public byte getDefaultOperandClass() {return Ptg.CLASS_REF;}
|
2002-09-04 20:26:28 -04:00
|
|
|
|
|
|
|
public Object clone() {
|
|
|
|
NamePtg ptg = new NamePtg();
|
2003-07-27 15:15:16 -04:00
|
|
|
ptg.field_1_label_index = field_1_label_index;
|
|
|
|
ptg.field_2_zero = field_2_zero;
|
2002-09-04 20:26:28 -04:00
|
|
|
return ptg;
|
|
|
|
}
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|