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.

XSSFChartSheet.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import javax.xml.namespace.QName;
  21. import org.apache.commons.io.input.UnsynchronizedByteArrayInputStream;
  22. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  23. import org.apache.poi.ooxml.POIXMLException;
  24. import org.apache.poi.openxml4j.opc.PackagePart;
  25. import org.apache.xmlbeans.XmlException;
  26. import org.apache.xmlbeans.XmlOptions;
  27. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTChartsheet;
  28. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDrawing;
  29. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTLegacyDrawing;
  30. import org.openxmlformats.schemas.spreadsheetml.x2006.main.ChartsheetDocument;
  31. /**
  32. * High level representation of Sheet Parts that are of type 'chartsheet'.
  33. * <p>
  34. * Chart sheet is a special kind of Sheet that contains only chart and no data.
  35. */
  36. public class XSSFChartSheet extends XSSFSheet {
  37. private static final byte[] BLANK_WORKSHEET = blankWorksheet();
  38. protected CTChartsheet chartsheet;
  39. /**
  40. * @since POI 3.14-Beta1
  41. */
  42. protected XSSFChartSheet(PackagePart part) {
  43. super(part);
  44. }
  45. @Override
  46. protected void read(InputStream is) throws IOException {
  47. //initialize the superclass with a blank worksheet
  48. super.read(UnsynchronizedByteArrayInputStream.builder().setByteArray(BLANK_WORKSHEET).get());
  49. try {
  50. chartsheet = ChartsheetDocument.Factory.parse(is, DEFAULT_XML_OPTIONS).getChartsheet();
  51. } catch (XmlException e){
  52. throw new POIXMLException(e);
  53. }
  54. }
  55. /**
  56. * Provide access to the CTChartsheet bean holding this sheet's data
  57. *
  58. * @return the CTChartsheet bean holding this sheet's data
  59. */
  60. public CTChartsheet getCTChartsheet() {
  61. return chartsheet;
  62. }
  63. @Override
  64. protected CTDrawing getCTDrawing() {
  65. return chartsheet.getDrawing();
  66. }
  67. @Override
  68. protected CTLegacyDrawing getCTLegacyDrawing() {
  69. return chartsheet.getLegacyDrawing();
  70. }
  71. @Override
  72. protected void write(OutputStream out) throws IOException {
  73. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  74. xmlOptions.setSaveSyntheticDocumentElement(
  75. new QName(CTChartsheet.type.getName().getNamespaceURI(), "chartsheet"));
  76. if (chartsheet != null) {
  77. chartsheet.save(out, xmlOptions);
  78. }
  79. }
  80. private static byte[] blankWorksheet(){
  81. UnsynchronizedByteArrayOutputStream out = UnsynchronizedByteArrayOutputStream.builder().get();
  82. try {
  83. new XSSFSheet().write(out);
  84. } catch (IOException e){
  85. throw new IllegalStateException(e);
  86. }
  87. return out.toByteArray();
  88. }
  89. }