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: import org.apache.poi.util.StringUtil;
022:
023: /**
024: * Title: Write Access Record<P>
025: * Description: Stores the username of that who owns the spreadsheet generator
026: * (on unix the user's login, on Windoze its the name you typed when
027: * you installed the thing)<P>
028: * REFERENCE: PG 424 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
029: * @author Andrew C. Oliver (acoliver at apache dot org)
030: * @version 2.0-pre
031: */
032:
033: public class WriteAccessRecord extends Record {
034: public final static short sid = 0x5c;
035: private String field_1_username;
036:
037: public WriteAccessRecord() {
038: }
039:
040: /**
041: * Constructs a WriteAccess record and sets its fields appropriately.
042: * @param in the RecordInputstream to read the record from
043: */
044:
045: public WriteAccessRecord(RecordInputStream in) {
046: super (in);
047: }
048:
049: protected void validateSid(short id) {
050: if (id != sid) {
051: throw new RecordFormatException("NOT A WRITEACCESS RECORD");
052: }
053: }
054:
055: protected void fillFields(RecordInputStream in) {
056: byte[] data = in.readRemainder();
057: //The string is always 112 characters (padded with spaces), therefore
058: //this record can not be continued.
059:
060: //What a wierd record, it is not really a unicode string because the
061: //header doesnt provide a correct size indication.???
062: //But the header is present, so we need to skip over it.
063: //Odd, Odd, Odd ;-)
064: field_1_username = StringUtil.getFromCompressedUnicode(data, 3,
065: data.length - 3);
066: }
067:
068: /**
069: * set the username for the user that created the report. HSSF uses the logged in user.
070: * @param username of the user who is logged in (probably "tomcat" or "apache")
071: */
072:
073: public void setUsername(String username) {
074: field_1_username = username;
075: }
076:
077: /**
078: * get the username for the user that created the report. HSSF uses the logged in user. On
079: * natively created M$ Excel sheet this would be the name you typed in when you installed it
080: * in most cases.
081: * @return username of the user who is logged in (probably "tomcat" or "apache")
082: */
083:
084: public String getUsername() {
085: return field_1_username;
086: }
087:
088: public String toString() {
089: StringBuffer buffer = new StringBuffer();
090:
091: buffer.append("[WRITEACCESS]\n");
092: buffer.append(" .name = ").append(
093: field_1_username.toString()).append("\n");
094: buffer.append("[/WRITEACCESS]\n");
095: return buffer.toString();
096: }
097:
098: public int serialize(int offset, byte[] data) {
099: String username = getUsername();
100: StringBuffer temp = new StringBuffer(0x70 - (0x3));
101:
102: temp.append(username);
103: while (temp.length() < 0x70 - 0x3) {
104: temp.append(" "); // (70 = fixed lenght -3 = the overhead bits of unicode string)
105: }
106: username = temp.toString();
107: UnicodeString str = new UnicodeString(username);
108: str.setOptionFlags((byte) 0x0);
109:
110: LittleEndian.putShort(data, 0 + offset, sid);
111: LittleEndian.putShort(data, 2 + offset, (short) 112); // 112 bytes (115 total)
112: UnicodeString.UnicodeRecordStats stats = new UnicodeString.UnicodeRecordStats();
113: stats.recordSize += 4;
114: stats.remainingSize -= 4;
115: str.serialize(stats, 4 + offset, data);
116:
117: return getRecordSize();
118: }
119:
120: public int getRecordSize() {
121: return 116;
122: }
123:
124: public short getSid() {
125: return sid;
126: }
127: }
|