88018fb503f10925f167506fa0248286f7421d1b
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / WorkflowDesigner.SerializationHelpers.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation
6 {
7     using System;
8     using System.Activities.Debugger;
9     using System.Activities.Debugger.Symbol;
10     using System.Activities.Presentation.View;
11     using System.Activities.Presentation.Xaml;
12     using System.Activities.XamlIntegration;
13     using System.Collections.Generic;
14     using System.Diagnostics.CodeAnalysis;
15     using System.IO;
16     using System.Runtime;
17     using System.Runtime.Versioning;
18     using System.Xaml;
19     using System.Xml;
20     using Microsoft.Activities.Presentation.Xaml;
21     
22     public partial class WorkflowDesigner : IWorkflowDesignerXamlHelperExecutionContext
23     {
24         FrameworkName IWorkflowDesignerXamlHelperExecutionContext.FrameworkName
25         {
26             get { return WorkflowDesigner.GetTargetFramework(this.context); }
27         }
28
29         WorkflowDesignerXamlSchemaContext IWorkflowDesignerXamlHelperExecutionContext.XamlSchemaContext
30         {
31             get { return this.XamlSchemaContext; }
32         }
33
34         ViewStateIdManager IWorkflowDesignerXamlHelperExecutionContext.IdManager
35         {
36             get { return this.idManager; }
37         }
38
39         WorkflowSymbol IWorkflowDesignerXamlHelperExecutionContext.LastWorkflowSymbol
40         {
41             get { return this.lastWorkflowSymbol; }
42             set { this.lastWorkflowSymbol = value; }
43         }
44
45         void IWorkflowDesignerXamlHelperExecutionContext.OnSerializationCompleted(Dictionary<object, object> sourceLocationObjectToModelItemObjectMapping)
46         {
47             this.ObjectToSourceLocationMapping.SourceLocationObjectToModelItemObjectMapping = sourceLocationObjectToModelItemObjectMapping;
48             this.ObjectReferenceService.OnSaveCompleted();
49         }
50
51         void IWorkflowDesignerXamlHelperExecutionContext.OnBeforeDeserialize()
52         {
53             this.ObjectToSourceLocationMapping.Clear();
54         }
55
56         void IWorkflowDesignerXamlHelperExecutionContext.OnSourceLocationFound(object target, SourceLocation sourceLocationFound)
57         {
58             this.ObjectToSourceLocationMapping.UpdateMap(target, sourceLocationFound);
59         }
60
61         void IWorkflowDesignerXamlHelperExecutionContext.OnAfterDeserialize(Dictionary<string, SourceLocation> viewStateDataSourceLocationMapping)
62         {
63             this.ObjectToSourceLocationMapping.ViewStateDataSourceLocationMapping = viewStateDataSourceLocationMapping;
64         }
65
66         string IWorkflowDesignerXamlHelperExecutionContext.LocalAssemblyName
67         {
68             get { return this.GetLocalAssemblyName(); }
69         }
70
71         WorkflowDesignerXamlSchemaContext XamlSchemaContext
72         {
73             get
74             {
75                 if (this.workflowDesignerXamlSchemaContext == null)
76                 {
77                     this.workflowDesignerXamlSchemaContext = new WorkflowDesignerXamlSchemaContext(this.GetLocalAssemblyName(), this.Context);
78                 }
79
80                 return this.workflowDesignerXamlSchemaContext;
81             }
82         }
83
84         internal object DeserializeString(string text)
85         {
86             return new WorkflowDesignerXamlHelper(this).DeserializeString(text);
87         }
88
89         internal object DeserializeString(string text, out IList<XamlLoadErrorInfo> loadErrors, out Dictionary<object, SourceLocation> sourceLocations)
90         {
91             return new WorkflowDesignerXamlHelper(this).DeserializeString(text, out loadErrors, out sourceLocations);
92         }
93
94         internal string SerializeToString(object obj, string fileName = null)
95         {
96             return new WorkflowDesignerXamlHelper(this).SerializeToString(obj, fileName);
97         }
98
99         [SuppressMessage(FxCop.Category.Design, FxCop.Rule.DoNotCatchGeneralExceptionTypes,
100             Justification = "Serializer might throw if it fails to serialize. Catching all exceptions to avoid VS Crash.")]
101         [SuppressMessage("Reliability", "Reliability108",
102             Justification = "Serializer might throw if it fails to serialize. Catching all exceptions to avoid VS crash.")]
103         void WriteModelToText(string fileName)
104         {
105             this.perfEventProvider.WorkflowDesignerSerializeStart();
106             object rootModelObject = this.modelTreeManager.Root.GetCurrentValue();
107             // if we are serializing a activity schema type, remove the namespace in the Name property.
108             ActivityBuilder activityBuilderType = rootModelObject as ActivityBuilder;
109
110             // now try to serialize
111             try
112             {
113                 string newText = SerializeToString(rootModelObject, fileName);
114                 if (string.IsNullOrEmpty(this.Text) ||
115                     (this.isModelChanged && !string.Equals(newText, this.Text, StringComparison.Ordinal)))
116                 {
117                     this.Text = newText;
118                     if (this.TextChanged != null)
119                     {
120                         this.TextChanged.Invoke(this, null);
121                     }
122                 }
123                 this.isModelChanged = false;
124             }
125             catch (Exception e)
126             {
127                 this.Context.Items.SetValue(new ErrorItem() { Message = e.Message, Details = e.ToString() });
128             }
129             this.perfEventProvider.WorkflowDesignerSerializeEnd();
130         }
131
132         void RaiseLoadError(Exception e)
133         {
134             if (this.xamlLoadErrorService != null)
135             {
136                 XamlLoadErrorInfo errorInfo = null;
137                 XamlException xamlEx = e as XamlException;
138                 if (xamlEx != null)
139                 {
140                     errorInfo = new XamlLoadErrorInfo(xamlEx.Message, xamlEx.LineNumber, xamlEx.LinePosition);
141                 }
142                 else
143                 {
144                     XmlException xmlEx = e as XmlException;
145                     if (xmlEx != null)
146                     {
147                         errorInfo = new XamlLoadErrorInfo(xmlEx.Message, xmlEx.LineNumber, xmlEx.LinePosition);
148                     }
149                 }
150                 if (errorInfo != null)
151                 {
152                     var errors = new XamlLoadErrorInfo[] { errorInfo };
153                     xamlLoadErrorService.ShowXamlLoadErrors(errors);
154                 }
155             }
156         }
157
158         void RaiseLoadErrors(IList<XamlLoadErrorInfo> loadErrors)
159         {
160             if (this.xamlLoadErrorService != null)
161             {
162                 this.xamlLoadErrorService.ShowXamlLoadErrors(loadErrors);
163             }
164         }
165     }
166 }