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.

XWPFChart.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.xwpf.usermodel;
  16. import static org.apache.poi.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.poi.POIXMLException;
  22. import org.apache.poi.openxml4j.opc.PackagePart;
  23. import org.apache.poi.util.Beta;
  24. import org.apache.poi.util.IOUtils;
  25. import org.apache.poi.xddf.usermodel.chart.XDDFChart;
  26. import org.apache.xmlbeans.XmlException;
  27. import org.apache.xmlbeans.XmlOptions;
  28. import org.openxmlformats.schemas.drawingml.x2006.chart.CTChartSpace;
  29. /**
  30. * Represents a Chart in a .docx file
  31. */
  32. @Beta
  33. public class XWPFChart extends XDDFChart {
  34. // lazy initialization
  35. private Long checksum;
  36. /**
  37. * Construct a chart from a package part.
  38. *
  39. * @param part the package part holding the chart data,
  40. * the content type must be <code>application/vnd.openxmlformats-officedocument.drawingml.chart+xml</code>
  41. *
  42. * @since POI 4.0.0
  43. */
  44. protected XWPFChart(PackagePart part) throws IOException, XmlException {
  45. super(part);
  46. }
  47. @Override
  48. protected void commit() throws IOException {
  49. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  50. xmlOptions.setSaveSyntheticDocumentElement(new QName(CTChartSpace.type.getName().getNamespaceURI(), "chartSpace", "c"));
  51. try (OutputStream out = getPackagePart().getOutputStream()) {
  52. chartSpace.save(out, xmlOptions);
  53. }
  54. }
  55. public Long getChecksum() {
  56. if (this.checksum == null) {
  57. InputStream is = null;
  58. byte[] data;
  59. try {
  60. is = getPackagePart().getInputStream();
  61. data = IOUtils.toByteArray(is);
  62. } catch (IOException e) {
  63. throw new POIXMLException(e);
  64. } finally {
  65. try {
  66. if (is != null) {
  67. is.close();
  68. }
  69. } catch (IOException e) {
  70. throw new POIXMLException(e);
  71. }
  72. }
  73. this.checksum = IOUtils.calculateChecksum(data);
  74. }
  75. return this.checksum;
  76. }
  77. @Override
  78. public boolean equals(Object obj) {
  79. /**
  80. * In case two objects ARE equal, but its not the same instance, this
  81. * implementation will always run through the whole
  82. * byte-array-comparison before returning true. If this will turn into a
  83. * performance issue, two possible approaches are available:<br>
  84. * a) Use the checksum only and take the risk that two images might have
  85. * the same CRC32 sum, although they are not the same.<br>
  86. * b) Use a second (or third) checksum algorithm to minimise the chance
  87. * that two images have the same checksums but are not equal (e.g.
  88. * CRC32, MD5 and SHA-1 checksums, additionally compare the
  89. * data-byte-array lengths).
  90. */
  91. if (obj == this) {
  92. return true;
  93. }
  94. if (obj == null) {
  95. return false;
  96. }
  97. if (!(obj instanceof XWPFChart)) {
  98. return false;
  99. }
  100. return false;
  101. }
  102. @Override
  103. public int hashCode() {
  104. return getChecksum().hashCode();
  105. }
  106. }