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.

Package.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.openxml4j.opc;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.util.Date;
  21. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  22. import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
  23. import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
  24. import org.apache.poi.openxml4j.opc.internal.ZipContentTypeManager;
  25. import org.apache.poi.openxml4j.util.Nullable;
  26. import org.apache.poi.util.POILogFactory;
  27. import org.apache.poi.util.POILogger;
  28. /**
  29. * @deprecated (name clash with {@link java.lang.Package} use {@link OPCPackage} instead.
  30. *
  31. * @author Julien Chable, CDubet
  32. *
  33. */
  34. public abstract class Package extends OPCPackage {
  35. /**
  36. * Logger.
  37. */
  38. private static POILogger logger = POILogFactory.getLogger(Package.class);
  39. /**
  40. * @deprecated use {@link OPCPackage}
  41. */
  42. protected Package(PackageAccess access) {
  43. super(access);
  44. }
  45. /**
  46. * @deprecated use {@link OPCPackage#open(String)}
  47. */
  48. public static Package open(String path) throws InvalidFormatException {
  49. return open(path, defaultPackageAccess);
  50. }
  51. /**
  52. * @deprecated use {@link OPCPackage#open(String,PackageAccess)}
  53. */
  54. public static Package open(String path, PackageAccess access)
  55. throws InvalidFormatException {
  56. if (path == null || "".equals(path.trim())
  57. || (new File(path).exists() && new File(path).isDirectory()))
  58. throw new IllegalArgumentException("path");
  59. Package pack = new ZipPackage(path, access);
  60. if (pack.partList == null && access != PackageAccess.WRITE) {
  61. pack.getParts();
  62. }
  63. pack.originalPackagePath = new File(path).getAbsolutePath();
  64. return pack;
  65. }
  66. /**
  67. * @deprecated use {@link OPCPackage#open(InputStream)}
  68. */
  69. public static Package open(InputStream in) throws InvalidFormatException,
  70. IOException {
  71. Package pack = new ZipPackage(in, PackageAccess.READ);
  72. if (pack.partList == null) {
  73. pack.getParts();
  74. }
  75. return pack;
  76. }
  77. /**
  78. * @deprecated use {@link OPCPackage#openOrCreate(java.io.File)}
  79. */
  80. public static Package openOrCreate(File file) throws InvalidFormatException {
  81. Package retPackage = null;
  82. if (file.exists()) {
  83. retPackage = open(file.getAbsolutePath());
  84. } else {
  85. retPackage = create(file);
  86. }
  87. return retPackage;
  88. }
  89. /**
  90. * @deprecated use {@link OPCPackage#create(String)}
  91. */
  92. public static Package create(String path) {
  93. return create(new File(path));
  94. }
  95. /**
  96. * @deprecated use {@link OPCPackage#create(File)}
  97. */
  98. public static Package create(File file) {
  99. if (file == null || (file.exists() && file.isDirectory()))
  100. throw new IllegalArgumentException("file");
  101. if (file.exists()) {
  102. throw new InvalidOperationException(
  103. "This package (or file) already exists : use the open() method or delete the file.");
  104. }
  105. // Creates a new package
  106. Package pkg = null;
  107. pkg = new ZipPackage();
  108. pkg.originalPackagePath = file.getAbsolutePath();
  109. configurePackage(pkg);
  110. return pkg;
  111. }
  112. /**
  113. * @deprecated use {@link OPCPackage#create(OutputStream)}
  114. */
  115. public static Package create(OutputStream output) {
  116. Package pkg = null;
  117. pkg = new ZipPackage();
  118. pkg.originalPackagePath = null;
  119. pkg.output = output;
  120. configurePackage(pkg);
  121. return pkg;
  122. }
  123. /**
  124. * Configure the package.
  125. *
  126. * @param pkg
  127. */
  128. private static void configurePackage(Package pkg) {
  129. try {
  130. // Content type manager
  131. pkg.contentTypeManager = new ZipContentTypeManager(null, pkg);
  132. // Add default content types for .xml and .rels
  133. pkg.contentTypeManager
  134. .addContentType(
  135. PackagingURIHelper
  136. .createPartName(PackagingURIHelper.PACKAGE_RELATIONSHIPS_ROOT_URI),
  137. ContentTypes.RELATIONSHIPS_PART);
  138. pkg.contentTypeManager
  139. .addContentType(PackagingURIHelper
  140. .createPartName("/default.xml"),
  141. ContentTypes.PLAIN_OLD_XML);
  142. // Init some Package properties
  143. pkg.packageProperties = new PackagePropertiesPart(pkg,
  144. PackagingURIHelper.CORE_PROPERTIES_PART_NAME);
  145. pkg.packageProperties.setCreatorProperty("Generated by OpenXML4J");
  146. pkg.packageProperties.setCreatedProperty(new Nullable<Date>(
  147. new Date()));
  148. } catch (InvalidFormatException e) {
  149. // Should never happen
  150. throw new IllegalStateException(e);
  151. }
  152. }
  153. }