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.

XSSFBuiltinTableStyle.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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.io.ByteArrayInputStream;
  17. import java.io.InputStream;
  18. import java.nio.charset.StandardCharsets;
  19. import java.util.EnumMap;
  20. import java.util.Map;
  21. import javax.xml.transform.OutputKeys;
  22. import javax.xml.transform.Transformer;
  23. import javax.xml.transform.TransformerException;
  24. import javax.xml.transform.dom.DOMSource;
  25. import javax.xml.transform.stream.StreamResult;
  26. import org.apache.commons.io.output.StringBuilderWriter;
  27. import org.apache.poi.ooxml.util.DocumentHelper;
  28. import org.apache.poi.ss.usermodel.DifferentialStyleProvider;
  29. import org.apache.poi.ss.usermodel.TableStyle;
  30. import org.apache.poi.ss.usermodel.TableStyleType;
  31. import org.apache.poi.util.XMLHelper;
  32. import org.apache.poi.xssf.model.StylesTable;
  33. import org.w3c.dom.Document;
  34. import org.w3c.dom.Element;
  35. import org.w3c.dom.Node;
  36. import org.w3c.dom.NodeList;
  37. /**
  38. * Table style names defined in the OOXML spec.
  39. * The actual styling is defined in presetTableStyles.xml
  40. */
  41. public enum XSSFBuiltinTableStyle {
  42. /***/
  43. TableStyleDark1,
  44. /***/
  45. TableStyleDark2,
  46. /***/
  47. TableStyleDark3,
  48. /***/
  49. TableStyleDark4,
  50. /***/
  51. TableStyleDark5,
  52. /***/
  53. TableStyleDark6,
  54. /***/
  55. TableStyleDark7,
  56. /***/
  57. TableStyleDark8,
  58. /***/
  59. TableStyleDark9,
  60. /***/
  61. TableStyleDark10,
  62. /***/
  63. TableStyleDark11,
  64. /***/
  65. TableStyleLight1,
  66. /***/
  67. TableStyleLight2,
  68. /***/
  69. TableStyleLight3,
  70. /***/
  71. TableStyleLight4,
  72. /***/
  73. TableStyleLight5,
  74. /***/
  75. TableStyleLight6,
  76. /***/
  77. TableStyleLight7,
  78. /***/
  79. TableStyleLight8,
  80. /***/
  81. TableStyleLight9,
  82. /***/
  83. TableStyleLight10,
  84. /***/
  85. TableStyleLight11,
  86. /***/
  87. TableStyleLight12,
  88. /***/
  89. TableStyleLight13,
  90. /***/
  91. TableStyleLight14,
  92. /***/
  93. TableStyleLight15,
  94. /***/
  95. TableStyleLight16,
  96. /***/
  97. TableStyleLight17,
  98. /***/
  99. TableStyleLight18,
  100. /***/
  101. TableStyleLight19,
  102. /***/
  103. TableStyleLight20,
  104. /***/
  105. TableStyleLight21,
  106. /***/
  107. TableStyleMedium1,
  108. /***/
  109. TableStyleMedium2,
  110. /***/
  111. TableStyleMedium3,
  112. /***/
  113. TableStyleMedium4,
  114. /***/
  115. TableStyleMedium5,
  116. /***/
  117. TableStyleMedium6,
  118. /***/
  119. TableStyleMedium7,
  120. /***/
  121. TableStyleMedium8,
  122. /***/
  123. TableStyleMedium9,
  124. /***/
  125. TableStyleMedium10,
  126. /***/
  127. TableStyleMedium11,
  128. /***/
  129. TableStyleMedium12,
  130. /***/
  131. TableStyleMedium13,
  132. /***/
  133. TableStyleMedium14,
  134. /***/
  135. TableStyleMedium15,
  136. /***/
  137. TableStyleMedium16,
  138. /***/
  139. TableStyleMedium17,
  140. /***/
  141. TableStyleMedium18,
  142. /***/
  143. TableStyleMedium19,
  144. /***/
  145. TableStyleMedium20,
  146. /***/
  147. TableStyleMedium21,
  148. /***/
  149. TableStyleMedium22,
  150. /***/
  151. TableStyleMedium23,
  152. /***/
  153. TableStyleMedium24,
  154. /***/
  155. TableStyleMedium25,
  156. /***/
  157. TableStyleMedium26,
  158. /***/
  159. TableStyleMedium27,
  160. /***/
  161. TableStyleMedium28,
  162. /***/
  163. PivotStyleMedium1,
  164. /***/
  165. PivotStyleMedium2,
  166. /***/
  167. PivotStyleMedium3,
  168. /***/
  169. PivotStyleMedium4,
  170. /***/
  171. PivotStyleMedium5,
  172. /***/
  173. PivotStyleMedium6,
  174. /***/
  175. PivotStyleMedium7,
  176. /***/
  177. PivotStyleMedium8,
  178. /***/
  179. PivotStyleMedium9,
  180. /***/
  181. PivotStyleMedium10,
  182. /***/
  183. PivotStyleMedium11,
  184. /***/
  185. PivotStyleMedium12,
  186. /***/
  187. PivotStyleMedium13,
  188. /***/
  189. PivotStyleMedium14,
  190. /***/
  191. PivotStyleMedium15,
  192. /***/
  193. PivotStyleMedium16,
  194. /***/
  195. PivotStyleMedium17,
  196. /***/
  197. PivotStyleMedium18,
  198. /***/
  199. PivotStyleMedium19,
  200. /***/
  201. PivotStyleMedium20,
  202. /***/
  203. PivotStyleMedium21,
  204. /***/
  205. PivotStyleMedium22,
  206. /***/
  207. PivotStyleMedium23,
  208. /***/
  209. PivotStyleMedium24,
  210. /***/
  211. PivotStyleMedium25,
  212. /***/
  213. PivotStyleMedium26,
  214. /***/
  215. PivotStyleMedium27,
  216. /***/
  217. PivotStyleMedium28,
  218. /***/
  219. PivotStyleLight1,
  220. /***/
  221. PivotStyleLight2,
  222. /***/
  223. PivotStyleLight3,
  224. /***/
  225. PivotStyleLight4,
  226. /***/
  227. PivotStyleLight5,
  228. /***/
  229. PivotStyleLight6,
  230. /***/
  231. PivotStyleLight7,
  232. /***/
  233. PivotStyleLight8,
  234. /***/
  235. PivotStyleLight9,
  236. /***/
  237. PivotStyleLight10,
  238. /***/
  239. PivotStyleLight11,
  240. /***/
  241. PivotStyleLight12,
  242. /***/
  243. PivotStyleLight13,
  244. /***/
  245. PivotStyleLight14,
  246. /***/
  247. PivotStyleLight15,
  248. /***/
  249. PivotStyleLight16,
  250. /***/
  251. PivotStyleLight17,
  252. /***/
  253. PivotStyleLight18,
  254. /***/
  255. PivotStyleLight19,
  256. /***/
  257. PivotStyleLight20,
  258. /***/
  259. PivotStyleLight21,
  260. /***/
  261. PivotStyleLight22,
  262. /***/
  263. PivotStyleLight23,
  264. /***/
  265. PivotStyleLight24,
  266. /***/
  267. PivotStyleLight25,
  268. /***/
  269. PivotStyleLight26,
  270. /***/
  271. PivotStyleLight27,
  272. /***/
  273. PivotStyleLight28,
  274. /***/
  275. PivotStyleDark1,
  276. /***/
  277. PivotStyleDark2,
  278. /***/
  279. PivotStyleDark3,
  280. /***/
  281. PivotStyleDark4,
  282. /***/
  283. PivotStyleDark5,
  284. /***/
  285. PivotStyleDark6,
  286. /***/
  287. PivotStyleDark7,
  288. /***/
  289. PivotStyleDark8,
  290. /***/
  291. PivotStyleDark9,
  292. /***/
  293. PivotStyleDark10,
  294. /***/
  295. PivotStyleDark11,
  296. /***/
  297. PivotStyleDark12,
  298. /***/
  299. PivotStyleDark13,
  300. /***/
  301. PivotStyleDark14,
  302. /***/
  303. PivotStyleDark15,
  304. /***/
  305. PivotStyleDark16,
  306. /***/
  307. PivotStyleDark17,
  308. /***/
  309. PivotStyleDark18,
  310. /***/
  311. PivotStyleDark19,
  312. /***/
  313. PivotStyleDark20,
  314. /***/
  315. PivotStyleDark21,
  316. /***/
  317. PivotStyleDark22,
  318. /***/
  319. PivotStyleDark23,
  320. /***/
  321. PivotStyleDark24,
  322. /***/
  323. PivotStyleDark25,
  324. /***/
  325. PivotStyleDark26,
  326. /***/
  327. PivotStyleDark27,
  328. /***/
  329. PivotStyleDark28,
  330. ;
  331. /**
  332. * Interestingly, this is initialized after the enum instances, so using an {@link EnumMap} works.
  333. */
  334. private static final Map<XSSFBuiltinTableStyle, TableStyle> styleMap = new EnumMap<>(XSSFBuiltinTableStyle.class);
  335. XSSFBuiltinTableStyle() {
  336. }
  337. /**
  338. * @return built-in {@link TableStyle} definition
  339. */
  340. public TableStyle getStyle() {
  341. init();
  342. return styleMap.get(this);
  343. }
  344. /**
  345. * NOTE: only checks by name, not definition.
  346. *
  347. * @return true if the style represents a built-in style, false if it is null or a custom style
  348. */
  349. public static boolean isBuiltinStyle(TableStyle style) {
  350. if (style == null) return false;
  351. try {
  352. XSSFBuiltinTableStyle.valueOf(style.getName());
  353. return true;
  354. } catch (IllegalArgumentException e) {
  355. return false;
  356. }
  357. }
  358. /**
  359. * Only init once - thus the synchronized. Lazy, after creating instances,
  360. * and only when a style is actually needed, to avoid overhead for uses
  361. * that don't need the actual style definitions.
  362. * <p>
  363. * Public so clients can initialize the map on startup rather than lazily
  364. * during evaluation if desired.
  365. */
  366. public static synchronized void init() {
  367. if (!styleMap.isEmpty()) return;
  368. /*
  369. * initialize map. Every built-in has this format:
  370. * <styleName>
  371. * <dxfs>
  372. * <dxf>...</dxf>
  373. * ...
  374. * </dxfs>
  375. * <tableStyles count="1">
  376. * <tableStyle>...</tableStyle>
  377. * </tableStyles>
  378. * </styleName>
  379. */
  380. try (InputStream is = XSSFBuiltinTableStyle.class.getResourceAsStream("presetTableStyles.xml")) {
  381. final Document doc = DocumentHelper.readDocument(is);
  382. final NodeList styleNodes = doc.getDocumentElement().getChildNodes();
  383. for (int i = 0; i < styleNodes.getLength(); i++) {
  384. final Node node = styleNodes.item(i);
  385. if (node.getNodeType() != Node.ELEMENT_NODE) continue; // only care about elements
  386. final Element tag = (Element) node;
  387. String styleName = tag.getTagName();
  388. XSSFBuiltinTableStyle builtIn = XSSFBuiltinTableStyle.valueOf(styleName);
  389. Node dxfsNode = tag.getElementsByTagName("dxfs").item(0);
  390. Node tableStyleNode = tag.getElementsByTagName("tableStyles").item(0);
  391. // hack because I can't figure out how to get XMLBeans to parse a sub-element in a standalone manner
  392. // - build a fake styles.xml file with just this built-in
  393. StylesTable styles = new StylesTable();
  394. styles.readFrom(new ByteArrayInputStream(styleXML(dxfsNode, tableStyleNode).getBytes(StandardCharsets.UTF_8)));
  395. styleMap.put(builtIn, new XSSFBuiltinTypeStyleStyle(builtIn, styles.getExplicitTableStyle(styleName)));
  396. }
  397. } catch (Exception e) {
  398. throw new RuntimeException(e);
  399. }
  400. }
  401. private static String styleXML(Node dxfsNode, Node tableStyleNode) throws TransformerException {
  402. // built-ins doc uses 1-based dxf indexing, Excel uses 0 based.
  403. // add a dummy node to adjust properly.
  404. dxfsNode.insertBefore(dxfsNode.getOwnerDocument().createElement("dxf"), dxfsNode.getFirstChild());
  405. return "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
  406. "<styleSheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" " +
  407. "xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" " +
  408. "xmlns:x14ac=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac\" " +
  409. "xmlns:x16r2=\"http://schemas.microsoft.com/office/spreadsheetml/2015/02/main\" " +
  410. "mc:Ignorable=\"x14ac x16r2\">\n" +
  411. writeToString(dxfsNode) +
  412. writeToString(tableStyleNode) +
  413. "</styleSheet>";
  414. }
  415. private static String writeToString(Node node) throws TransformerException {
  416. try (StringBuilderWriter sw = new StringBuilderWriter(1024)){
  417. Transformer transformer = XMLHelper.newTransformer();
  418. transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
  419. transformer.transform(new DOMSource(node), new StreamResult(sw));
  420. return sw.toString();
  421. }
  422. }
  423. /**
  424. * implementation for built-in styles
  425. */
  426. protected static class XSSFBuiltinTypeStyleStyle implements TableStyle {
  427. private final XSSFBuiltinTableStyle builtIn;
  428. private final TableStyle style;
  429. protected XSSFBuiltinTypeStyleStyle(XSSFBuiltinTableStyle builtIn, TableStyle style) {
  430. this.builtIn = builtIn;
  431. this.style = style;
  432. }
  433. @Override
  434. public String getName() {
  435. return style.getName();
  436. }
  437. @Override
  438. public int getIndex() {
  439. return builtIn.ordinal();
  440. }
  441. @Override
  442. public boolean isBuiltin() {
  443. return true;
  444. }
  445. @Override
  446. public DifferentialStyleProvider getStyle(TableStyleType type) {
  447. return style.getStyle(type);
  448. }
  449. }
  450. }