Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / TrustUi / MS / Internal / documents / Application / WriteableOnDemandPackagePart.cs / 1 / WriteableOnDemandPackagePart.cs
//------------------------------------------------------------------------------ //// Copyright (C) Microsoft Corporation. All rights reserved. // //// This class acts as a type of proxy; it is responsible for forwarding all // PackagePart requests to the active PackagePart. Unlike a proxy it controls // which part is active. Initially the active part is the stream provided at // construction, at the time of the first write operation the active part is // replaced with one provided by a delegate. // // History: // 07/04/2005: [....]: Initial implementation. // 08/28/2005: [....]: Adjusted spacing and style to match team better. //----------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.IO; using System.IO.Packaging; using System.Text; using System.Windows.TrustUI; using MS.Internal.IO; namespace MS.Internal.Documents.Application { ////// This class acts as a type of proxy; it is responsible for forwarding all /// PackagePart requests to the active PackagePart. Unlike a proxy it is /// controls which part is active. Initially the active part is the stream /// provided on construction, at the time of the first write operation the /// active part is replaced with one provided by a delegate.The class will /// get a writing PackagePart only when needed. /// ////// When GetStreamCore is called instead of returning the stream directly we /// return a WriteableOnDemandStream a proxy. We provide this stream proxy /// a read only stream from the active part and a factory method for /// creating a writeable stream. /// /// When the WriteableStreamFactory method is called, if our active part is /// writable we simply forward a GetStream request to our active part. /// /// Our active part becomes writeable on the first call to our /// WriteableStreamFactory method, we use the delegate provide at our /// construction to get a writeable part and set it as the active part. /// /// Currently this is the only way our active part becomes writeable. /// internal sealed class WriteableOnDemandPackagePart : PackagePart { #region Constructors //------------------------------------------------------------------------- // Constructors //------------------------------------------------------------------------- ////// Constructs the class with readingPart as the active PackagePart. /// ////// The Package the PackagePart belongs to. /// /// The read only PackagePart. /// The delegate used to create a /// writing part when needed. internal WriteableOnDemandPackagePart( Package container, PackagePart readingPart, WriteablePackagePartFactoryDelegate writeablePartFactory) : base(container, readingPart.Uri) { if (container == null) { throw new ArgumentNullException("container"); } if (readingPart == null) { throw new ArgumentNullException("readingPart"); } if (writeablePartFactory == null) { throw new ArgumentNullException("writeablePartFactory"); } _activePart = readingPart; _getWriteablePartInstance = writeablePartFactory; } #endregion Constructors #region Internal Properties //-------------------------------------------------------------------------- // Internal Properties //------------------------------------------------------------------------- /// /// Sets the target PackagePart for this proxy. /// internal PackagePart Target { set { _activePart = value; _isActiveWriteable = false; } } #endregion Internal Properties #region Internal Delegates //-------------------------------------------------------------------------- // Internal Delegates //-------------------------------------------------------------------------- ////// This a delegate to get a writeable PackagePart. /// /// The active part that is requesting the /// writeable part. ///A writable PackagePart. internal delegate PackagePart WriteablePackagePartFactoryDelegate( PackagePart requestor); #endregion Internal Delegates #region Protected Methods - PackagePart Overrides //------------------------------------------------------------------------- // Protected Methods - PackagePart Overrides //-------------------------------------------------------------------------- #region Pure Proxies for PackagePart //------------------------------------------------------------------------- // This region of code simply follows the proxy patern and only forwards // the calls to the active part. //------------------------------------------------------------------------- ////// Gets the content type of this PackagePart. /// ///The content type of this PackagePart. protected override string GetContentTypeCore() { return _activePart.ContentType; } #endregion Pure Proxies for PackagePart #region Decorating Proxies for PackagePart //------------------------------------------------------------------------- // This region of code performs other actions before forwarding // to the active part. //-------------------------------------------------------------------------- ////// Returns a proxy to a stream. /// /// Desired FileMode. /// Desired FileAccess. ///A stream. protected override System.IO.Stream GetStreamCore( System.IO.FileMode mode, System.IO.FileAccess access) { Trace.SafeWrite( Trace.Packaging, "Creating a proxy stream for {0}#{1} for {2} access", _activePart.Uri, _activePart.GetHashCode(), access); // we do not need to wory about cleaning up this proxy // when we are closed our base class handles it return new WriteableOnDemandStream( _activePart.GetStream(FileMode.Open, FileAccess.Read), mode, access, WriteableStreamFactory); } #endregion Decorating Proxies for PackagePart #endregion Protected Methods - PackagePart Overrides #region Private Methods //------------------------------------------------------------------------- // Private Methods //-------------------------------------------------------------------------- ////// This will return a writable stream with the desired access. /// ////// /// Regardless of mode & access a writable stream is returned. /// /// Desired FileMode. /// Desired FileAccess. ///A writeable stream. private Stream WriteableStreamFactory( FileMode mode, FileAccess access) { if (!_isActiveWriteable) { Trace.SafeWrite( Trace.Packaging, "Creating a writeable stream for {0} with {1} access", _activePart.Uri, access); PackagePart writingPart = _getWriteablePartInstance(this); if (writingPart == null) { throw new IOException( SR.Get(SRID.PackagingWriteableDelegateGaveNullPart)); } if (writingPart.Equals(this)) { throw new IOException( SR.Get(SRID.PackagingCircularReference)); } _activePart = writingPart; _isActiveWriteable = true; } return _activePart.GetStream(mode, access); } #endregion Private Methods #region Private Fields //-------------------------------------------------------------------------- // Private Fields //------------------------------------------------------------------------- ////// The current comparee of this proxy. /// private PackagePart _activePart; ////// Is the active part a writing part? /// private bool _isActiveWriteable; ////// Will return a part we can use for writing. /// private WriteablePackagePartFactoryDelegate _getWriteablePartInstance; #endregion Private Fields } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- BridgeDataRecord.cs
- PropertyChange.cs
- MSG.cs
- XhtmlTextWriter.cs
- TreeView.cs
- ReflectTypeDescriptionProvider.cs
- Line.cs
- XmlSchemaSimpleContent.cs
- Convert.cs
- SafeThemeHandle.cs
- StateMachineWorkflow.cs
- HandlerFactoryCache.cs
- WorkflowServiceAttributes.cs
- DBConcurrencyException.cs
- RotateTransform.cs
- WebBaseEventKeyComparer.cs
- GroupQuery.cs
- StorageEntitySetMapping.cs
- XmlSerializationWriter.cs
- Intellisense.cs
- PartialTrustHelpers.cs
- AdornedElementPlaceholder.cs
- RowTypeElement.cs
- PathFigure.cs
- BitmapEffectGroup.cs
- DesignerEditorPartChrome.cs
- SiteMap.cs
- ConcurrentStack.cs
- RefType.cs
- PriorityQueue.cs
- DecoderNLS.cs
- CacheRequest.cs
- WebCategoryAttribute.cs
- SessionStateSection.cs
- WindowsTokenRoleProvider.cs
- DataTableExtensions.cs
- ImageMapEventArgs.cs
- _FtpControlStream.cs
- DBDataPermissionAttribute.cs
- SqlGatherConsumedAliases.cs
- WebHttpSecurityElement.cs
- Visual3D.cs
- TimersDescriptionAttribute.cs
- CustomAttributeFormatException.cs
- ByteRangeDownloader.cs
- ReadOnlyDataSource.cs
- ContourSegment.cs
- TextDecoration.cs
- PackagePart.cs
- CopyNodeSetAction.cs
- ResXResourceReader.cs
- WebEvents.cs
- SchemaImporter.cs
- EventItfInfo.cs
- PtsPage.cs
- _BasicClient.cs
- Signature.cs
- ProcessProtocolHandler.cs
- AsyncPostBackErrorEventArgs.cs
- ViewPort3D.cs
- OracleConnectionStringBuilder.cs
- XmlCharCheckingWriter.cs
- XsdCachingReader.cs
- MessageQueuePermission.cs
- DayRenderEvent.cs
- DesignerLoader.cs
- XmlWriterTraceListener.cs
- Documentation.cs
- WebPartCollection.cs
- SqlTypesSchemaImporter.cs
- DoubleAnimationBase.cs
- ScriptComponentDescriptor.cs
- RecognizedPhrase.cs
- TraceEventCache.cs
- CorrelationService.cs
- ListItemCollection.cs
- DataGridParentRows.cs
- ImageMapEventArgs.cs
- TextEffectResolver.cs
- BitmapEffect.cs
- CompressionTransform.cs
- DurableInstanceManager.cs
- CodePageUtils.cs
- LineServices.cs
- CustomTypeDescriptor.cs
- TraceSection.cs
- XPathNode.cs
- Cursors.cs
- DateTimeFormatInfoScanner.cs
- OpenTypeLayoutCache.cs
- Rfc2898DeriveBytes.cs
- SoapFault.cs
- DocumentEventArgs.cs
- UIntPtr.cs
- TypeForwardedToAttribute.cs
- MissingFieldException.cs
- ApplicationFileCodeDomTreeGenerator.cs
- securitycriticaldata.cs
- EmptyQuery.cs
- SharedStatics.cs