KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > ProtectRecord


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.hssf.record;
20
21 import org.apache.poi.util.LittleEndian;
22
23 /**
24  * Title: Protect Record<P>
25  * Description: defines whether a sheet or workbook is protected (HSSF DOES NOT SUPPORT ENCRYPTION)<P>
26  * (kindly ask the US government to stop having arcane stupid encryption laws and we'll support it) <P>
27  * (after all terrorists will all use US-legal encrypton right??)<P>
28  * REFERENCE: PG 373 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
29  * @author Andrew C. Oliver (acoliver at apache dot org)
30  * @version 2.0-pre
31  */

32
33 public class ProtectRecord
34     extends Record
35 {
36     public final static short sid = 0x12;
37     private short field_1_protect;
38
39     public ProtectRecord()
40     {
41     }
42
43     /**
44      * Constructs a Protect record and sets its fields appropriately.
45      *
46      * @param id id must be 0x12 or an exception will be throw upon validation
47      * @param size size the size of the data area of the record
48      * @param data data of the record (should not contain sid/len)
49      */

50
51     public ProtectRecord(short id, short size, byte [] data)
52     {
53         super(id, size, data);
54     }
55
56     /**
57      * Constructs a Protect record and sets its fields appropriately.
58      *
59      * @param id id must be 0x12 or an exception will be throw upon validation
60      * @param size size the size of the data area of the record
61      * @param data data of the record (should not contain sid/len)
62      * @param offset of the data
63      */

64
65     public ProtectRecord(short id, short size, byte [] data, int offset)
66     {
67         super(id, size, data, offset);
68     }
69
70     protected void validateSid(short id)
71     {
72         if (id != sid)
73         {
74             throw new RecordFormatException("NOT A PROTECT RECORD");
75         }
76     }
77
78     protected void fillFields(byte [] data, short size, int offset)
79     {
80         field_1_protect = LittleEndian.getShort(data, 0 + offset);
81     }
82
83     /**
84      * set whether the sheet is protected or not
85      * @param protect whether to protect the sheet or not
86      */

87
88     public void setProtect(boolean protect)
89     {
90         if (protect)
91         {
92             field_1_protect = 1;
93         }
94         else
95         {
96             field_1_protect = 0;
97         }
98     }
99
100     /**
101      * get whether the sheet is protected or not
102      * @return whether to protect the sheet or not
103      */

104
105     public boolean getProtect()
106     {
107         return (field_1_protect == 1);
108     }
109
110     public String JavaDoc toString()
111     {
112         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
113
114         buffer.append("[PROTECT]\n");
115         buffer.append(" .protect = ").append(getProtect())
116             .append("\n");
117         buffer.append("[/PROTECT]\n");
118         return buffer.toString();
119     }
120
121     public int serialize(int offset, byte [] data)
122     {
123         LittleEndian.putShort(data, 0 + offset, sid);
124         LittleEndian.putShort(data, 2 + offset,
125                               (( short ) 0x02)); // 2 bytes (6 total)
126
LittleEndian.putShort(data, 4 + offset, field_1_protect);
127         return getRecordSize();
128     }
129
130     public int getRecordSize()
131     {
132         return 6;
133     }
134
135     public short getSid()
136     {
137         return this.sid;
138     }
139
140     public Object JavaDoc clone() {
141         ProtectRecord rec = new ProtectRecord();
142         rec.field_1_protect = field_1_protect;
143         return rec;
144     }
145 }
146
Popular Tags