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.

CellRangeAddressList.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.ss.util;
  16. import java.io.IOException;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.function.Supplier;
  21. import org.apache.poi.common.usermodel.GenericRecord;
  22. import org.apache.poi.hssf.record.RecordInputStream;
  23. import org.apache.poi.util.LittleEndianByteArrayOutputStream;
  24. import org.apache.poi.util.LittleEndianOutput;
  25. /**
  26. * Implementation of the cell range address lists,like is described
  27. * in OpenOffice.org's Excel Documentation: excelfileformat.pdf sec 2.5.14 -
  28. * 'Cell Range Address List'
  29. *
  30. * In BIFF8 there is a common way to store absolute cell range address lists in
  31. * several records (not formulas). A cell range address list consists of a field
  32. * with the number of ranges and the list of the range addresses. Each cell
  33. * range address (called an ADDR structure) contains 4 16-bit-values.
  34. * </p>
  35. */
  36. public class CellRangeAddressList implements GenericRecord {
  37. /**
  38. * List of <tt>CellRangeAddress</tt>es. Each structure represents a cell range
  39. */
  40. protected final List<CellRangeAddress> _list = new ArrayList<>();
  41. public CellRangeAddressList() {
  42. }
  43. /**
  44. * Convenience constructor for creating a <tt>CellRangeAddressList</tt> with a single
  45. * <tt>CellRangeAddress</tt>. Other <tt>CellRangeAddress</tt>es may be added later.
  46. */
  47. public CellRangeAddressList(int firstRow, int lastRow, int firstCol, int lastCol) {
  48. addCellRangeAddress(firstRow, firstCol, lastRow, lastCol);
  49. }
  50. /**
  51. * @param in the RecordInputstream to read the record from
  52. */
  53. public CellRangeAddressList(RecordInputStream in) {
  54. int nItems = in.readUShort();
  55. for (int k = 0; k < nItems; k++) {
  56. _list.add(new CellRangeAddress(in));
  57. }
  58. }
  59. /**
  60. * Get the number of following ADDR structures. The number of this
  61. * structures is automatically set when reading an Excel file and/or
  62. * increased when you manually add a new ADDR structure . This is the reason
  63. * there isn't a set method for this field .
  64. *
  65. * @return number of ADDR structures
  66. */
  67. public int countRanges() {
  68. return _list.size();
  69. }
  70. /**
  71. * Add a cell range structure.
  72. *
  73. * @param firstRow - the upper left hand corner's row
  74. * @param firstCol - the upper left hand corner's col
  75. * @param lastRow - the lower right hand corner's row
  76. * @param lastCol - the lower right hand corner's col
  77. */
  78. public void addCellRangeAddress(int firstRow, int firstCol, int lastRow, int lastCol) {
  79. CellRangeAddress region = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
  80. addCellRangeAddress(region);
  81. }
  82. public void addCellRangeAddress(CellRangeAddress cra) {
  83. _list.add(cra);
  84. }
  85. public CellRangeAddress remove(int rangeIndex) {
  86. if (_list.isEmpty()) {
  87. throw new RuntimeException("List is empty");
  88. }
  89. if (rangeIndex < 0 || rangeIndex >= _list.size()) {
  90. throw new RuntimeException("Range index (" + rangeIndex
  91. + ") is outside allowable range (0.." + (_list.size()-1) + ")");
  92. }
  93. return _list.remove(rangeIndex);
  94. }
  95. /**
  96. * @return <tt>CellRangeAddress</tt> at the given index
  97. */
  98. public CellRangeAddress getCellRangeAddress(int index) {
  99. return _list.get(index);
  100. }
  101. public int getSize() {
  102. return getEncodedSize(_list.size());
  103. }
  104. /**
  105. * @return the total size of for the specified number of ranges,
  106. * including the initial 2 byte range count
  107. */
  108. public static int getEncodedSize(int numberOfRanges) {
  109. return 2 + CellRangeAddress.getEncodedSize(numberOfRanges);
  110. }
  111. public int serialize(int offset, byte[] data) {
  112. int totalSize = getSize();
  113. try (LittleEndianByteArrayOutputStream lebaos =
  114. new LittleEndianByteArrayOutputStream(data, offset, totalSize)) {
  115. serialize(lebaos);
  116. }
  117. catch (IOException ioe) {
  118. // should never happen in practice
  119. throw new IllegalStateException(ioe);
  120. }
  121. return totalSize;
  122. }
  123. public void serialize(LittleEndianOutput out) {
  124. int nItems = _list.size();
  125. out.writeShort(nItems);
  126. for (CellRangeAddress region : _list) {
  127. region.serialize(out);
  128. }
  129. }
  130. public CellRangeAddressList copy() {
  131. CellRangeAddressList result = new CellRangeAddressList();
  132. for (CellRangeAddress region : _list) {
  133. result.addCellRangeAddress(region.copy());
  134. }
  135. return result;
  136. }
  137. public CellRangeAddress[] getCellRangeAddresses() {
  138. CellRangeAddress[] result = new CellRangeAddress[_list.size()];
  139. _list.toArray(result);
  140. return result;
  141. }
  142. @Override
  143. public Map<String, Supplier<?>> getGenericProperties() {
  144. return null;
  145. }
  146. @Override
  147. public List<CellRangeAddress> getGenericChildren() {
  148. return _list;
  149. }
  150. }