KickJava   Java API By Example, From Geeks To Geeks.

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


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: Password Record<P>
25  * Description: stores the encrypted password for a sheet or workbook (HSSF doesn't support encryption)
26  * REFERENCE: PG 371 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
27  * @author Andrew C. Oliver (acoliver at apache dot org)
28  * @version 2.0-pre
29  */

30
31 public class PasswordRecord
32     extends Record
33 {
34     public final static short sid = 0x13;
35     private short field_1_password; // not sure why this is only 2 bytes, but it is... go figure
36

37     public PasswordRecord()
38     {
39     }
40
41     /**
42      * Constructs a Password record and sets its fields appropriately.
43      *
44      * @param id id must be 0x13 or an exception will be throw upon validation
45      * @param size the size of the data area of the record
46      * @param data data of the record (should not contain sid/len)
47      */

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

62
63     public PasswordRecord(short id, short size, byte [] data, int offset)
64     {
65         super(id, size, data, offset);
66     }
67
68     protected void validateSid(short id)
69     {
70         if (id != sid)
71         {
72             throw new RecordFormatException("NOT A PASSWORD RECORD");
73         }
74     }
75
76     protected void fillFields(byte [] data, short size, int offset)
77     {
78         field_1_password = LittleEndian.getShort(data, 0 + offset);
79     }
80
81     /**
82      * set the password
83      *
84      * @param password representing the password
85      */

86
87     public void setPassword(short password)
88     {
89         field_1_password = password;
90     }
91
92     /**
93      * get the password
94      *
95      * @return short representing the password
96      */

97
98     public short getPassword()
99     {
100         return field_1_password;
101     }
102
103     public String JavaDoc toString()
104     {
105         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
106
107         buffer.append("[PASSWORD]\n");
108         buffer.append(" .password = ")
109             .append(Integer.toHexString(getPassword())).append("\n");
110         buffer.append("[/PASSWORD]\n");
111         return buffer.toString();
112     }
113
114     public int serialize(int offset, byte [] data)
115     {
116         LittleEndian.putShort(data, 0 + offset, sid);
117         LittleEndian.putShort(data, 2 + offset,
118                               (( short ) 0x02)); // 2 bytes (6 total)
119
LittleEndian.putShort(data, 4 + offset, getPassword());
120         return getRecordSize();
121     }
122
123     public int getRecordSize()
124     {
125         return 6;
126     }
127
128     public short getSid()
129     {
130         return this.sid;
131     }
132
133     /**
134      * Clone this record.
135      */

136     public Object JavaDoc clone() {
137       PasswordRecord clone = new PasswordRecord();
138       clone.setPassword(field_1_password);
139       return clone;
140     }
141
142 }
143
Popular Tags