Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Move iterator to the next part that has an associated filter and (re)initialize the
        /// relevant filter.
        /// </summary>
        /// <remarks>
        /// This function results in _progress and _currentFilter being updated.
        /// </remarks>
        private void MoveToNextFilter()
        {
            // Reset _isInternalFilter.
            _isInternalFilter = false;

            switch (_progress)
            {
            case Progress.FilteringNotStarted:

                #region Progress.FilteringNotStarted

                // Filtering not started yet. Start with core properties filter.

                IndexingFilterMarshaler corePropertiesFilterMarshaler
                    = new IndexingFilterMarshaler(
                          new CorePropertiesFilter(_package.PackageProperties));

                // Avoid exception on end of chunks from part filter.
                corePropertiesFilterMarshaler.ThrowOnEndOfChunks = false;

                _currentFilter = corePropertiesFilterMarshaler;
                _currentFilter.Init(_grfFlags, _cAttributes, _aAttributes);
                _isInternalFilter = true;

                // Update progress to indicate filtering core properties.
                _progress = Progress.FilteringCoreProperties;

                break;

                #endregion Progress.FilteringNotStarted

            case Progress.FilteringCoreProperties:

                #region Progress.FilteringCoreProperties

                // Core properties were being filtered. Next move to content filtering.

                #endregion Progress.FilteringCoreProperties

            case Progress.FilteringContent:

                #region Progress.FilteringContent

                //
                // Content being filtered. Move to next content part filter if it exists.
                // Update progress to indicate filtering content if there is a next content
                // filter, else to indicate filtering is completed.
                //

                if (_currentStream != null)
                {
                    // Close the stream for the previous PackagePart.
                    _currentStream.Close();
                    _currentStream = null;
                }

                for (_currentFilter = null; _partIterator.MoveNext(); _currentFilter = null)
                {
                    PackagePart currentPart = (PackagePart)_partIterator.Current;
                    ContentType contentType = currentPart.ValidatedContentType();

                    // Find the filter's CLSID based on the MIME content type.
                    string filterClsid = GetFilterClsid(contentType, currentPart.Uri);
                    if (filterClsid != null)
                    {
                        _currentFilter = GetFilterFromClsid(new Guid(filterClsid));
                        if (_currentFilter != null)
                        {
                            _currentStream = currentPart.GetStream();
                            ManagedIStream stream = new ManagedIStream(_currentStream);
                            try
                            {
                                IPersistStreamWithArrays filterLoader = (IPersistStreamWithArrays)_currentFilter;
                                filterLoader.Load(stream);
                                _currentFilter.Init(_grfFlags, _cAttributes, _aAttributes);

                                // Filter found and properly initialized. Search is over.
                                break;
                            }
                            catch (InvalidCastException)
                            {
                                // If a filter does not implement IPersistStream, then, by design, it should
                                // be ignored.
                            }
                            catch (COMException)
                            {
                                // Any initialization bug giving rise to an exception in the initialization
                                // code should be ignored, since this will be due to faulty external code.
                            }
                            catch (IOException)
                            {
                                // Initialization problem can be reported as IOException. See preceding comment.
                            }
                        }
                    }

                    //
                    // No valid externally registered filters found for this content part.
                    // If this is xaml part, use the internal XamlFilter.
                    //

                    if (BindUriHelper.IsXamlMimeType(contentType))
                    {
                        if (_currentStream == null)
                        {
                            _currentStream = currentPart.GetStream();
                        }

                        IndexingFilterMarshaler xamlFilterMarshaler
                            = new IndexingFilterMarshaler(new XamlFilter(_currentStream));

                        // Avoid exception on end of chunks from part filter.
                        xamlFilterMarshaler.ThrowOnEndOfChunks = false;

                        _currentFilter = xamlFilterMarshaler;
                        _currentFilter.Init(_grfFlags, _cAttributes, _aAttributes);
                        _isInternalFilter = true;

                        // Filter found and properly initialized. Search is over.
                        break;
                    }

                    if (_currentStream != null)
                    {
                        _currentStream.Close();
                        _currentStream = null;
                    }
                }

                if (_currentFilter == null)
                {
                    // Update progress to indicate filtering is completed.
                    _progress = Progress.FilteringCompleted;
                }
                else
                {
                    // Tell GetChunk that we are getting input from a new filter.
                    _firstChunkFromFilter = true;

                    // Update progress to indicate content being filtered.
                    _progress = Progress.FilteringContent;
                }
                break;

                #endregion Progress.FilteringContent

            case Progress.FilteringCompleted:

                #region Progress.FilteringCompleted

                Debug.Assert(false);
                break;

                #endregion Progress.FilteringCompleted

            default:

                #region Default

                Debug.Assert(false);
                break;

                #endregion Default
            }
        }