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.

XSSFBStylesTable.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.List;
  20. import java.util.SortedMap;
  21. import java.util.TreeMap;
  22. import org.apache.poi.POIXMLException;
  23. import org.apache.poi.ss.usermodel.BuiltinFormats;
  24. import org.apache.poi.util.Internal;
  25. @Internal
  26. public class XSSFBStylesTable extends XSSFBParser {
  27. private final SortedMap<Short, String> numberFormats = new TreeMap<Short,String>();
  28. private final List<Short> styleIds = new ArrayList<Short>();
  29. private boolean inCellXFS = false;
  30. private boolean inFmts = false;
  31. public XSSFBStylesTable(InputStream is) throws IOException {
  32. super(is);
  33. parse();
  34. }
  35. String getNumberFormatString(int idx) {
  36. if (numberFormats.containsKey(styleIds.get((short)idx))) {
  37. return numberFormats.get(styleIds.get((short)idx));
  38. }
  39. return BuiltinFormats.getBuiltinFormat(styleIds.get((short)idx));
  40. }
  41. @Override
  42. public void handleRecord(int recordType, byte[] data) throws XSSFBParseException {
  43. XSSFBRecordType type = XSSFBRecordType.lookup(recordType);
  44. switch (type) {
  45. case BrtBeginCellXFs:
  46. inCellXFS = true;
  47. break;
  48. case BrtEndCellXFs:
  49. inCellXFS = false;
  50. break;
  51. case BrtXf:
  52. if (inCellXFS) {
  53. handleBrtXFInCellXF(data);
  54. }
  55. break;
  56. case BrtBeginFmts:
  57. inFmts = true;
  58. break;
  59. case BrtEndFmts:
  60. inFmts = false;
  61. break;
  62. case BrtFmt:
  63. if (inFmts) {
  64. handleFormat(data);
  65. }
  66. break;
  67. }
  68. }
  69. private void handleFormat(byte[] data) {
  70. int ifmt = data[0] & 0xFF;
  71. if (ifmt > Short.MAX_VALUE) {
  72. throw new POIXMLException("Format id must be a short");
  73. }
  74. StringBuilder sb = new StringBuilder();
  75. XSSFBUtils.readXLWideString(data, 2, sb);
  76. String fmt = sb.toString();
  77. numberFormats.put((short)ifmt, fmt);
  78. }
  79. private void handleBrtXFInCellXF(byte[] data) {
  80. int ifmtOffset = 2;
  81. //int ifmtLength = 2;
  82. //numFmtId in xml terms
  83. int ifmt = data[ifmtOffset] & 0xFF;//the second byte is ignored
  84. styleIds.add((short)ifmt);
  85. }
  86. }