Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

BoolErrRecord.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 org.apache.poi.ss.usermodel.ErrorConstants;
  17. import org.apache.poi.util.HexDump;
  18. import org.apache.poi.util.LittleEndianOutput;
  19. /**
  20. * Creates new BoolErrRecord. (0x0205) <P>
  21. * REFERENCE: PG ??? Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
  22. * @author Michael P. Harhen
  23. * @author Jason Height (jheight at chariot dot net dot au)
  24. */
  25. public final class BoolErrRecord extends CellRecord {
  26. public final static short sid = 0x0205;
  27. private int _value;
  28. /**
  29. * If <code>true</code>, this record represents an error cell value, otherwise this record represents a boolean cell value
  30. */
  31. private boolean _isError;
  32. /** Creates new BoolErrRecord */
  33. public BoolErrRecord() {
  34. // fields uninitialised
  35. }
  36. /**
  37. * @param in the RecordInputstream to read the record from
  38. */
  39. public BoolErrRecord(RecordInputStream in) {
  40. super(in);
  41. switch (in.remaining()) {
  42. case 2:
  43. _value = in.readByte();
  44. break;
  45. case 3:
  46. _value = in.readUShort();
  47. break;
  48. default:
  49. throw new RecordFormatException("Unexpected size ("
  50. + in.remaining() + ") for BOOLERR record.");
  51. }
  52. int flag = in.readUByte();
  53. switch (flag) {
  54. case 0:
  55. _isError = false;
  56. break;
  57. case 1:
  58. _isError = true;
  59. break;
  60. default:
  61. throw new RecordFormatException("Unexpected isError flag ("
  62. + flag + ") for BOOLERR record.");
  63. }
  64. }
  65. /**
  66. * set the boolean value for the cell
  67. *
  68. * @param value representing the boolean value
  69. */
  70. public void setValue(boolean value) {
  71. _value = value ? 1 : 0;
  72. _isError = false;
  73. }
  74. /**
  75. * set the error value for the cell
  76. *
  77. * @param value error representing the error value
  78. * this value can only be 0,7,15,23,29,36 or 42
  79. * see bugzilla bug 16560 for an explanation
  80. */
  81. public void setValue(byte value) {
  82. switch(value) {
  83. case ErrorConstants.ERROR_NULL:
  84. case ErrorConstants.ERROR_DIV_0:
  85. case ErrorConstants.ERROR_VALUE:
  86. case ErrorConstants.ERROR_REF:
  87. case ErrorConstants.ERROR_NAME:
  88. case ErrorConstants.ERROR_NUM:
  89. case ErrorConstants.ERROR_NA:
  90. _value = value;
  91. _isError = true;
  92. return;
  93. }
  94. throw new IllegalArgumentException("Error Value can only be 0,7,15,23,29,36 or 42. It cannot be "+value);
  95. }
  96. /**
  97. * get the value for the cell
  98. *
  99. * @return boolean representing the boolean value
  100. */
  101. public boolean getBooleanValue() {
  102. return _value != 0;
  103. }
  104. /**
  105. * get the error value for the cell
  106. *
  107. * @return byte representing the error value
  108. */
  109. public byte getErrorValue() {
  110. return (byte)_value;
  111. }
  112. /**
  113. * Indicates whether the call holds a boolean value
  114. *
  115. * @return boolean true if the cell holds a boolean value
  116. */
  117. public boolean isBoolean() {
  118. return !_isError;
  119. }
  120. /**
  121. * Indicates whether the call holds an error value
  122. *
  123. * @return boolean true if the cell holds an error value
  124. */
  125. public boolean isError() {
  126. return _isError;
  127. }
  128. @Override
  129. protected String getRecordName() {
  130. return "BOOLERR";
  131. }
  132. @Override
  133. protected void appendValueText(StringBuilder sb) {
  134. if (isBoolean()) {
  135. sb.append(" .boolVal = ");
  136. sb.append(getBooleanValue());
  137. } else {
  138. sb.append(" .errCode = ");
  139. sb.append(ErrorConstants.getText(getErrorValue()));
  140. sb.append(" (").append(HexDump.byteToHex(getErrorValue())).append(")");
  141. }
  142. }
  143. @Override
  144. protected void serializeValue(LittleEndianOutput out) {
  145. out.writeByte(_value);
  146. out.writeByte(_isError ? 1 : 0);
  147. }
  148. @Override
  149. protected int getValueDataSize() {
  150. return 2;
  151. }
  152. public short getSid() {
  153. return sid;
  154. }
  155. public Object clone() {
  156. BoolErrRecord rec = new BoolErrRecord();
  157. copyBaseFields(rec);
  158. rec._value = _value;
  159. rec._isError = _isError;
  160. return rec;
  161. }
  162. }