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.

ProtectRecord.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.Map;
  17. import java.util.function.Supplier;
  18. import org.apache.poi.util.BitField;
  19. import org.apache.poi.util.BitFieldFactory;
  20. import org.apache.poi.util.GenericRecordUtil;
  21. import org.apache.poi.util.LittleEndianOutput;
  22. /**
  23. * Defines whether a sheet or workbook is protected (HSSF DOES NOT SUPPORT ENCRYPTION)<p>
  24. * HSSF now supports the simple "protected" sheets (where they are not encrypted and open office et al
  25. * ignore the password record entirely).
  26. */
  27. public final class ProtectRecord extends StandardRecord {
  28. public static final short sid = 0x0012;
  29. private static final BitField protectFlag = BitFieldFactory.getInstance(0x0001);
  30. private int _options;
  31. private ProtectRecord(int options) {
  32. _options = options;
  33. }
  34. private ProtectRecord(ProtectRecord other) {
  35. super(other);
  36. _options = other._options;
  37. }
  38. public ProtectRecord(boolean isProtected) {
  39. this(0);
  40. setProtect(isProtected);
  41. }
  42. public ProtectRecord(RecordInputStream in) {
  43. this(in.readShort());
  44. }
  45. /**
  46. * set whether the sheet is protected or not
  47. * @param protect whether to protect the sheet or not
  48. */
  49. public void setProtect(boolean protect) {
  50. _options = protectFlag.setBoolean(_options, protect);
  51. }
  52. /**
  53. * get whether the sheet is protected or not
  54. * @return whether to protect the sheet or not
  55. */
  56. public boolean getProtect() {
  57. return protectFlag.isSet(_options);
  58. }
  59. public void serialize(LittleEndianOutput out) {
  60. out.writeShort(_options);
  61. }
  62. protected int getDataSize() {
  63. return 2;
  64. }
  65. public short getSid() {
  66. return sid;
  67. }
  68. @Override
  69. public ProtectRecord copy() {
  70. return new ProtectRecord(this);
  71. }
  72. @Override
  73. public HSSFRecordTypes getGenericRecordType() {
  74. return HSSFRecordTypes.PROTECT;
  75. }
  76. @Override
  77. public Map<String, Supplier<?>> getGenericProperties() {
  78. return GenericRecordUtil.getGenericProperties(
  79. "options", () -> _options,
  80. "protect", this::getProtect
  81. );
  82. }
  83. }