GetRelationshipsByType() public méthode

public GetRelationshipsByType ( string relationshipType ) : PackageRelationshipCollection
relationshipType string
Résultat PackageRelationshipCollection
        public static PackageDefinition ReadPackageManifest(Package package)
        {
            var manifestPart = package.GetPart(
                package.GetRelationshipsByType(CtpFormatPackageDefinitionRelationshipType).Single().TargetUri);

            using (var manifestStream = manifestPart.GetStream())
            using (var xmlReader = XmlReader.Create(manifestStream, XmlUtils.DtdSafeReaderSettings))
            {
               return new PackageDefinition(XDocument.Load(xmlReader).Root);
            }
        }
        PackageMetadata ProcessManifest(Package package)
        {
            var packageRelationship =
                package.GetRelationshipsByType("http://schemas.microsoft.com/packaging/2010/07/manifest")
                .SingleOrDefault();

            if (packageRelationship == null)
            {
                throw new InvalidOperationException("Package does not contain a manifest");
            }

            var part = package.GetPart(packageRelationship.TargetUri);

            return ReadManifestStream(part.GetStream());
        }
        //------------------------------------------------------
        //
        //  Public Methods
        //
        //------------------------------------------------------

        #region Public Methods

        /// <summary>
        /// This method returns the list of selected PackageRelationships as per the
        /// given criteria, from a part in the Package provided
        /// </summary>
        /// <param name="package">Package object from which we get the relationsips</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">If package parameter is null</exception>
        public List <PackageRelationship> Select(Package package)
        {
            if (package == null)
            {
                throw new ArgumentNullException("package");
            }

            List <PackageRelationship> relationships = new List <PackageRelationship>(0);

            switch (SelectorType)
            {
            case PackageRelationshipSelectorType.Id:
                if (SourceUri.Equals(PackUriHelper.PackageRootUri))
                {
                    if (package.RelationshipExists(SelectionCriteria))
                    {
                        relationships.Add(package.GetRelationship(SelectionCriteria));
                    }
                }
                else
                {
                    if (package.PartExists(SourceUri))
                    {
                        PackagePart part = package.GetPart(SourceUri);
                        if (part.RelationshipExists(SelectionCriteria))
                        {
                            relationships.Add(part.GetRelationship(SelectionCriteria));
                        }
                    }
                }
                break;

            case PackageRelationshipSelectorType.Type:
                if (SourceUri.Equals(PackUriHelper.PackageRootUri))
                {
                    foreach (PackageRelationship r in package.GetRelationshipsByType(SelectionCriteria))
                    {
                        relationships.Add(r);
                    }
                }
                else
                {
                    if (package.PartExists(SourceUri))
                    {
                        foreach (PackageRelationship r in package.GetPart(SourceUri).GetRelationshipsByType(SelectionCriteria))
                        {
                            relationships.Add(r);
                        }
                    }
                }
                break;

            default:
                //Debug.Assert is fine here since the parameters have already been validated. And all the properties are
                //readonly
                Debug.Assert(false, "This option should never be called");
                break;
            }

            return(relationships);
        }
        private void SignAllParts(Package package, X509Certificate2 certificate)
        {
            var partsToSign = new List<Uri>();
            var relationshipsToSign = new List<PackageRelationshipSelector>();

            foreach (var relationship in package.GetRelationshipsByType(RtOfficeDocument))
            {
                AddSignableItems(relationship, partsToSign, relationshipsToSign);
            }

            var mgr = new PackageDigitalSignatureManager(package)
            {
                CertificateOption = CertificateEmbeddingOption.InSignaturePart
            };

            var officeObject = CreateOfficeObject(SignatureID, ManifestHashAlgorithm);
            var officeObjectReference = new Reference("#" + OfficeObjectID);
            mgr.Sign(partsToSign,
                     certificate,
                     relationshipsToSign,
                     SignatureID,
                     new[] { officeObject },
                     new[] { officeObjectReference });
            package.Close();
        }
        //------------------------------------------------------
        //
        //  Public Methods
        //
        //------------------------------------------------------

        #region Public Methods

        /// <summary>
        /// This method returns the list of selected PackageRelationships as per the
        /// given criteria, from a part in the Package provided
        /// </summary>
        /// <param name="package">Package object from which we get the relationsips</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">If package parameter is null</exception>
        public List<PackageRelationship> Select(Package package)
        {
            if (package == null)
            {
                throw new ArgumentNullException("package");
            }

            List<PackageRelationship> relationships = new List<PackageRelationship>(0);

            switch (SelectorType)
            {
                case PackageRelationshipSelectorType.Id:
                    if (SourceUri.Equals(PackUriHelper.PackageRootUri))
                    {
                        if (package.RelationshipExists(SelectionCriteria))
                            relationships.Add(package.GetRelationship(SelectionCriteria));
                    }
                    else
                    {
                        if (package.PartExists(SourceUri))
                        {
                            PackagePart part = package.GetPart(SourceUri);
                            if (part.RelationshipExists(SelectionCriteria))
                                relationships.Add(part.GetRelationship(SelectionCriteria));
                        }
                    }
                    break;

                case PackageRelationshipSelectorType.Type:
                    if (SourceUri.Equals(PackUriHelper.PackageRootUri))
                    {
                        foreach (PackageRelationship r in package.GetRelationshipsByType(SelectionCriteria))
                            relationships.Add(r);
                    }
                    else
                    {
                        if (package.PartExists(SourceUri))
                        {
                            foreach (PackageRelationship r in package.GetPart(SourceUri).GetRelationshipsByType(SelectionCriteria))
                                relationships.Add(r);
                        }
                    }
                    break;

                default:
                    //Debug.Assert is fine here since the parameters have already been validated. And all the properties are 
                    //readonly
                    Debug.Assert(false, "This option should never be called");
                    break;
            }

            return relationships;
        }
Exemple #6
0
 internal void CopyDecks(Package package)
 {
     string path = Path.Combine(basePath, "Decks");
     var decks = package.GetRelationshipsByType("http://schemas.octgn.org/set/deck");
     byte[] buffer = new byte[0x1000];
     foreach(var deckRel in decks)
     {
         var deck = package.GetPart(deckRel.TargetUri);
         string deckFilename = Path.Combine(path, Path.GetFileName(deck.Uri.ToString()));
         using(var deckStream = deck.GetStream(FileMode.Open, FileAccess.Read))
         using(var targetStream = File.Open(deckFilename, FileMode.Create, FileAccess.Write))
         {
             int read;
             while(true)
             {
                 read = deckStream.Read(buffer, 0, buffer.Length);
                 if(read == 0) break;
                 targetStream.Write(buffer, 0, read);
             }
         }
     }
 }
Exemple #7
0
 internal void CopyDecks(Package package)
 {
     string path = Path.Combine(GamesRepository.BasePath, "Decks");
     PackageRelationshipCollection decks = package.GetRelationshipsByType("http://schemas.octgn.org/set/deck");
     var buffer = new byte[0x1000];
     foreach (PackageRelationship deckRel in decks)
     {
         PackagePart deck = package.GetPart(deckRel.TargetUri);
         string deckuri = Path.GetFileName(deck.Uri.ToString());
         if (deckuri == null) continue;
         string deckFilename = Path.Combine(path, deckuri);
         using (Stream deckStream = deck.GetStream(FileMode.Open, FileAccess.Read))
         using (
             FileStream targetStream = File.Open(deckFilename, FileMode.Create, FileAccess.Write, FileShare.Read)
             )
         {
             while (true)
             {
                 int read = deckStream.Read(buffer, 0, buffer.Length);
                 if (read == 0) break;
                 targetStream.Write(buffer, 0, read);
             }
         }
     }
 }
 public static PackagePart GetDocumentPart(Package package)
 {
     PackageRelationship relationship = package.GetRelationshipsByType(OfficeDocumentRelType).FirstOrDefault();
     Uri docUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);
     return package.GetPart(docUri);
 }
        public void Unzip(string fullPathZipFile, string fullPathToUnzip, bool unZipWithDirectoryStructure)
        {
            int Size;

            Log.WriteLog(" -- Init -- ", "Log", this.GetType().Name, this.AppConfig);
            System.IO.FileStream zipFileStream = null;
            string location = string.Empty;

            try
            {
                if (Path.GetExtension(fullPathZipFile).ToLower() != ".zip")
                {
                    throw new IOException("File Extension in not valid");
                }
                if (!File.Exists(fullPathZipFile))
                {
                    throw new FileNotFoundException("Zip file not found");
                }
                zipFileStream = new System.IO.FileStream(fullPathZipFile, FileMode.Open);
                try
                {
                    this.ValidatePath(fullPathToUnzip);
                }
                catch (DirectoryNotFoundException directoryNotFoundException)
                {
                    Log.WriteLog(string.Concat("Create Directory ", fullPathToUnzip), "Log", this.GetType().Name, this.AppConfig);
                    Directory.CreateDirectory(fullPathToUnzip);
                }
                using (System.IO.Packaging.Package Package = System.IO.Packaging.Package.Open(zipFileStream, FileMode.Open, FileAccess.Read))
                {
                    foreach (PackageRelationship Relationship in Package.GetRelationshipsByType("http://schemas.microsoft.com/opc/2006/sample/document"))
                    {
                        Uri         UriTarget = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), Relationship.TargetUri);
                        PackagePart document  = Package.GetPart(UriTarget);
                        location = (!unZipWithDirectoryStructure ? string.Concat(fullPathToUnzip, "\\", Path.GetFileName(HttpUtility.UrlDecode(document.Uri.ToString()).Replace('\\', '/'))) : string.Concat(fullPathToUnzip, HttpUtility.UrlDecode(document.Uri.ToString()).Replace('\\', '/')));
                        string folder = Path.GetDirectoryName(location);
                        try
                        {
                            this.ValidatePath(folder);
                        }
                        catch (DirectoryNotFoundException directoryNotFoundException1)
                        {
                            Log.WriteLog(string.Concat("Create Directory ", folder), "Log", this.GetType().Name, this.AppConfig);
                            Directory.CreateDirectory(folder);
                        }
                        byte[] Data = new byte[1024];
                        using (System.IO.FileStream FileStream = new System.IO.FileStream(location, FileMode.Create))
                        {
                            Stream DocumentStream = document.GetStream();
                            do
                            {
                                Size = DocumentStream.Read(Data, 0, 1024);
                                FileStream.Write(Data, 0, Size);
                            }while (Size == 1024);
                            FileStream.Close();
                        }
                        FileSelectionManager affectedFiles = this;
                        affectedFiles.AffectedFiles = affectedFiles.AffectedFiles + 1;
                        FileSelectionManager involvedFiles = this;
                        involvedFiles.InvolvedFiles = involvedFiles.InvolvedFiles + 1;
                    }
                    if (File.Exists(string.Concat(fullPathToUnzip, "\\[Content_Types].xml")))
                    {
                        File.Delete(string.Concat(fullPathToUnzip, "\\[Content_Types].xml"));
                    }
                }
            }
            catch (Exception exception)
            {
                Exception e = exception;
                Log.WriteLog(e.Message, "Error", this.GetType().Name, this.AppConfig);
                throw e;
            }
        }