Bug id 21722: Protect record to Sheets

Submitted by Rick Berman..Thanks.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/branches/REL_2_BRANCH@353310 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Avik Sengupta 2003-08-23 18:54:28 +00:00
parent 9f0fb6e148
commit e1c8093b7a
6 changed files with 80 additions and 13 deletions

View File

@ -122,6 +122,7 @@ public class Sheet implements Model
private Iterator valueRecIterator = null; private Iterator valueRecIterator = null;
private Iterator rowRecIterator = null; private Iterator rowRecIterator = null;
protected int eofLoc = 0; protected int eofLoc = 0;
protected ProtectRecord protect = null;
public static final byte PANE_LOWER_RIGHT = (byte)0; public static final byte PANE_LOWER_RIGHT = (byte)0;
public static final byte PANE_UPPER_RIGHT = (byte)1; public static final byte PANE_UPPER_RIGHT = (byte)1;
@ -287,6 +288,10 @@ public class Sheet implements Model
{ {
retval.windowTwo = (WindowTwoRecord) rec; retval.windowTwo = (WindowTwoRecord) rec;
} }
else if ( rec.getSid() == ProtectRecord.sid )
{
retval.protect = (ProtectRecord) rec;
}
if (rec != null) if (rec != null)
{ {
@ -418,6 +423,8 @@ public class Sheet implements Model
retval.selection = retval.selection =
(SelectionRecord) retval.createSelection(); (SelectionRecord) retval.createSelection();
records.add(retval.selection); records.add(retval.selection);
retval.protect = (ProtectRecord) retval.createProtect();
records.add(retval.protect);
records.add(retval.createEOF()); records.add(retval.createEOF());
retval.records = records; retval.records = records;
log.log(log.DEBUG, "Sheet createsheet from scratch exit"); log.log(log.DEBUG, "Sheet createsheet from scratch exit");
@ -2524,6 +2531,27 @@ public class Sheet implements Model
{ {
this.selection = selection; this.selection = selection;
} }
/**
* creates a Protect record with protect set to false.
* @see org.apache.poi.hssf.record.ProtectRecord
* @see org.apache.poi.hssf.record.Record
* @return a ProtectRecord
*/
protected Record createProtect()
{
log.log(log.DEBUG, "create protect record with protection disabled");
ProtectRecord retval = new ProtectRecord();
retval.setProtect(false);
// by default even when we support encryption we won't
return retval;
}
public ProtectRecord getProtect()
{
return protect;
}
/** /**
* Sets whether the gridlines are shown in a viewer. * Sets whether the gridlines are shown in a viewer.

View File

@ -139,9 +139,9 @@ public class ProtectRecord
* @return whether to protect the sheet or not * @return whether to protect the sheet or not
*/ */
public short getProtect() public boolean getProtect()
{ {
return field_1_protect; return (field_1_protect == 1);
} }
public String toString() public String toString()
@ -149,8 +149,8 @@ public class ProtectRecord
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
buffer.append("[PROTECT]\n"); buffer.append("[PROTECT]\n");
buffer.append(" .protected = ") buffer.append(" .protect = ").append(getProtect())
.append(Integer.toHexString(getProtect())).append("\n"); .append("\n");
buffer.append("[/PROTECT]\n"); buffer.append("[/PROTECT]\n");
return buffer.toString(); return buffer.toString();
} }
@ -160,7 +160,7 @@ public class ProtectRecord
LittleEndian.putShort(data, 0 + offset, sid); LittleEndian.putShort(data, 0 + offset, sid);
LittleEndian.putShort(data, 2 + offset, LittleEndian.putShort(data, 2 + offset,
(( short ) 0x02)); // 2 bytes (6 total) (( short ) 0x02)); // 2 bytes (6 total)
LittleEndian.putShort(data, 4 + offset, getProtect()); LittleEndian.putShort(data, 4 + offset, field_1_protect);
return getRecordSize(); return getRecordSize();
} }

View File

@ -139,18 +139,18 @@ public class ProtectionRev4Record
* @return whether to protect the workbook or not * @return whether to protect the workbook or not
*/ */
public short getProtect() public boolean getProtect()
{ {
return field_1_protect; return (field_1_protect == 1);
} }
public String toString() public String toString()
{ {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
buffer.append("[PROT4REV]\n"); buffer.append("[PROT4REV]\n");
buffer.append(" .rowheight = ") buffer.append(" .protect = ").append(getProtect())
.append(Integer.toHexString(getProtect())).append("\n"); .append("\n");
buffer.append("[/PROT4REV]\n"); buffer.append("[/PROT4REV]\n");
return buffer.toString(); return buffer.toString();
} }
@ -160,7 +160,7 @@ public class ProtectionRev4Record
LittleEndian.putShort(data, 0 + offset, sid); LittleEndian.putShort(data, 0 + offset, sid);
LittleEndian.putShort(data, 2 + offset, LittleEndian.putShort(data, 2 + offset,
(( short ) 0x02)); // 2 bytes (6 total) (( short ) 0x02)); // 2 bytes (6 total)
LittleEndian.putShort(data, 4 + offset, getProtect()); LittleEndian.putShort(data, 4 + offset, field_1_protect);
return getRecordSize(); return getRecordSize();
} }

View File

@ -874,6 +874,22 @@ public class HSSFSheet
getSheet().setMargin( margin, size ); getSheet().setMargin( margin, size );
} }
/**
* Answer whether protection is enabled or disabled
* @return true => protection enabled; false => protection disabled
*/
public boolean getProtect() {
return getSheet().getProtect().getProtect();
}
/**
* Sets the protection on enabled or disabled
* @param protect true => protection enabled; false => protection disabled
*/
public void setProtect(boolean protect) {
getSheet().getProtect().setProtect(protect);
}
/** /**
* Sets the zoom magnication for the sheet. The zoom is expressed as a * Sets the zoom magnication for the sheet. The zoom is expressed as a
* fraction. For example to express a zoom of 75% use 3 for the numerator * fraction. For example to express a zoom of 75% use 3 for the numerator

View File

@ -112,6 +112,7 @@ extends TestCase {
} }

View File

@ -62,6 +62,7 @@ import junit.framework.TestCase;
import org.apache.poi.hssf.model.Sheet; import org.apache.poi.hssf.model.Sheet;
import org.apache.poi.hssf.record.HCenterRecord; import org.apache.poi.hssf.record.HCenterRecord;
import org.apache.poi.hssf.record.ProtectRecord;
import org.apache.poi.hssf.record.SCLRecord; import org.apache.poi.hssf.record.SCLRecord;
import org.apache.poi.hssf.record.VCenterRecord; import org.apache.poi.hssf.record.VCenterRecord;
import org.apache.poi.hssf.record.WSBoolRecord; import org.apache.poi.hssf.record.WSBoolRecord;
@ -239,6 +240,27 @@ public class TestHSSFSheet
assertEquals(cloned.getRow((short)0).getCell((short)0).getStringCellValue(), "clone_test"); assertEquals(cloned.getRow((short)0).getCell((short)0).getStringCellValue(), "clone_test");
} }
/**
* Test that the ProtectRecord is included when creating or cloning a sheet
*/
public void testProtect() {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet hssfSheet = workbook.createSheet();
Sheet sheet = hssfSheet.getSheet();
ProtectRecord protect = sheet.getProtect();
assertFalse(protect.getProtect());
// This will tell us that cloneSheet, and by extension,
// the list forms of createSheet leave us with an accessible
// ProtectRecord.
hssfSheet.setProtect(true);
Sheet cloned = sheet.cloneSheet();
assertNotNull(cloned.getProtect());
assertTrue(hssfSheet.getProtect());
}
public void testZoom() public void testZoom()
throws Exception throws Exception
{ {