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.

POIXMLRelation.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.io.IOException;
  17. import java.io.InputStream;
  18. import java.util.Iterator;
  19. import org.apache.logging.log4j.LogManager;
  20. import org.apache.logging.log4j.Logger;
  21. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  22. import org.apache.poi.openxml4j.opc.PackagePart;
  23. import org.apache.poi.openxml4j.opc.PackagePartName;
  24. import org.apache.poi.openxml4j.opc.PackageRelationship;
  25. import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
  26. import org.apache.poi.openxml4j.opc.PackagingURIHelper;
  27. import org.apache.poi.util.Internal;
  28. import org.apache.xmlbeans.XmlException;
  29. /**
  30. * Represents a descriptor of a OOXML relation.
  31. */
  32. public abstract class POIXMLRelation {
  33. @Internal
  34. public interface NoArgConstructor {
  35. POIXMLDocumentPart init();
  36. }
  37. @Internal
  38. public interface PackagePartConstructor {
  39. POIXMLDocumentPart init(PackagePart part) throws IOException, XmlException;
  40. }
  41. @Internal
  42. public interface ParentPartConstructor {
  43. POIXMLDocumentPart init(POIXMLDocumentPart parent, PackagePart part) throws IOException, XmlException;
  44. }
  45. private static final Logger LOGGER = LogManager.getLogger(POIXMLRelation.class);
  46. /**
  47. * Describes the content stored in a part.
  48. */
  49. private final String _type;
  50. /**
  51. * The kind of connection between a source part and a target part in a package.
  52. */
  53. private final String _relation;
  54. /**
  55. * The path component of a pack URI.
  56. */
  57. private final String _defaultName;
  58. /**
  59. * Constructors or factory method to construct instances of this relationship
  60. */
  61. private final NoArgConstructor noArgConstructor;
  62. private final PackagePartConstructor packagePartConstructor;
  63. private final ParentPartConstructor parentPartConstructor;
  64. /**
  65. * Instantiates a POIXMLRelation.
  66. *
  67. * @param type content type
  68. * @param rel relationship
  69. * @param defaultName default item name
  70. * @param noArgConstructor method used to construct instances of this relationship from scratch
  71. * @param packagePartConstructor method used to construct instances of this relationship with a package part
  72. */
  73. protected POIXMLRelation(String type, String rel, String defaultName,
  74. NoArgConstructor noArgConstructor,
  75. PackagePartConstructor packagePartConstructor,
  76. ParentPartConstructor parentPartConstructor) {
  77. _type = type;
  78. _relation = rel;
  79. _defaultName = defaultName;
  80. this.noArgConstructor = noArgConstructor;
  81. this.packagePartConstructor = packagePartConstructor;
  82. this.parentPartConstructor = parentPartConstructor;
  83. }
  84. /**
  85. * Instantiates a POIXMLRelation.
  86. *
  87. * @param type content type
  88. * @param rel relationship
  89. * @param defaultName default item name
  90. */
  91. protected POIXMLRelation(String type, String rel, String defaultName) {
  92. this(type, rel, defaultName, null, null, null);
  93. }
  94. /**
  95. * Return the content type. Content types define a media type, a subtype, and an
  96. * optional set of parameters, as defined in RFC 2616.
  97. *
  98. * @return the content type
  99. */
  100. public String getContentType() {
  101. return _type;
  102. }
  103. /**
  104. * Return the relationship, the kind of connection between a source part and a target part in a package.
  105. * Relationships make the connections between parts directly discoverable without looking at the content
  106. * in the parts, and without altering the parts themselves.
  107. *
  108. * @return the relationship
  109. */
  110. public String getRelation() {
  111. return _relation;
  112. }
  113. /**
  114. * Return the default part name. Part names are used to refer to a part in the context of a
  115. * package, typically as part of a URI.
  116. *
  117. * @return the default part name
  118. */
  119. public String getDefaultFileName() {
  120. return _defaultName;
  121. }
  122. /**
  123. * Returns the filename for the nth one of these, e.g. /xl/comments4.xml
  124. *
  125. * @param index the suffix for the document type
  126. * @return the filename including the suffix
  127. */
  128. public String getFileName(int index) {
  129. if(! _defaultName.contains("#")) {
  130. // Generic filename in all cases
  131. return getDefaultFileName();
  132. }
  133. return _defaultName.replace("#", Integer.toString(index));
  134. }
  135. /**
  136. * Returns the index of the filename within the package for the given part.
  137. * e.g. 4 for /xl/comments4.xml
  138. *
  139. * @param part the part to read the suffix from
  140. * @return the suffix
  141. */
  142. public Integer getFileNameIndex(POIXMLDocumentPart part) {
  143. String regex = _defaultName.replace("#", "(\\d+)");
  144. return Integer.valueOf(part.getPackagePart().getPartName().getName().replaceAll(regex, "$1"));
  145. }
  146. /**
  147. * @return the constructor method used to construct instances of this relationship from scratch
  148. *
  149. * @since 4.1.2
  150. */
  151. public NoArgConstructor getNoArgConstructor() {
  152. return noArgConstructor;
  153. }
  154. /**
  155. * @return the constructor method used to construct instances of this relationship with a package part
  156. *
  157. * @since 4.1.2
  158. */
  159. public PackagePartConstructor getPackagePartConstructor() {
  160. return packagePartConstructor;
  161. }
  162. /**
  163. * @return the constructor method used to construct instances of this relationship with a package part
  164. *
  165. * @since 4.1.2
  166. */
  167. public ParentPartConstructor getParentPartConstructor() {
  168. return parentPartConstructor;
  169. }
  170. /**
  171. * Fetches the InputStream to read the contents, based
  172. * of the specified core part, for which we are defined
  173. * as a suitable relationship
  174. *
  175. * @since 3.16-beta3
  176. */
  177. public InputStream getContents(PackagePart corePart) throws IOException, InvalidFormatException {
  178. PackageRelationshipCollection prc =
  179. corePart.getRelationshipsByType(getRelation());
  180. Iterator<PackageRelationship> it = prc.iterator();
  181. if(it.hasNext()) {
  182. PackageRelationship rel = it.next();
  183. PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
  184. PackagePart part = corePart.getPackage().getPart(relName);
  185. if (part == null) {
  186. throw new IllegalArgumentException("Could not read part " + relName + " from " + corePart);
  187. }
  188. return part.getInputStream();
  189. }
  190. LOGGER.atWarn().log("No part {} found", getDefaultFileName());
  191. return null;
  192. }
  193. }