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

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