Пример #1
0
        /// <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);
        }
Пример #2
0
        private object Load(Stream stream, Uri parentUri, ParserContext pc, ContentType mimeType, string rootElement)
        {
            object      result    = null;
            List <Type> safeTypes = new List <Type>
            {
                typeof(ResourceDictionary)
            };

            if (!XpsValidatingLoader.DocumentMode)
            {
                if (rootElement == null)
                {
                    XmlReader reader = XmlReader.Create(stream, null, pc);
                    result = XamlReader.Load(reader, pc, XamlParseMode.Synchronous, FrameworkCompatibilityPreferences.DisableLegacyDangerousXamlDeserializationMode, safeTypes);
                    stream.Close();
                }
            }
            else
            {
                XpsSchema schema  = XpsSchema.GetSchema(mimeType);
                Uri       baseUri = pc.BaseUri;
                Uri       uri;
                Uri       uri2;
                PackUriHelper.ValidateAndGetPackUriComponents(baseUri, out uri, out uri2);
                Package package = PreloadedPackages.GetPackage(uri);
                if (parentUri != null)
                {
                    Uri packageUri = PackUriHelper.GetPackageUri(parentUri);
                    if (!packageUri.Equals(uri))
                    {
                        throw new FileFormatException(SR.Get("XpsValidatingLoaderUriNotInSamePackage"));
                    }
                }
                schema.ValidateRelationships(new SecurityCriticalData <Package>(package), uri, uri2, mimeType);
                if (schema.AllowsMultipleReferencesToSameUri(mimeType))
                {
                    this._uniqueUriRef = null;
                }
                else
                {
                    this._uniqueUriRef = new Hashtable(11);
                }
                Hashtable hashtable = (XpsValidatingLoader._validResources.Count > 0) ? XpsValidatingLoader._validResources.Peek() : null;
                if (schema.HasRequiredResources(mimeType))
                {
                    hashtable = new Hashtable(11);
                    PackagePart part = package.GetPart(uri2);
                    PackageRelationshipCollection relationshipsByType = part.GetRelationshipsByType(XpsValidatingLoader._requiredResourceRel);
                    foreach (PackageRelationship packageRelationship in relationshipsByType)
                    {
                        Uri         partUri = PackUriHelper.ResolvePartUri(uri2, packageRelationship.TargetUri);
                        Uri         key     = PackUriHelper.Create(uri, partUri);
                        PackagePart part2   = package.GetPart(partUri);
                        if (schema.IsValidRequiredResourceMimeType(part2.ValidatedContentType))
                        {
                            if (!hashtable.ContainsKey(key))
                            {
                                hashtable.Add(key, true);
                            }
                        }
                        else if (!hashtable.ContainsKey(key))
                        {
                            hashtable.Add(key, false);
                        }
                    }
                }
                XpsSchemaValidator xpsSchemaValidator = new XpsSchemaValidator(this, schema, mimeType, stream, uri, uri2);
                XpsValidatingLoader._validResources.Push(hashtable);
                if (rootElement != null)
                {
                    xpsSchemaValidator.XmlReader.MoveToContent();
                    if (!rootElement.Equals(xpsSchemaValidator.XmlReader.Name))
                    {
                        throw new FileFormatException(SR.Get("XpsValidatingLoaderUnsupportedMimeType"));
                    }
                    while (xpsSchemaValidator.XmlReader.Read())
                    {
                    }
                }
                else
                {
                    result = XamlReader.Load(xpsSchemaValidator.XmlReader, pc, XamlParseMode.Synchronous, FrameworkCompatibilityPreferences.DisableLegacyDangerousXamlDeserializationMode, safeTypes);
                }
                XpsValidatingLoader._validResources.Pop();
            }
            return(result);
        }