public DynamicActivityXamlReader(bool isBuilder, XamlReader innerReader, XamlSchemaContext schemaContext)
 {
     this.isBuilder = isBuilder;
     this.innerReader = innerReader;
     this.schemaContext = schemaContext ?? innerReader.SchemaContext;
     this.xamlTypeXamlType = this.schemaContext.GetXamlType(typeof(XamlType));
     this.typeXamlType = this.schemaContext.GetXamlType(typeof(System.Type));
     this.baseActivityXamlType = this.schemaContext.GetXamlType(typeof(Activity));
     this.activityPropertyXamlType = this.schemaContext.GetXamlType(typeof(DynamicActivityProperty));
     this.activityPropertyType = this.activityPropertyXamlType.GetMember("Type");
     this.activityPropertyName = this.activityPropertyXamlType.GetMember("Name");
     this.activityPropertyValue = this.activityPropertyXamlType.GetMember("Value");
     this.activityPropertyAttributes = this.activityPropertyXamlType.GetMember("Attributes");
     this.namespaceTable = new NamespaceTable();
     this.frontLoadedDirectives = true;
     this.nodeQueue = new XamlNodeQueue(this.schemaContext);
     this.nodeReader = this.nodeQueue.Reader;
     IXamlLineInfo info = innerReader as IXamlLineInfo;
     if ((info != null) && info.HasLineInfo)
     {
         this.innerReaderLineInfo = info;
         this.nodeReaderLineInfo = (IXamlLineInfo) this.nodeQueue.Reader;
         this.hasLineInfo = true;
     }
 }
		public XamlBackgroundReader (XamlReader wrappedReader)
		{
			if (wrappedReader == null)
				throw new ArgumentNullException ("wrappedReader");
			r = wrappedReader;
			q = new XamlNodeQueue (r.SchemaContext) { LineInfoProvider = r as IXamlLineInfo };
		}
Esempio n. 3
0
 public XamlBackgroundReader(XamlReader wrappedReader)
 {
     if (wrappedReader == null)
     {
         throw new ArgumentNullException("wrappedReader");
     }
     r = wrappedReader;
     q = new XamlNodeQueue(r.SchemaContext)
     {
         LineInfoProvider = r as IXamlLineInfo
     };
 }
Esempio n. 4
0
		public void Read ()
		{
			var q = new XamlNodeQueue (new XamlSchemaContext ());
			Assert.IsFalse (q.Reader.Read (), "#1");
			Assert.IsTrue (q.Reader.IsEof, "#1-2");

			q.Writer.WriteStartObject (XamlLanguage.String);
			Assert.IsTrue (q.Reader.Read (), "#2");
			Assert.IsFalse (q.Reader.IsEof, "#2-2");
			Assert.AreEqual (XamlLanguage.String, q.Reader.Type, "#2-3");
			Assert.IsNull (q.Reader.Member, "#2-4");
			Assert.IsNull (q.Reader.Value, "#2-5");

			Assert.IsFalse (q.Reader.Read (), "#3");
			Assert.IsTrue (q.Reader.IsEof, "#3-2");
		}
 // We don't know what namespaces are actually used inside convertible values, so any namespaces
 // that were in the parent scope on load need to be regurgitated on save, unless the prefixes were
 // shadowed in the child scope.
 // This can potentially cause namespace bloat, but the alternative is emitting unloadable XAML.
 static XamlReader InsertNamespaces(XamlReader reader, IEnumerable<NamespaceDeclaration> parentNamespaces)
 {
     XamlNodeQueue namespaceNodes = new XamlNodeQueue(reader.SchemaContext);
     HashSet<string> childPrefixes = new HashSet<string>();
     while (reader.Read() && reader.NodeType == XamlNodeType.NamespaceDeclaration)
     {
         childPrefixes.Add(reader.Namespace.Prefix);
         namespaceNodes.Writer.WriteNode(reader);
     }
     foreach (NamespaceDeclaration parentNamespace in parentNamespaces)
     {
         if (!childPrefixes.Contains(parentNamespace.Prefix) &&
             !IsXmlNamespace(parentNamespace))
         {
             namespaceNodes.Writer.WriteNamespace(parentNamespace);
         }
     }
     if (!reader.IsEof)
     {
         namespaceNodes.Writer.WriteNode(reader);
     }
     return new ConcatenatingXamlReader(namespaceNodes.Reader, reader);
 }
 public PropertyValueNode(PropertyNode property, ActivityBuilderXamlWriter writer)
     : base(writer)
 {
     this.property = property;
     this.valueNodes = new XamlNodeQueue(writer.SchemaContext);
     base.CurrentWriter = this.valueNodes.Writer;
 }
Esempio n. 7
0
		public XamlNodeQueueWriter (XamlNodeQueue source)
		{
			this.source = source;
		}
            static XamlNodeQueue StripTypeWrapping(XamlNodeQueue valueNodes, XamlType propertyType)
            {
                XamlNodeQueue targetNodes = new XamlNodeQueue(valueNodes.Reader.SchemaContext);
                XamlReader source = valueNodes.Reader;
                XamlWriter target = targetNodes.Writer;
                int depth = 0;
                bool consumeWrapperEndTags = false;
                bool hasBufferedStartObject = false;

                while (source.Read())
                {
                    switch (source.NodeType)
                    {
                        case XamlNodeType.StartObject:
                            depth++;
                            // only strip the wrapping type nodes if we have exactly this sequence:
                            // StartObject StartMember(Intialization) Value EndMember EndObject.
                            if (targetNodes.Count == 0 && depth == 1 && source.Type == propertyType && valueNodes.Count == 5)
                            {
                                hasBufferedStartObject = true;
                                continue;
                            }
                            break;

                        case XamlNodeType.GetObject:
                            depth++;
                            break;

                        case XamlNodeType.StartMember:
                            depth++;
                            if (hasBufferedStartObject)
                            {
                                if (depth == 2 && source.Member == XamlLanguage.Initialization)
                                {
                                    consumeWrapperEndTags = true;
                                    continue;
                                }
                                else
                                {
                                    hasBufferedStartObject = false;
                                    targetNodes.Writer.WriteStartObject(propertyType);
                                }
                            }
                            break;

                        case XamlNodeType.EndMember:
                            depth--;
                            if (consumeWrapperEndTags && depth == 1)
                            {
                                continue;
                            }
                            break;

                        case XamlNodeType.EndObject:
                            depth--;
                            if (consumeWrapperEndTags && depth == 0)
                            {
                                consumeWrapperEndTags = false;
                                continue;
                            }
                            break;
                    }

                    target.WriteNode(source);
                }

                return targetNodes;
            }
 public PropertiesNode(BuilderClassNode classNode, ActivityBuilderXamlWriter writer)
     : base(writer)
 {
     this.classNode = classNode;
     this.propertiesNodes = new XamlNodeQueue(writer.SchemaContext);
     base.CurrentWriter = this.propertiesNodes.Writer;
     CurrentWriter.WriteStartMember(XamlLanguage.Members);
 }
 public void SetDefaultValue(XamlNodeQueue defaultValue)
 {
     this.defaultValue = defaultValue;
 }
 // Attributes [DependsOn("Name")]
 public void SetAttributes(XamlNodeQueue attributeNodes)
 {
     this.xClassAttributeNodes = attributeNodes;
 }
            void FlushPreamble()
            {
                if (this.otherNodes == null) // already flushed
                {
                    return;
                }
                CurrentWriter = this.Writer.innerWriter;
                string classNamespace = null;
                // first, see if we need to emit a namespace corresponding to our class
                if (this.defaultValueNodes != null)
                {
                    classNamespace = this.xClassNamespace;
                }

                this.rootNode.FlushPendingNodes(classNamespace);
                this.rootNode = null; // not needed anymore

                CurrentWriter.WriteStartObject(this.Writer.activityXamlType);

                // first dump x:Class
                if (this.xClassNodes == null)
                {
                    SetXClass(null, null); // this will setup a default
                }
                XamlServices.Transform(this.xClassNodes.Reader, CurrentWriter, false);

                // String default values get written in attribute form immediately.
                // Other values get deferred until after x:Members, etc.
                XamlNodeQueue deferredPropertyNodes = null;
                if (this.defaultValueNodes != null)
                {
                    foreach (KeyValuePair<string, XamlNodeQueue> defaultValueNode in this.defaultValueNodes)
                    {
                        XamlReader reader = defaultValueNode.Value.Reader;
                        if (reader.Read())
                        {
                            bool isStringValue = false;
                            if (reader.NodeType == XamlNodeType.Value)
                            {
                                string stringValue = reader.Value as string;
                                if (stringValue != null)
                                {
                                    isStringValue = true;
                                }
                            }
                            if (isStringValue)
                            {
                                CurrentWriter.WriteStartMember(new XamlMember(defaultValueNode.Key, this.xClassXamlType, true));
                                CurrentWriter.WriteNode(reader);
                                XamlServices.Transform(defaultValueNode.Value.Reader, CurrentWriter, false);
                                // don't need an EndMember since it will be sitting in the node list (we only needed to strip the StartMember)                                
                            }
                            else
                            {
                                // Else: We'll write this out in a minute, after the x:ClassAttributes and x:Properties
                                if (deferredPropertyNodes == null)
                                {
                                    deferredPropertyNodes = new XamlNodeQueue(Writer.SchemaContext);
                                }
                                deferredPropertyNodes.Writer.WriteStartMember(new XamlMember(defaultValueNode.Key, this.xClassXamlType, true));
                                deferredPropertyNodes.Writer.WriteNode(reader);
                                XamlServices.Transform(defaultValueNode.Value.Reader, deferredPropertyNodes.Writer, false);
                            }
                        }
                    }
                }

                // then dump x:ClassAttributes if we have any
                if (this.xClassAttributeNodes != null)
                {
                    XamlServices.Transform(this.xClassAttributeNodes.Reader, CurrentWriter, false);
                }

                // and x:Properties
                if (this.xPropertiesNodes != null)
                {
                    XamlServices.Transform(this.xPropertiesNodes.Reader, CurrentWriter, false);
                }

                if (deferredPropertyNodes != null)
                {
                    XamlServices.Transform(deferredPropertyNodes.Reader, CurrentWriter, false);
                }

                if (this.otherNodes.Count > 0)
                {
                    XamlServices.Transform(this.otherNodes.Reader, CurrentWriter, false);
                }
                this.otherNodes = null; // done with this
            }
            public BuilderClassNode(RootNode rootNode, ActivityBuilderXamlWriter writer)
                : base(writer)
            {
                this.rootNode = rootNode;

                // by default, if we're not in a special sub-tree, ferret the nodes away on the side
                this.otherNodes = new XamlNodeQueue(writer.SchemaContext);
                base.CurrentWriter = this.otherNodes.Writer;
            }
        public override bool Read()
        {
            switch (this.currentState)
            {
                case ActivityTemplateFactoryBuilderReaderStates.InitialState:
                    bool hasMoreNodes = this.underlyingReader.Read();
                    if (this.underlyingReader.NodeType == XamlNodeType.StartObject && IsActivityTemplateFactoryType(this.underlyingReader.Type))
                    {
                        Type underlyingType = this.underlyingReader.Type.UnderlyingType;
                        Type targetType = underlyingType.IsGenericType ? underlyingType.GetGenericArguments()[0] : null;

                        this.currentState = ActivityTemplateFactoryBuilderReaderStates.ReadingFromBufferState;
                        this.queuedNodes = new XamlNodeQueue(this.schemaContext);
                        this.queuedNodes.Writer.WriteStartObject(this.ActivityTemplateFactoryBuilderType, (IXamlLineInfo)this.underlyingReader);

                        string className;

                        while (this.underlyingReader.Read())
                        {
                            if (this.underlyingReader.NodeType == XamlNodeType.StartMember && this.underlyingReader.Member == XamlLanguage.Class)
                            {
                                this.underlyingReader.Read();
                                className = (string)this.underlyingReader.Value;
                                this.underlyingReader.Read();
                                this.queuedNodes.Writer.WriteStartMember(this.ActivityTemplateFactoryBuilderNameMember, (IXamlLineInfo)this.underlyingReader);
                                this.queuedNodes.Writer.WriteValue(className, (IXamlLineInfo)this.underlyingReader);
                                this.queuedNodes.Writer.WriteEndMember((IXamlLineInfo)this.underlyingReader);
                                if (targetType != null)
                                {
                                    this.queuedNodes.Writer.WriteStartMember(this.ActivityTemplateFactoryBuilderTargetTypeMember, (IXamlLineInfo)this.underlyingReader);
                                    object targetTypeString = targetType;
                                    this.queuedNodes.Writer.WriteValue(targetTypeString);
                                    this.queuedNodes.Writer.WriteEndMember();
                                }
                            }
                            else if (this.underlyingReader.NodeType == XamlNodeType.StartMember && this.IsActivityTemplateFactoryImplementationMember(this.underlyingReader.Member))
                            {
                                this.queuedNodes.Writer.WriteStartMember(this.ActivityTemplateFactoryBuilderImplementationMember, (IXamlLineInfo)this.underlyingReader);
                                return true;
                            }
                        }
                    }

                    return hasMoreNodes;

                case ActivityTemplateFactoryBuilderReaderStates.ReadingFromBufferState:
                    if (this.queuedNodes.Reader.Read())
                    {
                        return true;
                    }
                    else
                    {
                        this.currentState = ActivityTemplateFactoryBuilderReaderStates.BypassState;
                        this.queuedNodes = null;
                        return this.underlyingReader.Read();
                    }

                default:
                    SharedFx.Assert(this.currentState == ActivityTemplateFactoryBuilderReaderStates.BypassState, "This is the only remaining ActivityTemplateFactoryBuilderReaderStates.");
                    return this.underlyingReader.Read();
            }
        }
Esempio n. 15
0
 public XamlNodeQueueReader(XamlNodeQueue source)
 {
     this.source = source;
     node        = default(XamlNodeLineInfo);
 }
 public bool ProcessNode(XamlReader reader, XamlWriter targetWriter, int currentDepth, IXamlLineInfo readerLineInfo)
 {
     if ((currentDepth == this.Depth) && ((reader.NodeType == XamlNodeType.NamespaceDeclaration) || (reader.NodeType == XamlNodeType.None)))
     {
         this.bufferedNodes.Writer.WriteNode(reader, readerLineInfo);
         reader.Read();
         return true;
     }
     if ((((reader.NodeType == XamlNodeType.StartObject) && reader.Type.IsGeneric) && ((reader.Type.UnderlyingType != null) && (reader.Type.Name == "PropertyReferenceExtension"))) && (reader.Type.UnderlyingType.GetGenericTypeDefinition() == typeof(PropertyReferenceExtension<>)))
     {
         if (this.bufferedNodes.Count > 0)
         {
             XamlServices.Transform(this.bufferedNodes.Reader, targetWriter, false);
             this.bufferedNodes = null;
         }
         XamlType type = reader.Type;
         XamlReader reader2 = reader.ReadSubtree();
         XamlType xamlType = reader.SchemaContext.GetXamlType(typeof(ActivityBuilder));
         XamlType type3 = reader.SchemaContext.GetXamlType(typeof(ActivityPropertyReference));
         targetWriter.WriteStartMember(xamlType.GetAttachableMember("PropertyReference"), readerLineInfo);
         reader2.Read();
         targetWriter.WriteStartObject(type3, readerLineInfo);
         targetWriter.WriteStartMember(type3.GetMember("TargetProperty"), readerLineInfo);
         targetWriter.WriteValue(this.currentMember.Name, readerLineInfo);
         targetWriter.WriteEndMember(readerLineInfo);
         bool flag = reader2.Read();
         bool flag2 = false;
         while (flag)
         {
             if (((reader2.NodeType == XamlNodeType.StartMember) && (reader2.Member.DeclaringType == type)) && (reader2.Member.Name == "PropertyName"))
             {
                 flag2 = true;
             }
             else if (flag2)
             {
                 if (reader2.NodeType == XamlNodeType.EndMember)
                 {
                     flag2 = false;
                 }
                 else if (reader2.NodeType == XamlNodeType.Value)
                 {
                     targetWriter.WriteStartMember(type3.GetMember("SourceProperty"), readerLineInfo);
                     targetWriter.WriteValue((string) reader2.Value, readerLineInfo);
                     targetWriter.WriteEndMember(readerLineInfo);
                 }
             }
             flag = reader2.Read();
         }
         targetWriter.WriteEndObject(readerLineInfo);
         targetWriter.WriteEndMember(readerLineInfo);
         this.ExitObject = true;
         reader2.Close();
     }
     else
     {
         this.FlushBuffer(targetWriter);
         targetWriter.WriteNode(reader, readerLineInfo);
     }
     return false;
 }
 public void FlushBuffer(XamlWriter targetWriter)
 {
     targetWriter.WriteStartMember(this.currentMember, this.currentMemberLineNumber, this.currentMemberLinePosition);
     XamlServices.Transform(this.bufferedNodes.Reader, targetWriter, false);
     this.bufferedNodes = null;
 }
            public void SetXClass(string builderName, XamlNodeQueue nameNodes)
            {
                this.xClassNodes = new XamlNodeQueue(Writer.SchemaContext);
                this.xClassNodes.Writer.WriteStartMember(XamlLanguage.Class);
                this.xClassNamespace = null;
                string xClassName = builderName;
                if (string.IsNullOrEmpty(xClassName))
                {
                    xClassName = string.Format(CultureInfo.CurrentCulture, "_{0}", Guid.NewGuid().ToString().Replace("-", string.Empty).Substring(0, 4));
                }

                if (nameNodes != null)
                {
                    XamlServices.Transform(nameNodes.Reader, this.xClassNodes.Writer, false);
                }
                else
                {
                    this.xClassNodes.Writer.WriteValue(xClassName);
                    this.xClassNodes.Writer.WriteEndMember();
                }

                int nameStartIndex = xClassName.LastIndexOf('.');
                if (nameStartIndex > 0)
                {
                    this.xClassNamespace = builderName.Substring(0, nameStartIndex);
                    xClassName = builderName.Substring(nameStartIndex + 1);
                }

                this.xClassNamespace = string.Format(CultureInfo.CurrentUICulture, "clr-namespace:{0}", this.xClassNamespace ?? string.Empty);
                this.xClassXamlType = new XamlType(this.xClassNamespace, xClassName, null, Writer.SchemaContext);
            }
 internal void SetUntransformedPropertyReferences(XamlMember propertyReferencesMember, XamlNodeQueue untransformedNodes)
 {
     ObjectFrame currentFrame = this.objectStack.Peek();
     Fx.Assert(currentFrame.Type != null, "Should only create PropertyReferencesNode inside a StartObject");
     currentFrame.AddMember(propertyReferencesMember, untransformedNodes);
 }
            // Properties [DependsOn("Attributes")]
            public void SetProperties(XamlNodeQueue propertyNodes, List<KeyValuePair<string, XamlNodeQueue>> defaultValueNodes)
            {
                this.xPropertiesNodes = propertyNodes;
                this.defaultValueNodes = defaultValueNodes;

                // exiting the properties tag. So we've now accrued any instances of Name and Attributes
                // that could possibly be hit flush our preamble
                FlushPreamble();
            }
 public XamlWriter StartMember(XamlMember member, XamlWriter parentWriter)
 {
     this.CurrentMember = member;
     this.parentWriter = parentWriter;
     this.currentMemberNodes = new XamlNodeQueue(parentWriter.SchemaContext);
     return this.currentMemberNodes.Writer;
 }
 public BuilderNameNode(BuilderClassNode classNode, ActivityBuilderXamlWriter writer)
     : base(writer)
 {
     this.classNode = classNode;
     this.nameNodes = new XamlNodeQueue(writer.SchemaContext);
     base.CurrentWriter = this.nameNodes.Writer;
 }
 public XamlWriter EndMember()
 {
     AddMember(this.CurrentMember, this.currentMemberNodes);
     this.CurrentMember = null;
     this.currentMemberNodes = null;
     XamlWriter parentWriter = this.parentWriter;
     this.parentWriter = null;
     return parentWriter;
 }
            public void AddDefaultValue(string propertyName, XamlNodeQueue value)
            {
                if (this.defaultValueNodes == null)
                {
                    this.defaultValueNodes = new List<KeyValuePair<string, XamlNodeQueue>>();
                }

                if (string.IsNullOrEmpty(propertyName))
                {
                    // default a name if one doesn't exist
                    propertyName = string.Format(CultureInfo.CurrentCulture, "_{0}", Guid.NewGuid().ToString().Replace("-", string.Empty));
                }

                this.defaultValueNodes.Add(new KeyValuePair<string, XamlNodeQueue>(propertyName, value));
            }
 public void AddMember(XamlMember member, XamlNodeQueue content)
 {
     if (this.Members == null)
     {
         this.Members = new List<KeyValuePair<XamlMember, XamlNodeQueue>>();
     }
     this.Members.Add(new KeyValuePair<XamlMember, XamlNodeQueue>(member, content));
 }
            protected internal override void Complete()
            {
                if (this.defaultValue != null)
                {
                    if (string.IsNullOrEmpty(this.propertyName))
                    {
                        // default a name if one doesn't exist
                        this.propertyName = string.Format(CultureInfo.CurrentCulture, "_{0}", Guid.NewGuid().ToString().Replace("-", string.Empty));
                    }

                    if (this.defaultValue != null && this.propertyType != null)
                    {
                        // post-process the default value nodes to strip out 
                        // StartObject+StartMember _Initialization+EndMember+EndObject 
                        // wrapper nodes if the type of the object matches the 
                        // property Type (since we are moving from "object Value" to "T Value"
                        this.defaultValue = StripTypeWrapping(this.defaultValue, this.propertyType);
                    }

                    this.properties.AddDefaultValue(this.propertyName, this.defaultValue);
                }
            }
 public PropertyReferencesNode(ActivityBuilderXamlWriter writer, XamlMember originalStartMember, ImplementationNode parent)
     : base(writer)
 {
     this.untransformedNodes = new XamlNodeQueue(Writer.SchemaContext);
     this.originalStartMember = originalStartMember;
     this.Parent = parent;
     base.CurrentWriter = this.untransformedNodes.Writer;
 }
Esempio n. 28
0
        private void Initialize(Stream stream, 
            Baml2006SchemaContext schemaContext, 
            Baml2006ReaderSettings settings)
        { 
            schemaContext.Settings = settings;
            _settings = settings;
            _context = new Baml2006ReaderContext(schemaContext);
            _xamlMainNodeQueue = new XamlNodeQueue(schemaContext); 
            _xamlNodesReader = _xamlMainNodeQueue.Reader;
            _xamlNodesWriter = _xamlMainNodeQueue.Writer; 
            _lookingForAKeyOnAMarkupExtensionInADictionaryDepth = -1; 

            _isBinaryProvider = !settings.ValuesMustBeString; 

            // Since the reader owns the stream and is responsible for its lifetime
            // it can safely hand out shared streams.
            if (_settings.OwnsStream) 
            {
                stream = new SharedStream(stream); 
            } 

            _binaryReader = new BamlBinaryReader(stream); 

            _context.TemplateStartDepth = -1;

            if (!_settings.IsBamlFragment) 
            {
                Process_Header(); 
            } 
        }
            public PropertyReferenceNode(ActivityBuilderXamlWriter writer, PropertyReferencesNode parent)
                : base(writer)
            {
                this.propertyReferenceNodes = new XamlNodeQueue(writer.SchemaContext);
                this.parent = parent;

                // save the untransformed output in case we're not able to perform the transformation
                base.CurrentWriter = this.propertyReferenceNodes.Writer;
            }
Esempio n. 30
0
 public XamlNodeQueueWriter(XamlNodeQueue source)
 {
     this.source = source;
 }
 public RootNode(ActivityBuilderXamlWriter writer)
     : base(writer)
 {
     this.pendingNodes = new XamlNodeQueue(writer.SchemaContext);
     base.CurrentWriter = this.pendingNodes.Writer;
 }
        public override void WriteStartObject(XamlType type)
        {
            switch (this.currentState)
            {
                case ActivityTemplateFactoryBuilderWriterStates.InitialState:
                    if (type.Equals(new XamlType(typeof(ActivityTemplateFactoryBuilder), this.schemaContext)))
                    {
                        this.queuedNodes = new XamlNodeQueue(this.schemaContext);
                        this.currentState = ActivityTemplateFactoryBuilderWriterStates.BufferingState;
                    }
                    else
                    {
                        this.currentState = ActivityTemplateFactoryBuilderWriterStates.BypassState;
                        this.underlyingWriter.WriteStartObject(type);
                    }

                    break;
                case ActivityTemplateFactoryBuilderWriterStates.BypassState:
                    this.underlyingWriter.WriteStartObject(type);
                    break;
                default:
                    SharedFx.Assert(
                        this.currentState == ActivityTemplateFactoryBuilderWriterStates.BufferingState
                        || this.currentState == ActivityTemplateFactoryBuilderWriterStates.BufferingNameState
                        || this.currentState == ActivityTemplateFactoryBuilderWriterStates.BufferingTargetTypeState,
                        "These are the only possible ActivityTemplateFactoryBuilderWriterStates.");
                    SharedFx.Assert("It is impossible to start any object during the buffering state.");
                    break;
            }
        }
            public void FlushPendingNodes(string classNamespace)
            {
                base.CurrentWriter = this.Writer.innerWriter;
                if (!Writer.notRewriting)
                {
                    // make sure we have any required namespaces
                    if (!this.wroteXamlNamespace)
                    {
                        string xamlNamespaceAlias = GenerateNamespacePrefix(PreferredXamlNamespaceAlias);
                        this.WriteNamespace(new NamespaceDeclaration(XamlLanguage.Xaml2006Namespace, xamlNamespaceAlias));
                    }

                    // If there's an x:Class="Foo.Bar", add a namespace declaration for Foo in the local assembly so we can 
                    // say stuff like this:Bar.MyProperty later on. DON'T add the namespace declaration if somebody has already 
                    // declared the namespace in the nodestream though (duplicates are an error).
                    if (classNamespace != null)
                    {
                        bool sawClassNamespace = false;

                        XamlReader reader = this.pendingNodes.Reader;
                        XamlWriter writer = this.Writer.innerWriter;
                        while (reader.Read() && reader.NodeType == XamlNodeType.NamespaceDeclaration)
                        {
                            if (classNamespace.Equals(reader.Namespace.Namespace))
                            {
                                sawClassNamespace = true;
                            }
                            writer.WriteNode(reader);
                        }

                        if (!sawClassNamespace)
                        {
                            string classNamespaceAlias = GenerateNamespacePrefix(PreferredClassAlias);
                            writer.WriteNamespace(new NamespaceDeclaration(classNamespace, classNamespaceAlias));
                        }

                        // We may have consumed the first non-namespace node off the reader in order 
                        // to check it for being a NamespaceDeclaration. Make sure it still gets written.
                        if (!reader.IsEof)
                        {
                            writer.WriteNode(reader);
                        }
                    }

                    this.rootLevelPrefixes = null; // not needed anymore
                }

                XamlServices.Transform(this.pendingNodes.Reader, this.Writer.innerWriter, false);
                this.pendingNodes = null;
            }