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.

XSSFTableStyle.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.usermodel;
  16. import java.util.ArrayList;
  17. import java.util.EnumMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. import org.apache.logging.log4j.LogManager;
  21. import org.apache.logging.log4j.Logger;
  22. import org.apache.poi.ss.usermodel.DifferentialStyleProvider;
  23. import org.apache.poi.ss.usermodel.TableStyle;
  24. import org.apache.poi.ss.usermodel.TableStyleType;
  25. import org.apache.xmlbeans.XmlCursor;
  26. import org.apache.xmlbeans.XmlException;
  27. import org.apache.xmlbeans.XmlObject;
  28. import org.apache.xmlbeans.XmlOptions;
  29. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDxf;
  30. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDxfs;
  31. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyle;
  32. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleElement;
  33. /**
  34. * {@link TableStyle} implementation for styles defined in the OOXML styles.xml.
  35. * Also used for built-in styles via dummy XML generated from presetTableStyles.xml.
  36. */
  37. public class XSSFTableStyle implements TableStyle {
  38. private static final Logger LOG = LogManager.getLogger(XSSFTableStyle.class);
  39. private final String name;
  40. private final int index;
  41. private final Map<TableStyleType, DifferentialStyleProvider> elementMap = new EnumMap<>(TableStyleType.class);
  42. /**
  43. * @param index style definition index or built-in ordinal depending on use
  44. * @param colorMap indexed color map - default or custom
  45. * @see TableStyle#getIndex()
  46. */
  47. public XSSFTableStyle(int index, CTDxfs dxfs, CTTableStyle tableStyle, IndexedColorMap colorMap) {
  48. this.name = tableStyle.getName();
  49. this.index = index;
  50. List<CTDxf> dxfList = new ArrayList<>();
  51. // CT* classes don't handle "mc:AlternateContent" elements, so get the Dxf instances manually
  52. try (XmlCursor cur = dxfs.newCursor()) {
  53. // sometimes there are namespaces sometimes not.
  54. String xquery = "declare namespace x='"+XSSFRelation.NS_SPREADSHEETML+"' .//x:dxf | .//dxf";
  55. cur.selectPath(xquery);
  56. while (cur.toNextSelection()) {
  57. XmlObject obj = cur.getObject();
  58. String parentName = obj.getDomNode().getParentNode().getNodeName();
  59. // ignore alternate content choices, we won't know anything about their namespaces
  60. if (parentName.equals("mc:Fallback") || parentName.equals("x:dxfs") || parentName.contentEquals("dxfs")) {
  61. CTDxf dxf;
  62. try {
  63. if (obj instanceof CTDxf) {
  64. dxf = (CTDxf) obj;
  65. } else {
  66. dxf = CTDxf.Factory.parse(obj.newXMLStreamReader(), new XmlOptions().setDocumentType(CTDxf.type));
  67. }
  68. if (dxf != null) dxfList.add(dxf);
  69. } catch (XmlException e) {
  70. LOG.atWarn().withThrowable(e).log("Error parsing XSSFTableStyle");
  71. }
  72. }
  73. }
  74. }
  75. for (CTTableStyleElement element : tableStyle.getTableStyleElementList()) {
  76. TableStyleType type = TableStyleType.valueOf(element.getType().toString());
  77. DifferentialStyleProvider dstyle = null;
  78. if (element.isSetDxfId()) {
  79. int idx = (int) element.getDxfId();
  80. CTDxf dxf;
  81. dxf = dxfList.get(idx);
  82. int stripeSize = 0;
  83. if (element.isSetSize()) stripeSize = (int) element.getSize();
  84. if (dxf != null) dstyle = new XSSFDxfStyleProvider(dxf, stripeSize, colorMap);
  85. }
  86. elementMap.put(type, dstyle);
  87. }
  88. }
  89. @Override
  90. public String getName() {
  91. return name;
  92. }
  93. @Override
  94. public int getIndex() {
  95. return index;
  96. }
  97. /**
  98. * Always false for these, these are user defined styles
  99. */
  100. @Override
  101. public boolean isBuiltin() {
  102. return false;
  103. }
  104. @Override
  105. public DifferentialStyleProvider getStyle(TableStyleType type) {
  106. return elementMap.get(type);
  107. }
  108. }