NGCSerializer.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Print / Reach / Serialization / manager / NGCSerializer.cs / 2 / NGCSerializer.cs

                            /*++ 

    Copyright (C) 2004- 2005 Microsoft Corporation
    All rights reserved.
 
    Module Name:
        ReachFixedDocumentSerializer.cs 
 
    Abstract:
 
    Author:
        [....] ([....]) 12-Jan-2005

    Revision History: 
--*/
 
using System; 
using System.Collections;
using System.Collections.Specialized; 
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using System.Xml; 
using System.IO;
using System.Security; 
using System.Security.Permissions; 
using System.ComponentModel.Design.Serialization;
using System.Windows.Xps.Packaging; 
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Markup;
using System.Printing; 

// 
// Ngc = Next Generation Converter. It means to convert the avalon element tree 
//  to the downlevel GDI primitives.
// 
// NgcSerializationManger will call ReachSerializer base class to invoke
// the Ngc serilizer. NgCSerializationManger.GetSerializerType will link
// the serilizer with each different type.
// Ngc will create the different serializer for the following reach serializer 
//      ReachFixedDocumentSerializer
//      ReachFixedPageSerializer 
//      ReachVisualSerializer 
//      ReachDocumentPageSerializer
//      ReachDocumentSequenceSerializer 
//      ReachIDocumentPaginatorSerializer
//      ReachPageContentCollectionSerializer
//      ReachPageContentSerializer
//      ReachUIElementCollectionSerializer 

namespace System.Windows.Xps.Serialization 
{ 
    static class NgcSerializerUtil
    { 
        internal static String InferJobName(object  o)
        {
            String  jobName = null;
 
            if(o != null)
            { 
                IFrameworkInputElement inputElement = o as IFrameworkInputElement; 
                if (inputElement != null)
                { 
                    jobName = inputElement.Name;
                }
                if (jobName == null || jobName.Length == 0)
                { 
                    jobName = o.ToString();
                } 
            } 

            return jobName; 
        }
    }

    ///  
    ///
    ///  
    internal class NgcFixedDocumentSerializer : 
                 ReachSerializer
    { 
        /// 
        ///
        /// 
        public 
        NgcFixedDocumentSerializer(
            PackageSerializationManager   manager 
            ): 
        base(manager)
        { 
            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            } 
        }
 
        ///  
        ///
        ///  
        public
        override
        void
        SerializeObject( 
            Object serializedObject
            ) 
        { 
            if (serializedObject == null)
            { 
                throw new ArgumentNullException("serializedObject");
            }
            FixedDocument fd = serializedObject as FixedDocument;
            if( fd == null ) 
            {
 
               throw new ArgumentException(ReachSR.Get(ReachSRID.ReachSerialization_ExpectedFixedDocument)); 
            }
            NgcSerializationManager ngcManager = SerializationManager as NgcSerializationManager; 

            ngcManager.StartDocument(fd,true);

            ReachSerializer serializer = ngcManager.GetSerializer(fd.Pages); 
            serializer.SerializeObject(fd.Pages);
 
            ngcManager.EndDocument(); 
        }
 

        /// 
        /// The method is called once the object data is discovered at that
        /// point of the serialization process. 
        /// 
        ///  
        /// The context of the object to be serialized at this time. 
        /// 
        internal 
        override
        void
        PersistObjectData(
            SerializableObjectContext   serializableObjectContext 
            )
        { 
            // 
            //We should not hit this method.
            // 
            throw new NotImplementedException();
        }
    };
 
    /// 
    /// 
    ///  
    internal class NgcFixedPageSerializer :
                 ReachSerializer 
    {
        /// 
        ///
        ///  
        public
        NgcFixedPageSerializer( 
            PackageSerializationManager   manager 
            ):
        base(manager) 
        {
            if (manager == null)
            {
                throw new ArgumentNullException("manager"); 
            }
        } 
 
        /// 
        /// 
        /// 
        public
        override
        void 
        SerializeObject(
            Object serializedObject 
            ) 
        {
            NgcSerializationManager ngcManager = SerializationManager as NgcSerializationManager; 
            if (serializedObject == null)
            {
                throw new ArgumentNullException("serializedObject");
            } 

            FixedPage fp = serializedObject as FixedPage; 
            if( fp == null ) 
            {
 
               throw new ArgumentException(ReachSR.Get(ReachSRID.ReachSerialization_ExpectedFixedPage));
            }

            bool bManualStartDoc = ngcManager.StartPage(); 

            Size pageSize = new Size(fp.Width, fp.Height); 
            ngcManager.PageSize = pageSize; 

            Visual visual = (Visual)serializedObject as Visual; 

            if (visual != null)
            {
                ngcManager.WalkVisual(visual); 
            }
 
            ngcManager.EndPage(); 
            if (bManualStartDoc)
            { 
                ngcManager.EndDocument();
            }
        }
 
        /// 
        /// The method is called once the object data is discovered at that 
        /// point of the serialization process. 
        /// 
        ///  
        /// The context of the object to be serialized at this time.
        /// 
        internal
        override 
        void
        PersistObjectData( 
            SerializableObjectContext   serializableObjectContext 
            )
        { 
            //
            //We should not hit this method.
            //
            throw new NotImplementedException(); 
        }
    }; 
 

    ///  
    ///
    /// 
    internal class NgcReachVisualSerializer :
                 ReachSerializer 
    {
        ///  
        /// 
        /// 
        public 
        NgcReachVisualSerializer(
            PackageSerializationManager   manager
            ):
        base(manager) 
        {
            if (manager == null) 
            { 
                throw new ArgumentNullException("manager");
            } 
        }

        /// 
        /// 
        /// 
        public 
        override 
        void
        SerializeObject( 
            Object serializedObject
            )
        {
 
            NgcSerializationManager NgcManager = SerializationManager as NgcSerializationManager;
 
            Visual visual = (Visual)serializedObject as Visual; 

            if (visual != null) 
            {
                NgcManager.WalkVisual(visual);
            }
        } 

        ///  
        /// 
        /// 
        internal 
        override
        void
        SerializeObject(
            SerializablePropertyContext serializedProperty 
            )
        { 
            // 
            // Do nothing here.
            // We do not support serializing visuals that come in as 
            // properties out of context of a FixedPage or a DocumentPage
            //
        }
 
        /// 
        /// The method is called once the object data is discovered at that 
        /// point of the serialization process. 
        /// 
        ///  
        /// The context of the object to be serialized at this time.
        /// 
        internal
        override 
        void
        PersistObjectData( 
            SerializableObjectContext   serializableObjectContext 
            )
        { 
            //
            //We should not hit this method.
            //
            throw new NotImplementedException(); 
        }
    }; 
 

 
    /// 
    ///
    /// 
    internal class NgcDocumentPageSerializer : 
                 ReachSerializer
    { 
        ///  
        ///
        ///  
        public
        NgcDocumentPageSerializer(
            PackageSerializationManager   manager
            ): 
        base(manager)
        { 
            if (manager == null) 
            {
                throw new ArgumentNullException("manager"); 
            }
        }

 
        /// 
        /// 
        ///  
        public
        override 
        void
        SerializeObject(
            Object serializedObject
            ) 
        {
 
            DocumentPage dp = serializedObject as DocumentPage; 
            if (dp != null)
            { 
                Toolbox.StartEvent(Toolbox.DRXGETVISUALGUID);
                Visual pageRootVisual = dp.Visual;
                Toolbox.EndEvent(Toolbox.DRXGETVISUALGUID);
 
                NgcSerializationManager NgcManager = SerializationManager as NgcSerializationManager;
 
                bool bManualStartDoc = NgcManager.StartPage(); 

                ReachSerializer serializer = SerializationManager.GetSerializer(pageRootVisual); 
                serializer.SerializeObject(pageRootVisual);

                NgcManager.EndPage();
                if (bManualStartDoc) 
                {
                    NgcManager.EndDocument(); 
                } 
            }
        } 


        /// 
        /// The method is called once the object data is discovered at that 
        /// point of the serialization process.
        ///  
        ///  
        /// The context of the object to be serialized at this time.
        ///  
        internal
        override
        void
        PersistObjectData( 
            SerializableObjectContext   serializableObjectContext
            ) 
        { 
            //
            //We should not hit this method. 
            //
            throw new NotImplementedException();
        }
    }; 

    ///  
    /// 
    /// 
    internal class NgcDocumentPaginatorSerializer : 
                 ReachSerializer
    {
        /// 
        /// 
        /// 
        public 
        NgcDocumentPaginatorSerializer( 
            PackageSerializationManager manager
            ) 
            :
        base(manager)
        {
 
        }
 
        ///  
        ///
        ///  
        public
        override
        void
        SerializeObject( 
            Object serializedObject
            ) 
        { 
            NgcSerializationManager ngcManager = SerializationManager as NgcSerializationManager;
            DocumentPaginator paginator = (DocumentPaginator)serializedObject; 

            //
            // For FlowDocument, the application might attach a PrintTicket DP on it.
            // 
            DependencyObject dependencyObject = paginator != null ? paginator.Source as DependencyObject : null;
            if (dependencyObject != null) 
            { 
                if(!ngcManager.IsPrintTicketEventHandlerEnabled)
                { 
                    //ngcManager.FdPrintTicket = dependencyObject.GetValue(FixedDocument.PrintTicketProperty) as PrintTicket;
                }
            }
 
            ngcManager.StartDocument(paginator,true);
 
            if (paginator != null) 
            {
                for (int i = 0; !paginator.IsPageCountValid || (i < paginator.PageCount); i++) 
                {
                    DocumentPage page = Toolbox.GetPage(paginator, i);

                    ReachSerializer serializer = SerializationManager.GetSerializer(page); 

                    if (serializer != null) 
                    { 
                        serializer.SerializeObject(page);
                    } 
                }
            }

            ngcManager.EndDocument(); 
        }
 
        ///  
        /// The method is called once the object data is discovered at that
        /// point of the serialization process. 
        /// 
        /// 
        /// The context of the object to be serialized at this time.
        ///  
        internal
        override 
        void 
        PersistObjectData(
            SerializableObjectContext   serializableObjectContext 
            )
        {
            //
            //We should not hit this method. 
            //
            throw new NotImplementedException(); 
        } 

    }; 

    /// 
    ///
    ///  
    internal class NgcDocumentSequenceSerializer :
                 ReachSerializer 
    { 
        /// 
        /// 
        /// 
        public
        NgcDocumentSequenceSerializer(
            PackageSerializationManager manager 
            )
            : 
        base(manager) 
        {
 
        }

        /// 
        /// 
        /// 
        public 
        override 
        void
        SerializeObject( 
            Object serializedObject
            )
        {
            if (serializedObject == null) 
            {
                throw new ArgumentNullException("serializedObject"); 
            } 
            FixedDocumentSequence fds = serializedObject as FixedDocumentSequence;
            if( fds == null ) 
            {

               throw new ArgumentException(ReachSR.Get(ReachSRID.ReachSerialization_ExpectedFixedDocumentSequence));
            } 

            NgcSerializationManager ngcManager = SerializationManager as NgcSerializationManager; 
 
            if(!ngcManager.IsPrintTicketEventHandlerEnabled)
            { 
                //ngcManager.FdsPrintTicket = fds.PrintTicket as PrintTicket;
            }
            else
            { 
                XpsSerializationPrintTicketRequiredEventArgs printTicketEvent =
                new XpsSerializationPrintTicketRequiredEventArgs(PrintTicketLevel.FixedDocumentSequencePrintTicket, 
                                                                 0); 
                ngcManager.OnNGCSerializationPrintTicketRequired(printTicketEvent);
            } 

            ngcManager.StartDocument(fds,false);

            ReachSerializer serializer = ngcManager.GetSerializer(fds.References); 
            serializer.SerializeObject(fds.References);
 
            ngcManager.EndDocument(); 

            XpsSerializationProgressChangedEventArgs e = 
            new XpsSerializationProgressChangedEventArgs(XpsWritingProgressChangeLevel.FixedDocumentSequenceWritingProgress,
                                                         0,
                                                         0,
                                                         null); 
            ngcManager.OnNGCSerializationProgressChagned(e);
        } 
 
        /// 
        /// The method is called once the object data is discovered at that 
        /// point of the serialization process.
        /// 
        /// 
        /// The context of the object to be serialized at this time. 
        /// 
        internal 
        override 
        void
        PersistObjectData( 
            SerializableObjectContext   serializableObjectContext
            )
        {
            // 
            //We should not hit this method.
            // 
            throw new NotImplementedException(); 
        }
 
     };

    /// 
    /// 
    /// 
    internal class NgcDocumentReferenceCollectionSerializer : 
                 ReachSerializer 
    {
        ///  
        ///
        /// 
        public
        NgcDocumentReferenceCollectionSerializer( 
            PackageSerializationManager manager
            ): 
        base(manager) 
        {
 
        }

        /// 
        /// 
        /// 
        public 
        override 
        void
        SerializeObject( 
            Object serializedObject
            )
        {
            SerializeDocumentReferences( serializedObject ); 
        }
 
        ///  
        /// This is being called to serialize the Document Reference items
        /// contained within the colleciton 
        /// 
        private
        void
        SerializeDocumentReferences( 
            object serializableObject
            ) 
        { 
            System.Collections.Generic.IEnumerable drList = (System.Collections.Generic.IEnumerable)serializableObject;
            // 
            // Serialize each PageContent in PageContentColleciton
            //
            if (drList != null)
            { 
                foreach (object documentReference in drList)
                { 
                    if (documentReference != null) 
                    {
                        // 
                        // Serialize the current ui element
                        //
                        SerializeDocumentReference((DocumentReference)documentReference);
                    } 
                }
            } 
        } 

 
        /// 
        ///     Called to serialize a single DocumentReferenceElement
        /// 
        private 
        void
        SerializeDocumentReference( 
            DocumentReference dre 
            )
        { 
            IDocumentPaginatorSource idp = dre.GetDocument(false);

            if (idp != null)
            { 
                FixedDocument fixedDoc = idp as FixedDocument;
 
                if (fixedDoc != null) 
                {
                    ReachSerializer serializer = SerializationManager.GetSerializer(fixedDoc); 
                    if (serializer != null)
                    {
                        serializer.SerializeObject(fixedDoc);
                    } 
                }
                else 
                { 
                    ReachSerializer serializer = SerializationManager.GetSerializer(idp.DocumentPaginator);
                    if (serializer != null) 
                    {
                        serializer.SerializeObject(idp);
                    }
                } 
            }
        } 
 
        /// 
        /// The method is called once the object data is discovered at that 
        /// point of the serialization process.
        /// 
        /// 
        /// The context of the object to be serialized at this time. 
        /// 
        internal 
        override 
        void
        PersistObjectData( 
            SerializableObjectContext   serializableObjectContext
            )
        {
            // 
            //We should not hit this method.
            // 
            throw new NotImplementedException(); 
        }
    }; 

    /// 
    /// Class defining common functionality required to
    /// serialize a PageContentCollectionSerializer. 
    /// 
    internal class NGCReachPageContentCollectionSerializer : 
                   ReachSerializer 
    {
        #region Constructor 

        /// 
        /// Constructor for class ReachPageContentCollectionSerializer
        ///  
        /// 
        /// The serialization manager, the services of which are 
        /// used later in the serialization process of the type. 
        /// 
        public 
        NGCReachPageContentCollectionSerializer(
            PackageSerializationManager manager
            ):
        base(manager) 
        {
 
        } 

        #endregion Constructor 


        /// 
        /// 
        /// 
        public 
        override 
        void
        SerializeObject( 
            Object serializedObject
            )
        {
            IEnumerable enumerableObject =  serializedObject as IEnumerable; 

            if (enumerableObject == null) 
            { 
                throw new XpsSerializationException(ReachSR.Get(ReachSRID.MustBeOfType, "serializableObjectContext.TargetObject", typeof(IEnumerable)));
            } 

            //
            // Serialize the PageContent Items contained within the collection
            // 
            SerializePageContents(enumerableObject);
        } 
 
        #region Private Methods
 
        /// 
        /// This is being called to serialize the Page Content items
        /// contained within the collection
        ///  
        /// 
        /// The context of the object to be serialized at this time. 
        ///  
        private
        void 
        SerializePageContents(
             IEnumerable enumerableObject
            )
        { 
            //
            // Serialize each PageContent in PageContentColleciton 
            // 
            foreach (object pageContent in enumerableObject)
            { 
                if (pageContent != null)
                {
                    // Serialize the current item
                    SerializePageContent(pageContent); 
                }
            } 
        } 

 
        /// 
        /// Called to serialize a single PageContent
        /// 
        ///  
        /// The PageContent to be serialized.
        ///  
        private 
        void
        SerializePageContent( 
            object pageContent
            )
        {
            Toolbox.StartEvent(MS.Utility.EventTraceGuidId.DRXSAVEPAGEGUID); 

            ReachSerializer serializer = SerializationManager.GetSerializer(pageContent); 
 
            if(serializer!=null)
            { 
                serializer.SerializeObject(pageContent);
            }
            else
            { 
                throw new XpsSerializationException(ReachSR.Get(ReachSRID.ReachSerialization_NoSerializer));
            } 
 
            Toolbox.EndEvent(MS.Utility.EventTraceGuidId.DRXSAVEPAGEGUID);
        } 

        #endregion Private Methods

        ///  
        /// The method is called once the object data is discovered at that
        /// point of the serialization process. 
        ///  
        /// 
        /// The context of the object to be serialized at this time. 
        /// 
        internal
        override
        void 
        PersistObjectData(
            SerializableObjectContext   serializableObjectContext 
            ) 
        {
            // 
            //We should not hit this method.
            //
            throw new NotImplementedException();
        } 
    };
 
    ///  
    /// Class defining common functionality required to
    /// serialize a ReachPageContentSerializer. 
    /// 
    internal class NGCReachPageContentSerializer :
                   ReachSerializer
    { 
        #region Constructor
 
        ///  
        /// Constructor for class ReachPageContentSerializer
        ///  
        /// 
        /// The serialization manager, the services of which are
        /// used later in the serialization process of the type.
        ///  
        public
        NGCReachPageContentSerializer( 
            PackageSerializationManager   manager 
            ):
        base(manager) 
        {

        }
 
        #endregion Constructor
        ///  
        /// 
        /// 
        public 
        override
        void
        SerializeObject(
            Object serializedObject 
            )
        { 
            FixedPage fixedPage = Toolbox.GetPageRoot(serializedObject); 

            if(fixedPage != null) 
            {
                ReachSerializer serializer = SerializationManager.GetSerializer(fixedPage);

                if(serializer!=null) 
                {
                    NgcSerializationManager manager = SerializationManager as NgcSerializationManager; 
 
                    XpsSerializationPrintTicketRequiredEventArgs e =
                        new XpsSerializationPrintTicketRequiredEventArgs(PrintTicketLevel.FixedPagePrintTicket, 
                                                                                                 0);

                    manager.OnNGCSerializationPrintTicketRequired(e);
 
                    Toolbox.Layout(fixedPage, manager.GetActivePrintTicket());
                    serializer.SerializeObject(fixedPage); 
                } 
                else
                { 
                    throw new XpsSerializationException(ReachSR.Get(ReachSRID.ReachSerialization_NoSerializer));
                }
            }
        } 

 
        ///  
        /// The method is called once the object data is discovered at that
        /// point of the serialization process. 
        /// 
        /// 
        /// The context of the object to be serialized at this time.
        ///  
        internal
        override 
        void 
        PersistObjectData(
            SerializableObjectContext   serializableObjectContext 
            )
        {
            //
            //We should not hit this method. 
            //
            throw new NotImplementedException(); 
        } 
    }
 
    /// 
    /// Class defining common functionality required to
    /// serialize a UIElementCollection.
    ///  
    internal class NGCReachUIElementCollectionSerializer :
                   ReachSerializer 
    { 
        #region Constructor
 
        /// 
        /// Constructor for class ReachUIElementCollectionSerializer
        /// 
        ///  
        /// The serialization manager, the services of which are
        /// used later in the serialization process of the type. 
        ///  
        public
        NGCReachUIElementCollectionSerializer( 
            PackageSerializationManager manager
            ):
        base(manager)
        { 

        } 
 
        #endregion Constructor
 
        #region Public Methods

        /// 
        /// The main method that is called to serialize a UIElementCollection. 
        /// 
        ///  
        /// Instance of object to be serialized. 
        /// 
        public 
        override
        void
        SerializeObject(
            Object serializedObject 
            )
        { 
           IEnumerable enumerableObject = serializedObject as IEnumerable; 

            if (enumerableObject == null) 
            {
                throw new XpsSerializationException(ReachSR.Get(ReachSRID.MustBeOfType, "serializableObjectContext.TargetObject", typeof(IEnumerable)));
            }
 
            //
            // Serialize the PageContent Items contained within the collection 
            // 
            SerializeUIElements(enumerableObject);
       } 

        #endregion Public Methods

        #region Private Methods 

        ///  
        /// This is being called to serialize the Page Content items 
        /// contained within the collection
        ///  
        private
        void
        SerializeUIElements(
            IEnumerable enumerableObject 
            )
        { 
            // 
            // Serialize each PageContent in PageContentCollection
            // 
            foreach (object uiElement in enumerableObject)
            {
                if (uiElement != null)
                { 
                    //
                    // Serialize the current ui element 
                    // 
                    SerializeUIElement(uiElement);
                } 
            }
        }

 
        /// 
        /// Called to serialize a single UIElement 
        ///  
        private
        void 
        SerializeUIElement(
            object uiElement
            )
        { 
            Visual visual = uiElement as Visual;
 
            if(visual != null) 
            {
                ReachSerializer serializer = SerializationManager.GetSerializer(visual); 

                if(serializer!=null)
                {
                    serializer.SerializeObject(visual); 
                }
                else 
                { 
                    throw new XpsSerializationException(ReachSR.Get(ReachSRID.ReachSerialization_NoSerializer));
                } 
            }
        }

        #endregion Private Methods 

        ///  
        /// The method is called once the object data is discovered at that 
        /// point of the serialization process.
        ///  
        /// 
        /// The context of the object to be serialized at this time.
        /// 
        internal 
        override
        void 
        PersistObjectData( 
            SerializableObjectContext   serializableObjectContext
            ) 
        {
            //
            //We should not hit this method.
            // 
            throw new NotImplementedException();
        } 
    }; 
}
 


// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK