PageRenderTime 70ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/sysmodb/xml/XSSFXMLStyleHelper.java

https://github.com/myGrid/simple-spreadsheet-extractor
Java | 111 lines | 87 code | 11 blank | 13 comment | 29 complexity | ccdc160313252ccc036bd5537c50037c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*******************************************************************************
  2. * Copyright (c) 2009-2013, University of Manchester
  3. *
  4. * Licensed under the New BSD License.
  5. * Please see LICENSE file that is distributed with the source code
  6. ******************************************************************************/
  7. package org.sysmodb.xml;
  8. import javax.xml.stream.XMLStreamException;
  9. import javax.xml.stream.XMLStreamWriter;
  10. import org.apache.poi.ss.usermodel.CellStyle;
  11. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  12. import org.apache.poi.xssf.usermodel.XSSFColor;
  13. import org.apache.poi.xssf.usermodel.XSSFFont;
  14. /**
  15. *
  16. * @author Finn
  17. */
  18. public class XSSFXMLStyleHelper implements XMLStyleHelper {
  19. public String getBGColour(CellStyle style) {
  20. XSSFCellStyle newStyle = (XSSFCellStyle) style;
  21. XSSFColor colour = newStyle.getFillForegroundXSSFColor();
  22. return getRGBString(colour);
  23. }
  24. public boolean areFontsEmpty(CellStyle style) {
  25. XSSFCellStyle newStyle = (XSSFCellStyle) style;
  26. XSSFFont font = newStyle.getFont();
  27. if (font.getBold())
  28. return false;
  29. if (font.getItalic())
  30. return false;
  31. if (font.getUnderline() != XSSFFont.U_NONE)
  32. return false;
  33. if (font.getFontHeight() != XSSFFont.DEFAULT_FONT_SIZE)
  34. return false;
  35. if (!font.getFontName().equals(XSSFFont.DEFAULT_FONT_NAME))
  36. return false;
  37. if (font.getColor() != XSSFFont.DEFAULT_FONT_COLOR) {
  38. String colorString = getRGBString(font.getXSSFColor());
  39. if (colorString != null) {
  40. return false;
  41. }
  42. }
  43. return true;
  44. }
  45. @Override
  46. public void writeFontProperties(XMLStreamWriter xmlWriter, CellStyle style) throws XMLStreamException {
  47. XSSFCellStyle newStyle = (XSSFCellStyle) style;
  48. XSSFFont font = newStyle.getFont();
  49. if (font.getBold()) {
  50. xmlWriter.writeStartElement("font-weight");
  51. xmlWriter.writeCharacters("bold");
  52. xmlWriter.writeEndElement();
  53. }
  54. if (font.getItalic()) {
  55. xmlWriter.writeStartElement("font-style");
  56. xmlWriter.writeCharacters("italics");
  57. xmlWriter.writeEndElement();
  58. }
  59. if (font.getUnderline() != XSSFFont.U_NONE) {
  60. xmlWriter.writeStartElement("text-decoration");
  61. xmlWriter.writeCharacters("underline");
  62. xmlWriter.writeEndElement();
  63. }
  64. if (font.getFontHeight() != XSSFFont.DEFAULT_FONT_SIZE) {
  65. xmlWriter.writeStartElement("font-size");
  66. xmlWriter.writeCharacters(String.valueOf(font.getFontHeightInPoints()) + "pt");
  67. xmlWriter.writeEndElement();
  68. }
  69. if (!font.getFontName().equals(XSSFFont.DEFAULT_FONT_NAME)) {
  70. xmlWriter.writeStartElement("font-family");
  71. xmlWriter.writeCharacters(String.valueOf(font.getFontName()));
  72. xmlWriter.writeEndElement();
  73. }
  74. if (font.getColor() != XSSFFont.DEFAULT_FONT_COLOR) {
  75. String colorString = getRGBString(font.getXSSFColor());
  76. if (colorString != null) {
  77. xmlWriter.writeStartElement("color");
  78. xmlWriter.writeCharacters(colorString);
  79. xmlWriter.writeEndElement();
  80. }
  81. }
  82. }
  83. private String getRGBString(XSSFColor colour) {
  84. String string = null;
  85. // Disregard default/automatic colours, to avoid cluttering XML
  86. if (colour == null || colour.isAuto()) {
  87. return string;
  88. } else {
  89. String rgb = colour.getARGBHex();
  90. // XSSF has a bug where the above can sometimes return null
  91. // so we check here
  92. if (rgb != null) {
  93. if (rgb.length() > 6)
  94. rgb = rgb.substring(2, rgb.length());
  95. string = "#" + rgb;
  96. }
  97. }
  98. return string;
  99. }
  100. }