Create() public static méthode

public static Create ( Uri packageUri ) : Uri
packageUri System.Uri
Résultat System.Uri
        /// <summary>
        /// Adds a uri, package pair to the package store.
        /// </summary>
        /// <param name="uri">key uri</param>
        /// <param name="package">package</param>
        /// <permission cref="EnvironmentPermission"></permission>
        /// <remarks>
        /// If a package with the uri is already in the store,it throws an exception.
        /// The package will not be automatically replaced within the store.
        /// </remarks>
        ///<SecurityNote>
        /// Demands EnvironmentPermission() if package is custom type of Package.
        /// This prevents Partially Trusted callers from performing this operation. However, Partially Trusted callers can still
        /// add well-known platform Package type (ZipPackage) to PackageStore.
        /// the application's PackageStore.
        ///</SecurityNote>
        public static void AddPackage(Uri uri, Package package)
        {
            // Allow well known platform Package to be added into PackageStore under Partial Trust.
            // Otherwise, demand Environment Permission to make sure only Full Trust app can add a custom Package
            DemandSecurityPermissionIfCustomPackage(package);

            ValidatePackageUri(uri);

            // There are well-known package types that are only for internal use (for resource loading)
            //  (i.e. ResourceContainer - "application://" and SiteOriginContainer - "siteoforigin://"
            // Adding packages with such key uri will have no effect on PackWebRequest since
            //  they cannot be overriden. So, calling this method with such key Uris should be prevented
            //  However, uri.Equal cannot be used here since the key Uris are used as a pack Uri form and
            //  only PackUriHelper.ComparePackUri can do the proper comparison of pack Uris.

            Uri packUri = PackUriHelper.Create(uri);

            if (PackUriHelper.ComparePackUri(packUri, BaseUriHelper.PackAppBaseUri) == 0 ||
                PackUriHelper.ComparePackUri(packUri, BaseUriHelper.SiteOfOriginBaseUri) == 0)
            {
                throw new ArgumentException(SR.Get(SRID.NotAllowedPackageUri), "uri");
            }

            if (package == null)
            {
                throw new ArgumentNullException("package");
            }

            lock (_globalLock)
            {
                if (_packages == null)
                {
                    _packages = new HybridDictionary(2);
                }

                if (_packages.Contains(uri))
                {
                    throw new InvalidOperationException(SR.Get(SRID.PackageAlreadyExists));
                }

                _packages.Add(uri, package);
            }
        }
Exemple #2
0
        GetFixedDocumentSequence(
            )
        {
            CheckDisposed();

            if (CurrentXpsManager == null)
            {
                throw new InvalidOperationException(SR.Get(SRID.ReachPackaging_DocumentWasClosed));
            }
            if (!IsReader)
            {
                throw new XpsPackagingException(SR.Get(SRID.ReachPackaging_NotOpenForReading));
            }

            if (null == Uri)
            {
                throw new XpsPackagingException(SR.Get(SRID.ReachPackaging_PackageUriNull));
            }

            if (CurrentXpsManager.StartingPart == null)
            {
                return(null);
            }

            ContentType startPartType = CurrentXpsManager.StartingPart.ValidatedContentType();

            if (!startPartType.AreTypeAndSubTypeEqual(XpsS0Markup.DocumentSequenceContentType))
            {
                throw new XpsPackagingException(SR.Get(SRID.ReachPackaging_InvalidStartingPart));
            }
            ParserContext parserContext = new ParserContext();

            parserContext.BaseUri = PackUriHelper.Create(Uri, CurrentXpsManager.StartingPart.Uri);

            object fixedObject = XamlReader.Load(CurrentXpsManager.StartingPart.GetStream(), parserContext);

            if (!(fixedObject is FixedDocumentSequence))
            {
                throw new XpsPackagingException(SR.Get(SRID.ReachPackaging_NotAFixedDocumentSequence));
            }
            return(fixedObject as FixedDocumentSequence);
        }
        /// <summary>
        /// rootElement == null: Load elements, validation of root element will occur in caller by checking object type or casting
        /// rootElement != null: Only perform validation, and expect rootElement at root of markup
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="parentUri"></param>
        /// <param name="pc"></param>
        /// <param name="mimeType"></param>
        /// <param name="rootElement"></param>
        /// <returns></returns>
        private object Load(Stream stream, Uri parentUri, ParserContext pc, ContentType mimeType, string rootElement)
        {
            object obj = null;

            if (!DocumentMode)
            {                       // Loose XAML, just check against schema, don't check content type
                if (rootElement == null)
                {
                    obj = XamlReader.Load(stream, pc);
                }
            }
            else
            {                       // inside an XPS Document. Perform maximum validation
                XpsSchema schema = XpsSchema.GetSchema(mimeType);
                Uri       uri    = pc.BaseUri;

                Uri packageUri = PackUriHelper.GetPackageUri(uri);
                Uri partUri    = PackUriHelper.GetPartUri(uri);

                Package package = PreloadedPackages.GetPackage(packageUri);

                Uri parentPackageUri = null;

                if (parentUri != null)
                {
                    parentPackageUri = PackUriHelper.GetPackageUri(parentUri);
                    if (!parentPackageUri.Equals(packageUri))
                    {
                        throw new FileFormatException(SR.Get(SRID.XpsValidatingLoaderUriNotInSamePackage));
                    }
                }

                schema.ValidateRelationships(new SecurityCriticalData <Package>(package), packageUri, partUri, mimeType);

                if (schema.AllowsMultipleReferencesToSameUri(mimeType))
                {
                    _uniqueUriRef = null;
                }
                else
                {
                    _uniqueUriRef = new Hashtable(11);
                }

                Hashtable validResources = (_validResources.Count > 0 ? _validResources.Peek() : null);
                if (schema.HasRequiredResources(mimeType))
                {
                    validResources = new Hashtable(11);

                    PackagePart part = package.GetPart(partUri);
                    PackageRelationshipCollection requiredResources = part.GetRelationshipsByType(_requiredResourceRel);

                    foreach (PackageRelationship relationShip in requiredResources)
                    {
                        Uri targetUri    = PackUriHelper.ResolvePartUri(partUri, relationShip.TargetUri);
                        Uri absTargetUri = PackUriHelper.Create(packageUri, targetUri);

                        PackagePart targetPart = package.GetPart(targetUri);

                        if (schema.IsValidRequiredResourceMimeType(targetPart.ValidatedContentType()))
                        {
                            if (!validResources.ContainsKey(absTargetUri))
                            {
                                validResources.Add(absTargetUri, true);
                            }
                        }
                        else
                        {
                            if (!validResources.ContainsKey(absTargetUri))
                            {
                                validResources.Add(absTargetUri, false);
                            }
                        }
                    }
                }

                XpsSchemaValidator xpsSchemaValidator = new XpsSchemaValidator(this, schema, mimeType,
                                                                               stream, packageUri, partUri);
                _validResources.Push(validResources);
                if (rootElement != null)
                {
                    xpsSchemaValidator.XmlReader.MoveToContent();

                    if (!rootElement.Equals(xpsSchemaValidator.XmlReader.Name))
                    {
                        throw new FileFormatException(SR.Get(SRID.XpsValidatingLoaderUnsupportedMimeType));
                    }

                    while (xpsSchemaValidator.XmlReader.Read())
                    {
                        ;
                    }
                }
                else
                {
                    obj = XamlReader.Load(xpsSchemaValidator.XmlReader,
                                          pc,
                                          XamlParseMode.Synchronous);
                }
                _validResources.Pop();
            }

            return(obj);
        }