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.

WorksheetProtectionBlock.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.aggregates;
  16. import org.apache.poi.hssf.model.RecordStream;
  17. import org.apache.poi.hssf.record.ObjectProtectRecord;
  18. import org.apache.poi.hssf.record.PasswordRecord;
  19. import org.apache.poi.hssf.record.ProtectRecord;
  20. import org.apache.poi.hssf.record.Record;
  21. import org.apache.poi.hssf.record.RecordFormatException;
  22. import org.apache.poi.hssf.record.ScenarioProtectRecord;
  23. import org.apache.poi.poifs.crypt.CryptoFunctions;
  24. /**
  25. * Groups the sheet protection records for a worksheet.
  26. * <p/>
  27. *
  28. * See OOO excelfileformat.pdf sec 4.18.2 'Sheet Protection in a Workbook
  29. * (BIFF5-BIFF8)'
  30. *
  31. * @author Josh Micich
  32. */
  33. public final class WorksheetProtectionBlock extends RecordAggregate {
  34. // Every one of these component records is optional
  35. // (The whole WorksheetProtectionBlock may not be present)
  36. private ProtectRecord _protectRecord;
  37. private ObjectProtectRecord _objectProtectRecord;
  38. private ScenarioProtectRecord _scenarioProtectRecord;
  39. private PasswordRecord _passwordRecord;
  40. /**
  41. * Creates an empty WorksheetProtectionBlock
  42. */
  43. public WorksheetProtectionBlock() {
  44. // all fields empty
  45. }
  46. /**
  47. * @return <code>true</code> if the specified Record sid is one belonging to
  48. * the 'Page Settings Block'.
  49. */
  50. public static boolean isComponentRecord(int sid) {
  51. switch (sid) {
  52. case ProtectRecord.sid:
  53. case ObjectProtectRecord.sid:
  54. case ScenarioProtectRecord.sid:
  55. case PasswordRecord.sid:
  56. return true;
  57. }
  58. return false;
  59. }
  60. private boolean readARecord(RecordStream rs) {
  61. switch (rs.peekNextSid()) {
  62. case ProtectRecord.sid:
  63. checkNotPresent(_protectRecord);
  64. _protectRecord = (ProtectRecord) rs.getNext();
  65. break;
  66. case ObjectProtectRecord.sid:
  67. checkNotPresent(_objectProtectRecord);
  68. _objectProtectRecord = (ObjectProtectRecord) rs.getNext();
  69. break;
  70. case ScenarioProtectRecord.sid:
  71. checkNotPresent(_scenarioProtectRecord);
  72. _scenarioProtectRecord = (ScenarioProtectRecord) rs.getNext();
  73. break;
  74. case PasswordRecord.sid:
  75. checkNotPresent(_passwordRecord);
  76. _passwordRecord = (PasswordRecord) rs.getNext();
  77. break;
  78. default:
  79. // all other record types are not part of the PageSettingsBlock
  80. return false;
  81. }
  82. return true;
  83. }
  84. private void checkNotPresent(Record rec) {
  85. if (rec != null) {
  86. throw new RecordFormatException("Duplicate PageSettingsBlock record (sid=0x"
  87. + Integer.toHexString(rec.getSid()) + ")");
  88. }
  89. }
  90. public void visitContainedRecords(RecordVisitor rv) {
  91. // Replicates record order from Excel 2007, though this is not critical
  92. visitIfPresent(_protectRecord, rv);
  93. visitIfPresent(_objectProtectRecord, rv);
  94. visitIfPresent(_scenarioProtectRecord, rv);
  95. visitIfPresent(_passwordRecord, rv);
  96. }
  97. private static void visitIfPresent(Record r, RecordVisitor rv) {
  98. if (r != null) {
  99. rv.visitRecord(r);
  100. }
  101. }
  102. public PasswordRecord getPasswordRecord() {
  103. return _passwordRecord;
  104. }
  105. public ScenarioProtectRecord getHCenter() {
  106. return _scenarioProtectRecord;
  107. }
  108. /**
  109. * This method reads {@link WorksheetProtectionBlock} records from the supplied RecordStream
  110. * until the first non-WorksheetProtectionBlock record is encountered. As each record is read,
  111. * it is incorporated into this WorksheetProtectionBlock.
  112. * <p/>
  113. * As per the OOO documentation, the protection block records can be expected to be written
  114. * together (with no intervening records), but earlier versions of POI (prior to Jun 2009)
  115. * didn't do this. Workbooks with sheet protection created by those earlier POI versions
  116. * seemed to be valid (Excel opens them OK). So PO allows continues to support reading of files
  117. * with non continuous worksheet protection blocks.
  118. *
  119. * <p/>
  120. * <b>Note</b> - when POI writes out this WorksheetProtectionBlock, the records will always be
  121. * written in one consolidated block (in the standard ordering) regardless of how scattered the
  122. * records were when they were originally read.
  123. */
  124. public void addRecords(RecordStream rs) {
  125. while (true) {
  126. if (!readARecord(rs)) {
  127. break;
  128. }
  129. }
  130. }
  131. /**
  132. * @return the ProtectRecord. If one is not contained in the sheet, then one
  133. * is created.
  134. */
  135. private ProtectRecord getProtect() {
  136. if (_protectRecord == null) {
  137. _protectRecord = new ProtectRecord(false);
  138. }
  139. return _protectRecord;
  140. }
  141. /**
  142. * @return the PasswordRecord. If one is not contained in the sheet, then
  143. * one is created.
  144. */
  145. private PasswordRecord getPassword() {
  146. if (_passwordRecord == null) {
  147. _passwordRecord = createPassword();
  148. }
  149. return _passwordRecord;
  150. }
  151. /**
  152. * protect a spreadsheet with a password (not encrypted, just sets protect
  153. * flags and the password.
  154. *
  155. * @param password to set. Pass <code>null</code> to remove all protection
  156. * @param shouldProtectObjects are protected
  157. * @param shouldProtectScenarios are protected
  158. */
  159. public void protectSheet(String password, boolean shouldProtectObjects,
  160. boolean shouldProtectScenarios) {
  161. if (password == null) {
  162. _passwordRecord = null;
  163. _protectRecord = null;
  164. _objectProtectRecord = null;
  165. _scenarioProtectRecord = null;
  166. return;
  167. }
  168. ProtectRecord prec = getProtect();
  169. PasswordRecord pass = getPassword();
  170. prec.setProtect(true);
  171. pass.setPassword((short)CryptoFunctions.createXorVerifier1(password));
  172. if (_objectProtectRecord == null && shouldProtectObjects) {
  173. ObjectProtectRecord rec = createObjectProtect();
  174. rec.setProtect(true);
  175. _objectProtectRecord = rec;
  176. }
  177. if (_scenarioProtectRecord == null && shouldProtectScenarios) {
  178. ScenarioProtectRecord srec = createScenarioProtect();
  179. srec.setProtect(true);
  180. _scenarioProtectRecord = srec;
  181. }
  182. }
  183. public boolean isSheetProtected() {
  184. return _protectRecord != null && _protectRecord.getProtect();
  185. }
  186. public boolean isObjectProtected() {
  187. return _objectProtectRecord != null && _objectProtectRecord.getProtect();
  188. }
  189. public boolean isScenarioProtected() {
  190. return _scenarioProtectRecord != null && _scenarioProtectRecord.getProtect();
  191. }
  192. /**
  193. * creates an ObjectProtect record with protect set to false.
  194. */
  195. private static ObjectProtectRecord createObjectProtect() {
  196. ObjectProtectRecord retval = new ObjectProtectRecord();
  197. retval.setProtect(false);
  198. return retval;
  199. }
  200. /**
  201. * creates a ScenarioProtect record with protect set to false.
  202. */
  203. private static ScenarioProtectRecord createScenarioProtect() {
  204. ScenarioProtectRecord retval = new ScenarioProtectRecord();
  205. retval.setProtect(false);
  206. return retval;
  207. }
  208. /**
  209. * creates a Password record with password set to 0x0000.
  210. */
  211. private static PasswordRecord createPassword() {
  212. return new PasswordRecord(0x0000);
  213. }
  214. public int getPasswordHash() {
  215. if (_passwordRecord == null) {
  216. return 0;
  217. }
  218. return _passwordRecord.getPassword();
  219. }
  220. }