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.

PackageHelper.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.ooxml.util;
  16. import org.apache.poi.openxml4j.opc.*;
  17. import org.apache.poi.openxml4j.opc.OPCPackage;
  18. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
  19. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  20. import org.apache.poi.ooxml.POIXMLException;
  21. import org.apache.poi.util.IOUtils;
  22. import java.io.*;
  23. import java.net.URI;
  24. /**
  25. * Provides handy methods to work with OOXML packages
  26. */
  27. public final class PackageHelper {
  28. public static OPCPackage open(InputStream is) throws IOException {
  29. try {
  30. return OPCPackage.open(is);
  31. } catch (InvalidFormatException e){
  32. throw new POIXMLException(e);
  33. }
  34. }
  35. /**
  36. * Clone the specified package.
  37. *
  38. * @param pkg the package to clone
  39. * @param file the destination file
  40. * @return the cloned package
  41. */
  42. public static OPCPackage clone(OPCPackage pkg, File file) throws OpenXML4JException, IOException {
  43. String path = file.getAbsolutePath();
  44. OPCPackage dest = OPCPackage.create(path);
  45. PackageRelationshipCollection rels = pkg.getRelationships();
  46. for (PackageRelationship rel : rels) {
  47. PackagePart part = pkg.getPart(rel);
  48. PackagePart part_tgt;
  49. if (rel.getRelationshipType().equals(PackageRelationshipTypes.CORE_PROPERTIES)) {
  50. copyProperties(pkg.getPackageProperties(), dest.getPackageProperties());
  51. continue;
  52. }
  53. dest.addRelationship(part.getPartName(), rel.getTargetMode(), rel.getRelationshipType());
  54. part_tgt = dest.createPart(part.getPartName(), part.getContentType());
  55. OutputStream out = part_tgt.getOutputStream();
  56. IOUtils.copy(part.getInputStream(), out);
  57. out.close();
  58. if(part.hasRelationships()) {
  59. copy(pkg, part, dest, part_tgt);
  60. }
  61. }
  62. dest.close();
  63. //the temp file will be deleted when JVM terminates
  64. new File(path).deleteOnExit();
  65. return OPCPackage.open(path);
  66. }
  67. /**
  68. * Recursively copy package parts to the destination package
  69. */
  70. private static void copy(OPCPackage pkg, PackagePart part, OPCPackage tgt, PackagePart part_tgt) throws OpenXML4JException, IOException {
  71. PackageRelationshipCollection rels = part.getRelationships();
  72. if(rels != null) for (PackageRelationship rel : rels) {
  73. PackagePart p;
  74. if(rel.getTargetMode() == TargetMode.EXTERNAL){
  75. part_tgt.addExternalRelationship(rel.getTargetURI().toString(), rel.getRelationshipType(), rel.getId());
  76. //external relations don't have associated package parts
  77. continue;
  78. }
  79. URI uri = rel.getTargetURI();
  80. if(uri.getRawFragment() != null) {
  81. part_tgt.addRelationship(uri, rel.getTargetMode(), rel.getRelationshipType(), rel.getId());
  82. continue;
  83. }
  84. PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
  85. p = pkg.getPart(relName);
  86. part_tgt.addRelationship(p.getPartName(), rel.getTargetMode(), rel.getRelationshipType(), rel.getId());
  87. PackagePart dest;
  88. if(!tgt.containPart(p.getPartName())){
  89. dest = tgt.createPart(p.getPartName(), p.getContentType());
  90. OutputStream out = dest.getOutputStream();
  91. IOUtils.copy(p.getInputStream(), out);
  92. out.close();
  93. copy(pkg, p, tgt, dest);
  94. }
  95. }
  96. }
  97. /**
  98. * Copy core package properties
  99. *
  100. * @param src source properties
  101. * @param tgt target properties
  102. */
  103. private static void copyProperties(PackageProperties src, PackageProperties tgt) {
  104. tgt.setCategoryProperty(src.getCategoryProperty());
  105. tgt.setContentStatusProperty(src.getContentStatusProperty());
  106. tgt.setContentTypeProperty(src.getContentTypeProperty());
  107. tgt.setCreatorProperty(src.getCreatorProperty());
  108. tgt.setDescriptionProperty(src.getDescriptionProperty());
  109. tgt.setIdentifierProperty(src.getIdentifierProperty());
  110. tgt.setKeywordsProperty(src.getKeywordsProperty());
  111. tgt.setLanguageProperty(src.getLanguageProperty());
  112. tgt.setRevisionProperty(src.getRevisionProperty());
  113. tgt.setSubjectProperty(src.getSubjectProperty());
  114. tgt.setTitleProperty(src.getTitleProperty());
  115. tgt.setVersionProperty(src.getVersionProperty());
  116. }
  117. }