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.

XSSFBCommentsTable.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.xssf.binary;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.util.ArrayList;
  19. import java.util.Comparator;
  20. import java.util.LinkedList;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Queue;
  24. import java.util.TreeMap;
  25. import org.apache.poi.ss.util.CellAddress;
  26. import org.apache.poi.util.Internal;
  27. import org.apache.poi.util.LittleEndian;
  28. @Internal
  29. public class XSSFBCommentsTable extends XSSFBParser {
  30. private Map<CellAddress, XSSFBComment> comments = new TreeMap<CellAddress, XSSFBComment>(new CellAddressComparator());//String is the cellAddress A1
  31. private Queue<CellAddress> commentAddresses = new LinkedList<CellAddress>();
  32. private List<String> authors = new ArrayList<String>();
  33. //these are all used only during parsing, and they are mutable!
  34. private int authorId = -1;
  35. private CellAddress cellAddress = null;
  36. private XSSFBCellRange cellRange = null;
  37. private String comment = null;
  38. private StringBuilder authorBuffer = new StringBuilder();
  39. public XSSFBCommentsTable(InputStream is) throws IOException {
  40. super(is);
  41. parse();
  42. commentAddresses.addAll(comments.keySet());
  43. }
  44. @Override
  45. public void handleRecord(int id, byte[] data) throws XSSFBParseException {
  46. XSSFBRecordType recordType = XSSFBRecordType.lookup(id);
  47. switch (recordType) {
  48. case BrtBeginComment:
  49. int offset = 0;
  50. authorId = XSSFBUtils.castToInt(LittleEndian.getUInt(data)); offset += LittleEndian.INT_SIZE;
  51. cellRange = XSSFBCellRange.parse(data, offset, cellRange);
  52. offset+= XSSFBCellRange.length;
  53. //for strict parsing; confirm that firstRow==lastRow and firstCol==colLats (2.4.28)
  54. cellAddress = new CellAddress(cellRange.firstRow, cellRange.firstCol);
  55. break;
  56. case BrtCommentText:
  57. XSSFBRichStr xssfbRichStr = XSSFBRichStr.build(data, 0);
  58. comment = xssfbRichStr.getString();
  59. break;
  60. case BrtEndComment:
  61. comments.put(cellAddress, new XSSFBComment(cellAddress, authors.get(authorId), comment));
  62. authorId = -1;
  63. cellAddress = null;
  64. break;
  65. case BrtCommentAuthor:
  66. authorBuffer.setLength(0);
  67. XSSFBUtils.readXLWideString(data, 0, authorBuffer);
  68. authors.add(authorBuffer.toString());
  69. break;
  70. }
  71. }
  72. public Queue<CellAddress> getAddresses() {
  73. return commentAddresses;
  74. }
  75. public XSSFBComment get(CellAddress cellAddress) {
  76. if (cellAddress == null) {
  77. return null;
  78. }
  79. return comments.get(cellAddress);
  80. }
  81. private final static class CellAddressComparator implements Comparator<CellAddress> {
  82. @Override
  83. public int compare(CellAddress o1, CellAddress o2) {
  84. if (o1.getRow() < o2.getRow()) {
  85. return -1;
  86. } else if (o1.getRow() > o2.getRow()) {
  87. return 1;
  88. }
  89. if (o1.getColumn() < o2.getColumn()) {
  90. return -1;
  91. } else if (o1.getColumn() > o2.getColumn()) {
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. }
  97. }