Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / Tools / System.Activities.Presentation / System / Activities / Presentation / Xaml / DesignTimeXamlWriter.cs / 1305376 / DesignTimeXamlWriter.cs
//---------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. //--------------------------------------------------------------- namespace System.Activities.Presentation.Xaml { using System.Collections.Generic; using System.Globalization; using System.IO; using System.Xaml; using System.Xml; class DesignTimeXamlWriter : XamlXmlWriter { //namespaces to ignore (don't load assembilies for) at root node HashSetnamespacesToIgnore; //namespaces we've seen at root level, we use this to figure out appropriate alias for MC namespace HashSet rootLevelNamespaces; // for duplicate namespace filtering (happens if we're using the local assembly to compile itself) HashSet emittedNamespacesInLocalAssembly; //For namespace defined in local assembly with assembly info in namespace declaration, we'll strip out the assembly info //and hold the namespace temporarily. Before writing the start object, we'll check whether the short version gets written //as a separate declaration, if not, we write it out. List localNamespacesWithAssemblyInfo; bool hasWrittenObject; WorkflowDesignerXamlSchemaContext schemaContext; public DesignTimeXamlWriter(TextWriter textWriter, WorkflowDesignerXamlSchemaContext context) : base(XmlWriter.Create(textWriter, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true }), context, // Setting AssumeValidInput to true allows to save a document even if it has duplicate members new XamlXmlWriterSettings { AssumeValidInput = true }) { this.namespacesToIgnore = new HashSet (); this.rootLevelNamespaces = new HashSet (); this.schemaContext = context; } public override void WriteNamespace(NamespaceDeclaration namespaceDeclaration) { if (!this.hasWrittenObject) { //we need to track every namespace alias appeared in root element to figure out right alias for MC namespace this.rootLevelNamespaces.Add(namespaceDeclaration.Prefix); //Remember namespaces needed to be ignored at top level so we will add ignore attribute for them when we write start object if (NameSpaces.ShouldIgnore(namespaceDeclaration.Namespace)) { this.namespacesToIgnore.Add(namespaceDeclaration.Prefix); } } EmitNamespace(namespaceDeclaration); } void EmitNamespace(NamespaceDeclaration namespaceDeclaration) { // Write the namespace, filtering for duplicates in the local assembly because VS might be using it to compile itself. if (schemaContext.IsClrNamespaceWithNoAssembly(namespaceDeclaration.Namespace)) { // Might still need to trim a semicolon, even though it shouldn't strictly be there. string nonassemblyQualifedNamespace = namespaceDeclaration.Namespace; if (nonassemblyQualifedNamespace[nonassemblyQualifedNamespace.Length - 1] == ';') { nonassemblyQualifedNamespace = nonassemblyQualifedNamespace.Substring(0, nonassemblyQualifedNamespace.Length - 1); namespaceDeclaration = new NamespaceDeclaration(nonassemblyQualifedNamespace, namespaceDeclaration.Prefix); } EmitLocalNamespace(namespaceDeclaration); } else if (schemaContext.IsClrNamespaceInLocalAssembly(namespaceDeclaration.Namespace)) { string nonassemblyQualifedNamespace = schemaContext.TrimLocalAssembly(namespaceDeclaration.Namespace); namespaceDeclaration = new NamespaceDeclaration(nonassemblyQualifedNamespace, namespaceDeclaration.Prefix); if (this.localNamespacesWithAssemblyInfo == null) { this.localNamespacesWithAssemblyInfo = new List (); } this.localNamespacesWithAssemblyInfo.Add(namespaceDeclaration); } else { base.WriteNamespace(namespaceDeclaration); } } void EmitLocalNamespace(NamespaceDeclaration namespaceDeclaration) { if (this.emittedNamespacesInLocalAssembly == null) // lazy initialization { this.emittedNamespacesInLocalAssembly = new HashSet (); } // Write the namespace only once. Add() returns false if it was already there. if (this.emittedNamespacesInLocalAssembly.Add(namespaceDeclaration.Namespace)) { base.WriteNamespace(namespaceDeclaration); } } public override void WriteStartObject(XamlType type) { // this is the top-level object if (!this.hasWrittenObject) { // we need to write MC namespace if any namespaces need to be ignored if (this.namespacesToIgnore.Count > 0) { string mcNamespaceAlias = GenerateNamespaceAlias(NameSpaces.McPrefix); this.WriteNamespace(new NamespaceDeclaration(NameSpaces.Mc, mcNamespaceAlias)); } if (this.localNamespacesWithAssemblyInfo != null) { foreach (NamespaceDeclaration xamlNamespace in this.localNamespacesWithAssemblyInfo) { if ((this.emittedNamespacesInLocalAssembly == null) || (!this.emittedNamespacesInLocalAssembly.Contains(xamlNamespace.Namespace))) { base.WriteNamespace(xamlNamespace); } } } } base.WriteStartObject(type); if (!this.hasWrittenObject) { // we need to add Ignore attribute for all namespaces which we don't want to load assemblies for // this has to be done after WriteStartObject if (this.namespacesToIgnore.Count > 0) { foreach (string ns in this.namespacesToIgnore) { XamlDirective ignorable = new XamlDirective(NameSpaces.Mc, "Ignorable"); base.WriteStartMember(ignorable); base.WriteValue(ns); base.WriteEndMember(); } this.namespacesToIgnore.Clear(); } this.hasWrittenObject = true; } } string GenerateNamespaceAlias(string prefix) { string aliasPostfix = string.Empty; //try "mc"~"mc1000" first for (int i = 1; i <= 1000; i++) { string mcAlias = prefix + aliasPostfix; if (!this.rootLevelNamespaces.Contains(mcAlias)) { return mcAlias; } aliasPostfix = i.ToString(CultureInfo.InvariantCulture); } //roll the dice return prefix + Guid.NewGuid().ToString(); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //---------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. //--------------------------------------------------------------- namespace System.Activities.Presentation.Xaml { using System.Collections.Generic; using System.Globalization; using System.IO; using System.Xaml; using System.Xml; class DesignTimeXamlWriter : XamlXmlWriter { //namespaces to ignore (don't load assembilies for) at root node HashSet namespacesToIgnore; //namespaces we've seen at root level, we use this to figure out appropriate alias for MC namespace HashSet rootLevelNamespaces; // for duplicate namespace filtering (happens if we're using the local assembly to compile itself) HashSet emittedNamespacesInLocalAssembly; //For namespace defined in local assembly with assembly info in namespace declaration, we'll strip out the assembly info //and hold the namespace temporarily. Before writing the start object, we'll check whether the short version gets written //as a separate declaration, if not, we write it out. List localNamespacesWithAssemblyInfo; bool hasWrittenObject; WorkflowDesignerXamlSchemaContext schemaContext; public DesignTimeXamlWriter(TextWriter textWriter, WorkflowDesignerXamlSchemaContext context) : base(XmlWriter.Create(textWriter, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true }), context, // Setting AssumeValidInput to true allows to save a document even if it has duplicate members new XamlXmlWriterSettings { AssumeValidInput = true }) { this.namespacesToIgnore = new HashSet (); this.rootLevelNamespaces = new HashSet (); this.schemaContext = context; } public override void WriteNamespace(NamespaceDeclaration namespaceDeclaration) { if (!this.hasWrittenObject) { //we need to track every namespace alias appeared in root element to figure out right alias for MC namespace this.rootLevelNamespaces.Add(namespaceDeclaration.Prefix); //Remember namespaces needed to be ignored at top level so we will add ignore attribute for them when we write start object if (NameSpaces.ShouldIgnore(namespaceDeclaration.Namespace)) { this.namespacesToIgnore.Add(namespaceDeclaration.Prefix); } } EmitNamespace(namespaceDeclaration); } void EmitNamespace(NamespaceDeclaration namespaceDeclaration) { // Write the namespace, filtering for duplicates in the local assembly because VS might be using it to compile itself. if (schemaContext.IsClrNamespaceWithNoAssembly(namespaceDeclaration.Namespace)) { // Might still need to trim a semicolon, even though it shouldn't strictly be there. string nonassemblyQualifedNamespace = namespaceDeclaration.Namespace; if (nonassemblyQualifedNamespace[nonassemblyQualifedNamespace.Length - 1] == ';') { nonassemblyQualifedNamespace = nonassemblyQualifedNamespace.Substring(0, nonassemblyQualifedNamespace.Length - 1); namespaceDeclaration = new NamespaceDeclaration(nonassemblyQualifedNamespace, namespaceDeclaration.Prefix); } EmitLocalNamespace(namespaceDeclaration); } else if (schemaContext.IsClrNamespaceInLocalAssembly(namespaceDeclaration.Namespace)) { string nonassemblyQualifedNamespace = schemaContext.TrimLocalAssembly(namespaceDeclaration.Namespace); namespaceDeclaration = new NamespaceDeclaration(nonassemblyQualifedNamespace, namespaceDeclaration.Prefix); if (this.localNamespacesWithAssemblyInfo == null) { this.localNamespacesWithAssemblyInfo = new List (); } this.localNamespacesWithAssemblyInfo.Add(namespaceDeclaration); } else { base.WriteNamespace(namespaceDeclaration); } } void EmitLocalNamespace(NamespaceDeclaration namespaceDeclaration) { if (this.emittedNamespacesInLocalAssembly == null) // lazy initialization { this.emittedNamespacesInLocalAssembly = new HashSet (); } // Write the namespace only once. Add() returns false if it was already there. if (this.emittedNamespacesInLocalAssembly.Add(namespaceDeclaration.Namespace)) { base.WriteNamespace(namespaceDeclaration); } } public override void WriteStartObject(XamlType type) { // this is the top-level object if (!this.hasWrittenObject) { // we need to write MC namespace if any namespaces need to be ignored if (this.namespacesToIgnore.Count > 0) { string mcNamespaceAlias = GenerateNamespaceAlias(NameSpaces.McPrefix); this.WriteNamespace(new NamespaceDeclaration(NameSpaces.Mc, mcNamespaceAlias)); } if (this.localNamespacesWithAssemblyInfo != null) { foreach (NamespaceDeclaration xamlNamespace in this.localNamespacesWithAssemblyInfo) { if ((this.emittedNamespacesInLocalAssembly == null) || (!this.emittedNamespacesInLocalAssembly.Contains(xamlNamespace.Namespace))) { base.WriteNamespace(xamlNamespace); } } } } base.WriteStartObject(type); if (!this.hasWrittenObject) { // we need to add Ignore attribute for all namespaces which we don't want to load assemblies for // this has to be done after WriteStartObject if (this.namespacesToIgnore.Count > 0) { foreach (string ns in this.namespacesToIgnore) { XamlDirective ignorable = new XamlDirective(NameSpaces.Mc, "Ignorable"); base.WriteStartMember(ignorable); base.WriteValue(ns); base.WriteEndMember(); } this.namespacesToIgnore.Clear(); } this.hasWrittenObject = true; } } string GenerateNamespaceAlias(string prefix) { string aliasPostfix = string.Empty; //try "mc"~"mc1000" first for (int i = 1; i <= 1000; i++) { string mcAlias = prefix + aliasPostfix; if (!this.rootLevelNamespaces.Contains(mcAlias)) { return mcAlias; } aliasPostfix = i.ToString(CultureInfo.InvariantCulture); } //roll the dice return prefix + Guid.NewGuid().ToString(); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- CombinedGeometry.cs
- ProviderConnectionPointCollection.cs
- SchemaCollectionPreprocessor.cs
- _FtpDataStream.cs
- X509Certificate.cs
- Object.cs
- XmlDataSourceNodeDescriptor.cs
- DisplayMemberTemplateSelector.cs
- ConstraintConverter.cs
- SqlRowUpdatingEvent.cs
- RestrictedTransactionalPackage.cs
- NegatedCellConstant.cs
- UnicodeEncoding.cs
- MimeXmlReflector.cs
- OleDbTransaction.cs
- AppSettingsExpressionBuilder.cs
- BeginCreateSecurityTokenRequest.cs
- ApplicationSettingsBase.cs
- DataGridViewRowCancelEventArgs.cs
- FixedSOMPageElement.cs
- ExpressionVisitor.cs
- InputEventArgs.cs
- RootBrowserWindowProxy.cs
- Cell.cs
- SQLResource.cs
- DataGridViewCell.cs
- XmlConvert.cs
- CompilerCollection.cs
- PaperSize.cs
- TableItemStyle.cs
- HtmlInputText.cs
- ReliableChannelFactory.cs
- VisualBasicSettingsConverter.cs
- CodeAccessSecurityEngine.cs
- OverrideMode.cs
- ValueUtilsSmi.cs
- DefaultProxySection.cs
- XPathExpr.cs
- RewritingValidator.cs
- StoryFragments.cs
- SemaphoreFullException.cs
- XmlArrayItemAttribute.cs
- DoubleUtil.cs
- HtmlInputFile.cs
- BinarySerializer.cs
- FileRegion.cs
- SemanticBasicElement.cs
- Ref.cs
- MessageQueue.cs
- DesignerVerb.cs
- StrokeCollectionDefaultValueFactory.cs
- ConstraintStruct.cs
- _ServiceNameStore.cs
- ConfigurationValues.cs
- DataGridDefaultColumnWidthTypeConverter.cs
- GB18030Encoding.cs
- SqlUDTStorage.cs
- ApplicationHost.cs
- CrossContextChannel.cs
- BinaryUtilClasses.cs
- OdbcConnectionPoolProviderInfo.cs
- TextBoxAutomationPeer.cs
- DataGridParentRows.cs
- ResourceSet.cs
- ELinqQueryState.cs
- HeaderedContentControl.cs
- TabPanel.cs
- TextChangedEventArgs.cs
- CompilationUtil.cs
- SiteMapDataSourceView.cs
- CalendarTable.cs
- MulticastNotSupportedException.cs
- InvalidWorkflowException.cs
- WebPartConnectionsDisconnectVerb.cs
- SapiRecognizer.cs
- ContentPlaceHolder.cs
- ParenExpr.cs
- LogicalExpressionTypeConverter.cs
- InvalidComObjectException.cs
- CultureInfoConverter.cs
- Main.cs
- TreeBuilderBamlTranslator.cs
- SmiEventSink.cs
- x509utils.cs
- ChangeInterceptorAttribute.cs
- HashRepartitionStream.cs
- SharedUtils.cs
- OrderedDictionary.cs
- EncodingTable.cs
- TextEditorCopyPaste.cs
- ListControl.cs
- RewritingValidator.cs
- CqlQuery.cs
- ChangePassword.cs
- PreviewControlDesigner.cs
- SystemFonts.cs
- DefaultDiscoveryServiceExtension.cs
- DetailsViewDeletedEventArgs.cs
- PerformanceCountersElement.cs
- DerivedKeyCachingSecurityTokenSerializer.cs