001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.record;
019:
020: import org.apache.poi.util.LittleEndian;
021:
022: /**
023: * Title: Protect Record<P>
024: * Description: defines whether a sheet or workbook is protected (HSSF DOES NOT SUPPORT ENCRYPTION)<P>
025: * (kindly ask the US government to stop having arcane stupid encryption laws and we'll support it) <P>
026: * (after all terrorists will all use US-legal encrypton right??)<P>
027: * HSSF now supports the simple "protected" sheets (where they are not encrypted and open office et al
028: * ignore the password record entirely).
029: * REFERENCE: PG 373 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
030: * @author Andrew C. Oliver (acoliver at apache dot org)
031: */
032:
033: public class ProtectRecord extends Record {
034: public final static short sid = 0x12;
035: private short field_1_protect;
036:
037: public ProtectRecord() {
038: }
039:
040: /**
041: * Constructs a Protect record and sets its fields appropriately.
042: * @param in the RecordInputstream to read the record from
043: */
044:
045: public ProtectRecord(RecordInputStream in) {
046: super (in);
047: }
048:
049: protected void validateSid(short id) {
050: if (id != sid) {
051: throw new RecordFormatException("NOT A PROTECT RECORD");
052: }
053: }
054:
055: protected void fillFields(RecordInputStream in) {
056: field_1_protect = in.readShort();
057: }
058:
059: /**
060: * set whether the sheet is protected or not
061: * @param protect whether to protect the sheet or not
062: */
063:
064: public void setProtect(boolean protect) {
065: if (protect) {
066: field_1_protect = 1;
067: } else {
068: field_1_protect = 0;
069: }
070: }
071:
072: /**
073: * get whether the sheet is protected or not
074: * @return whether to protect the sheet or not
075: */
076:
077: public boolean getProtect() {
078: return (field_1_protect == 1);
079: }
080:
081: public String toString() {
082: StringBuffer buffer = new StringBuffer();
083:
084: buffer.append("[PROTECT]\n");
085: buffer.append(" .protect = ").append(getProtect())
086: .append("\n");
087: buffer.append("[/PROTECT]\n");
088: return buffer.toString();
089: }
090:
091: public int serialize(int offset, byte[] data) {
092: LittleEndian.putShort(data, 0 + offset, sid);
093: LittleEndian.putShort(data, 2 + offset, ((short) 0x02)); // 2 bytes (6 total)
094: LittleEndian.putShort(data, 4 + offset, field_1_protect);
095: return getRecordSize();
096: }
097:
098: public int getRecordSize() {
099: return 6;
100: }
101:
102: public short getSid() {
103: return sid;
104: }
105:
106: public Object clone() {
107: ProtectRecord rec = new ProtectRecord();
108: rec.field_1_protect = field_1_protect;
109: return rec;
110: }
111: }
|