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 4.8KB

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