PR: Obtained from: Submitted by: Reviewed by: git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352471 13f79535-47bb-0310-9956-ffa450edef68tags/REL_1_10
@@ -0,0 +1,176 @@ | |||
/* ==================================================================== | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2002 The Apache Software Foundation. All rights | |||
* reserved. | |||
* | |||
* Redistribution and use in source and binary forms, with or without | |||
* modification, are permitted provided that the following conditions | |||
* are met: | |||
* | |||
* 1. Redistributions of source code must retain the above copyright | |||
* notice, this list of conditions and the following disclaimer. | |||
* | |||
* 2. Redistributions in binary form must reproduce the above copyright | |||
* notice, this list of conditions and the following disclaimer in | |||
* the documentation and/or other materials provided with the | |||
* distribution. | |||
* | |||
* 3. The end-user documentation included with the redistribution, | |||
* if any, must include the following acknowledgment: | |||
* "This product includes software developed by the | |||
* Apache Software Foundation (http://www.apache.org/)." | |||
* Alternately, this acknowledgment may appear in the software itself, | |||
* if and wherever such third-party acknowledgments normally appear. | |||
* | |||
* 4. The names "Apache" and "Apache Software Foundation" and | |||
* "Apache POI" must not be used to endorse or promote products | |||
* derived from this software without prior written permission. For | |||
* written permission, please contact apache@apache.org. | |||
* | |||
* 5. Products derived from this software may not be called "Apache", | |||
* "Apache POI", nor may "Apache" appear in their name, without | |||
* prior written permission of the Apache Software Foundation. | |||
* | |||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||
* SUCH DAMAGE. | |||
* ==================================================================== | |||
* | |||
* This software consists of voluntary contributions made by many | |||
* individuals on behalf of the Apache Software Foundation. For more | |||
* information on the Apache Software Foundation, please see | |||
* <http://www.apache.org/>. | |||
*/ | |||
package org.apache.poi.hssf.record; | |||
import org.apache.poi.util.LittleEndian; | |||
import java.util.ArrayList; | |||
/** | |||
* Title: Sup Book <P> | |||
* Description: A Extrenal Workbook Deciption (Sup Book) | |||
* Its only a dummy record for making new ExternSheet Record <P> | |||
* REFERENCE: <P> | |||
* @author Libin Roman (Vista Portal LDT. Developer) | |||
* @version 1.0-pre | |||
*/ | |||
public class SupBookRecord extends Record | |||
{ | |||
public final static short sid = 0x1AE; | |||
private short field_1_number_of_sheets; | |||
private short field_2_flag; | |||
public SupBookRecord() | |||
{ | |||
} | |||
/** | |||
* Constructs a Extern Sheet record and sets its fields appropriately. | |||
* | |||
* @param id id must be 0x16 or an exception will be throw upon validation | |||
* @param size the size of the data area of the record | |||
* @param data data of the record (should not contain sid/len) | |||
*/ | |||
public SupBookRecord(short id, short size, byte[] data) | |||
{ | |||
super(id, size, data); | |||
} | |||
/** | |||
* Constructs a Extern Sheet record and sets its fields appropriately. | |||
* | |||
* @param id id must be 0x16 or an exception will be throw upon validation | |||
* @param size the size of the data area of the record | |||
* @param data data of the record (should not contain sid/len) | |||
* @param offset of the record's data | |||
*/ | |||
public SupBookRecord(short id, short size, byte[] data, int offset) | |||
{ | |||
super(id, size, data, offset); | |||
} | |||
protected void validateSid(short id) | |||
{ | |||
if (id != sid) | |||
{ | |||
throw new RecordFormatException("NOT An ExternSheet RECORD"); | |||
} | |||
} | |||
/** | |||
* called by the constructor, should set class level fields. Should throw | |||
* runtime exception for bad/icomplete data. | |||
* | |||
* @param data raw data | |||
* @param size size of data | |||
* @param offset of the record's data (provided a big array of the file) | |||
*/ | |||
protected void fillFields(byte [] data, short size, int offset) | |||
{ | |||
//For now We use it only for one case | |||
//When we need to add an named range when no named ranges was | |||
//before it | |||
} | |||
public String toString() | |||
{ | |||
return ""; | |||
} | |||
/** | |||
* called by the class that is responsible for writing this sucker. | |||
* Subclasses should implement this so that their data is passed back in a | |||
* byte array. | |||
* | |||
* @param offset to begin writing at | |||
* @param data byte array containing instance data | |||
* @return number of bytes written | |||
*/ | |||
public int serialize(int offset, byte [] data) | |||
{ | |||
LittleEndian.putShort(data, 0 + offset, sid); | |||
LittleEndian.putShort(data, 2 + offset, (short) 4); | |||
LittleEndian.putShort(data, 4 + offset, field_1_number_of_sheets); | |||
LittleEndian.putShort(data, 6 + offset, field_2_flag); | |||
return getRecordSize(); | |||
} | |||
public void setNumberOfSheets(short number){ | |||
field_1_number_of_sheets = number; | |||
} | |||
public void setFlag(){ | |||
field_2_flag = 0x0401; | |||
} | |||
public int getRecordSize() | |||
{ | |||
return 4 + 4; | |||
} | |||
public short getSid() | |||
{ | |||
return this.sid; | |||
} | |||
} |
@@ -0,0 +1,247 @@ | |||
/* ==================================================================== | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2002 The Apache Software Foundation. All rights | |||
* reserved. | |||
* | |||
* Redistribution and use in source and binary forms, with or without | |||
* modification, are permitted provided that the following conditions | |||
* are met: | |||
* | |||
* 1. Redistributions of source code must retain the above copyright | |||
* notice, this list of conditions and the following disclaimer. | |||
* | |||
* 2. Redistributions in binary form must reproduce the above copyright | |||
* notice, this list of conditions and the following disclaimer in | |||
* the documentation and/or other materials provided with the | |||
* distribution. | |||
* | |||
* 3. The end-user documentation included with the redistribution, | |||
* if any, must include the following acknowledgment: | |||
* "This product includes software developed by the | |||
* Apache Software Foundation (http://www.apache.org/)." | |||
* Alternately, this acknowledgment may appear in the software itself, | |||
* if and wherever such third-party acknowledgments normally appear. | |||
* | |||
* 4. The names "Apache" and "Apache Software Foundation" and | |||
* "Apache POI" must not be used to endorse or promote products | |||
* derived from this software without prior written permission. For | |||
* written permission, please contact apache@apache.org. | |||
* | |||
* 5. Products derived from this software may not be called "Apache", | |||
* "Apache POI", nor may "Apache" appear in their name, without | |||
* prior written permission of the Apache Software Foundation. | |||
* | |||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||
* SUCH DAMAGE. | |||
* ==================================================================== | |||
* | |||
* This software consists of voluntary contributions made by many | |||
* individuals on behalf of the Apache Software Foundation. For more | |||
* information on the Apache Software Foundation, please see | |||
* <http://www.apache.org/>. | |||
*/ | |||
package org.apache.poi.hssf.record.formula; | |||
import org.apache.poi.util.LittleEndian; | |||
import org.apache.poi.hssf.util.RangeAddress; | |||
/** | |||
* Title: Area 3D Ptg <P> | |||
* Description: Defined a area in Extern Sheet. <P> | |||
* REFERENCE: <P> | |||
* @author Libin Roman (Vista Portal LDT. Developer) | |||
* @version 1.0-pre | |||
*/ | |||
public class Area3DPtg extends Ptg | |||
{ | |||
public final static short sid = 0x3b; | |||
private final static int SIZE = 11; // 10 + 1 for Ptg | |||
private short field_1_index_extern_sheet; | |||
private short field_2_first_row; | |||
private short field_3_last_row; | |||
private short field_4_first_column; | |||
private short field_5_last_column; | |||
/** Creates new AreaPtg */ | |||
public Area3DPtg() | |||
{ | |||
} | |||
public Area3DPtg(byte[] data, int offset) | |||
{ | |||
offset++; | |||
field_1_index_extern_sheet = LittleEndian.getShort(data, 0 + offset); | |||
field_2_first_row = LittleEndian.getShort(data, 2 + offset); | |||
field_3_last_row = LittleEndian.getShort(data, 4 + offset); | |||
field_4_first_column = LittleEndian.getShort(data, 6 + offset); | |||
field_5_last_column = LittleEndian.getShort(data, 8 + offset); | |||
} | |||
public String toString() | |||
{ | |||
StringBuffer buffer = new StringBuffer(); | |||
buffer.append("AreaPtg\n"); | |||
buffer.append("Index to Extern Sheet = " + getExternSheetIndex()).append("\n"); | |||
buffer.append("firstRow = " + getFirstRow()).append("\n"); | |||
buffer.append("lastRow = " + getLastRow()).append("\n"); | |||
buffer.append("firstCol = " + getFirstColumn()).append("\n"); | |||
buffer.append("lastCol = " + getLastColumn()).append("\n"); | |||
buffer.append("firstColRowRel= " | |||
+ isFirstColRowRelative()).append("\n"); | |||
buffer.append("lastColRowRel = " | |||
+ isLastColRowRelative()).append("\n"); | |||
buffer.append("firstColRel = " + isFirstColRelative()).append("\n"); | |||
buffer.append("lastColRel = " + isLastColRelative()).append("\n"); | |||
return buffer.toString(); | |||
} | |||
public void writeBytes(byte [] array, int offset) | |||
{ | |||
array[ 0 + offset ] = sid; | |||
LittleEndian.putShort(array, 1 + offset , getExternSheetIndex()); | |||
LittleEndian.putShort(array, 3 + offset , getFirstRow()); | |||
LittleEndian.putShort(array, 5 + offset , getLastRow()); | |||
LittleEndian.putShort(array, 7 + offset , getFirstColumnRaw()); | |||
LittleEndian.putShort(array, 9 + offset , getLastColumnRaw()); | |||
} | |||
public int getSize() | |||
{ | |||
return SIZE; | |||
} | |||
public short getExternSheetIndex(){ | |||
return field_1_index_extern_sheet; | |||
} | |||
public void setExternSheetIndex(short index){ | |||
field_1_index_extern_sheet = index; | |||
} | |||
public short getFirstRow() | |||
{ | |||
return field_2_first_row; | |||
} | |||
public void setFirstRow(short row) | |||
{ | |||
field_2_first_row = row; | |||
} | |||
public short getLastRow() | |||
{ | |||
return field_3_last_row; | |||
} | |||
public void setLastRow(short row) | |||
{ | |||
field_3_last_row = row; | |||
} | |||
public short getFirstColumn() | |||
{ | |||
return ( short ) (field_4_first_column & 0xFF); | |||
} | |||
public short getFirstColumnRaw() | |||
{ | |||
return field_4_first_column; | |||
} | |||
public boolean isFirstColRowRelative() | |||
{ | |||
return (((getFirstColumnRaw()) & 0x8000) == 0x8000); | |||
} | |||
public boolean isFirstColRelative() | |||
{ | |||
return (((getFirstColumnRaw()) & 0x4000) == 0x4000); | |||
} | |||
public void setFirstColumn(short column) | |||
{ | |||
field_4_first_column &= 0xFF00; | |||
field_4_first_column |= column & 0xFF; | |||
} | |||
public void setFirstColumnRaw(short column) | |||
{ | |||
field_4_first_column = column; | |||
} | |||
public short getLastColumn() | |||
{ | |||
return ( short ) (field_5_last_column & 0xFF); | |||
} | |||
public short getLastColumnRaw() | |||
{ | |||
return field_5_last_column; | |||
} | |||
public boolean isLastColRowRelative() | |||
{ | |||
return (((getLastColumnRaw()) & 0x8000) == 1); | |||
} | |||
public boolean isLastColRelative() | |||
{ | |||
return (((getFirstColumnRaw()) & 0x4000) == 1); | |||
} | |||
public void setLastColumn(short column) | |||
{ | |||
field_5_last_column &= 0xFF00; | |||
field_5_last_column |= column & 0xFF; | |||
} | |||
public void setLastColumnRaw(short column) | |||
{ | |||
field_5_last_column = column; | |||
} | |||
public String getArea(){ | |||
RangeAddress ra = new RangeAddress( getFirstColumn(),getFirstRow() + 1, getLastColumn(), getLastRow() + 1); | |||
String result = ra.getAddress(); | |||
return result; | |||
} | |||
public void setArea(String ref){ | |||
RangeAddress ra = new RangeAddress(ref); | |||
String from = ra.getFromCell(); | |||
String to = ra.getToCell(); | |||
setFirstColumn((short) (ra.getXPosition(from) -1)); | |||
setFirstRow((short) (ra.getYPosition(from) -1)); | |||
setLastColumn((short) (ra.getXPosition(to) -1)); | |||
setLastRow((short) (ra.getYPosition(to) -1)); | |||
} | |||
public String toFormulaString() | |||
{ | |||
String result = getArea(); | |||
return result; | |||
} | |||
} |
@@ -113,9 +113,7 @@ public abstract class Ptg | |||
{ | |||
byte id = data[ offset + 0 ]; | |||
Ptg retval = null; | |||
System.out.println("PTG = " + Integer.toHexString(id) + " (" + id | |||
+ ")"); | |||
switch (id) | |||
{ | |||
@@ -175,6 +173,14 @@ public abstract class Ptg | |||
retval = new ExpPtg(data, offset); | |||
break; | |||
case Area3DPtg.sid : | |||
retval = new Area3DPtg(data, offset); | |||
break; | |||
case Ref3DPtg.sid: | |||
retval = new Ref3DPtg(data, offset); | |||
break; | |||
default : | |||
// retval = new UnknownPtg(); |
@@ -0,0 +1,177 @@ | |||
/* ==================================================================== | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2002 The Apache Software Foundation. All rights | |||
* reserved. | |||
* | |||
* Redistribution and use in source and binary forms, with or without | |||
* modification, are permitted provided that the following conditions | |||
* are met: | |||
* | |||
* 1. Redistributions of source code must retain the above copyright | |||
* notice, this list of conditions and the following disclaimer. | |||
* | |||
* 2. Redistributions in binary form must reproduce the above copyright | |||
* notice, this list of conditions and the following disclaimer in | |||
* the documentation and/or other materials provided with the | |||
* distribution. | |||
* | |||
* 3. The end-user documentation included with the redistribution, | |||
* if any, must include the following acknowledgment: | |||
* "This product includes software developed by the | |||
* Apache Software Foundation (http://www.apache.org/)." | |||
* Alternately, this acknowledgment may appear in the software itself, | |||
* if and wherever such third-party acknowledgments normally appear. | |||
* | |||
* 4. The names "Apache" and "Apache Software Foundation" and | |||
* "Apache POI" must not be used to endorse or promote products | |||
* derived from this software without prior written permission. For | |||
* written permission, please contact apache@apache.org. | |||
* | |||
* 5. Products derived from this software may not be called "Apache", | |||
* "Apache POI", nor may "Apache" appear in their name, without | |||
* prior written permission of the Apache Software Foundation. | |||
* | |||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||
* SUCH DAMAGE. | |||
* ==================================================================== | |||
* | |||
* This software consists of voluntary contributions made by many | |||
* individuals on behalf of the Apache Software Foundation. For more | |||
* information on the Apache Software Foundation, please see | |||
* <http://www.apache.org/>. | |||
*/ | |||
package org.apache.poi.hssf.record.formula; | |||
import org.apache.poi.util.LittleEndian; | |||
import org.apache.poi.hssf.util.RangeAddress; | |||
/** | |||
* Title: Reference 3D Ptg <P> | |||
* Description: Defined a cell in extern sheet. <P> | |||
* REFERENCE: <P> | |||
* @author Libin Roman (Vista Portal LDT. Developer) | |||
* @version 1.0-pre | |||
*/ | |||
public class Ref3DPtg extends Ptg { | |||
public final static short sid = 0x3a; | |||
private final static int SIZE = 7; // 6 + 1 for Ptg | |||
private short field_1_index_extern_sheet; | |||
private short field_2_row; | |||
private short field_3_column; | |||
/** Creates new AreaPtg */ | |||
public Ref3DPtg() { | |||
} | |||
public Ref3DPtg(byte[] data, int offset) { | |||
offset++; | |||
field_1_index_extern_sheet = LittleEndian.getShort(data, 0 + offset); | |||
field_2_row = LittleEndian.getShort(data, 2 + offset); | |||
field_3_column = LittleEndian.getShort(data, 4 + offset); | |||
} | |||
public String toString() { | |||
StringBuffer buffer = new StringBuffer(); | |||
buffer.append("Ref3dPrg\n"); | |||
buffer.append("Index to Extern Sheet = " + getExternSheetIndex()).append("\n"); | |||
buffer.append("Row = " + getRow()).append("\n"); | |||
buffer.append("Col = " + getColumn()).append("\n"); | |||
buffer.append("ColRowRel= " | |||
+ isColRowRelative()).append("\n"); | |||
buffer.append("ColRel = " + isColRelative()).append("\n"); | |||
return buffer.toString(); | |||
} | |||
public void writeBytes(byte [] array, int offset) { | |||
array[ 0 + offset ] = sid; | |||
LittleEndian.putShort(array, 1 + offset , getExternSheetIndex()); | |||
LittleEndian.putShort(array, 3 + offset , getRow()); | |||
LittleEndian.putShort(array, 5 + offset , getColumnRaw()); | |||
} | |||
public int getSize() { | |||
return SIZE; | |||
} | |||
public short getExternSheetIndex(){ | |||
return field_1_index_extern_sheet; | |||
} | |||
public void setExternSheetIndex(short index){ | |||
field_1_index_extern_sheet = index; | |||
} | |||
public short getRow() { | |||
return field_2_row; | |||
} | |||
public void setRow(short row) { | |||
field_2_row = row; | |||
} | |||
public short getColumn() { | |||
return ( short ) (field_3_column & 0xFF); | |||
} | |||
public short getColumnRaw() { | |||
return field_3_column; | |||
} | |||
public boolean isColRowRelative() { | |||
return (((getColumnRaw()) & 0x8000) == 0x8000); | |||
} | |||
public boolean isColRelative() { | |||
return (((getColumnRaw()) & 0x4000) == 0x4000); | |||
} | |||
public void setColumn(short column) { | |||
field_3_column &= 0xFF00; | |||
field_3_column |= column & 0xFF; | |||
} | |||
public void setColumnRaw(short column) { | |||
field_3_column = column; | |||
} | |||
public String getArea(){ | |||
RangeAddress ra = new RangeAddress(""); | |||
String result = (ra.numTo26Sys(getColumn()) + (getRow() + 1)); | |||
return result; | |||
} | |||
public void setArea(String ref){ | |||
RangeAddress ra = new RangeAddress(ref); | |||
String from = ra.getFromCell(); | |||
setColumn((short) (ra.getXPosition(from) -1)); | |||
setRow((short) (ra.getYPosition(from) -1)); | |||
} | |||
public String toFormulaString() { | |||
String result = getArea(); | |||
return result; | |||
} | |||
} |
@@ -0,0 +1,170 @@ | |||
/* ==================================================================== | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2002 The Apache Software Foundation. All rights | |||
* reserved. | |||
* | |||
* Redistribution and use in source and binary forms, with or without | |||
* modification, are permitted provided that the following conditions | |||
* are met: | |||
* | |||
* 1. Redistributions of source code must retain the above copyright | |||
* notice, this list of conditions and the following disclaimer. | |||
* | |||
* 2. Redistributions in binary form must reproduce the above copyright | |||
* notice, this list of conditions and the following disclaimer in | |||
* the documentation and/or other materials provided with the | |||
* distribution. | |||
* | |||
* 3. The end-user documentation included with the redistribution, | |||
* if any, must include the following acknowledgment: | |||
* "This product includes software developed by the | |||
* Apache Software Foundation (http://www.apache.org/)." | |||
* Alternately, this acknowledgment may appear in the software itself, | |||
* if and wherever such third-party acknowledgments normally appear. | |||
* | |||
* 4. The names "Apache" and "Apache Software Foundation" and | |||
* "Apache POI" must not be used to endorse or promote products | |||
* derived from this software without prior written permission. For | |||
* written permission, please contact apache@apache.org. | |||
* | |||
* 5. Products derived from this software may not be called "Apache", | |||
* "Apache POI", nor may "Apache" appear in their name, without | |||
* prior written permission of the Apache Software Foundation. | |||
* | |||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||
* SUCH DAMAGE. | |||
* ==================================================================== | |||
* | |||
* This software consists of voluntary contributions made by many | |||
* individuals on behalf of the Apache Software Foundation. For more | |||
* information on the Apache Software Foundation, please see | |||
* <http://www.apache.org/>. | |||
*/ | |||
package org.apache.poi.hssf.usermodel; | |||
import org.apache.poi.util.POILogFactory; | |||
import org.apache.poi.hssf.model.Sheet; | |||
import org.apache.poi.hssf.model.Workbook; | |||
import org.apache.poi.hssf.record.*; | |||
import org.apache.poi.hssf.util.Region; | |||
import org.apache.poi.util.POILogger; | |||
import java.util.Iterator; | |||
import java.util.TreeMap; | |||
import org.apache.poi.hssf.util.RangeAddress; | |||
/** | |||
* Title: High Level Represantion of Named Range <P> | |||
* REFERENCE: <P> | |||
* @author Libin Roman (Vista Portal LDT. Developer) | |||
* @version 1.0-pre | |||
*/ | |||
public class HSSFName { | |||
private Workbook book; | |||
private NameRecord name; | |||
/** Creates new HSSFName - called by HSSFWorkbook to create a sheet from | |||
* scratch. | |||
* | |||
* @see #org.apache.poi.hssf.usermodel.HSSFWorkbook.createName() | |||
* @param name the Name Record | |||
* @param book - lowlevel Workbook object associated with the sheet. | |||
* @param book the Workbook */ | |||
protected HSSFName(Workbook book, NameRecord name) { | |||
this.book = book; | |||
this.name = name; | |||
} | |||
/** private default constructor prevents bogus initializationless construction */ | |||
private HSSFName() { | |||
} | |||
/** Get the sheets name which this named range is referenced to | |||
* @return sheet name, which this named range refered to | |||
*/ | |||
public String getSheetName() { | |||
String result ; | |||
short indexToExternSheet = name.getExternSheetNumber(); | |||
result = book.findSheetNameFromExternSheet(indexToExternSheet); | |||
return result; | |||
} | |||
/** gets the name of the named range | |||
* @return named range name | |||
*/ | |||
public String getNameName(){ | |||
String result = name.getNameText(); | |||
return result; | |||
} | |||
/** sets the name of the named range | |||
* @param nameName named range name to set | |||
*/ | |||
public void setNameName(String nameName){ | |||
name.setNameText(nameName); | |||
name.setNameTextLength((byte)nameName.length()); | |||
} | |||
/** gets the reference of the named range | |||
* @return reference of the named range | |||
*/ | |||
public String getReference() { | |||
String result; | |||
result = getSheetName() + "." + name.getAreaReference(); | |||
return result; | |||
} | |||
/** sets the sheet name which this named range referenced to | |||
* @param sheetName the sheet name of the reference | |||
*/ | |||
public void setSheetName(String sheetName){ | |||
int sheetNumber = book.getSheetIndex(sheetName); | |||
short externSheetNumber = book.checkExternSheet(sheetNumber); | |||
name.setExternSheetNumber(externSheetNumber); | |||
// name.setIndexToSheet(externSheetNumber); | |||
} | |||
/** sets the reference of this named range | |||
* @param ref the reference to set | |||
*/ | |||
public void setReference(String ref){ | |||
RangeAddress ra = new RangeAddress(ref); | |||
String sheetName = ra.getSheetName(); | |||
if (ra.hasSheetName()) { | |||
setSheetName(sheetName); | |||
} | |||
if (ra.getFromCell().equals(ra.getToCell()) == false) { | |||
name.setAreaReference(ra.getFromCell() + ":" + ra.getToCell()); | |||
} else { | |||
name.setAreaReference(ra.getFromCell()); | |||
} | |||
} | |||
} |
@@ -110,6 +110,13 @@ public class HSSFWorkbook | |||
*/ | |||
private ArrayList sheets; | |||
/** | |||
* this holds the HSSFName objects attached to this workbook | |||
*/ | |||
private ArrayList names; | |||
private static POILogger log = POILogFactory.getLogger(HSSFWorkbook.class); | |||
/** | |||
@@ -121,6 +128,7 @@ public class HSSFWorkbook | |||
{ | |||
workbook = Workbook.createWorkbook(); | |||
sheets = new ArrayList(INITIAL_CAPACITY); | |||
names = new ArrayList(INITIAL_CAPACITY); | |||
} | |||
/** | |||
@@ -136,6 +144,8 @@ public class HSSFWorkbook | |||
throws IOException | |||
{ | |||
sheets = new ArrayList(INITIAL_CAPACITY); | |||
names = new ArrayList(INITIAL_CAPACITY); | |||
InputStream stream = fs.createDocumentInputStream("Workbook"); | |||
List records = RecordFactory.createRecords(stream); | |||
@@ -157,6 +167,11 @@ public class HSSFWorkbook | |||
// workbook.setSheetName(sheets.size() -1, "Sheet"+sheets.size()); | |||
} | |||
for (int i = 0 ; i < workbook.getNumNames() ; ++i){ | |||
HSSFName name = new HSSFName(workbook, workbook.getNameRecord(i)); | |||
names.add(name); | |||
} | |||
} | |||
/** | |||
@@ -216,29 +231,23 @@ public class HSSFWorkbook | |||
return workbook.getSheetName(sheet); | |||
} | |||
/** | |||
/* | |||
* get the sheet's index | |||
* @param name sheet name | |||
* @return sheet index or -1 if it was not found. | |||
*/ | |||
/** Returns the index of the sheet by his name | |||
* @param name the sheet name | |||
* @return index of the sheet (0 based) | |||
*/ | |||
public int getSheetIndex(String name) | |||
{ | |||
int retval = -1; | |||
for (int k = 0; k < sheets.size(); k++) | |||
{ | |||
String sheet = workbook.getSheetName(k); | |||
if (sheet.equals(name)) | |||
{ | |||
retval = k; | |||
break; | |||
} | |||
} | |||
int retval = workbook.getSheetIndex(name); | |||
return retval; | |||
} | |||
/** | |||
* create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns | |||
* the high level representation. Use this to create new sheets. | |||
@@ -534,4 +543,87 @@ public class HSSFWorkbook | |||
{ | |||
return workbook; | |||
} | |||
/** gets the total number of named ranges in the workboko | |||
* @return number of named ranges | |||
*/ | |||
public int getNumberOfNames(){ | |||
int result = names.size(); | |||
return result; | |||
} | |||
/** gets the Named range | |||
* @param index position of the named range | |||
* @return named range high level | |||
*/ | |||
public HSSFName getNameAt(int index){ | |||
HSSFName result = (HSSFName) names.get(index); | |||
return result; | |||
} | |||
/** gets the named range name | |||
* @param index the named range index (0 based) | |||
* @return named range name | |||
*/ | |||
public String getNameName(int index){ | |||
String result = getNameAt(index).getNameName(); | |||
return result; | |||
} | |||
/** creates a new named range and add it to the model | |||
* @return named range high level | |||
*/ | |||
public HSSFName createName(){ | |||
NameRecord nameRecord = workbook.createName(); | |||
HSSFName newName = new HSSFName(workbook, nameRecord); | |||
names.add(newName); | |||
return newName; | |||
} | |||
/** gets the named range index by his name | |||
* @param name named range name | |||
* @return named range index | |||
*/ | |||
public int getNameIndex(String name) | |||
{ | |||
int retval = -1; | |||
for (int k = 0; k < names.size(); k++) | |||
{ | |||
String nameName = getNameName(k); | |||
if (nameName.equals(name)) | |||
{ | |||
retval = k; | |||
break; | |||
} | |||
} | |||
return retval; | |||
} | |||
/** remove the named range by his index | |||
* @param index named range index (0 based) | |||
*/ | |||
public void removeName(int index){ | |||
names.remove(index); | |||
workbook.removeName(index); | |||
} | |||
/** remove the named range by his name | |||
* @param name named range name | |||
*/ | |||
public void removeName(String name){ | |||
int index = getNameIndex(name); | |||
removeName(index); | |||
} | |||
} |
@@ -0,0 +1,392 @@ | |||
package org.apache.poi.hssf.util; | |||
/* ==================================================================== | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2002 The Apache Software Foundation. All rights | |||
* reserved. | |||
* | |||
* Redistribution and use in source and binary forms, with or without | |||
* modification, are permitted provided that the following conditions | |||
* are met: | |||
* | |||
* 1. Redistributions of source code must retain the above copyright | |||
* notice, this list of conditions and the following disclaimer. | |||
* | |||
* 2. Redistributions in binary form must reproduce the above copyright | |||
* notice, this list of conditions and the following disclaimer in | |||
* the documentation and/or other materials provided with the | |||
* distribution. | |||
* | |||
* 3. The end-user documentation included with the redistribution, | |||
* if any, must include the following acknowledgment: | |||
* "This product includes software developed by the | |||
* Apache Software Foundation (http://www.apache.org/)." | |||
* Alternately, this acknowledgment may appear in the software itself, | |||
* if and wherever such third-party acknowledgments normally appear. | |||
* | |||
* 4. The names "Apache" and "Apache Software Foundation" and | |||
* "Apache POI" must not be used to endorse or promote products | |||
* derived from this software without prior written permission. For | |||
* written permission, please contact apache@apache.org. | |||
* | |||
* 5. Products derived from this software may not be called "Apache", | |||
* "Apache POI", nor may "Apache" appear in their name, without | |||
* prior written permission of the Apache Software Foundation. | |||
* | |||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||
* SUCH DAMAGE. | |||
* ==================================================================== | |||
* | |||
* This software consists of voluntary contributions made by many | |||
* individuals on behalf of the Apache Software Foundation. For more | |||
* information on the Apache Software Foundation, please see | |||
* <http://www.apache.org/>. | |||
*/ | |||
/** | |||
* Title: Range Address <P> | |||
* Description: provides connectivity utilities for ranges<P> | |||
* REFERENCE: <P> | |||
* @author IgOr KaTz && EuGeNe BuMaGiN (Tal Moshaiov) (VistaPortal LDT.) | |||
* @version 1.0 | |||
*/ | |||
public class RangeAddress { | |||
final static int WRONG_POS = -1; | |||
final static int MAX_HEIGHT = 66666; | |||
final static char SO_FORMNAME_ENCLOSURE = '\''; | |||
String m_sheetName; | |||
String m_cellFrom; | |||
String m_cellTo; | |||
public RangeAddress (String _url) { | |||
init (_url); | |||
} | |||
public RangeAddress (int _startCol, int _startRow, int _endCol, int _endRow) { | |||
init (numTo26Sys (_startCol) + _startRow + ":" | |||
+ numTo26Sys (_endCol) + _endRow); | |||
} | |||
public String getAddress (){ | |||
String result = ""; | |||
if(m_sheetName != null) | |||
result += m_sheetName; | |||
if(m_cellFrom != null){ | |||
result += m_cellFrom; | |||
if(m_cellTo != null) | |||
result += ":" + m_cellTo; | |||
} | |||
return result; | |||
} | |||
public String getSheetName (){ | |||
return m_sheetName; | |||
} | |||
public String getRange (){ | |||
String result = ""; | |||
if(m_cellFrom != null){ | |||
result += m_cellFrom; | |||
if(m_cellTo != null) | |||
result += ":" + m_cellTo; | |||
} | |||
return result; | |||
} | |||
public boolean isCellOk (String _cell){ | |||
if (_cell != null){ | |||
if ( (getYPosition (_cell) != WRONG_POS) && | |||
(getXPosition (_cell) != WRONG_POS) ) | |||
return true; | |||
else | |||
return false; | |||
} else | |||
return false; | |||
} | |||
public boolean isSheetNameOk (){ | |||
return isSheetNameOk (m_sheetName); | |||
} | |||
private static boolean intern_isSheetNameOk (String _sheetName, boolean _canBeWaitSpace){ | |||
for (int i = 0 ; i < _sheetName.length (); i++){ | |||
char ch = _sheetName.charAt (i); | |||
if (! (Character.isLetterOrDigit (ch) || (ch == '_')|| | |||
_canBeWaitSpace&&(ch == ' '))){ | |||
return false; | |||
} | |||
} | |||
return true; | |||
} | |||
public static boolean isSheetNameOk (String _sheetName){ | |||
boolean res = false; | |||
if ( ( _sheetName != null) && !_sheetName.equals ("")){ | |||
res = intern_isSheetNameOk (_sheetName,true); | |||
}else | |||
res = true; | |||
return res; | |||
} | |||
public String getFromCell (){ | |||
return m_cellFrom; | |||
} | |||
public String getToCell (){ | |||
return m_cellTo; | |||
} | |||
public int getWidth (){ | |||
if(m_cellFrom != null && m_cellTo != null){ | |||
int toX = getXPosition (m_cellTo); | |||
int fromX = getXPosition (m_cellFrom); | |||
if ((toX == WRONG_POS) || (fromX == WRONG_POS)){ | |||
return 0; | |||
}else | |||
return toX - fromX + 1; | |||
} | |||
return 0; | |||
} | |||
public int getHeight (){ | |||
if(m_cellFrom != null && m_cellTo != null){ | |||
int toY = getYPosition (m_cellTo); | |||
int fromY = getYPosition (m_cellFrom); | |||
if ((toY == WRONG_POS) || (fromY == WRONG_POS)){ | |||
return 0; | |||
}else | |||
return toY - fromY + 1; | |||
} | |||
return 0; | |||
} | |||
public void setSize (int _width, int _height){ | |||
if(m_cellFrom == null) | |||
m_cellFrom = "a1"; | |||
int tlX, tlY, rbX, rbY; | |||
tlX = getXPosition (m_cellFrom); | |||
tlY = getYPosition (m_cellFrom); | |||
m_cellTo = numTo26Sys (tlX + _width - 1); | |||
m_cellTo += String.valueOf (tlY + _height - 1); | |||
} | |||
public boolean hasSheetName (){ | |||
if(m_sheetName == null) | |||
return false; | |||
return true; | |||
} | |||
public boolean hasRange (){ | |||
if(m_cellFrom == null || m_cellTo == null) | |||
return false; | |||
return true; | |||
} | |||
public boolean hasCell (){ | |||
if(m_cellFrom == null) | |||
return false; | |||
return true; | |||
} | |||
private void init (String _url){ | |||
_url = removeString(_url, "$"); | |||
_url = removeString(_url, "'"); | |||
String[] urls = parseURL (_url); | |||
m_sheetName = urls[0]; | |||
m_cellFrom = urls[1]; | |||
m_cellTo = urls[2]; | |||
//What if range is one celled ? | |||
if (m_cellTo == null){ | |||
m_cellTo = m_cellFrom; | |||
} | |||
//Removing noneeds characters | |||
m_cellTo = removeString(m_cellTo,"."); | |||
} | |||
private String[] parseURL (String _url){ | |||
String[] result = new String[3]; | |||
int index = _url.indexOf(':'); | |||
if (index >= 0) { | |||
String fromStr = _url.substring(0, index); | |||
String toStr = _url.substring(index+1); | |||
index = fromStr.indexOf('.'); | |||
if (index >= 0) { | |||
result[0] = fromStr.substring(0, index); | |||
result[1] = fromStr.substring(index+1); | |||
} else { | |||
result[1] = fromStr; | |||
} | |||
index = toStr.indexOf('.'); | |||
if (index >= 0) { | |||
result[2] = toStr.substring(index+1); | |||
} else { | |||
result[2] = toStr; | |||
} | |||
} else { | |||
index = _url.indexOf('.'); | |||
if (index >= 0) { | |||
result[0] = _url.substring(0, index); | |||
result[1] = _url.substring(index+1); | |||
} else { | |||
result[1] = _url; | |||
} | |||
} | |||
return result; | |||
} | |||
public int getYPosition (String _subrange){ | |||
int result = WRONG_POS; | |||
_subrange = _subrange.trim (); | |||
if (_subrange.length () != 0){ | |||
String digitstr = getDigitPart (_subrange); | |||
try { | |||
result = Integer.parseInt (digitstr); | |||
if (result > MAX_HEIGHT){ | |||
result = WRONG_POS; | |||
} | |||
} | |||
catch (Exception ex) { | |||
result = WRONG_POS; | |||
} | |||
} | |||
return result; | |||
} | |||
private static boolean isLetter (String _str){ | |||
boolean res = true; | |||
if ( !_str.equals ("") ){ | |||
for (int i = 0 ; i < _str.length (); i++){ | |||
char ch = _str.charAt (i); | |||
if (! Character.isLetter (ch)){ | |||
res = false; | |||
break; | |||
} | |||
} | |||
}else | |||
res = false; | |||
return res; | |||
} | |||
public int getXPosition (String _subrange){ | |||
int result = WRONG_POS; | |||
String tmp = filter$ (_subrange); | |||
tmp = this.getCharPart (_subrange); | |||
// we will process only 2 letters ranges | |||
if (isLetter (tmp) && ((tmp.length () == 2)|| (tmp.length () == 1) )){ | |||
result = get26Sys (tmp); | |||
} | |||
return result; | |||
} | |||
public String getDigitPart (String _value){ | |||
String result = ""; | |||
int digitpos = getFirstDigitPosition (_value); | |||
if(digitpos >= 0){ | |||
result = _value.substring (digitpos); | |||
} | |||
return result; | |||
} | |||
public String getCharPart (String _value){ | |||
String result = ""; | |||
int digitpos = getFirstDigitPosition (_value); | |||
if(digitpos >= 0){ | |||
result = _value.substring (0, digitpos); | |||
} | |||
return result; | |||
} | |||
private String filter$ (String _range){ | |||
String res = ""; | |||
for (int i = 0 ; i < _range.length () ; i++){ | |||
char ch = _range.charAt (i); | |||
if ( ch != '$' ){ | |||
res = res + ch; | |||
} | |||
} | |||
return res; | |||
} | |||
private int getFirstDigitPosition (String _value){ | |||
int result = WRONG_POS; | |||
if(_value != null && _value.trim ().length () == 0){ | |||
return result; | |||
} | |||
_value = _value.trim (); | |||
int length = _value.length (); | |||
for(int i = 0; i < length; i++){ | |||
if(Character.isDigit (_value.charAt (i))){ | |||
result = i; | |||
break; | |||
} | |||
} | |||
return result; | |||
} | |||
public int get26Sys (String _s){ | |||
int sum = 0; | |||
int multiplier = 1; | |||
if (_s != "") { | |||
for (int i = _s.length ()-1 ; i >= 0 ; i--){ | |||
char ch = _s.charAt (i); | |||
int val = Character.getNumericValue (ch) - Character.getNumericValue ('A')+1; | |||
sum = sum + val * multiplier; | |||
multiplier = multiplier * 26; | |||
} | |||
return sum; | |||
} | |||
return WRONG_POS; | |||
} | |||
public String numTo26Sys (int _num){ | |||
int sum = 0; | |||
int reminder; | |||
String s =""; | |||
do{ | |||
_num --; | |||
reminder = _num % 26; | |||
int val = 65 + reminder; | |||
_num = _num / 26; | |||
s = (char)val + s; // reverce | |||
}while(_num > 0); | |||
return s; | |||
} | |||
public String replaceString(String _source , String _oldPattern, | |||
String _newPattern){ | |||
StringBuffer res = new StringBuffer(_source); | |||
int pos = -1; | |||
while ((pos = res.toString().indexOf(_oldPattern, pos)) > -1){ | |||
res.replace(pos, pos + _oldPattern.length(), _newPattern); | |||
} | |||
return res.toString(); | |||
} | |||
public String removeString(String _source, String _match){ | |||
return replaceString(_source, _match, ""); | |||
} | |||
} |