DesignTimeXamlWriter(NamespaceIndentingXmlWriter underlyingWriter, WorkflowDesignerXamlSchemaContext context, bool shouldWriteDebugSymbol)
     : base(underlyingWriter, context,
         // Setting AssumeValidInput to true allows to save a document even if it has duplicate members
         new XamlXmlWriterSettings { AssumeValidInput = true })
 {
     underlyingWriter.Parent = this;
     this.namespacesToIgnore = new HashSet<string>();
     this.rootLevelNamespaces = new HashSet<string>();
     this.schemaContext = context;
     this.currentDepth = 0;
     this.shouldWriteDebugSymbol = shouldWriteDebugSymbol;
 }
 DesignTimeXamlWriter(NamespaceIndentingXmlWriter underlyingWriter, WorkflowDesignerXamlSchemaContext context, bool shouldWriteDebugSymbol)
     : base(underlyingWriter, context,
            // Setting AssumeValidInput to true allows to save a document even if it has duplicate members
            new XamlXmlWriterSettings {
     AssumeValidInput = true
 })
 {
     underlyingWriter.Parent     = this;
     this.namespacesToIgnore     = new HashSet <string>();
     this.rootLevelNamespaces    = new HashSet <string>();
     this.schemaContext          = context;
     this.currentDepth           = 0;
     this.shouldWriteDebugSymbol = shouldWriteDebugSymbol;
 }
 public DesignTimeXamlWriter(TextWriter textWriter, WorkflowDesignerXamlSchemaContext context, bool shouldWriteDebugSymbol)
     : this(new NamespaceIndentingXmlWriter(textWriter), context, shouldWriteDebugSymbol)
 {
 }
        internal string SerializeToString(object obj, string fileName)
        {
            FrameworkName targetFramework = this.FrameworkName;

            string   sourceFile          = null;
            Activity rootWorkflowElement = GetRootWorkflowElement(obj);
            Dictionary <int, object> modelItemObjectSequence = null;

            // If the current target is 4.5 or Higher, let us not write the filename attribute as DebugSymbol eliminates the need for it.
            // We will serialize without the Filename by removing it from the element and adding it back after serialization
            if (targetFramework.Is45OrHigher())
            {
                if (AttachablePropertyServices.TryGetProperty <string>(rootWorkflowElement, XamlDebuggerXmlReader.FileNameName, out sourceFile))
                {
                    AttachablePropertyServices.RemoveProperty(rootWorkflowElement, XamlDebuggerXmlReader.FileNameName);
                }
            }

            TextWriter textWriter = new StringWriter(CultureInfo.InvariantCulture);

            WorkflowDesignerXamlSchemaContext schemaContext = obj is ActivityBuilder ? this.XamlSchemaContext : new WorkflowDesignerXamlSchemaContext(null);

            bool shouldWriteDebugSymbol = true;

            if (targetFramework.IsLessThan45())
            {
                shouldWriteDebugSymbol = false;
            }

            System.Xaml.XamlReader       outerReader;
            XamlObjectReaderWithSequence innerReader;

            using (textWriter)
            {
                UsingXamlWriter(
                    new DesignTimeXamlWriter(textWriter, schemaContext, shouldWriteDebugSymbol),
                    delegate(DesignTimeXamlWriter designTimeXamlWriter)
                {
                    UsingXamlWriter(
                        ActivityXamlServices.CreateBuilderWriter(designTimeXamlWriter),
                        delegate(System.Xaml.XamlWriter activityBuilderWriter)
                    {
                        UsingXamlWriter(
                            new ActivityTemplateFactoryBuilderWriter(activityBuilderWriter, schemaContext),
                            delegate(System.Xaml.XamlWriter writer)
                        {
                            // If ViewStateManager property is attached, remove it. It needs to be regenerated if we target 4.5.
                            // It should be removed if we're targeting 4.0.
                            AttachablePropertyServices.RemoveProperty(obj, WorkflowViewState.ViewStateManagerProperty);

                            this.CreateXamlObjectReaders(obj, schemaContext, out outerReader, out innerReader);

                            using (innerReader)
                            {
                                using (outerReader)
                                {
#if ERROR_TOLERANT_SUPPORT
                                    if (ErrorActivity.GetHasErrorActivities(obj))
                                    {
                                        ErrorTolerantObjectWriter.TransformAndStripErrors(outerReader, writer);
                                    }
                                    else
                                    {
#endif
                                    XamlServices.Transform(outerReader, writer);
#if ERROR_TOLERANT_SUPPORT
                                }
#endif
                                }
                            }

                            modelItemObjectSequence = innerReader.SequenceNumberToObjectMap;
                        });
                    });
                });
            }

            string retVal = textWriter.ToString();

            if (targetFramework.IsLessThan45())
            {
                if (sourceFile != null)
                {
                    XamlDebuggerXmlReader.SetFileName(rootWorkflowElement, sourceFile);
                }
            }

            IList <XamlLoadErrorInfo> loadErrors;
            Dictionary <object, SourceLocation> sourceLocations;
            object deserializedObject = this.DeserializeString(retVal, out loadErrors, out sourceLocations);

            if (!string.IsNullOrEmpty(fileName) && targetFramework.Is45OrHigher())
            {
                this.LastWorkflowSymbol = this.GetWorkflowSymbol(fileName, deserializedObject, sourceLocations);
                if (this.LastWorkflowSymbol != null)
                {
                    retVal = retVal.Replace(DesignTimeXamlWriter.EmptyWorkflowSymbol, this.LastWorkflowSymbol.Encode());
                }
            }

            // The symbol is actually removed in GetAttachedWorkflowSymbol() after deserialization completes.
            System.Xaml.AttachablePropertyServices.RemoveProperty(GetRootWorkflowElement(deserializedObject), DebugSymbol.SymbolName);
            this.CreateXamlObjectReaders(deserializedObject, schemaContext, out outerReader, out innerReader);
            Dictionary <object, object> sourceLocationObjectToModelItemObjectMapping = new Dictionary <object, object>(ObjectReferenceEqualityComparer <object> .Default);
            using (innerReader)
            {
                using (outerReader)
                {
                    while (outerReader.Read())
                    {
                    }

                    Dictionary <int, object> sourceLocationObjectSequence = innerReader.SequenceNumberToObjectMap;
                    foreach (KeyValuePair <int, object> sourceLocationObjectEntry in sourceLocationObjectSequence)
                    {
                        int    key = sourceLocationObjectEntry.Key;
                        object sourceLocationObject = sourceLocationObjectEntry.Value;
                        object modelItemObject;

                        if (modelItemObjectSequence.TryGetValue(key, out modelItemObject))
                        {
                            sourceLocationObjectToModelItemObjectMapping.Add(sourceLocationObject, modelItemObject);
                        }
                    }
                }
            }

            this.OnSerializationCompleted(sourceLocationObjectToModelItemObjectMapping);
            return(retVal);
        }
 public DesignTimeXamlWriter(TextWriter textWriter, WorkflowDesignerXamlSchemaContext context, bool shouldWriteDebugSymbol)
     : this(new NamespaceIndentingXmlWriter(textWriter), context, shouldWriteDebugSymbol)
 {
 }