Ejemplo n.º 1
0
        ///////////////////////////////////////////////////////////////////////////////
        // Description: Gets the target part of a relationship with the 'Internal' target mode.
        ///////////////////////////////////////////////////////////////////////////////
        private static void GetRelationshipTargetPart(IOpcPartSet partSet,           // Set of the parts in the package.
                                                      IOpcRelationship relationship, // Relationship that targets the required part.
                                                      string expectedContentType,    // Content type expected for the target part.
                                                      out IOpcPart targetPart        // Recieves pointer to target part. Method may return a valid
                                                                                     // pointer even on failure, and the caller must always release if a non-default value is returned.
                                                      )
        {
            targetPart = null;

            OPC_URI_TARGET_MODE targetMode = relationship.GetTargetMode();

            if (targetMode != OPC_URI_TARGET_MODE.OPC_URI_TARGET_MODE_INTERNAL)
            {
                // The relationship's target is not a part.
                var relationshipType = relationship.GetRelationshipType();

                Console.Error.Write("Invalid music bundle package: relationship with type {0} must have Internal target mode.\n", relationshipType);

                // Set the return code to an error.
                throw new InvalidOperationException();
            }

            // Relationship's target is a part; the target mode is 'Internal'.

            // Get the URI of the relationship source.
            IOpcUri sourceUri = relationship.GetSourceUri();

            // Get the URI of the relationship target.
            IUri targetUri = relationship.GetTargetUri();

            // Resolve the target URI to the part name of the target part.
            IOpcPartUri targetPartUri = sourceUri.CombinePartUri(targetUri);

            // Check that a part with the resolved part name exists in the part set.
            var partExists = partSet.PartExists(targetPartUri);

            if (!partExists)
            {
                // The part does not exist in the part set.
                Console.Error.Write("Invalid music bundle package: the target part of relationship does not exist.\n");

                // Set the return code to an error.
                throw new InvalidOperationException();
            }

            // Get the part.
            targetPart = partSet.GetPart(targetPartUri);

            // Get the content type of the part.
            var contentType = targetPart.GetContentType();

            if (contentType != expectedContentType)
            {
                // Content type of the part did not match the expected content type.
                Console.Error.Write("Invalid music bundle package: the target part does not have correct content type.\n");

                // Set the return code to an error.
                throw new InvalidOperationException();
            }
        }
Ejemplo n.º 2
0
        ///////////////////////////////////////////////////////////////////////////////
        // Description: Gets the relationship of the specified type from a specified relationship set.
        // Note: Method expects exactly one relationship of the specified type. This limitation is described in the Music Bundle Package
        // specification--and is not imposed by the Packaging APIs or the OPC.
        ///////////////////////////////////////////////////////////////////////////////
        private static void GetRelationshipByType(IOpcRelationshipSet relsSet,            // Relationship set that contains the relationship.
                                                  string relationshipType,                // The relationship type of the relationship.
                                                  out IOpcRelationship targetRelationship // Recieves the relationship. Method may return a valid
                                                                                          // pointer even on failure, and the caller must always release if a non-default value is returned.
                                                  )
        {
            targetRelationship = null;
            HRESULT hr = HRESULT.S_OK;

            // Get an enumerator of all relationships of the required type.
            IOpcRelationshipEnumerator relsEnumerator = relsSet.GetEnumeratorForType(relationshipType);

            // Enumerate through relationships, ensuring that there is exactly one relationship that has the specified type.
            var count = 0;

            while (hr.Succeeded && (hr = relsEnumerator.MoveNext(out var hasNext)).Succeeded && hasNext)
            {
                count++;

                if (count > 1)
                {
                    // There is more than one relationship of the specified type.
                    Console.Error.Write("Invalid music bundle package: cannot have more than 1 relationship with type: {0}.\n", relationshipType);

                    // Set the return code to an error.
                    hr = HRESULT.E_FAIL;

                    // An error was encountered; stop enumerating.
                    break;
                }

                if (hr.Succeeded)
                {
                    // Get the relationship at the current position of the enumerator.
                    hr = relsEnumerator.GetCurrent(out targetRelationship);
                }
            }

            if (hr.Succeeded)
            {
                if (count == 0)
                {
                    // There were no relationships in the set that had the specified type.
                    Console.Error.Write("Invalid music bundle package: relationship with type {0} does not exist.\n", relationshipType);

                    throw new InvalidOperationException();
                }
            }
            else
            {
                throw new InvalidOperationException();
            }
        }