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

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