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.

StylesTable.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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.model;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.OutputStream;
  19. import java.util.*;
  20. import java.util.Map.Entry;
  21. import org.apache.poi.ss.usermodel.FontFamily;
  22. import org.apache.poi.ss.usermodel.FontScheme;
  23. import org.apache.poi.ss.usermodel.BuiltinFormats;
  24. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  25. import org.apache.poi.xssf.usermodel.XSSFFont;
  26. import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
  27. import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
  28. import org.apache.poi.POIXMLDocumentPart;
  29. import org.apache.xmlbeans.XmlException;
  30. import org.apache.xmlbeans.XmlOptions;
  31. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
  32. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorders;
  33. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellStyleXfs;
  34. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellXfs;
  35. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDxf;
  36. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDxfs;
  37. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
  38. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFills;
  39. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
  40. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFonts;
  41. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTNumFmt;
  42. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTNumFmts;
  43. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
  44. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
  45. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
  46. import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;
  47. import org.apache.poi.openxml4j.opc.PackagePart;
  48. import org.apache.poi.openxml4j.opc.PackageRelationship;
  49. /**
  50. * Table of styles shared across all sheets in a workbook.
  51. *
  52. * @author ugo
  53. */
  54. public class StylesTable extends POIXMLDocumentPart {
  55. private final Map<Integer, String> numberFormats = new LinkedHashMap<Integer,String>();
  56. private final List<XSSFFont> fonts = new ArrayList<XSSFFont>();
  57. private final List<XSSFCellFill> fills = new ArrayList<XSSFCellFill>();
  58. private final List<XSSFCellBorder> borders = new ArrayList<XSSFCellBorder>();
  59. private final List<CTXf> styleXfs = new ArrayList<CTXf>();
  60. private final List<CTXf> xfs = new ArrayList<CTXf>();
  61. private final List<CTDxf> dxfs = new ArrayList<CTDxf>();
  62. /**
  63. * The first style id available for use as a custom style
  64. */
  65. public static final int FIRST_CUSTOM_STYLE_ID = BuiltinFormats.FIRST_USER_DEFINED_FORMAT_INDEX + 1;
  66. private StyleSheetDocument doc;
  67. private ThemesTable theme;
  68. /**
  69. * Create a new, empty StylesTable
  70. */
  71. public StylesTable() {
  72. super();
  73. doc = StyleSheetDocument.Factory.newInstance();
  74. doc.addNewStyleSheet();
  75. // Initialization required in order to make the document readable by MSExcel
  76. initialize();
  77. }
  78. public StylesTable(PackagePart part, PackageRelationship rel) throws IOException {
  79. super(part, rel);
  80. readFrom(part.getInputStream());
  81. }
  82. public ThemesTable getTheme() {
  83. return theme;
  84. }
  85. public void setTheme(ThemesTable theme) {
  86. this.theme = theme;
  87. // Pass the themes table along to things which need to
  88. // know about it, but have already been created by now
  89. for(XSSFFont font : fonts) {
  90. font.setThemesTable(theme);
  91. }
  92. for(XSSFCellBorder border : borders) {
  93. border.setThemesTable(theme);
  94. }
  95. }
  96. /**
  97. * Read this shared styles table from an XML file.
  98. *
  99. * @param is The input stream containing the XML document.
  100. * @throws IOException if an error occurs while reading.
  101. */
  102. @SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support
  103. protected void readFrom(InputStream is) throws IOException {
  104. try {
  105. doc = StyleSheetDocument.Factory.parse(is);
  106. CTStylesheet styleSheet = doc.getStyleSheet();
  107. // Grab all the different bits we care about
  108. CTNumFmts ctfmts = styleSheet.getNumFmts();
  109. if( ctfmts != null){
  110. for (CTNumFmt nfmt : ctfmts.getNumFmtArray()) {
  111. numberFormats.put((int)nfmt.getNumFmtId(), nfmt.getFormatCode());
  112. }
  113. }
  114. CTFonts ctfonts = styleSheet.getFonts();
  115. if(ctfonts != null){
  116. int idx = 0;
  117. for (CTFont font : ctfonts.getFontArray()) {
  118. // Create the font and save it. Themes Table supplied later
  119. XSSFFont f = new XSSFFont(font, idx);
  120. fonts.add(f);
  121. idx++;
  122. }
  123. }
  124. CTFills ctfills = styleSheet.getFills();
  125. if(ctfills != null){
  126. for (CTFill fill : ctfills.getFillArray()) {
  127. fills.add(new XSSFCellFill(fill));
  128. }
  129. }
  130. CTBorders ctborders = styleSheet.getBorders();
  131. if(ctborders != null) {
  132. for (CTBorder border : ctborders.getBorderArray()) {
  133. borders.add(new XSSFCellBorder(border));
  134. }
  135. }
  136. CTCellXfs cellXfs = styleSheet.getCellXfs();
  137. if(cellXfs != null) xfs.addAll(Arrays.asList(cellXfs.getXfArray()));
  138. CTCellStyleXfs cellStyleXfs = styleSheet.getCellStyleXfs();
  139. if(cellStyleXfs != null) styleXfs.addAll(Arrays.asList(cellStyleXfs.getXfArray()));
  140. CTDxfs styleDxfs = styleSheet.getDxfs();
  141. if(styleDxfs != null) dxfs.addAll(Arrays.asList(styleDxfs.getDxfArray()));
  142. } catch (XmlException e) {
  143. throw new IOException(e.getLocalizedMessage());
  144. }
  145. }
  146. // ===========================================================
  147. // Start of style related getters and setters
  148. // ===========================================================
  149. public String getNumberFormatAt(int idx) {
  150. return numberFormats.get(idx);
  151. }
  152. public int putNumberFormat(String fmt) {
  153. if (numberFormats.containsValue(fmt)) {
  154. // Find the key, and return that
  155. for(Integer key : numberFormats.keySet() ) {
  156. if(numberFormats.get(key).equals(fmt)) {
  157. return key;
  158. }
  159. }
  160. throw new IllegalStateException("Found the format, but couldn't figure out where - should never happen!");
  161. }
  162. // Find a spare key, and add that
  163. int newKey = FIRST_CUSTOM_STYLE_ID;
  164. while(numberFormats.containsKey(newKey)) {
  165. newKey++;
  166. }
  167. numberFormats.put(newKey, fmt);
  168. return newKey;
  169. }
  170. public XSSFFont getFontAt(int idx) {
  171. return fonts.get(idx);
  172. }
  173. /**
  174. * Records the given font in the font table.
  175. * Will re-use an existing font index if this
  176. * font matches another, EXCEPT if forced
  177. * registration is requested.
  178. * This allows people to create several fonts
  179. * then customise them later.
  180. * Note - End Users probably want to call
  181. * {@link XSSFFont#registerTo(StylesTable)}
  182. */
  183. public int putFont(XSSFFont font, boolean forceRegistration) {
  184. int idx = -1;
  185. if(!forceRegistration) {
  186. idx = fonts.indexOf(font);
  187. }
  188. if (idx != -1) {
  189. return idx;
  190. }
  191. idx = fonts.size();
  192. fonts.add(font);
  193. return idx;
  194. }
  195. public int putFont(XSSFFont font) {
  196. return putFont(font, false);
  197. }
  198. public XSSFCellStyle getStyleAt(int idx) {
  199. int styleXfId = 0;
  200. // 0 is the empty default
  201. if(xfs.get(idx).getXfId() > 0) {
  202. styleXfId = (int) xfs.get(idx).getXfId();
  203. }
  204. return new XSSFCellStyle(idx, styleXfId, this, theme);
  205. }
  206. public int putStyle(XSSFCellStyle style) {
  207. CTXf mainXF = style.getCoreXf();
  208. if(! xfs.contains(mainXF)) {
  209. xfs.add(mainXF);
  210. }
  211. return xfs.indexOf(mainXF);
  212. }
  213. public XSSFCellBorder getBorderAt(int idx) {
  214. return borders.get(idx);
  215. }
  216. public int putBorder(XSSFCellBorder border) {
  217. int idx = borders.indexOf(border);
  218. if (idx != -1) {
  219. return idx;
  220. }
  221. borders.add(border);
  222. border.setThemesTable(theme);
  223. return borders.size() - 1;
  224. }
  225. public XSSFCellFill getFillAt(int idx) {
  226. return fills.get(idx);
  227. }
  228. public List<XSSFCellBorder> getBorders(){
  229. return borders;
  230. }
  231. public List<XSSFCellFill> getFills(){
  232. return fills;
  233. }
  234. public List<XSSFFont> getFonts(){
  235. return fonts;
  236. }
  237. public Map<Integer, String> getNumberFormats(){
  238. return numberFormats;
  239. }
  240. public int putFill(XSSFCellFill fill) {
  241. int idx = fills.indexOf(fill);
  242. if (idx != -1) {
  243. return idx;
  244. }
  245. fills.add(fill);
  246. return fills.size() - 1;
  247. }
  248. public CTXf getCellXfAt(int idx) {
  249. return xfs.get(idx);
  250. }
  251. public int putCellXf(CTXf cellXf) {
  252. xfs.add(cellXf);
  253. return xfs.size();
  254. }
  255. public CTXf getCellStyleXfAt(int idx) {
  256. return styleXfs.get(idx);
  257. }
  258. public int putCellStyleXf(CTXf cellStyleXf) {
  259. styleXfs.add(cellStyleXf);
  260. return styleXfs.size();
  261. }
  262. /**
  263. * get the size of cell styles
  264. */
  265. public int getNumCellStyles(){
  266. // Each cell style has a unique xfs entry
  267. // Several might share the same styleXfs entry
  268. return xfs.size();
  269. }
  270. /**
  271. * For unit testing only
  272. */
  273. public int _getNumberFormatSize() {
  274. return numberFormats.size();
  275. }
  276. /**
  277. * For unit testing only
  278. */
  279. public int _getXfsSize() {
  280. return xfs.size();
  281. }
  282. /**
  283. * For unit testing only
  284. */
  285. public int _getStyleXfsSize() {
  286. return styleXfs.size();
  287. }
  288. /**
  289. * For unit testing only!
  290. */
  291. public CTStylesheet getCTStylesheet() {
  292. return doc.getStyleSheet();
  293. }
  294. /**
  295. * Write this table out as XML.
  296. *
  297. * @param out The stream to write to.
  298. * @throws IOException if an error occurs while writing.
  299. */
  300. public void writeTo(OutputStream out) throws IOException {
  301. XmlOptions options = new XmlOptions(DEFAULT_XML_OPTIONS);
  302. // Work on the current one
  303. // Need to do this, as we don't handle
  304. // all the possible entries yet
  305. CTStylesheet styleSheet = doc.getStyleSheet();
  306. // Formats
  307. CTNumFmts formats = CTNumFmts.Factory.newInstance();
  308. formats.setCount(numberFormats.size());
  309. for (Entry<Integer, String> fmt : numberFormats.entrySet()) {
  310. CTNumFmt ctFmt = formats.addNewNumFmt();
  311. ctFmt.setNumFmtId(fmt.getKey());
  312. ctFmt.setFormatCode(fmt.getValue());
  313. }
  314. styleSheet.setNumFmts(formats);
  315. int idx;
  316. // Fonts
  317. CTFonts ctFonts = CTFonts.Factory.newInstance();
  318. ctFonts.setCount(fonts.size());
  319. CTFont[] ctfnt = new CTFont[fonts.size()];
  320. idx = 0;
  321. for(XSSFFont f : fonts) ctfnt[idx++] = f.getCTFont();
  322. ctFonts.setFontArray(ctfnt);
  323. styleSheet.setFonts(ctFonts);
  324. // Fills
  325. CTFills ctFills = CTFills.Factory.newInstance();
  326. ctFills.setCount(fills.size());
  327. CTFill[] ctf = new CTFill[fills.size()];
  328. idx = 0;
  329. for(XSSFCellFill f : fills) ctf[idx++] = f.getCTFill();
  330. ctFills.setFillArray(ctf);
  331. styleSheet.setFills(ctFills);
  332. // Borders
  333. CTBorders ctBorders = CTBorders.Factory.newInstance();
  334. ctBorders.setCount(borders.size());
  335. CTBorder[] ctb = new CTBorder[borders.size()];
  336. idx = 0;
  337. for(XSSFCellBorder b : borders) ctb[idx++] = b.getCTBorder();
  338. ctBorders.setBorderArray(ctb);
  339. styleSheet.setBorders(ctBorders);
  340. // Xfs
  341. if(xfs.size() > 0) {
  342. CTCellXfs ctXfs = CTCellXfs.Factory.newInstance();
  343. ctXfs.setCount(xfs.size());
  344. ctXfs.setXfArray(
  345. xfs.toArray(new CTXf[xfs.size()])
  346. );
  347. styleSheet.setCellXfs(ctXfs);
  348. }
  349. // Style xfs
  350. if(styleXfs.size() > 0) {
  351. CTCellStyleXfs ctSXfs = CTCellStyleXfs.Factory.newInstance();
  352. ctSXfs.setCount(styleXfs.size());
  353. ctSXfs.setXfArray(
  354. styleXfs.toArray(new CTXf[styleXfs.size()])
  355. );
  356. styleSheet.setCellStyleXfs(ctSXfs);
  357. }
  358. // Style dxfs
  359. if(dxfs.size() > 0) {
  360. CTDxfs ctDxfs = CTDxfs.Factory.newInstance();
  361. ctDxfs.setCount(dxfs.size());
  362. ctDxfs.setDxfArray(dxfs.toArray(new CTDxf[dxfs.size()])
  363. );
  364. styleSheet.setDxfs(ctDxfs);
  365. }
  366. // Save
  367. doc.save(out, options);
  368. }
  369. @Override
  370. protected void commit() throws IOException {
  371. PackagePart part = getPackagePart();
  372. OutputStream out = part.getOutputStream();
  373. writeTo(out);
  374. out.close();
  375. }
  376. private void initialize() {
  377. //CTFont ctFont = createDefaultFont();
  378. XSSFFont xssfFont = createDefaultFont();
  379. fonts.add(xssfFont);
  380. CTFill[] ctFill = createDefaultFills();
  381. fills.add(new XSSFCellFill(ctFill[0]));
  382. fills.add(new XSSFCellFill(ctFill[1]));
  383. CTBorder ctBorder = createDefaultBorder();
  384. borders.add(new XSSFCellBorder(ctBorder));
  385. CTXf styleXf = createDefaultXf();
  386. styleXfs.add(styleXf);
  387. CTXf xf = createDefaultXf();
  388. xf.setXfId(0);
  389. xfs.add(xf);
  390. }
  391. private static CTXf createDefaultXf() {
  392. CTXf ctXf = CTXf.Factory.newInstance();
  393. ctXf.setNumFmtId(0);
  394. ctXf.setFontId(0);
  395. ctXf.setFillId(0);
  396. ctXf.setBorderId(0);
  397. return ctXf;
  398. }
  399. private static CTBorder createDefaultBorder() {
  400. CTBorder ctBorder = CTBorder.Factory.newInstance();
  401. ctBorder.addNewBottom();
  402. ctBorder.addNewTop();
  403. ctBorder.addNewLeft();
  404. ctBorder.addNewRight();
  405. ctBorder.addNewDiagonal();
  406. return ctBorder;
  407. }
  408. private static CTFill[] createDefaultFills() {
  409. CTFill[] ctFill = new CTFill[]{CTFill.Factory.newInstance(),CTFill.Factory.newInstance()};
  410. ctFill[0].addNewPatternFill().setPatternType(STPatternType.NONE);
  411. ctFill[1].addNewPatternFill().setPatternType(STPatternType.DARK_GRAY);
  412. return ctFill;
  413. }
  414. private static XSSFFont createDefaultFont() {
  415. CTFont ctFont = CTFont.Factory.newInstance();
  416. XSSFFont xssfFont=new XSSFFont(ctFont, 0);
  417. xssfFont.setFontHeightInPoints(XSSFFont.DEFAULT_FONT_SIZE);
  418. xssfFont.setColor(XSSFFont.DEFAULT_FONT_COLOR);//setTheme
  419. xssfFont.setFontName(XSSFFont.DEFAULT_FONT_NAME);
  420. xssfFont.setFamily(FontFamily.SWISS);
  421. xssfFont.setScheme(FontScheme.MINOR);
  422. return xssfFont;
  423. }
  424. protected CTDxf getDxf(int idx) {
  425. if (dxfs.size()==0) {
  426. return CTDxf.Factory.newInstance();
  427. }
  428. return dxfs.get(idx);
  429. }
  430. protected int putDxf(CTDxf dxf) {
  431. this.dxfs.add(dxf);
  432. return this.dxfs.size();
  433. }
  434. public XSSFCellStyle createCellStyle() {
  435. CTXf xf = CTXf.Factory.newInstance();
  436. xf.setNumFmtId(0);
  437. xf.setFontId(0);
  438. xf.setFillId(0);
  439. xf.setBorderId(0);
  440. xf.setXfId(0);
  441. int xfSize = styleXfs.size();
  442. int indexXf = putCellXf(xf);
  443. return new XSSFCellStyle(indexXf - 1, xfSize - 1, this, theme);
  444. }
  445. /**
  446. * Finds a font that matches the one with the supplied attributes
  447. */
  448. public XSSFFont findFont(short boldWeight, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) {
  449. for (XSSFFont font : fonts) {
  450. if ( (font.getBoldweight() == boldWeight)
  451. && font.getColor() == color
  452. && font.getFontHeight() == fontHeight
  453. && font.getFontName().equals(name)
  454. && font.getItalic() == italic
  455. && font.getStrikeout() == strikeout
  456. && font.getTypeOffset() == typeOffset
  457. && font.getUnderline() == underline)
  458. {
  459. return font;
  460. }
  461. }
  462. return null;
  463. }
  464. }