Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

XSSFBuiltinTableStyle.java 12KB

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