KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.poi.util.StringUtil;
23
24 /**
25  * Title: Write Access Record<P>
26  * Description: Stores the username of that who owns the spreadsheet generator
27  * (on unix the user's login, on Windoze its the name you typed when
28  * you installed the thing)<P>
29  * REFERENCE: PG 424 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
30  * @author Andrew C. Oliver (acoliver at apache dot org)
31  * @version 2.0-pre
32  */

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

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

65
66     public WriteAccessRecord(short id, short size, byte [] data, int offset)
67     {
68         super(id, size, data, offset);
69     }
70
71     protected void validateSid(short id)
72     {
73         if (id != sid)
74         {
75             throw new RecordFormatException("NOT A WRITEACCESS RECORD");
76         }
77     }
78
79     protected void fillFields(byte [] data, short size, int offset)
80     {
81         field_1_username = StringUtil.getFromCompressedUnicode(data, 3 + offset, data.length - 4);
82     }
83
84     /**
85      * set the username for the user that created the report. HSSF uses the logged in user.
86      * @param username of the user who is logged in (probably "tomcat" or "apache")
87      */

88
89     public void setUsername(String JavaDoc username)
90     {
91         field_1_username = username;
92     }
93
94     /**
95      * get the username for the user that created the report. HSSF uses the logged in user. On
96      * natively created M$ Excel sheet this would be the name you typed in when you installed it
97      * in most cases.
98      * @return username of the user who is logged in (probably "tomcat" or "apache")
99      */

100
101     public String JavaDoc getUsername()
102     {
103         return field_1_username;
104     }
105
106     public String JavaDoc toString()
107     {
108         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
109
110         buffer.append("[WRITEACCESS]\n");
111         buffer.append(" .name = ")
112             .append(field_1_username.toString()).append("\n");
113         buffer.append("[/WRITEACCESS]\n");
114         return buffer.toString();
115     }
116
117     public int serialize(int offset, byte [] data)
118     {
119         String JavaDoc username = getUsername();
120         StringBuffer JavaDoc temp = new StringBuffer JavaDoc(0x70 - (0x3));
121
122         temp.append(username);
123         while (temp.length() < 0x70 - 0x3)
124         {
125             temp.append(
126                 " "); // (70 = fixed lenght -3 = the overhead bits of unicode string)
127
}
128         username = temp.toString();
129         UnicodeString str = new UnicodeString();
130
131         str.setString(username);
132         str.setOptionFlags(( byte ) 0x0);
133         str.setCharCount(( short ) 0x4);
134         byte[] stringbytes = str.serialize();
135
136         LittleEndian.putShort(data, 0 + offset, sid);
137         LittleEndian.putShort(data, 2 + offset,
138                               ( short ) (stringbytes
139                                   .length)); // 112 bytes (115 total)
140
System.arraycopy(stringbytes, 0, data, 4 + offset,
141                          stringbytes.length);
142         return getRecordSize();
143     }
144
145     public int getRecordSize()
146     {
147         return 116;
148     }
149
150     public short getSid()
151     {
152         return this.sid;
153     }
154 }
155
Popular Tags