You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WriteAccessRecord.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.record;
  16. import java.util.Arrays;
  17. import org.apache.poi.util.LittleEndian;
  18. import org.apache.poi.util.LittleEndianOutput;
  19. import org.apache.poi.util.StringUtil;
  20. /**
  21. * Title: Write Access Record (0x005C)<p/>
  22. *
  23. * Description: Stores the username of that who owns the spreadsheet generator (on unix the user's
  24. * login, on Windoze its the name you typed when you installed the thing)
  25. * <p/>
  26. * REFERENCE: PG 424 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)
  27. * <p/>
  28. *
  29. * @author Andrew C. Oliver (acoliver at apache dot org)
  30. */
  31. public final class WriteAccessRecord extends StandardRecord {
  32. public final static short sid = 0x005C;
  33. private static final byte PAD_CHAR = (byte) ' ';
  34. private static final int DATA_SIZE = 112;
  35. private String field_1_username;
  36. /** this record is always padded to a constant length */
  37. private static final byte[] PADDING = new byte[DATA_SIZE];
  38. static {
  39. Arrays.fill(PADDING, PAD_CHAR);
  40. }
  41. public WriteAccessRecord() {
  42. setUsername("");
  43. }
  44. public WriteAccessRecord(RecordInputStream in) {
  45. if (in.remaining() > DATA_SIZE) {
  46. throw new RecordFormatException("Expected data size (" + DATA_SIZE + ") but got ("
  47. + in.remaining() + ")");
  48. }
  49. // The string is always 112 characters (padded with spaces), therefore
  50. // this record can not be continued.
  51. int nChars = in.readUShort();
  52. int is16BitFlag = in.readUByte();
  53. if (nChars > DATA_SIZE || (is16BitFlag & 0xFE) != 0) {
  54. // String header looks wrong (probably missing)
  55. // OOO doc says this is optional anyway.
  56. // reconstruct data
  57. byte[] data = new byte[3 + in.remaining()];
  58. LittleEndian.putUShort(data, 0, nChars);
  59. LittleEndian.putByte(data, 2, is16BitFlag);
  60. in.readFully(data, 3, data.length-3);
  61. String rawValue = new String(data);
  62. setUsername(rawValue.trim());
  63. return;
  64. }
  65. String rawText;
  66. if ((is16BitFlag & 0x01) == 0x00) {
  67. rawText = StringUtil.readCompressedUnicode(in, nChars);
  68. } else {
  69. rawText = StringUtil.readUnicodeLE(in, nChars);
  70. }
  71. field_1_username = rawText.trim();
  72. // consume padding
  73. int padSize = in.remaining();
  74. while (padSize > 0) {
  75. // in some cases this seems to be garbage (non spaces)
  76. in.readUByte();
  77. padSize--;
  78. }
  79. }
  80. /**
  81. * set the username for the user that created the report. HSSF uses the
  82. * logged in user.
  83. *
  84. * @param username of the user who is logged in (probably "tomcat" or "apache")
  85. */
  86. public void setUsername(String username) {
  87. boolean is16bit = StringUtil.hasMultibyte(username);
  88. int encodedByteCount = 3 + username.length() * (is16bit ? 2 : 1);
  89. int paddingSize = DATA_SIZE - encodedByteCount;
  90. if (paddingSize < 0) {
  91. throw new IllegalArgumentException("Name is too long: " + username);
  92. }
  93. field_1_username = username;
  94. }
  95. /**
  96. * get the username for the user that created the report. HSSF uses the
  97. * logged in user. On natively created M$ Excel sheet this would be the name
  98. * you typed in when you installed it in most cases.
  99. *
  100. * @return username of the user who is logged in (probably "tomcat" or "apache")
  101. */
  102. public String getUsername() {
  103. return field_1_username;
  104. }
  105. public String toString() {
  106. StringBuffer buffer = new StringBuffer();
  107. buffer.append("[WRITEACCESS]\n");
  108. buffer.append(" .name = ").append(field_1_username.toString()).append("\n");
  109. buffer.append("[/WRITEACCESS]\n");
  110. return buffer.toString();
  111. }
  112. public void serialize(LittleEndianOutput out) {
  113. String username = getUsername();
  114. boolean is16bit = StringUtil.hasMultibyte(username);
  115. out.writeShort(username.length());
  116. out.writeByte(is16bit ? 0x01 : 0x00);
  117. if (is16bit) {
  118. StringUtil.putUnicodeLE(username, out);
  119. } else {
  120. StringUtil.putCompressedUnicode(username, out);
  121. }
  122. int encodedByteCount = 3 + username.length() * (is16bit ? 2 : 1);
  123. int paddingSize = DATA_SIZE - encodedByteCount;
  124. out.write(PADDING, 0, paddingSize);
  125. }
  126. protected int getDataSize() {
  127. return DATA_SIZE;
  128. }
  129. public short getSid() {
  130. return sid;
  131. }
  132. }