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.

POIXMLFactory.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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;
  16. import java.lang.reflect.InvocationTargetException;
  17. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  18. import org.apache.poi.openxml4j.opc.PackagePart;
  19. import org.apache.poi.openxml4j.opc.PackageRelationship;
  20. import org.apache.poi.util.POILogFactory;
  21. import org.apache.poi.util.POILogger;
  22. /**
  23. * Defines a factory API that enables sub-classes to create instances of <code>POIXMLDocumentPart</code>
  24. */
  25. public abstract class POIXMLFactory {
  26. private static final POILogger LOGGER = POILogFactory.getLogger(POIXMLFactory.class);
  27. private static final Class<?>[] PARENT_PART = {POIXMLDocumentPart.class, PackagePart.class};
  28. private static final Class<?>[] ORPHAN_PART = {PackagePart.class};
  29. /**
  30. * Create a POIXMLDocumentPart from existing package part and relation. This method is called
  31. * from {@link POIXMLDocument#load(POIXMLFactory)} when parsing a document
  32. *
  33. * @param parent parent part
  34. * @param part the PackagePart representing the created instance
  35. * @return A new instance of a POIXMLDocumentPart.
  36. *
  37. * @since by POI 3.14-Beta1
  38. */
  39. public POIXMLDocumentPart createDocumentPart(POIXMLDocumentPart parent, PackagePart part) {
  40. final PackageRelationship rel = getPackageRelationship(parent, part);
  41. final String relType = rel.getRelationshipType();
  42. final POIXMLRelation descriptor = getDescriptor(relType);
  43. // don't parse the document parts, if its class can't be determined
  44. // or if it's a package relation of another embedded resource
  45. if (descriptor == null || descriptor.getRelationClass() == null || POIXMLDocument.PACK_OBJECT_REL_TYPE.equals(relType)) {
  46. LOGGER.log(POILogger.DEBUG, "using default POIXMLDocumentPart for " + rel.getRelationshipType());
  47. return new POIXMLDocumentPart(parent, part);
  48. }
  49. Class<? extends POIXMLDocumentPart> cls = descriptor.getRelationClass();
  50. try {
  51. try {
  52. return createDocumentPart(cls, PARENT_PART, new Object[]{parent, part});
  53. } catch (NoSuchMethodException e) {
  54. return createDocumentPart(cls, ORPHAN_PART, new Object[]{part});
  55. }
  56. } catch (Exception e) {
  57. throw new POIXMLException((e.getCause() != null ? e.getCause() : e).getMessage(), e);
  58. }
  59. }
  60. /**
  61. * Need to delegate instantiation to sub class because of constructor visibility
  62. *
  63. * @param cls the document class to be instantiated
  64. * @param classes the classes of the constructor arguments
  65. * @param values the values of the constructor arguments
  66. * @return the new document / part
  67. * @throws SecurityException thrown if the object can't be instantiated
  68. * @throws NoSuchMethodException thrown if there is no constructor found for the given arguments
  69. * @throws InstantiationException thrown if the object can't be instantiated
  70. * @throws IllegalAccessException thrown if the object can't be instantiated
  71. * @throws InvocationTargetException thrown if the object can't be instantiated
  72. *
  73. * @since POI 3.14-Beta1
  74. */
  75. protected abstract POIXMLDocumentPart createDocumentPart
  76. (Class<? extends POIXMLDocumentPart> cls, Class<?>[] classes, Object[] values)
  77. throws SecurityException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException;
  78. /**
  79. * returns the descriptor for the given relationship type
  80. *
  81. * @param relationshipType the relationship type of the descriptor
  82. * @return the descriptor or null if type is unknown
  83. *
  84. * @since POI 3.14-Beta1
  85. */
  86. protected abstract POIXMLRelation getDescriptor(String relationshipType);
  87. /**
  88. * Create a new POIXMLDocumentPart using the supplied descriptor. This method is used when adding new parts
  89. * to a document, for example, when adding a sheet to a workbook, slide to a presentation, etc.
  90. *
  91. * @param descriptor describes the object to create
  92. * @return A new instance of a POIXMLDocumentPart.
  93. */
  94. public POIXMLDocumentPart newDocumentPart(POIXMLRelation descriptor) {
  95. Class<? extends POIXMLDocumentPart> cls = descriptor.getRelationClass();
  96. try {
  97. return createDocumentPart(cls, null, null);
  98. } catch (Exception e) {
  99. throw new POIXMLException(e);
  100. }
  101. }
  102. /**
  103. * Retrieves the package relationship of the child part within the parent
  104. *
  105. * @param parent the parent to search for the part
  106. * @param part the part to look for
  107. *
  108. * @return the relationship
  109. *
  110. * @throws POIXMLException if the relations are erroneous or the part is not related
  111. *
  112. * @since POI 3.14-Beta1
  113. */
  114. protected PackageRelationship getPackageRelationship(POIXMLDocumentPart parent, PackagePart part) {
  115. try {
  116. String partName = part.getPartName().getName();
  117. for (PackageRelationship pr : parent.getPackagePart().getRelationships()) {
  118. String packName = pr.getTargetURI().toASCIIString();
  119. if (packName.equalsIgnoreCase(partName)) {
  120. return pr;
  121. }
  122. }
  123. } catch (InvalidFormatException e) {
  124. throw new POIXMLException("error while determining package relations", e);
  125. }
  126. throw new POIXMLException("package part isn't a child of the parent document.");
  127. }
  128. }