/// <summary> /// Pushes a new state into the stack (thus changing the current state to the new state). /// This method allows for both a new node and a new state to be supplied. /// </summary> /// <param name="newAncestor"> /// The new XmlNode representing the ancestor for all new nodes. /// </param> /// <param name="state"> /// The new state. /// </param> internal void Push(DynaXmlElement newAncestor, DynaXmlBuilderState state) { this.stack.Push(new DynaXmlContextState(this.Document, newAncestor, newAncestor, new DynaXmlNamespaceContext(this.CurrentNamespaceContext), state)); }
/// <summary> /// Initializes a new instance of the DynaXmlContextState class. /// </summary> /// <param name="document">The xml document.</param> /// <param name="currentAncestor">The current ancestor of all new nodes.</param> /// <param name="lastCreated">The last node that was created.</param> /// <param name="namespaceContext">The current namespace context.</param> /// <param name="state">The current state of the state machine.</param> public DynaXmlContextState(DynaXmlDocument document, DynaXmlElement currentAncestor, DynaXmlElement lastCreated, DynaXmlNamespaceContext namespaceContext, DynaXmlBuilderState state) { this.Document = document; this.State = state; this.CurentAncestorElement = currentAncestor; this.CurrentNamespaceContext = namespaceContext; this.LastCreated = lastCreated; }
/// <summary> /// Creates an Attribute as a component of the current element. /// </summary> /// <param name="name"> /// The name of the attribute. /// </param> /// <param name="value"> /// The value for the attribute. /// </param> /// <returns> /// Always true. /// </returns> private bool AttributeBuilderInvokeMember(string name, string value) { // Determine if we are at the root level and if we are trying to // create more than one node. if (this.CurrentAncestorNode == this.document && this.document.Items.Count != 0) { throw new InvalidOperationException("Attributes can not be added before a root element is defined."); } DynaXmlElement element = this.CurrentAncestorElement; element.Items.Add(this.CreateAttribute(name, value)); // If we are NOT in a AttributeListBuild, we pop the state // if we are in a list attribute build, we do not. if (this.State != DynaXmlBuilderState.AttributeListBuilder) { this.context.Pop(); } return(true); }
/// <summary> /// Adds an element with content to the tree. /// </summary> /// <param name="name"> /// The name of the element to add. /// </param> /// <param name="value"> /// The value to place as the text of the element. /// </param> /// <returns> /// Always true. /// </returns> private bool ElementBuilderInvokeMember(string name, string value) { // Determine if we are at the root level and if we are trying to // create more than one node. if (this.CurrentAncestorNode == this.document && this.document.Items.Count != 0) { throw new InvalidOperationException("An Xml Document may not have more than one root element."); } DynaXmlElement element = this.CreateElement(name); DynaXmlText text = new DynaXmlText() { Value = value }; element.Items.Add(text); this.CurrentAncestorNode.Items.Add(element); if (this.State == DynaXmlBuilderState.LiteralElementBuilder) { this.context.Pop(); } return(true); }
/// <summary> /// Creates a new element in the XmlDocument. /// </summary> /// <param name="name"> /// The name of the element. /// </param> /// <returns> /// The XmlElement that was created. /// </returns> private DynaXmlElement CreateElement(string name) { DynaXmlElement element = null; var currentAlias = this.CurrentNamespaceContext.CurrentAlias; if (currentAlias.IsNotNullOrEmpty()) { element = new DynaXmlElement() { Prefix = currentAlias, LocalName = name, XmlNamespace = this.CurrentNamespaceContext.AliasTable[currentAlias] }; // We've used the "CurrentAlias" so remove it as it's a (use once state); this.CurrentNamespaceContext.CurrentAlias = string.Empty; this.CurrentNamespaceContext.ApplyCurrentToAttributes = false; } else if (this.CurrentNamespaceContext.DefaultNamespace.IsNullOrEmpty()) { element = new DynaXmlElement() { LocalName = name }; } else { element = new DynaXmlElement() { LocalName = name, XmlNamespace = this.CurrentNamespaceContext.DefaultNamespace }; } if (this.CurrentAncestorElement.IsNull()) { element.Items.AddRange(this.rootNodeNamespaceDefinitions); } return(element); }
/// <summary> /// Pushes a new state into the stack (thus changing the current state to the new state). /// </summary> /// <param name="element"> /// The XmlELement representing the current state. /// </param> public void PushElement(DynaXmlElement element) { this.CurrentAncestorNode.Items.Add(element); this.Push(element, DynaXmlBuilderState.ElementListBuilder); }