Exemplo n.º 1
0
 /// <summary>
 /// Creates a new element position.
 /// </summary>
 /// <param name="element">The current element.</param>
 /// <param name="elementTag">The element tag of the specified element.</param>
 /// <param name="elementPosition">The previous element position.</param>
 /// <returns>A new element position.</returns>
 private static NvgElementPosition CreateElementPosition(INvgElement element, NvgElementTag elementTag, NvgElementPosition elementPosition)
 {
     return(new NvgElementPosition
     {
         Element = element,
         ElementTag = elementTag,
         PreviousElementPosition = elementPosition
     });
 }
Exemplo n.º 2
0
        /// <summary>
        /// Reads the next NVG element from the stream.
        /// </summary>
        /// <returns>The next NVG element or <code>null</code> when there are no more NVG elements.</returns>
        public INvgElement ReadNextElement()
        {
            NvgElementPosition elementPosition = null;
            INvgElement        element         = null;

            while (_xmlTextReader.Read())
            {
                switch (_xmlTextReader.NodeType)
                {
                case XmlNodeType.Element:
                    var startTag = new NvgElementTag(_xmlTextReader.LocalName);
                    if (startTag.IsNvgTag)
                    {
                        // Read the NVG element
                        element = new NvgElement();
                        element.ConstructFromReader(_xmlTextReader);
                        elementPosition = CreateElementPosition(element, startTag, elementPosition);
                    }
                    else if (startTag.IsGroupTag)
                    {
                        // Read the NVG group element
                        element = new NvgGroupElement();
                        element.ConstructFromReader(_xmlTextReader);
                        elementPosition = CreateElementPosition(element, startTag, elementPosition);
                    }
                    else if (startTag.IsPointTag)
                    {
                        // Read the NVG point element
                        element = new NvgPointElement();
                        element.ConstructFromReader(_xmlTextReader);
                        elementPosition = CreateElementPosition(element, startTag, elementPosition);
                    }
                    break;

                case XmlNodeType.EndElement:
                    var endTag = new NvgElementTag(_xmlTextReader.LocalName);

                    // Add children and reset the position to the matching start tag
                    elementPosition = AddChildren(elementPosition, endTag);
                    if (null != elementPosition)
                    {
                        // Reset the current element to the start tag
                        element = elementPosition.Element;
                    }
                    if (endTag.IsNvgTag)
                    {
                        return(element);
                    }
                    break;
                }
            }
            return(null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Adds all children by iterating backwards using the specified element position.
        /// The first element matching the specified element tag is treated as the parent element.
        /// </summary>
        /// <param name="elementPosition">The current element position refering to the last created child element.</param>
        /// <param name="elementTag">The element tag of the parent element.</param>
        /// <returns>The element position of the parent element.</returns>
        private static NvgElementPosition AddChildren(NvgElementPosition elementPosition, NvgElementTag elementTag)
        {
            var childElements = new List <INvgElement>();

            for (; null != elementPosition && !elementPosition.ElementTag.IsEqualTo(elementTag); elementPosition = elementPosition.PreviousElementPosition)
            {
                childElements.Add(elementPosition.Element);
            }
            if (null == elementPosition)
            {
                // No start tag found!
                return(null);
            }

            // Add all child elements
            foreach (var childElement in childElements)
            {
                elementPosition.Element.Children.Add(childElement);
            }
            return(elementPosition);
        }