/** * Add a relationship to a part (except relationships part). * <p> * Check rule M1.25: The Relationships part shall not have relationships to * any other part. Package implementers shall enforce this requirement upon * the attempt to create such a relationship and shall treat any such * relationship as invalid. * </p> * @param targetPartName * Name of the target part. This one must be relative to the * source root directory of the part. * @param targetMode * Mode [Internal|External]. * @param relationshipType * Type of relationship. * @param id * Relationship unique id. * @return The newly created and added relationship * * @throws InvalidFormatException * If the URI point to a relationship part URI. * @see org.apache.poi.OpenXml4Net.opc.RelationshipSource#AddRelationship(org.apache.poi.OpenXml4Net.opc.PackagePartName, * org.apache.poi.OpenXml4Net.opc.TargetMode, java.lang.String, java.lang.String) */ public PackageRelationship AddRelationship(PackagePartName targetPartName, TargetMode targetMode, String relationshipType, String id) { _container.ThrowExceptionIfReadOnly(); if (targetPartName == null) { throw new ArgumentException("targetPartName"); } //if (targetMode == null) //{ // throw new ArgumentException("targetMode"); //} if (relationshipType == null) { throw new ArgumentException("relationshipType"); } if (this.IsRelationshipPart || targetPartName.IsRelationshipPartURI()) { throw new InvalidOperationException( "Rule M1.25: The Relationships part shall not have relationships to any other part."); } if (_relationships == null) { _relationships = new PackageRelationshipCollection(); } return(_relationships.AddRelationship(targetPartName.URI, targetMode, relationshipType, id)); }
/** * Build a part name where the relationship should be stored ((ex * /word/document.xml -> /word/_rels/document.xml.rels) * * @param partName * Source part Uri * @return the full path (as Uri) of the relation file * @throws InvalidOperationException * Throws if the specified Uri is a relationshp part. */ public static PackagePartName GetRelationshipPartName( PackagePartName partName) { if (partName == null) { throw new ArgumentException("partName"); } if (PackagingUriHelper.PACKAGE_ROOT_URI.OriginalString == partName.URI .OriginalString) { return(PackagingUriHelper.PACKAGE_RELATIONSHIPS_ROOT_PART_NAME); } if (partName.IsRelationshipPartURI()) { throw new InvalidOperationException("Can't be a relationship part"); } String fullPath = partName.URI.OriginalString; String filename = GetFilename(partName.URI); fullPath = fullPath.Substring(0, fullPath.Length - filename.Length); fullPath = Combine(fullPath, PackagingUriHelper.RELATIONSHIP_PART_SEGMENT_NAME); fullPath = Combine(fullPath, filename); fullPath = fullPath + PackagingUriHelper.RELATIONSHIP_PART_EXTENSION_NAME; PackagePartName retPartName; try { retPartName = CreatePartName(fullPath); } catch (InvalidFormatException) { // Should never happen in production as all data are fixed but in // case of return null: return(null); } return(retPartName); }