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.

PackageRelationship.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.net.URI;
  17. import java.net.URISyntaxException;
  18. import java.util.Objects;
  19. /**
  20. * A part relationship.
  21. * @version 1.0
  22. */
  23. public final class PackageRelationship {
  24. private static URI containerRelationshipPart;
  25. static {
  26. try {
  27. containerRelationshipPart = new URI("/_rels/.rels");
  28. } catch (URISyntaxException e) {
  29. // Do nothing
  30. }
  31. }
  32. /* XML markup */
  33. public static final String ID_ATTRIBUTE_NAME = "Id";
  34. public static final String RELATIONSHIPS_TAG_NAME = "Relationships";
  35. public static final String RELATIONSHIP_TAG_NAME = "Relationship";
  36. public static final String TARGET_ATTRIBUTE_NAME = "Target";
  37. public static final String TARGET_MODE_ATTRIBUTE_NAME = "TargetMode";
  38. public static final String TYPE_ATTRIBUTE_NAME = "Type";
  39. /* End XML markup */
  40. /**
  41. * Relation id.
  42. */
  43. private final String id;
  44. /**
  45. * Reference to the package.
  46. */
  47. private final OPCPackage container;
  48. /**
  49. * Relationship type
  50. */
  51. private final String relationshipType;
  52. /**
  53. * Part of this relationship source
  54. */
  55. private final PackagePart source;
  56. /**
  57. * Targeting mode [Internal|External]
  58. */
  59. private final TargetMode targetMode;
  60. /**
  61. * Target URI
  62. */
  63. private final URI targetUri;
  64. /**
  65. * Constructor.
  66. */
  67. public PackageRelationship(OPCPackage pkg, PackagePart sourcePart,
  68. URI targetUri, TargetMode targetMode, String relationshipType,
  69. String id) {
  70. if (pkg == null)
  71. throw new IllegalArgumentException("pkg");
  72. if (targetUri == null)
  73. throw new IllegalArgumentException("targetUri");
  74. if (relationshipType == null)
  75. throw new IllegalArgumentException("relationshipType");
  76. if (id == null)
  77. throw new IllegalArgumentException("id");
  78. this.container = pkg;
  79. this.source = sourcePart;
  80. this.targetUri = targetUri;
  81. this.targetMode = targetMode;
  82. this.relationshipType = relationshipType;
  83. this.id = id;
  84. }
  85. @Override
  86. public boolean equals(Object obj) {
  87. if (!(obj instanceof PackageRelationship)) {
  88. return false;
  89. }
  90. PackageRelationship rel = (PackageRelationship) obj;
  91. return (this.id.equals(rel.id)
  92. && this.relationshipType.equals(rel.relationshipType)
  93. && (rel.source == null || rel.source.equals(this.source))
  94. && this.targetMode == rel.targetMode && this.targetUri
  95. .equals(rel.targetUri));
  96. }
  97. @Override
  98. public int hashCode() {
  99. return Objects.hash(id,relationshipType,source,targetMode,targetUri);
  100. }
  101. /* Getters */
  102. public static URI getContainerPartRelationship() {
  103. return containerRelationshipPart;
  104. }
  105. /**
  106. * @return the container
  107. */
  108. public OPCPackage getPackage() {
  109. return container;
  110. }
  111. /**
  112. * @return the id
  113. */
  114. public String getId() {
  115. return id;
  116. }
  117. /**
  118. * @return the relationshipType
  119. */
  120. public String getRelationshipType() {
  121. return relationshipType;
  122. }
  123. /**
  124. * @return the source
  125. */
  126. public PackagePart getSource() {
  127. return source;
  128. }
  129. /**
  130. *
  131. * @return URL of the source part of this relationship
  132. */
  133. public URI getSourceURI() {
  134. if (source == null) {
  135. return PackagingURIHelper.PACKAGE_ROOT_URI;
  136. }
  137. return source._partName.getURI();
  138. }
  139. /**
  140. * @return the targetMode
  141. */
  142. public TargetMode getTargetMode() {
  143. return targetMode;
  144. }
  145. /**
  146. * @return the targetUri
  147. */
  148. public URI getTargetURI() {
  149. // If it's an external target, we don't
  150. // need to apply our normal validation rules
  151. if(targetMode == TargetMode.EXTERNAL) {
  152. return targetUri;
  153. }
  154. // If it's an internal hyperlink target, we don't
  155. // need to apply our normal validation rules
  156. if (PackageRelationshipTypes.HYPERLINK_PART.equals(relationshipType)) {
  157. return targetUri;
  158. }
  159. // Internal target
  160. // If it isn't absolute, resolve it relative
  161. // to ourselves
  162. if (!targetUri.toASCIIString().startsWith("/")) {
  163. // So it's a relative part name, try to resolve it
  164. return PackagingURIHelper.resolvePartUri(getSourceURI(), targetUri);
  165. }
  166. return targetUri;
  167. }
  168. @Override
  169. public String toString() {
  170. return "id=" + id +
  171. " - container=" + container +
  172. " - relationshipType=" + relationshipType +
  173. (source == null ? " - source=null" : " - source=" + getSourceURI().toASCIIString()) +
  174. " - target=" + getTargetURI().toASCIIString() +
  175. (targetMode == null ? ",targetMode=null" : ",targetMode=" + targetMode);
  176. }
  177. }