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.

XSSFPivotCacheRecords.java 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.poi.ooxml.POIXMLDocumentPart;
  22. import org.apache.poi.openxml4j.opc.PackagePart;
  23. import org.apache.poi.util.Beta;
  24. import org.apache.poi.util.Internal;
  25. import org.apache.xmlbeans.XmlException;
  26. import org.apache.xmlbeans.XmlOptions;
  27. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotCacheRecords;
  28. public class XSSFPivotCacheRecords extends POIXMLDocumentPart {
  29. private CTPivotCacheRecords ctPivotCacheRecords;
  30. @Beta
  31. public XSSFPivotCacheRecords() {
  32. super();
  33. ctPivotCacheRecords = CTPivotCacheRecords.Factory.newInstance();
  34. }
  35. /**
  36. * Creates an XSSFPivotCacheRecords representing the given package part and relationship.
  37. * Should only be called when reading in an existing file.
  38. *
  39. * @param part - The package part that holds xml data representing this pivot cache records.
  40. *
  41. * @since POI 3.14-Beta1
  42. */
  43. @Beta
  44. protected XSSFPivotCacheRecords(PackagePart part) throws IOException {
  45. super(part);
  46. readFrom(part.getInputStream());
  47. }
  48. @Beta
  49. protected void readFrom(InputStream is) throws IOException {
  50. try {
  51. XmlOptions options = new XmlOptions(DEFAULT_XML_OPTIONS);
  52. //Removing root element
  53. options.setLoadReplaceDocumentElement(null);
  54. ctPivotCacheRecords = CTPivotCacheRecords.Factory.parse(is, options);
  55. } catch (XmlException e) {
  56. throw new IOException(e.getLocalizedMessage());
  57. }
  58. }
  59. @Beta
  60. @Internal
  61. public CTPivotCacheRecords getCtPivotCacheRecords() {
  62. return ctPivotCacheRecords;
  63. }
  64. @Beta
  65. @Override
  66. protected void commit() throws IOException {
  67. PackagePart part = getPackagePart();
  68. OutputStream out = part.getOutputStream();
  69. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  70. //Sets the pivotCacheDefinition tag
  71. xmlOptions.setSaveSyntheticDocumentElement(new QName(CTPivotCacheRecords.type.getName().
  72. getNamespaceURI(), "pivotCacheRecords"));
  73. ctPivotCacheRecords.save(out, xmlOptions);
  74. out.close();
  75. }
  76. }