Exemplo 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();
            }
        }
Exemplo n.º 2
0
        ///////////////////////////////////////////////////////////////////////////////
        // Description: Opens a stream to the path and file name specified, and deserializes the file's content as the content of the part.
        ///////////////////////////////////////////////////////////////////////////////
        private static void WriteFileContentToPart(IOpcFactory opcFactory,
                                                   string filePath, // Directory where the file is located.
                                                   string fileName, // Name of file whose content will be deserialized.
                                                   IOpcPart opcPart // Part into which the file content is deserialized.
                                                   )
        {
            // Get the full file name of the file.
            GetFullFileName(filePath, fileName, out var fullFileName);

            // Create a read-only stream over the file.
            opcFactory.CreateStreamOnFile(fullFileName, OPC_STREAM_IO_MODE.OPC_STREAM_IO_READ, default,