// Locate core properties part using the package relationship that points to it.
        private PackagePart?GetPropertyPart()
        {
            // Find a package-wide relationship of type CoreDocumentPropertiesRelationshipType.
            PackageRelationship?corePropertiesRelationship = GetCorePropertiesRelationship();

            if (corePropertiesRelationship == null)
            {
                return(null);
            }

            // Retrieve the part referenced by its target URI.
            if (corePropertiesRelationship.TargetMode != TargetMode.Internal)
            {
                throw new FileFormatException(SR.NoExternalTargetForMetadataRelationship);
            }

            PackagePart?propertiesPart    = null;
            Uri         propertiesPartUri = PackUriHelper.ResolvePartUri(
                PackUriHelper.PackageRootUri,
                corePropertiesRelationship.TargetUri);

            if (!_package.PartExists(propertiesPartUri))
            {
                throw new FileFormatException(SR.DanglingMetadataRelationship);
            }

            propertiesPart = _package.GetPart(propertiesPartUri);
            if (!propertiesPart.ValidatedContentType.AreTypeAndSubTypeEqual(s_coreDocumentPropertiesContentType))
            {
                throw new FileFormatException(SR.WrongContentTypeForPropertyPart);
            }

            return(propertiesPart);
        }
        // Find a package-wide relationship of type CoreDocumentPropertiesRelationshipType.
        private PackageRelationship?GetCorePropertiesRelationship()
        {
            PackageRelationship?propertiesPartRelationship = null;

            foreach (PackageRelationship rel
                     in _package.GetRelationshipsByType(CoreDocumentPropertiesRelationshipType))
            {
                if (propertiesPartRelationship != null)
                {
                    throw new FileFormatException(SR.MoreThanOneMetadataRelationships);
                }
                propertiesPartRelationship = rel;
            }
            return(propertiesPartRelationship);
        }
Esempio n. 3
0
        /// <summary>
        /// Retrieve a relationship per ID.
        /// </summary>
        /// <param name="id">The relationship ID.</param>
        /// <returns>The relationship with ID 'id' or throw an exception if not found.</returns>
        /// <exception cref="InvalidOperationException">If this part has been deleted</exception>
        /// <exception cref="InvalidOperationException">If the parent package has been closed or disposed</exception>
        /// <exception cref="IOException">If the package is write only, no information can be retrieved from it</exception>
        /// <exception cref="ArgumentNullException">If parameter "id" is null</exception>
        /// <exception cref="System.Xml.XmlException">If parameter "id" is not a valid Xsd Id</exception>
        /// <exception cref="InvalidOperationException">If the requested relationship does not exist in the Package</exception>
        public PackageRelationship GetRelationship(string id)
        {
            //All the validations for dispose and file access are done in the
            //GetRelationshipHelper method.

            PackageRelationship?returnedRelationship = GetRelationshipHelper(id);

            if (returnedRelationship == null)
            {
                throw new InvalidOperationException(SR.PackagePartRelationshipDoesNotExist);
            }
            else
            {
                return(returnedRelationship);
            }
        }