Search in sources :

Example 1 with PasswordRecord

use of org.apache.poi.hssf.record.PasswordRecord in project poi by apache.

the class WorksheetProtectionBlock method protectSheet.

/**
	 * protect a spreadsheet with a password (not encrypted, just sets protect
	 * flags and the password.
	 *
	 * @param password to set. Pass <code>null</code> to remove all protection
	 * @param shouldProtectObjects are protected
	 * @param shouldProtectScenarios are protected
	 */
public void protectSheet(String password, boolean shouldProtectObjects, boolean shouldProtectScenarios) {
    if (password == null) {
        _passwordRecord = null;
        _protectRecord = null;
        _objectProtectRecord = null;
        _scenarioProtectRecord = null;
        return;
    }
    ProtectRecord prec = getProtect();
    PasswordRecord pass = getPassword();
    prec.setProtect(true);
    pass.setPassword((short) CryptoFunctions.createXorVerifier1(password));
    if (_objectProtectRecord == null && shouldProtectObjects) {
        ObjectProtectRecord rec = createObjectProtect();
        rec.setProtect(true);
        _objectProtectRecord = rec;
    }
    if (_scenarioProtectRecord == null && shouldProtectScenarios) {
        ScenarioProtectRecord srec = createScenarioProtect();
        srec.setProtect(true);
        _scenarioProtectRecord = srec;
    }
}
Also used : ProtectRecord(org.apache.poi.hssf.record.ProtectRecord) ObjectProtectRecord(org.apache.poi.hssf.record.ObjectProtectRecord) ScenarioProtectRecord(org.apache.poi.hssf.record.ScenarioProtectRecord) ScenarioProtectRecord(org.apache.poi.hssf.record.ScenarioProtectRecord) PasswordRecord(org.apache.poi.hssf.record.PasswordRecord) ObjectProtectRecord(org.apache.poi.hssf.record.ObjectProtectRecord)

Example 2 with PasswordRecord

use of org.apache.poi.hssf.record.PasswordRecord in project poi by apache.

the class TestHSSFSheet method protectSheetRecordOrder_bug47363a.

/**
     * {@link PasswordRecord} belongs with the rest of the Worksheet Protection Block
     * (which should be before {@link DimensionsRecord}).
     */
@Test
public void protectSheetRecordOrder_bug47363a() throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet s = wb.createSheet();
    s.protectSheet("secret");
    RecordCollector rc = new RecordCollector();
    s.getSheet().visitContainedRecords(rc, 0);
    Record[] recs = rc.getRecords();
    int nRecs = recs.length;
    if (recs[nRecs - 2] instanceof PasswordRecord && recs[nRecs - 5] instanceof DimensionsRecord) {
        fail("Identified bug 47363a - PASSWORD after DIMENSION");
    }
    // Check that protection block is together, and before DIMENSION
    confirmRecordClass(recs, nRecs - 4, DimensionsRecord.class);
    confirmRecordClass(recs, nRecs - 9, ProtectRecord.class);
    confirmRecordClass(recs, nRecs - 8, ObjectProtectRecord.class);
    confirmRecordClass(recs, nRecs - 7, ScenarioProtectRecord.class);
    confirmRecordClass(recs, nRecs - 6, PasswordRecord.class);
    wb.close();
}
Also used : PasswordRecord(org.apache.poi.hssf.record.PasswordRecord) RecordCollector(org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector) VCenterRecord(org.apache.poi.hssf.record.VCenterRecord) ScenarioProtectRecord(org.apache.poi.hssf.record.ScenarioProtectRecord) GridsetRecord(org.apache.poi.hssf.record.GridsetRecord) NameRecord(org.apache.poi.hssf.record.NameRecord) SCLRecord(org.apache.poi.hssf.record.SCLRecord) FtCblsSubRecord(org.apache.poi.hssf.record.FtCblsSubRecord) ObjectProtectRecord(org.apache.poi.hssf.record.ObjectProtectRecord) ProtectRecord(org.apache.poi.hssf.record.ProtectRecord) LbsDataSubRecord(org.apache.poi.hssf.record.LbsDataSubRecord) Record(org.apache.poi.hssf.record.Record) ObjRecord(org.apache.poi.hssf.record.ObjRecord) PasswordRecord(org.apache.poi.hssf.record.PasswordRecord) EscherDgRecord(org.apache.poi.ddf.EscherDgRecord) WSBoolRecord(org.apache.poi.hssf.record.WSBoolRecord) SubRecord(org.apache.poi.hssf.record.SubRecord) HCenterRecord(org.apache.poi.hssf.record.HCenterRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) AutoFilterInfoRecord(org.apache.poi.hssf.record.AutoFilterInfoRecord) CommonObjectDataSubRecord(org.apache.poi.hssf.record.CommonObjectDataSubRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) Test(org.junit.Test)

Aggregations

ObjectProtectRecord (org.apache.poi.hssf.record.ObjectProtectRecord)2 PasswordRecord (org.apache.poi.hssf.record.PasswordRecord)2 ProtectRecord (org.apache.poi.hssf.record.ProtectRecord)2 ScenarioProtectRecord (org.apache.poi.hssf.record.ScenarioProtectRecord)2 EscherDgRecord (org.apache.poi.ddf.EscherDgRecord)1 AutoFilterInfoRecord (org.apache.poi.hssf.record.AutoFilterInfoRecord)1 CommonObjectDataSubRecord (org.apache.poi.hssf.record.CommonObjectDataSubRecord)1 DimensionsRecord (org.apache.poi.hssf.record.DimensionsRecord)1 FtCblsSubRecord (org.apache.poi.hssf.record.FtCblsSubRecord)1 GridsetRecord (org.apache.poi.hssf.record.GridsetRecord)1 HCenterRecord (org.apache.poi.hssf.record.HCenterRecord)1 LbsDataSubRecord (org.apache.poi.hssf.record.LbsDataSubRecord)1 NameRecord (org.apache.poi.hssf.record.NameRecord)1 ObjRecord (org.apache.poi.hssf.record.ObjRecord)1 Record (org.apache.poi.hssf.record.Record)1 SCLRecord (org.apache.poi.hssf.record.SCLRecord)1 SubRecord (org.apache.poi.hssf.record.SubRecord)1 VCenterRecord (org.apache.poi.hssf.record.VCenterRecord)1 WSBoolRecord (org.apache.poi.hssf.record.WSBoolRecord)1 WindowTwoRecord (org.apache.poi.hssf.record.WindowTwoRecord)1