コード例 #1
0
        public override void WriteValue(object value)
        {
            this._handled = false;
            switch (this._mode)
            {
            case DeferringMode.Off:
                return;

            case DeferringMode.TemplateStarting:
                if (!(value is XamlNodeList))
                {
                    this.StartDeferredList();
                    this._mode = DeferringMode.TemplateDeferring;
                    break;
                }
                this._deferredList = (XamlNodeList)value;
                this._mode         = DeferringMode.TemplateReady;
                this._handled      = true;
                return;

            case DeferringMode.TemplateDeferring:
                break;

            case DeferringMode.TemplateReady:
                throw new XamlInternalException(System.Xaml.SR.Get("TemplateNotCollected", new object[] { "WriteValue" }));

            default:
                throw new XamlInternalException(System.Xaml.SR.Get("MissingCase", new object[] { this._mode.ToString(), "WriteValue" }));
            }
            this._deferredWriter.WriteValue(value);
            this._handled = true;
        }
 public XamlNodeList CollectTemplateList()
 {
     XamlNodeList list = this._deferredList;
     this._deferredList = null;
     this._mode = DeferringMode.Off;
     return list;
 }
コード例 #3
0
 public void Clear()
 {
     this._handled           = false;
     this._mode              = DeferringMode.Off;
     this._deferredList      = null;
     this._deferredTreeDepth = -1;
 }
コード例 #4
0
ファイル: DeferredWriter.cs プロジェクト: redmcg/wpf
 public void Clear()
 {
     _handled           = false;
     _mode              = DeferringMode.Off;
     _deferredList      = null;
     _deferredTreeDepth = -1;
 }
 public void Clear()
 {
     this._handled = false;
     this._mode = DeferringMode.Off;
     this._deferredList = null;
     this._deferredTreeDepth = -1;
 }
コード例 #6
0
        public XamlNodeList CollectTemplateList()
        {
            XamlNodeList list = this._deferredList;

            this._deferredList = null;
            this._mode         = DeferringMode.Off;
            return(list);
        }
コード例 #7
0
ファイル: DeferredWriter.cs プロジェクト: redmcg/wpf
        public XamlNodeList CollectTemplateList()
        {
            XamlNodeList retValue = _deferredList;

            _deferredList = null;
            _mode         = DeferringMode.Off;
            return(retValue);
        }
コード例 #8
0
ファイル: DeferredWriter.cs プロジェクト: redmcg/wpf
 private void StartDeferredList()
 {
     // the list may have been created already by SetLineInfo
     if (_deferredList == null)
     {
         _deferredList             = new XamlNodeList(_context.SchemaContext);
         _deferredWriter           = _deferredList.Writer;
         _deferredLineInfoConsumer = _deferredWriter as IXamlLineInfoConsumer;
         _deferredTreeDepth        = 0;
     }
 }
コード例 #9
0
 internal static void WriteNodeList(XamlWriter writer, XamlNodeList nodeList)
 {
     // We need to pass the ErrorNodes contents through as a NodeList, because XOW doesn't
     // currently support unknown types, even inside a DeferLoad block.
     // But if a NodeList is written to XOW as a Value, XOW will unpack, forcing us to re-buffer
     // the nodes in our deferring loader. So we wrap the NodeList value inside a dummy StartObject.
     writer.WriteStartObject(XamlLanguage.Object);
     writer.WriteStartMember(XamlLanguage.Initialization);
     writer.WriteValue(nodeList);
     writer.WriteEndMember();
     writer.WriteEndObject();
 }
コード例 #10
0
        public override object Load(XamlReader xamlReader, IServiceProvider serviceProvider)
        {
            IXamlObjectWriterFactory objectWriterFactory =
                (IXamlObjectWriterFactory) serviceProvider.GetService(typeof(IXamlObjectWriterFactory));

            IXamlSchemaContextProvider schema =
                (IXamlSchemaContextProvider)serviceProvider.GetService(typeof(IXamlSchemaContextProvider));

            XamlSchemaContext schemaContext = xamlReader.SchemaContext;

            XamlNodeList nodeList = new XamlNodeList(schema.SchemaContext);
            Dictionary<string, Type> typesByName = new Dictionary<string, Type>();
            bool nextValueIsName = false;
            Type currentType = null;

            using (XamlWriter writer = nodeList.Writer)
            {
                while (xamlReader.Read())
                {
                    switch (xamlReader.NodeType)
                    {
                        case XamlNodeType.StartObject:
                            currentType = xamlReader.Type.UnderlyingType;
                            break;

                        case XamlNodeType.StartMember:
                            // HACK: This matches any Name property. Should probably just match
                            // FrameworkElement and x:Name but this'll do for now...
                            if (xamlReader.Member.Name == "Name")
                            {
                                nextValueIsName = true;
                            }

                            break;

                        case XamlNodeType.Value:
                            if (nextValueIsName)
                            {
                                typesByName.Add((string)xamlReader.Value, currentType);
                                nextValueIsName = false;
                            }

                            break;
                    }

                    writer.WriteNode(xamlReader);
                }
            }

            return new DHierarchyTemplateContent(nodeList, typesByName);
        }
コード例 #11
0
 public DynamicActivityXamlType(XamlNodeList xamlNodes, IEnumerable<string> xamlNamespaces)
     : base(LoadAndValidateActivity(xamlNodes.CheckNotNull("xamlNodes")), xamlNodes.Writer.SchemaContext)
 {
     XamlNodes = xamlNodes;
     if (xamlNamespaces != null)
     {
         List<string> namespaces = xamlNamespaces.ToList();
         if (namespaces.Count > 0)
         {
             namespaces.Add(ClrNamespace);
             _namespaces = namespaces.AsReadOnly();
         }
     }
 }
コード例 #12
0
        public void WriteDeferred(XamlDeferringLoader loader, XamlNodeList nodeList, bool setValue)
        {
            nodeList.Writer.Close();
            var obj = loader.Load(nodeList.GetReader(), service_provider);
            var cms = CurrentMemberState;

            if (cms != null)
            {
                cms.Value = obj;
            }
            else
            {
                var state = object_states.Peek();
                state.Value          = obj;
                state.IsInstantiated = true;
            }
            if (setValue)
            {
                manager.Value();
            }
        }
コード例 #13
0
ファイル: DeferredWriter.cs プロジェクト: redmcg/wpf
        public override void WriteValue(object value)
        {
            _handled = false;
            switch (_mode)
            {
            case DeferringMode.Off:
                break;

            case DeferringMode.TemplateReady:
                throw new XamlInternalException(SR.Get(SRID.TemplateNotCollected, "WriteValue"));

            case DeferringMode.TemplateStarting:
                // This handles the case of SM template; V object; EM
                Debug.Assert(_deferredTreeDepth == 0);
                if (value is XamlNodeList)
                {
                    _deferredList = (XamlNodeList)value;
                    _mode         = DeferringMode.TemplateReady;
                    _handled      = true;
                }
                else
                {
                    StartDeferredList();
                    _mode = DeferringMode.TemplateDeferring;
                    goto case DeferringMode.TemplateDeferring;
                }
                break;

            case DeferringMode.TemplateDeferring:
                _deferredWriter.WriteValue(value);
                _handled = true;
                break;

            default:
                throw new XamlInternalException(SR.Get(SRID.MissingCase, _mode.ToString(), "WriteValue"));
            }
        }
コード例 #14
0
        // This method reads ViewStateManager nodes from the xaml nodestream and outputs that in the
        // viewStateInfo dictionary. The input reader is positioned at the begining of the workflow definition.
        // The method returns a reader positioned at the begining of the workflow definition with the ViewStateManager 
        // nodes removed.
        static XamlReader StripViewStateElement(XamlReader inputReader, out Dictionary<string, XamlNodeList> viewStateInfo, out Dictionary<string, SourceLocation> viewStateSourceLocationMap)
        {
            viewStateSourceLocationMap = null;
            XamlNodeList strippedNodeList = new XamlNodeList(inputReader.SchemaContext);
            XamlMember viewStateManager = new XamlMember(ViewStateManager, GetViewStateManager, SetViewStateManager, inputReader.SchemaContext);
            using (XamlWriter strippedWriter = strippedNodeList.Writer)
            {
                IXamlLineInfo lineInfo = inputReader as IXamlLineInfo;
                IXamlLineInfoConsumer lineInfoComsumer = strippedWriter as IXamlLineInfoConsumer;
                bool shouldPassLineInfo = lineInfo != null && lineInfo.HasLineInfo && lineInfoComsumer != null && lineInfoComsumer.ShouldProvideLineInfo;

                viewStateInfo = null;
                while (inputReader.Read())
                {
                    if (inputReader.NodeType == XamlNodeType.StartMember && inputReader.Member.Equals(viewStateManager))
                    {
                        ReadViewStateInfo(inputReader.ReadSubtree(), out viewStateInfo, out viewStateSourceLocationMap);
                        
                    }

                    if (shouldPassLineInfo)
                    {
                        lineInfoComsumer.SetLineInfo(lineInfo.LineNumber, lineInfo.LinePosition);
                    }

                    strippedWriter.WriteNode(inputReader);
                }
            }

            return strippedNodeList.GetReader();
        }
コード例 #15
0
        // This method collects view state attached properties and generates a Xaml node stream 
        // with all view state information appearing within the ViewStateManager node. 
        // It is called when workflow definition is being serialized to string.
        // inputReader  - Nodestream with view state information as attached properties on the activity nodes.
        //                The reader is positioned at the begining of the workflow definition.
        // idManager    - This component issues running sequence numbers for IdRef.
        // Result       - Node stream positioned at the begining of the workflow definition with a  
        //                ViewStateManager node containing all view state information.
        // Implementation logic:
        // 1. Scan the input nodestream Objects for attached properties that need to be converted (VirtualizedContainerService.HintSize and WorkflowViewStateService.ViewState).
        // 2. If the Object had a IdRef value then use it otherwise generate a new value.
        // 3. Store idRef value and corresponding viewstate related attached property nodes (from step 1) 
        //    in the viewStateInfo dictionary.
        // 4. Use the viewStateInfo dictionary to generate ViewStateManager node which is then inserted
        //    into the end of output nodestream.
        public static XamlReader ConvertAttachedPropertiesToViewState(XamlObjectReader inputReader, ViewStateIdManager idManager)
        {
            // Stack to track StartObject/GetObject and EndObject nodes.
            Stack<Frame> stack = new Stack<Frame>();

            XamlMember viewStateManager = new XamlMember(ViewStateManager, GetViewStateManager, SetViewStateManager, inputReader.SchemaContext);
            XamlMember idRefMember = new XamlMember(IdRef, GetIdRef, SetIdRef, inputReader.SchemaContext);
            
            // Xaml member corresponding to x:Class property of the workflow definition. Used to find x:Class value in the node stream.
            XamlMember activityBuilderName = new XamlMember(typeof(ActivityBuilder).GetProperty("Name"), inputReader.SchemaContext);
            string activityBuilderTypeName = typeof(ActivityBuilder).Name;

            // Dictionary to keep track of IdRefs and corresponding viewstate related
            // attached property nodes.
            Dictionary<string, XamlNodeList> viewStateInfo = new Dictionary<string, XamlNodeList>();

            // Output node list
            XamlNodeList workflowDefinition = new XamlNodeList(inputReader.SchemaContext);

            using (XamlWriter workflowDefinitionWriter = workflowDefinition.Writer)
            {
                bool design2010NamespaceFound = false;
                bool inIdRefMember = false;
                bool inxClassMember = false;
                bool skipWritingWorkflowDefinition = false;
                bool skipReadingWorkflowDefinition = false;
                string xClassName = null;

                while (skipReadingWorkflowDefinition || inputReader.Read())
                {
                    skipWritingWorkflowDefinition = false;
                    skipReadingWorkflowDefinition = false;
                    switch (inputReader.NodeType)
                    {
                        case XamlNodeType.NamespaceDeclaration:
                            if (inputReader.Namespace.Namespace.Equals(NameSpaces.Design2010, StringComparison.Ordinal))
                            {
                                design2010NamespaceFound = true;
                            }
                            break;

                        case XamlNodeType.StartObject:
                            // Save the Xaml type and clr object on the stack frame. These are used later to generate 
                            // IdRef values and attaching the same to the clr object.
                            stack.Push(new Frame() { Type = inputReader.Type, InstanceObject = inputReader.Instance });

                            // If the design2010 namespace was not found add the namespace node
                            // before the start object is written out.
                            if (!design2010NamespaceFound)
                            {
                                workflowDefinitionWriter.WriteNamespace(new NamespaceDeclaration(NameSpaces.Design2010, NameSpaces.Design2010Prefix));
                                design2010NamespaceFound = true;
                            }
                            break;

                        case XamlNodeType.GetObject:
                            // Push an empty frame to balance the Pop operation when the EndObject node 
                            // is encountered.
                            stack.Push(new Frame() { Type = null });
                            break;

                        case XamlNodeType.StartMember:
                            // Track when we enter IdRef member so that we can save its value.
                            if (inputReader.Member.Equals(idRefMember))
                            {
                                inIdRefMember = true;
                            }
                            // Track when we enter x:Class member so that we can save its value.
                            else if (inputReader.Member.Equals(activityBuilderName))
                            {
                                inxClassMember = true;
                            }
                            // Start of VirtualizedContainerService.HintSize or WorkflowViewStateService.ViewState property.
                            else if (IsAttachablePropertyForConvert(inputReader)) 
                            {
                                // The top of stack here corresponds to the activity on which
                                // the above properties are attached.
                                if (stack.Peek().AttachedPropertyNodes == null)
                                {
                                    stack.Peek().AttachedPropertyNodes = new XamlNodeList(inputReader.SchemaContext);
                                }

                                // Write the attached property's xaml nodes into the stack.
                                XamlReader subTreeReader = inputReader.ReadSubtree();
                                XamlWriter attachedPropertyWriter = stack.Peek().AttachedPropertyNodes.Writer;
                                while (subTreeReader.Read())
                                {
                                    attachedPropertyWriter.WriteNode(subTreeReader);
                                }

                                // The subtree reader loop put us at the begining of the next node in the input stream. 
                                // So skip reading/writing it out just yet.
                                skipReadingWorkflowDefinition = true;
                                skipWritingWorkflowDefinition = true;
                            }
                            break;

                        case XamlNodeType.Value:
                            // Read and save IdRef/x:Class member values.
                            // Also update idManager to keep track of prefixes and ids seen.
                            if (inIdRefMember)
                            {
                                string idRef = inputReader.Value as string;
                                stack.Peek().IdRef = idRef;
                                idManager.UpdateMap(idRef); 
                            }
                            else if (inxClassMember)
                            {
                                xClassName = inputReader.Value as string;
                                idManager.UpdateMap(xClassName);
                            }
                            break;

                        case XamlNodeType.EndMember:
                            // Exit IdRef/x:Class member state.
                            if (inIdRefMember)
                            {
                                inIdRefMember = false;
                            }
                            else if (inxClassMember)
                            {
                                inxClassMember = false;
                            }
                            break;

                        case XamlNodeType.EndObject:
                            // Remove an item from the stack because we encountered the end of an object definition.
                            Frame frameObject = stack.Pop();

                            // If the object had (viewstate related) attached properties we need to save them
                            // into the viewStateInfo dictionary.
                            if (frameObject.AttachedPropertyNodes != null)
                            {
                                frameObject.AttachedPropertyNodes.Writer.Close();

                                // If the object didn't have IdRef, generate a new one.
                                if (string.IsNullOrWhiteSpace(frameObject.IdRef))
                                {
                                    // Use the object type name (or x:Class value) to generate a new id.
                                    if (frameObject.Type != null)
                                    {
                                        string prefix = frameObject.Type.Name;
                                        if (frameObject.Type.UnderlyingType != null)
                                        {
                                            prefix = frameObject.Type.UnderlyingType.Name;
                                        }

                                        if (string.CompareOrdinal(prefix, activityBuilderTypeName) == 0 && !string.IsNullOrWhiteSpace(xClassName))
                                        {
                                            frameObject.IdRef = idManager.GetNewId(xClassName);
                                        }
                                        else
                                        {
                                            frameObject.IdRef = idManager.GetNewId(prefix);
                                        }
                                    }
                                    else //Fallback to generating a guid value.
                                    {
                                        frameObject.IdRef = Guid.NewGuid().ToString();
                                    }

                                    // Since we didn't see a IdRef on this object, insert the generated 
                                    // viewstate id into the output Xaml node-stream.
                                    workflowDefinitionWriter.WriteStartMember(idRefMember);
                                    workflowDefinitionWriter.WriteValue(frameObject.IdRef);
                                    workflowDefinitionWriter.WriteEndMember();

                                    // Save the generated idRef on the corresponding clr object as well.
                                    if (frameObject.InstanceObject != null)
                                    {
                                        WorkflowViewState.SetIdRef(frameObject.InstanceObject, frameObject.IdRef);
                                    }
                                }

                                viewStateInfo[frameObject.IdRef] = frameObject.AttachedPropertyNodes;
                            }

                            // We're at the end of input nodestream and have collected data in viewStateInfo
                            // so we need to create and insert the ViewStateManager nodes into the output nodestream.
                            if (stack.Count == 0 && viewStateInfo.Count > 0)
                            {
                                XamlNodeList viewStateManagerNodeList = CreateViewStateManagerNodeList(viewStateInfo, inputReader.SchemaContext);
                                XamlReader viewStateManagerNodeReader = viewStateManagerNodeList.GetReader();

                                // Insert the ViewStateManager nodes into the output node stream.
                                workflowDefinitionWriter.WriteStartMember(viewStateManager);
                                while (viewStateManagerNodeReader.Read())
                                {
                                    workflowDefinitionWriter.WriteNode(viewStateManagerNodeReader);
                                }
                                workflowDefinitionWriter.WriteEndMember(); // viewStateManager
                            }
                            break;
                    }

                    if (!skipWritingWorkflowDefinition)
                    {
                        workflowDefinitionWriter.WriteNode(inputReader);
                    }
                }
            }

            return workflowDefinition.GetReader();
        }
コード例 #16
0
        // This method converts view state information stored within the ViewStateManager node back as
        // attached properties on corresponding activity nodes.
        // It is called when workflow definition is being deserialized from a string.
        // inputReader - Nodestream that may have all view state information in the ViewStateManager node at the end of workflow definition. 
        //               The reader is positioned at the begining of the workflow definition.
        // idManager   - This component issues running sequence numbers for IdRef.
        // viewStateManager - (output) ViewStateManager object instance deserialized from the workflow definition.
        // Result      - Node stream positioned at the begining of the workflow definition with view state related information
        //               appearing as attached properties on activities. The ViewStateManager nodes are removed from the stream.
        // Implementation logic:
        // 1. Scan the input nodestream for ViewStateManager node.
        // 2. If ViewStateManager node is found, store Id and corresponding attached property nodes 
        //    in viewStateInfo dictionary. Otherwise return early.
        // 3. Walk activity nodes in the workflow definition and apply viewstate related attached properties (from 
        //    viewStateInfo dictionary) to each node.
        // 4. If multiple activities have same IdRef values then corresponding viewstate related attached properties 
        //    (from viewStateInfo dictionary) are applied to the first of those activities. The other activities with duplicate
        //    IdRef values do not get view state information.
        public static XamlReader ConvertViewStateToAttachedProperties(XamlReader inputReader, ViewStateIdManager idManager, out Dictionary<string, SourceLocation> viewStateSourceLocationMap)
        {
            int idRefLineNumber = 0;
            int idRefLinePosition = 0;
            bool shouldWriteIdRefEndMember = false;

            XamlReader retVal = null;

            // Xaml member definition for IdRef. Used to identify existing IdRef properties in the input nodestream.
            XamlMember idRefMember = new XamlMember(IdRef, GetIdRef, SetIdRef, inputReader.SchemaContext);

            // Dictionary containing Ids and corresponding viewstate related
            // attached property nodes. Populated by StripViewStateElement method.
            Dictionary<string, XamlNodeList> viewStateInfo = null;
            XamlReader workflowDefinition = StripViewStateElement(inputReader, out viewStateInfo, out viewStateSourceLocationMap);

            // This is used to keep track of duplicate IdRefs in the workflow definition.
            HashSet<string> idRefsSeen = new HashSet<string>();
            
            // If the inputReader did not have a ViewStateManager node (4.0 format)
            // return early.
            if (viewStateInfo == null)
            {
                retVal = workflowDefinition;
            }
            else
            {
                // Stack to track StartObject/GetObject and EndObject nodes.
                Stack<Frame> stack = new Stack<Frame>();

                // Output node list.
                XamlNodeList mergedNodeList = new XamlNodeList(workflowDefinition.SchemaContext);
                bool inIdRefMember = false;

                using (XamlWriter mergedNodeWriter = mergedNodeList.Writer)
                {
                    IXamlLineInfo lineInfo = workflowDefinition as IXamlLineInfo;
                    IXamlLineInfoConsumer lineInfoComsumer = mergedNodeWriter as IXamlLineInfoConsumer;
                    bool shouldPassLineInfo = lineInfo != null && lineInfo.HasLineInfo && lineInfoComsumer != null && lineInfoComsumer.ShouldProvideLineInfo;

                    while (workflowDefinition.Read())
                    {
                        bool skipWritingWorkflowDefinition = false;

                        switch (workflowDefinition.NodeType)
                        {
                            case XamlNodeType.StartObject:
                                stack.Push(new Frame { Type = workflowDefinition.Type });
                                break;

                            case XamlNodeType.GetObject:
                                stack.Push(new Frame { Type = null });
                                break;

                            case XamlNodeType.StartMember:
                                // Track when the reader enters IdRef. Skip writing the start 
                                // node to the output nodelist until we check for duplicates.
                                if (workflowDefinition.Member.Equals(idRefMember))
                                {
                                    inIdRefMember = true;
                                    skipWritingWorkflowDefinition = true;

                                    if (shouldPassLineInfo)
                                    {
                                        idRefLineNumber = lineInfo.LineNumber;
                                        idRefLinePosition = lineInfo.LinePosition;
                                    }
                                }
                                break;

                            case XamlNodeType.Value:
                                if (inIdRefMember)
                                {
                                    string idRef = workflowDefinition.Value as string;
                                    if (!string.IsNullOrWhiteSpace(idRef))
                                    {
                                        // If IdRef value is a duplicate then do not associate it with 
                                        // the stack frame (top of stack == activity node with IdRef member on it).
                                        if (idRefsSeen.Contains(idRef))
                                        {
                                            stack.Peek().IdRef = null;
                                        }
                                        // If the IdRef value is unique then associate it with the
                                        // stack frame and also write its value into the output nodestream.
                                        else
                                        {
                                            stack.Peek().IdRef = idRef;
                                            idManager.UpdateMap(idRef);
                                            idRefsSeen.Add(idRef);

                                            if (shouldPassLineInfo)
                                            {
                                                lineInfoComsumer.SetLineInfo(idRefLineNumber, idRefLinePosition);
                                            }

                                            mergedNodeWriter.WriteStartMember(idRefMember);

                                            if (shouldPassLineInfo)
                                            {
                                                lineInfoComsumer.SetLineInfo(lineInfo.LineNumber, lineInfo.LinePosition);
                                            }

                                            mergedNodeWriter.WriteValue(idRef);

                                            shouldWriteIdRefEndMember = true;
                                        }
                                    }
                                    // Don't need to write IdRef value into the output
                                    // nodestream. If the value was valid, it would have been written above.
                                    skipWritingWorkflowDefinition = true;
                                }
                                break;

                            case XamlNodeType.EndMember:
                                // Exit IdRef node. Skip writing the EndMember node, we would have done 
                                // it as part of reading the IdRef value.
                                if (inIdRefMember)
                                {
                                    inIdRefMember = false;
                                    skipWritingWorkflowDefinition = true;

                                    if (shouldWriteIdRefEndMember)
                                    {
                                        shouldWriteIdRefEndMember = false;
                                        
                                        if (shouldPassLineInfo)
                                        {
                                            lineInfoComsumer.SetLineInfo(lineInfo.LineNumber, lineInfo.LinePosition);
                                        }

                                        mergedNodeWriter.WriteEndMember();
                                    }
                                }
                                break;

                            case XamlNodeType.EndObject:
                                Frame frameObject = stack.Pop();
                                // Before we exit the end of an object, check if it had IdRef
                                // associated with it. If it did, look-up viewStateInfo for viewstate
                                // related attached property nodes and add them to the output nodelist.
                                if (!string.IsNullOrWhiteSpace(frameObject.IdRef))
                                {
                                    XamlNodeList viewStateNodeList;
                                    if (viewStateInfo.TryGetValue(frameObject.IdRef, out viewStateNodeList))
                                    {
                                        XamlReader viewStateReader = viewStateNodeList.GetReader();

                                        IXamlLineInfo viewStateLineInfo = viewStateReader as IXamlLineInfo;
                                        bool viewStateShouldPassLineInfo = viewStateLineInfo != null && viewStateLineInfo.HasLineInfo && lineInfoComsumer != null && lineInfoComsumer.ShouldProvideLineInfo;

                                        while (viewStateReader.Read())
                                        {
                                            if (viewStateShouldPassLineInfo)
                                            {
                                                lineInfoComsumer.SetLineInfo(viewStateLineInfo.LineNumber, viewStateLineInfo.LinePosition);
                                            }

                                            mergedNodeWriter.WriteNode(viewStateReader);
                                        }
                                    }
                                }
                                break;
                        }
                        if (!skipWritingWorkflowDefinition)
                        {
                            if (shouldPassLineInfo)
                            {
                                lineInfoComsumer.SetLineInfo(lineInfo.LineNumber, lineInfo.LinePosition);
                            }

                            mergedNodeWriter.WriteNode(workflowDefinition);
                        }
                    }
                }

                retVal = mergedNodeList.GetReader();
            }

            return retVal;
        }
コード例 #17
0
 public KeyRecord(bool shared, bool sharedSet, int valuePosition, XamlSchemaContext context) :
     this(shared, sharedSet, valuePosition)
 {
     _data = new XamlNodeList(context, 8);
 }
コード例 #18
0
        // Throws InvalidOperationException at DesignTime: Input XAML contains invalid constructs for generating a class. For example, unexpected content or unknown class or field modifiers.
        public ClassData ReadFromXaml(XamlNodeList nodes)
        {
            if (nodes == null)
            {
                throw FxTrace.Exception.ArgumentNull("nodeList");
            }

            Stack<NamedObject> currentTypes = new Stack<NamedObject>();
            XamlReader reader = nodes.GetReader();
            XamlSchemaContext xsc = reader.SchemaContext;
            XamlNodeList strippedXamlNodes = new XamlNodeList(xsc);
            XamlWriter strippedXamlNodesWriter = strippedXamlNodes.Writer;

            ClassData result = new ClassData()
                {
                    FileName = this.xamlFileName,
                    IsPublic = this.DefaultClassIsPublic,
                    RootNamespace = this.rootNamespace
                };

            // We loop through the provided XAML; for each node, we do two things:
            //  1. If it's a directive that's relevant to x:Class, we extract the data.
            //  2. Unless it's a directive that's exclusively relevant to x:Class, we write it to strippedXamlNodes.
            // The result is two outputs: class data, and stripped XAML that can be used to initialize the
            // an instance of the class.

            bool readNextNode = false;
            while (readNextNode || reader.Read())
            {
                bool stripNodeFromXaml = false;
                readNextNode = false;

                namespaceTable.ManageNamespace(reader);

                switch (reader.NodeType)
                {
                    case XamlNodeType.StartObject:
                        if (result.BaseType == null)
                        {
                            result.BaseType = reader.Type;
                        }
                        currentTypes.Push(new NamedObject()
                            {
                                Type = reader.Type,
                                Visibility = DefaultFieldVisibility,
                            });
                        break;

                    case XamlNodeType.EndObject:
                        currentTypes.Pop();
                        break;

                    case XamlNodeType.StartMember:
                        XamlMember member = reader.Member;

                        if (member.IsDirective)
                        {
                            bool isRootElement = (currentTypes.Count == 1);
                            stripNodeFromXaml = ProcessDirective(reader, result, currentTypes.Peek(), isRootElement, strippedXamlNodes, out readNextNode);
                        }
                        else
                        {
                            NamedObject currentType = currentTypes.Peek();
                            XamlType currentXamlType = currentType.Type;
                            if (currentXamlType.IsUnknown)
                            {
                                result.RequiresCompilationPass2 = true;
                            }
                        }
                        break;

                    case XamlNodeType.EndMember:
                        break;

                    case XamlNodeType.Value:
                        break;

                    case XamlNodeType.NamespaceDeclaration:
                        break;

                    case XamlNodeType.None:
                        break;

                    case XamlNodeType.GetObject:
                        //Push a dummy NamedObject so that it gets popped when you see the corresponding EndObject
                        currentTypes.Push(new NamedObject());
                        break;

                    default:

                        Debug.Fail("Unrecognized XamlNodeType value" + reader.NodeType.ToString());
                        break;
                }

                if (!stripNodeFromXaml)
                {
                    WritestrippedXamlNode(reader, strippedXamlNodesWriter);
                }
            }

            // ClassData.Name should be initialized to a non-null non-empty value if 
            // the file contains x:Class. Throw an error if neither is found.
            if (result.Name == null)
            {
                string xClassDirectiveName = "{" + XamlLanguage.Class.PreferredXamlNamespace + "}" + XamlLanguage.Class.Name;

                throw FxTrace.Exception.AsError(LogInvalidOperationException(null, SR.TaskCannotProcessFileWithoutType(xClassDirectiveName)));
            }

            strippedXamlNodes.Writer.Close();
            strippedXamlNodes = RewriteRootNode(strippedXamlNodes, result.Name, result.Namespace);

            result.EmbeddedResourceXaml = strippedXamlNodes;
            return result;
        }
コード例 #19
0
        XamlNodeList RewriteRootNode(XamlNodeList strippedXamlNodes, string name, string @namespace)
        {
            // Rewrite the root node to have the name of class declared via x:Class (rather than the base class)
            // Also, for any properties on the root object that are declared in this class, need to rewrite the
            // namespace to include the root namespace, if there is one.

            string oldNamespace = null;
            if (!string.IsNullOrEmpty(this.rootNamespace))
            {
                oldNamespace = @namespace;
                if (!string.IsNullOrEmpty(@namespace))
                {
                    @namespace = this.rootNamespace + "." + @namespace;
                }
                else
                {
                    @namespace = this.rootNamespace;
                }
            }

            string namespaceName = string.Format(CultureInfo.InvariantCulture, "{0}{1};{2}{3}", XamlBuildTaskServices.ClrNamespaceUriNamespacePart, @namespace, XamlBuildTaskServices.ClrNamespaceUriAssemblyPart, this.localAssemblyName);

            XamlReader reader = strippedXamlNodes.GetReader();
            XamlSchemaContext xsc = reader.SchemaContext;
            XamlNodeList newStrippedXamlNodes = new XamlNodeList(xsc);
            XamlWriter writer = newStrippedXamlNodes.Writer;

            int depth = 0;
            XamlType rootXamlType = null;
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XamlNodeType.StartObject:
                    case XamlNodeType.GetObject:
                        depth++;
                        break;
                    case XamlNodeType.EndObject:
                        depth--;
                        break;
                }
                if (reader.NodeType == XamlNodeType.StartObject && depth == 1)
                {
                    rootXamlType = new XamlType(namespaceName, name, null, xsc);
                    writer.WriteStartObject(rootXamlType);
                }
                else if (reader.NodeType == XamlNodeType.StartMember && depth == 1 && reader.Member.IsUnknown 
                    && reader.Member.DeclaringType != null && reader.Member.DeclaringType.Name == rootXamlType.Name)
                {
                    string clrNs;
                    XamlMember member = reader.Member;
                    if (XamlBuildTaskServices.TryExtractClrNs(member.PreferredXamlNamespace, out clrNs) &&
                        clrNs == oldNamespace)
                    {
                        // This is a member defined on the document root type, but missing the project root namespace. Fix it.
                        XamlMember newMember = new XamlMember(member.Name, rootXamlType, member.IsAttachable);
                        Fx.Assert(rootXamlType != null, "First StartObject should already have been processed");
                        writer.WriteStartMember(newMember);
                    }
                    else
                    {
                        writer.WriteNode(reader);
                    }
                }
                else
                {
                    writer.WriteNode(reader);
                }
            }

            writer.Close();
            return newStrippedXamlNodes;
        }
コード例 #20
0
 bool ValidateXaml(XamlNodeList xamlNodeList, string xamlFileName)
 {
     using (XamlReader xamlReader = xamlNodeList.GetReader())
     {
         IList<LogData> validationErrors = null;
         ClassValidator validator = new ClassValidator(xamlFileName, null, null);
         return validator.ValidateXaml(xamlReader, true, this.AssemblyName, out validationErrors);
     }
 }
コード例 #21
0
 void RemoveXamlSpaceAttribute(ClassData classData)
 {
     using (XamlReader reader = classData.EmbeddedResourceXaml.GetReader())
     {
         XamlNodeList newList = new XamlNodeList(reader.SchemaContext);
         using (XamlWriter writer = newList.Writer)
         {
             bool nodesAvailable = reader.Read();
             while (nodesAvailable)
             {                        
                 if (reader.NodeType == XamlNodeType.StartMember && reader.Member == XamlLanguage.Space)
                 {
                     reader.Skip();
                 }
                 else
                 {
                     writer.WriteNode(reader);
                     nodesAvailable = reader.Read();
                 }
             }
         }
         classData.EmbeddedResourceXaml = newList;
     }
 }
コード例 #22
0
 ClassData ReadClassData(XamlNodeList xamlNodes, string xamlFileName)
 {
     ClassImporter importer = new ClassImporter(xamlFileName, this.AssemblyName, this.Language.Equals("VB") ? this.RootNamespace : null);
     ClassData classData = importer.ReadFromXaml(xamlNodes);
     return classData;
 }
コード例 #23
0
        XamlNodeList ReadXamlNodes(string xamlFileName)
        {
            XamlNodeList nodeList = new XamlNodeList(this.SchemaContext);

            try
            {
                XamlXmlReaderSettings settings = new XamlXmlReaderSettings
                {
                    AllowProtectedMembersOnRoot = true,
                    ProvideLineInfo = true
                };

                using (StreamReader streamReader = new StreamReader(xamlFileName))
                {
                    XamlReader reader = new XamlXmlReader(XmlReader.Create(streamReader), this.SchemaContext, settings);
                    XamlServices.Transform(reader, nodeList.Writer);
                }
            }
            catch (XmlException e)
            {
                XamlBuildTaskServices.LogException(this.BuildLogger, e.Message, xamlFileName, e.LineNumber, e.LinePosition); 
                return null;
            }
            catch (XamlException e)
            {
                XamlBuildTaskServices.LogException(this.BuildLogger, e.Message, xamlFileName, e.LineNumber, e.LinePosition);
                return null;
            }
            
            if (nodeList.Count > 0)
            {
                return nodeList;
            }
            else
            {
                return null;
            }
        }
コード例 #24
0
ファイル: Baml2006Reader.cs プロジェクト: sjyanxin/WPFSource
        internal XamlReader ReadObject(KeyRecord record)
        { 
            // This means we're at the end of the Deferred Content 
            // Break out and return null
            if (record.ValuePosition == _binaryReader.BaseStream.Length) 
            {
                return null;
            }
 
            _binaryReader.BaseStream.Seek(record.ValuePosition, SeekOrigin.Begin);
 
            _context.CurrentKey = _context.KeyList.IndexOf(record); 

            if (_xamlMainNodeQueue.Count > 0) 
            {
                //
                throw new XamlParseException();
            } 

            if (Read_RecordType() != Baml2006RecordType.ElementStart) 
            { 
                throw new XamlParseException();
            } 

            XamlWriter oldQueueWriter = _xamlNodesWriter;

            // estimates from statistical examiniation of Theme Resource Data. 
            // small RD entries appear to have about a 2.2 bytes to XamlNode ration.
            // larger RD entries are less dense (due to data) at about 4.25. 
            // Presizing the XamlNodeLists save upto 50% in memory useage for same. 
            int initialSizeOfNodeList = (record.ValueSize < 800)
                        ? (int)(record.ValueSize / 2.2) 
                        : (int)(record.ValueSize / 4.25);

            initialSizeOfNodeList = (initialSizeOfNodeList < 8) ? 8 : initialSizeOfNodeList;
 
            var result = new XamlNodeList(_xamlNodesReader.SchemaContext, initialSizeOfNodeList);
            _xamlNodesWriter = result.Writer; 
 
            Baml2006ReaderFrame baseFrame = _context.CurrentFrame;
            Process_ElementStart(); 

            while (baseFrame != _context.CurrentFrame)
            {
                Process_OneBamlRecord(); 
            }
            _xamlNodesWriter.Close(); 
 
            _xamlNodesWriter = oldQueueWriter;
            return result.GetReader(); 
        }
コード例 #25
0
ファイル: Baml2006Reader.cs プロジェクト: sjyanxin/WPFSource
 private void StartSavingFirstItemInDictionary()
 {
     _lookingForAKeyOnAMarkupExtensionInADictionaryDepth = _context.CurrentFrame.Depth;
     _lookingForAKeyOnAMarkupExtensionInADictionaryNodeList = new XamlNodeList(_xamlNodesWriter.SchemaContext); 
     _xamlWriterStack.Push(_xamlNodesWriter);
     _xamlNodesWriter = _lookingForAKeyOnAMarkupExtensionInADictionaryNodeList.Writer; 
 } 
コード例 #26
0
ファイル: Baml2006Reader.cs プロジェクト: sjyanxin/WPFSource
        private void InjectPropertyAndFrameIfNeeded(XamlType elementType, SByte flags)
        {
            // If we saved the Node stream for the first ME element of a dictionary to 
            // check if it had a key then process that now.
            if (_lookingForAKeyOnAMarkupExtensionInADictionaryDepth == _context.CurrentFrame.Depth) 
            { 
                RestoreSavedFirstItemInDictionary();
            } 

            XamlType parentType = _context.CurrentFrame.XamlType;
            XamlMember parentProperty = _context.CurrentFrame.Member;
 
            if (parentType != null)
            { 
                if (parentProperty == null) 
                {
                    // We have got two consecutive ElementStart records 
                    // We must insert an implicit content property between them
                    if (_context.CurrentFrame.ContentProperty != null)
                    {
                        _context.CurrentFrame.Member = parentProperty = _context.CurrentFrame.ContentProperty; 
                    }
                    else if (parentType.ContentProperty != null) 
                    { 
                        _context.CurrentFrame.Member = parentProperty = parentType.ContentProperty;
                    } 
                    else
                    {
                        if (parentType.IsCollection || parentType.IsDictionary)
                        { 
                            _context.CurrentFrame.Member = parentProperty = XamlLanguage.Items;
                        } 
                        else if (parentType.TypeConverter != null) 
                        {
                            _context.CurrentFrame.Member = parentProperty = XamlLanguage.Initialization; 
                        }
                        else
                        {
                            throw new XamlParseException(SR.Get(SRID.RecordOutOfOrder, parentType.Name)); 
                        }
                    } 
                    _context.CurrentFrame.Flags = Baml2006ReaderFrameFlags.HasImplicitProperty; 
                    _xamlNodesWriter.WriteStartMember(parentProperty);
 
                    // if we are NOT already spooling a template
                    if (_context.TemplateStartDepth < 0 && _isBinaryProvider)
                    {
                        if (parentProperty == BamlSchemaContext.FrameworkTemplateTemplateProperty) 
                        {
                            // If this is a template then spool the template into a Node List. 
                            _context.TemplateStartDepth = _context.CurrentFrame.Depth; 
                            _xamlTemplateNodeList = new XamlNodeList(_xamlNodesWriter.SchemaContext);
 
                            _xamlWriterStack.Push(_xamlNodesWriter);
                            _xamlNodesWriter = _xamlTemplateNodeList.Writer;
                        }
                    } 
                }
 
                XamlType parentPropertyType = parentProperty.Type; 
                // Normally an error except for collections
                if (parentPropertyType != null && (parentPropertyType.IsCollection || parentPropertyType.IsDictionary) && 
                    !parentProperty.IsDirective && (flags & ReaderFlags_AddedToTree) == 0)
                {
                    bool emitPreamble = false;
 
                    // If the collection property is Readonly then "retrieve" the collection.
                    if (parentProperty.IsReadOnly) 
                    { 
                        emitPreamble = true;
                    } 
                    // OR if the Value isn't assignable to the Collection emit the preable.
                    else if (!elementType.CanAssignTo(parentPropertyType))
                    {
                        // UNLESS: the Value is a Markup extension, then it is assumed that 
                        // the ProvidValue type will be AssignableFrom.
                        if (!elementType.IsMarkupExtension) 
                        { 
                            emitPreamble = true;
                        } 
                        // EXCEPT: if the BAML said it was Retrived
                        else if (_context.CurrentFrame.Flags == Baml2006ReaderFrameFlags.HasImplicitProperty)
                        {
                            emitPreamble = true; 
                        }
                        // OR: the ME is Array 
                        else if (elementType == XamlLanguage.Array) 
                        {
                            emitPreamble = true; 
                        }
                    }
                    if (emitPreamble)
                    { 
                        EmitGoItemsPreamble(parentPropertyType);
                    } 
 
                    // We may need to look for an x:Key on the ME in a dictionary.
                    // so save up the node stream for the whole element definition and check it 
                    // for an x:Key later.
                    if (!emitPreamble && parentPropertyType.IsDictionary && elementType.IsMarkupExtension)
                    {
                        StartSavingFirstItemInDictionary(); 
                    }
                } 
            } 
        }
コード例 #27
0
ファイル: XamlNodeListReader.cs プロジェクト: codeex/corewf
 public XamlNodeListReader(XamlNodeList source)
 {
     this.source = source;
 }
コード例 #28
0
 public DynamicActivityXamlType(XamlNodeList xamlNodes)
     : this(xamlNodes, null)
 {
 }
コード例 #29
0
        bool ProcessDirective(XamlReader reader, ClassData classData,
            NamedObject currentObject, bool isRootElement, XamlNodeList strippedXamlNodes, out bool readNextNode)
        {
            Fx.Assert(reader.NodeType == XamlNodeType.StartMember, "Current node should be a Start Member Node");

            XamlMember member = reader.Member;
            bool directiveRecognized = false;
            readNextNode = false;

            switch (member.Name)
            {
                case "Name":
                    // Unlike all the other directives that we process, x:Name should be written
                    // to the stripped output.
                    strippedXamlNodes.Writer.WriteStartMember(member);

                    string objectName = ReadAtom(reader, XamlLanguage.Name.Name);
                    if (!objectName.StartsWith(XamlBuildTaskServices.SerializerReferenceNamePrefix,
                        StringComparison.Ordinal))
                    {
                        currentObject.Name = objectName;
                        classData.NamedObjects.Add(currentObject);
                    }

                    strippedXamlNodes.Writer.WriteValue(objectName);
                    strippedXamlNodes.Writer.WriteEndMember();
                    directiveRecognized = true;
                    break;

                case "Class":
                    if (isRootElement)
                    {
                        string fullClassName = ReadAtom(reader, XamlLanguage.Class.Name);
                        SetClassName(fullClassName, classData);
                        directiveRecognized = true;
                    }
                    break;

                case "ClassModifier":
                    if (isRootElement)
                    {
                        string classModifier = ReadAtom(reader, XamlLanguage.ClassModifier.Name);
                        classData.IsPublic = XamlBuildTaskServices.IsPublic(classModifier);
                        directiveRecognized = true;
                    }
                    break;

                case "FieldModifier":
                    string fieldModifier = ReadAtom(reader, XamlLanguage.FieldModifier.Name);
                    currentObject.Visibility = XamlBuildTaskServices.GetMemberVisibility(fieldModifier);
                    directiveRecognized = true;
                    break;

                case "Code":
                    string codeSnippet = ReadAtom(reader, XamlLanguage.Code.Name);
                    classData.CodeSnippets.Add(codeSnippet);
                    directiveRecognized = true;
                    break;

                case "Members":
                    foreach (PropertyData property in ReadProperties(reader.ReadSubtree()))
                    {
                        classData.Properties.Add(property);
                    }
                    if (!classData.RequiresCompilationPass2)
                    {
                        foreach (PropertyData property in classData.Properties)
                        {
                            if (property.Type.IsUnknown)
                            {
                                classData.RequiresCompilationPass2 = true;
                                break;
                            }
                        }
                    }
                    directiveRecognized = true;
                    readNextNode = true;
                    break;

                case "ClassAttributes":
                    foreach (AttributeData attribute in ReadAttributesCollection(reader.ReadSubtree()))
                    {
                        classData.Attributes.Add(attribute);
                    }
                    directiveRecognized = true;
                    readNextNode = true;
                    break;

            }

            if (directiveRecognized == true && readNextNode == false)
            {
                reader.Read();
                Fx.Assert(reader.NodeType == XamlNodeType.EndMember, "Current node should be a XamlEndmember");
            }

            return directiveRecognized;
        }
コード例 #30
0
 private static ICustomTypeDescriptor LoadAndValidateActivity(XamlNodeList xamlNodes)
 {
     object result = LoadActivity(xamlNodes);
     if (result == null || !result.GetType().IsDynamicActivity())
     {
         throw new ArgumentException("The root type of the XAML must be Activity, and the root element must contain an x:Class directive", "xamlNodes");
     }
     return (ICustomTypeDescriptor)result;
 }
コード例 #31
0
        private DynamicActivityXamlType CreateXamlType(string path)
        {
            XamlNodeList xamlNodes = new XamlNodeList(this);
            using (XamlXmlReader xamlReader = new XamlXmlReader(path, this))
            {
                XamlServices.Transform(xamlReader, xamlNodes.Writer);
            }

            // Namespaces are only evaluated at construction, so if an additional namespace is
            // registered later, we don't update the XamlType.
            IEnumerable<string> namespaces = GetNamespacesFor(path).OrderBy(ns => ns, StringComparer.Ordinal);
            DynamicActivityXamlType result = new DynamicActivityXamlType(xamlNodes, namespaces);
            return result;
        }
コード例 #32
0
 private static object LoadActivity(XamlNodeList xamlNodes)
 {
     XamlReader reader = ActivityXamlServices.CreateReader(xamlNodes.GetReader(), xamlNodes.Writer.SchemaContext);
     return XamlServices.Load(reader);
 }
コード例 #33
0
 public StaticResource(XamlType type, XamlSchemaContext schemaContext)
 {
     ResourceNodeList = new XamlNodeList(schemaContext, 8);
     ResourceNodeList.Writer.WriteStartObject(type);
 }
コード例 #34
0
 private void StartDeferredList()
 {
     this._deferredList      = new XamlNodeList(this._context.SchemaContext);
     this._deferredWriter    = this._deferredList.Writer;
     this._deferredTreeDepth = 0;
 }
コード例 #35
0
        // This method removes IdRef nodes from the nodestream. This method would be called
        // when a 4.5 workflow definition is retargeted to 4.0.
        public static XamlReader RemoveIdRefs(XamlObjectReader inputReader)
        {
            XamlMember idRefMember = new XamlMember(IdRef, GetIdRef, SetIdRef, inputReader.SchemaContext);

            XamlNodeList outputNodeList = new XamlNodeList(inputReader.SchemaContext);
            using (XamlWriter outputWriter = outputNodeList.Writer)
            {
                while (inputReader.Read())
                {
                    if (inputReader.NodeType == XamlNodeType.StartMember && inputReader.Member.Equals(idRefMember))
                    {
                        // Exhaust the idRefMember sub-tree.
                        XamlReader idRefReader = inputReader.ReadSubtree();
                        while (idRefReader.Read());
                    }
                    outputWriter.WriteNode(inputReader);
                }
            }
            return outputNodeList.GetReader();
        }
コード例 #36
0
 internal TemplateContent(XamlNodeList nodeList, Dictionary<string, Type> typesByName)
 {
     this.nodeList = nodeList;
     this.typesByName = typesByName;
 }
コード例 #37
0
        // Given the viewStateInfo dictionary, this method returns a xaml node list matching a ViewStateManager
        // object.
        static XamlNodeList CreateViewStateManagerNodeList(Dictionary<string, XamlNodeList> viewStateInfo, XamlSchemaContext schemaContext)
        {
            XamlNodeList viewStateManagerNodeList = new XamlNodeList(schemaContext);

            XamlMember viewStateDataMember = new XamlMember(typeof(ViewStateManager).GetProperty("ViewStateData"), schemaContext);
            XamlType viewStateManagerType = new XamlType(typeof(ViewStateManager), schemaContext);
            XamlType viewStateDataType = new XamlType(typeof(ViewStateData), schemaContext);
            XamlMember idMember = new XamlMember(typeof(ViewStateData).GetProperty("Id"), schemaContext);

            using (XamlWriter viewStateManagerNodeWriter = viewStateManagerNodeList.Writer)
            {
                viewStateManagerNodeWriter.WriteStartObject(viewStateManagerType);
                viewStateManagerNodeWriter.WriteStartMember(viewStateDataMember);
                viewStateManagerNodeWriter.WriteGetObject();
                viewStateManagerNodeWriter.WriteStartMember(XamlLanguage.Items);

                foreach (KeyValuePair<string, XamlNodeList> entry in viewStateInfo)
                {
                    viewStateManagerNodeWriter.WriteStartObject(viewStateDataType);

                    viewStateManagerNodeWriter.WriteStartMember(idMember);
                    viewStateManagerNodeWriter.WriteValue(entry.Key);
                    viewStateManagerNodeWriter.WriteEndMember(); // idMember

                    XamlReader viewStateValueReader = entry.Value.GetReader();
                    while (viewStateValueReader.Read())
                    {
                        viewStateManagerNodeWriter.WriteNode(viewStateValueReader);
                    }

                    viewStateManagerNodeWriter.WriteEndObject(); // viewStateDataType
                }

                viewStateManagerNodeWriter.WriteEndMember(); // XamlLanguage.Items
                viewStateManagerNodeWriter.WriteEndObject(); // GetObject
                viewStateManagerNodeWriter.WriteEndMember(); // viewStateDataMember
                viewStateManagerNodeWriter.WriteEndObject(); // viewStateManagerType
                viewStateManagerNodeWriter.Close();
            }

            return viewStateManagerNodeList;
        }
コード例 #38
0
        // This method converts view state information stored within the ViewStateManager node back as
        // attached properties on corresponding activity nodes.
        // It is called when workflow definition is being deserialized from a string.
        // inputReader - Nodestream that may have all view state information in the ViewStateManager node at the end of workflow definition. 
        //               The reader is positioned at the begining of the workflow definition.
        // idManager   - This component issues running sequence numbers for IdRef.
        // viewStateManager - (output) ViewStateManager object instance deserialized from the workflow definition.
        // Result      - Node stream positioned at the begining of the workflow definition with view state related information
        //               appearing as attached properties on activities. The ViewStateManager nodes are removed from the stream.
        // Implementation logic:
        // 1. Scan the input nodestream for ViewStateManager node.
        // 2. If ViewStateManager node is found, store Id and corresponding attached property nodes 
        //    in viewStateInfo dictionary. Otherwise return early.
        // 3. Walk activity nodes in the workflow definition and apply viewstate related attached properties (from 
        //    viewStateInfo dictionary) to each node.
        // 4. If multiple activities have same IdRef values then corresponding viewstate related attached properties 
        //    (from viewStateInfo dictionary) are applied to the first of those activities. The other activities with duplicate
        //    IdRef values do not get view state information.
        public static XamlReader ConvertViewStateToAttachedProperties(XamlReader inputReader, ViewStateIdManager idManager, out Dictionary<string, SourceLocation> viewStateSourceLocationMap)
        {
            int idRefLineNumber = 0;
            int idRefLinePosition = 0;
            bool shouldWriteIdRefEndMember = false;

            XamlReader retVal = null;

            // Xaml member definition for IdRef. Used to identify existing IdRef properties in the input nodestream.
            XamlMember idRefMember = new XamlMember(IdRef, GetIdRef, SetIdRef, inputReader.SchemaContext);

            // These are used to ignore the IdRef members that are inside a DynamicUpdateInfo.OriginalDefinition/OriginalActivityBuilder attached property.
            // We need to ignore these because if we don't, the IdRef values for the objects in the actual workflow defintion will be ignored because of the
            // duplicate IdRef value. This causes problems with activity designers that depend on the ViewStateManager data to correctly display the workflow
            // on the WorkflowDesigner canvas.
            XamlMember originalDefinitionMember = new XamlMember(DynamicUpdateOriginalDefinitionMemberName, GetOriginalDefinition, SetOriginalDefinition, inputReader.SchemaContext);
            XamlMember originalActivityBuilderMember = new XamlMember(DynamicUpdateOriginalActivityBuilderMemberName, GetOriginalActivityBuilder, SetOriginalActivityBuilder, inputReader.SchemaContext);

            // insideOriginalDefintion gets set to true when we find a "StartMember" node for either of the above two attached properties.
            // originalDefintionMemberCount gets incremented if we find any "StartMember" and insideOriginalDefinition is true.
            // originalDefintionMemberCount gets decremented if we find any "EndMember" and insideOriginalDefintion is true.
            // insideOriginalDefintion gets set to false when we find an "EndMember" and originalDefinitionMemberCount gets decremented to 0.
            // If insideOriginalDefintion is true when we find an "IdRef" member, we do NOT add that IdRef to the idRefsSeen HashSet to avoid
            // duplicates being defined by the IdRefs inside of the OriginalDefinition attached properties.
            bool insideOriginalDefinition = false;
            int originalDefinitionMemberCount = 0;

            // Dictionary containing Ids and corresponding viewstate related
            // attached property nodes. Populated by StripViewStateElement method.
            Dictionary<string, XamlNodeList> viewStateInfo = null;
            XamlReader workflowDefinition = StripViewStateElement(inputReader, out viewStateInfo, out viewStateSourceLocationMap);

            // This is used to keep track of duplicate IdRefs in the workflow definition.
            HashSet<string> idRefsSeen = new HashSet<string>();
            
            // If the inputReader did not have a ViewStateManager node (4.0 format)
            // return early.
            if (viewStateInfo == null)
            {
                retVal = workflowDefinition;
            }
            else
            {
                // Stack to track StartObject/GetObject and EndObject nodes.
                Stack<Frame> stack = new Stack<Frame>();

                // Output node list.
                XamlNodeList mergedNodeList = new XamlNodeList(workflowDefinition.SchemaContext);
                bool inIdRefMember = false;

                using (XamlWriter mergedNodeWriter = mergedNodeList.Writer)
                {
                    IXamlLineInfo lineInfo = workflowDefinition as IXamlLineInfo;
                    IXamlLineInfoConsumer lineInfoComsumer = mergedNodeWriter as IXamlLineInfoConsumer;
                    bool shouldPassLineInfo = lineInfo != null && lineInfo.HasLineInfo && lineInfoComsumer != null && lineInfoComsumer.ShouldProvideLineInfo;

                    while (workflowDefinition.Read())
                    {
                        bool skipWritingWorkflowDefinition = false;

                        switch (workflowDefinition.NodeType)
                        {
                            case XamlNodeType.StartObject:
                                stack.Push(new Frame { Type = workflowDefinition.Type });
                                break;

                            case XamlNodeType.GetObject:
                                stack.Push(new Frame { Type = null });
                                break;

                            case XamlNodeType.StartMember:
                                // If we find a StartMember for DynamicUpdateInfo.OriginalDefinition or OriginalActivityBuilder, remember that we are
                                // inside one of those. We don't want to "remember" IdRef values in the idRefsSeen HashSet while inside these attached properties.
                                if (workflowDefinition.Member.Equals(originalDefinitionMember) || workflowDefinition.Member.Equals(originalActivityBuilderMember))
                                {
                                    insideOriginalDefinition = true;
                                }
                                
                                if (insideOriginalDefinition)
                                {
                                    originalDefinitionMemberCount++;
                                }

                                // Track when the reader enters IdRef. Skip writing the start 
                                // node to the output nodelist until we check for duplicates.
                                else if (workflowDefinition.Member.Equals(idRefMember))
                                {
                                    inIdRefMember = true;
                                    skipWritingWorkflowDefinition = true;

                                    if (shouldPassLineInfo)
                                    {
                                        idRefLineNumber = lineInfo.LineNumber;
                                        idRefLinePosition = lineInfo.LinePosition;
                                    }
                                }
                                break;

                            case XamlNodeType.Value:
                                if (inIdRefMember)
                                {
                                    // We don't want to deal with the IdRef if we are inside a DynamicUpdateInfo.OriginalDefinition/OriginalActivityBuilder
                                    // attached property.
                                    if (!insideOriginalDefinition)
                                    {
                                        string idRef = workflowDefinition.Value as string;
                                        if (!string.IsNullOrWhiteSpace(idRef))
                                        {
                                            // If IdRef value is a duplicate then do not associate it with 
                                            // the stack frame (top of stack == activity node with IdRef member on it).
                                            if (idRefsSeen.Contains(idRef))
                                            {
                                                stack.Peek().IdRef = null;
                                            }
                                            // If the IdRef value is unique then associate it with the
                                            // stack frame and also write its value into the output nodestream.
                                            else
                                            {
                                                stack.Peek().IdRef = idRef;
                                                idManager.UpdateMap(idRef);
                                                idRefsSeen.Add(idRef);

                                                if (shouldPassLineInfo)
                                                {
                                                    lineInfoComsumer.SetLineInfo(idRefLineNumber, idRefLinePosition);
                                                }

                                                mergedNodeWriter.WriteStartMember(idRefMember);

                                                if (shouldPassLineInfo)
                                                {
                                                    lineInfoComsumer.SetLineInfo(lineInfo.LineNumber, lineInfo.LinePosition);
                                                }

                                                mergedNodeWriter.WriteValue(idRef);

                                                shouldWriteIdRefEndMember = true;
                                            }
                                        }
                                    }
                                    // Don't need to write IdRef value into the output
                                    // nodestream. If the value was valid, it would have been written above.
                                    skipWritingWorkflowDefinition = true;
                                }
                                break;

                            case XamlNodeType.EndMember:
                                // If we are inside an OriginalDefinition/OriginalActivityBuilder attached property,
                                // decrement the count and if it goes to zero, set insideOriginalDefintion to false
                                // because we just encountered the EndMember for it.
                                if (insideOriginalDefinition)
                                {
                                    originalDefinitionMemberCount--;
                                    if (originalDefinitionMemberCount == 0)
                                    {
                                        insideOriginalDefinition = false;
                                    }
                                }

                                // Exit IdRef node. Skip writing the EndMember node, we would have done 
                                // it as part of reading the IdRef value.
                                if (inIdRefMember && !insideOriginalDefinition)
                                {
                                    inIdRefMember = false;
                                    skipWritingWorkflowDefinition = true;

                                    if (shouldWriteIdRefEndMember)
                                    {
                                        shouldWriteIdRefEndMember = false;
                                        
                                        if (shouldPassLineInfo)
                                        {
                                            lineInfoComsumer.SetLineInfo(lineInfo.LineNumber, lineInfo.LinePosition);
                                        }

                                        mergedNodeWriter.WriteEndMember();
                                    }
                                }

                                break;

                            case XamlNodeType.EndObject:
                                Frame frameObject = stack.Pop();
                                // Before we exit the end of an object, check if it had IdRef
                                // associated with it. If it did, look-up viewStateInfo for viewstate
                                // related attached property nodes and add them to the output nodelist.
                                if (!string.IsNullOrWhiteSpace(frameObject.IdRef))
                                {
                                    XamlNodeList viewStateNodeList;
                                    if (viewStateInfo.TryGetValue(frameObject.IdRef, out viewStateNodeList))
                                    {
                                        XamlReader viewStateReader = viewStateNodeList.GetReader();

                                        IXamlLineInfo viewStateLineInfo = viewStateReader as IXamlLineInfo;
                                        bool viewStateShouldPassLineInfo = viewStateLineInfo != null && viewStateLineInfo.HasLineInfo && lineInfoComsumer != null && lineInfoComsumer.ShouldProvideLineInfo;

                                        while (viewStateReader.Read())
                                        {
                                            if (viewStateShouldPassLineInfo)
                                            {
                                                lineInfoComsumer.SetLineInfo(viewStateLineInfo.LineNumber, viewStateLineInfo.LinePosition);
                                            }

                                            mergedNodeWriter.WriteNode(viewStateReader);
                                        }
                                    }
                                }
                                break;
                        }
                        if (!skipWritingWorkflowDefinition)
                        {
                            if (shouldPassLineInfo)
                            {
                                lineInfoComsumer.SetLineInfo(lineInfo.LineNumber, lineInfo.LinePosition);
                            }

                            mergedNodeWriter.WriteNode(workflowDefinition);
                        }
                    }
                }

                retVal = mergedNodeList.GetReader();
            }

            return retVal;
        }
コード例 #39
0
        // This method reads a ViewStateData node from the xaml nodestream. It outputs the Id property into viewStateId
        // and the attached viewstate related properties in viewStateNodes. The input reader is positioned on a 
        // ViewStateData node within ViewStateManager.
        static void ReadViewState(XamlType viewStateType, XamlReader xamlReader, out string viewStateId, out XamlNodeList viewStateNodes, out SourceLocation sourceLocation)
        {
            int globalMemberLevel = 0;
            bool skippingUnexpectedAttachedProperty = false;
            int skippingUnexpectedAttachedPropertyLevel = 0;
            viewStateId = null;
            viewStateNodes = new XamlNodeList(viewStateType.SchemaContext);
            sourceLocation = null;

            Stack<Frame> objectNodes = new Stack<Frame>();
            XamlMember idMember = new XamlMember(typeof(ViewStateData).GetProperty("Id"), xamlReader.SchemaContext);
            int[] viewStateDataSourceLocation = new int[4];
            int sourceLocationIndex = -1;

            IXamlLineInfo lineInfo = xamlReader as IXamlLineInfo;
            IXamlLineInfoConsumer lineInfoComsumer = viewStateNodes.Writer as IXamlLineInfoConsumer;
            bool shouldPassLineInfo = lineInfo != null && lineInfo.HasLineInfo && lineInfoComsumer != null && lineInfoComsumer.ShouldProvideLineInfo;

            while (xamlReader.Read())
            {
                bool skipWritingToNodeList = false;
                switch (xamlReader.NodeType)
                {
                    case XamlNodeType.StartObject:
                        if (xamlReader.Type.Equals(viewStateType))
                        {
                            skipWritingToNodeList = true;
                        }
                        objectNodes.Push(new Frame { Type = xamlReader.Type });
                        break;

                    case XamlNodeType.GetObject:
                        objectNodes.Push(new Frame { Type = null });
                        break;

                    case XamlNodeType.StartMember:
                        globalMemberLevel++;
                        if (xamlReader.Member.Equals(idMember))
                        {
                            XamlReader idNode = xamlReader.ReadSubtree();
                            while (idNode.Read())
                            {
                                if (idNode.NodeType == XamlNodeType.Value)
                                {
                                    viewStateId = idNode.Value as string;
                                }
                            }
                        } 
                        else if (globalMemberLevel == 1 && !IsAttachablePropertyForConvert(xamlReader)) 
                        {
                            skippingUnexpectedAttachedProperty = true;
                        }
                        if (skippingUnexpectedAttachedProperty)
                        {
                            skippingUnexpectedAttachedPropertyLevel++;
                        }

                        sourceLocationIndex = GetViewStateDataSourceLocationIndexFromCurrentReader(xamlReader);
                        break;

                    case XamlNodeType.EndMember:
                        globalMemberLevel--;
                        if (skippingUnexpectedAttachedProperty)
                        {
                            skippingUnexpectedAttachedPropertyLevel--;
                        }
                        break;
                    case XamlNodeType.Value:
                        if (xamlReader.Value is int
                            && sourceLocationIndex >= 0 
                            && sourceLocationIndex < viewStateDataSourceLocation.Length)
                        {
                            viewStateDataSourceLocation[sourceLocationIndex] = (int)xamlReader.Value;
                        }

                        break;
                    case XamlNodeType.EndObject:
                        Frame objectNode = objectNodes.Pop();
                        if (objectNode.Type != null && objectNode.Type.Equals(viewStateType))
                        {
                            skipWritingToNodeList = true;
                            // The ViewStateData's source location should be valid, because
                            // before each EndObject, its SourceLocation is injected.
                            // If not, an exception will be thrown from constructor 
                            // of SourceLocation.
                            sourceLocation = new SourceLocation(null,
                                viewStateDataSourceLocation[0],
                                viewStateDataSourceLocation[1],
                                viewStateDataSourceLocation[2],
                                viewStateDataSourceLocation[3]
                                );
                        }

                        Array.Clear(viewStateDataSourceLocation, 0, viewStateDataSourceLocation.Length);
                        break;
                };

                if (!skipWritingToNodeList && !skippingUnexpectedAttachedProperty)
                {
                    if (shouldPassLineInfo)
                    {
                        lineInfoComsumer.SetLineInfo(lineInfo.LineNumber, lineInfo.LinePosition);
                    }

                    viewStateNodes.Writer.WriteNode(xamlReader);
                }

                if (skippingUnexpectedAttachedPropertyLevel == 0)
                {
                    skippingUnexpectedAttachedProperty = false;
                }
            }
            viewStateNodes.Writer.Close();
        }
コード例 #40
0
 public DeferredWriter(XamlSchemaContext context, XamlDeferringLoader loader)
 {
     List   = new XamlNodeList(context);
     Loader = loader;
 }
コード例 #41
0
 public XamlNodeListWriter(XamlNodeList source)
 {
     this.source = source;
 }
コード例 #42
0
ファイル: Baml2006Reader.cs プロジェクト: sjyanxin/WPFSource
        private void RemoveImplicitFrame() 
        {
            if (_context.CurrentFrame.Flags == Baml2006ReaderFrameFlags.IsImplict) 
            {
                _xamlNodesWriter.WriteEndMember();
                _xamlNodesWriter.WriteEndObject();
 
                _context.PopScope();
            } 
 
            if (_context.CurrentFrame.Flags == Baml2006ReaderFrameFlags.HasImplicitProperty)
            { 
                // If we are encoding a template there is some extra checking.
                if (_context.CurrentFrame.Depth == _context.TemplateStartDepth)
                {
                    // If the template is done.  Switch back to the previous writer. 
                    // Write the spooled Template Node List as a single Value.
                    _xamlNodesWriter.Close(); 
                    _xamlNodesWriter = _xamlWriterStack.Pop(); 
                    _xamlNodesWriter.WriteValue(_xamlTemplateNodeList);
                    _xamlTemplateNodeList = null; 
                    _context.TemplateStartDepth = -1;
                }

                _xamlNodesWriter.WriteEndMember(); 

                _context.CurrentFrame.Member = null; 
                _context.CurrentFrame.Flags = Baml2006ReaderFrameFlags.None; 
            }
        }