Beispiel #1
0
        private bool DeleteElement(SvgElement element, bool deleteFromParent)
        {
            if (element == null)
            {
                return(false);
            }

            var parent = element.GetParent();

            if (parent == null)
            {
                // root node cannot be delete!
                ErrorMessage = "root node cannot be delete!";
                return(false);
            }

            // set the Next reference of the previous
            if (element.GetPrevious() != null)
            {
                element.GetPrevious().SetNext(element.GetNext());
            }

            // set the Previous reference of the next
            if (element.GetNext() != null)
            {
                element.GetNext().SetPrevious(element.GetPrevious());
            }

            // check if the element is the first child
            // the deleteFromParent flag is used to avoid deleting
            // all parent-child relationship. This is used in the Cut
            // operation where the subtree can be pasted
            if (deleteFromParent)
            {
                if (IsFirstChild(element))
                {
                    // set the Child reference of the parent to the next
                    element.GetParent().SetChild(element.GetNext());
                }
            }

            // delete its children
            var child = element.GetChild();

            while (child != null)
            {
                DeleteElement(child, false);
                child = child.GetNext();
            }

            // delete the element from the colloection
            svgDocumentElements.Remove(element.GetInternalId());

            return(true);
        }
Beispiel #2
0
        private bool IsFirstChild(SvgElement element)
        {
            if (element.GetParent() == null)
            {
                return(false);
            }

            if (element.GetParent().GetChild() == null)
            {
                return(false);
            }

            return(element.GetInternalId() == element.GetParent().GetChild().GetInternalId());
        }
Beispiel #3
0
        /// <summary>
        /// It moves the element before its current previous sibling.
        /// </summary>
        /// <param name="element">Element to be moved.</param>
        /// <returns>
        /// true if the operation succeeded.
        /// </returns>
        public bool ElementPositionUp(SvgElement element)
        {
            var parent = element.GetParent();

            if (parent == null)
            {
                ErrorMessage = "Root node cannot be moved";
                return(false);
            }

            if (IsFirstChild(element))
            {
                ErrorMessage = "Element is already at the first position";
                return(false);
            }

            var        next      = element.GetNext();
            var        previous  = element.GetPrevious();
            SvgElement previous2 = null;

            element.SetNext(null);
            element.SetPrevious(null);

            // fix Next
            if (next != null)
            {
                next.SetPrevious(previous);
            }

            // fix Previous
            if (previous != null)
            {
                previous.SetNext(next);
                previous2 = previous.GetPrevious();
                previous.SetPrevious(element);

                // check if the Previous is the first child
                if (IsFirstChild(previous))
                {
                    // if yes the moved element has to became the new first child
                    if (previous.GetParent() != null)
                    {
                        previous.GetParent().SetChild(element);
                    }
                }
            }

            // fix Previous/Previous
            if (previous2 != null)
            {
                previous2.SetNext(element);
            }

            // fix Element
            element.SetNext(previous);
            element.SetPrevious(previous2);

            return(true);
        }
Beispiel #4
0
        /// <summary>
        /// It moves the element after its current next sibling.
        /// </summary>
        /// <param name="element">Element to be moved.</param>
        /// <returns>
        /// true if the operation succeeded.
        /// </returns>
        public bool ElementPositionDown(SvgElement element)
        {
            var parent = element.GetParent();

            if (parent == null)
            {
                ErrorMessage = "Root node cannot be moved";
                return(false);
            }

            if (IsLastSibling(element))
            {
                ErrorMessage = "Element is already at the last sibling position";
                return(false);
            }

            var        next     = element.GetNext();
            SvgElement next2    = null;
            var        previous = element.GetPrevious();

            // fix Next
            if (next != null)
            {
                next.SetPrevious(element.GetPrevious());
                next2 = next.GetNext();
                next.SetNext(element);
            }

            // fix Previous
            if (previous != null)
            {
                previous.SetNext(next);
            }

            // fix Element
            if (IsFirstChild(element))
            {
                parent.SetChild(next);
            }

            element.SetPrevious(next);
            element.SetNext(next2);

            if (next2 != null)
            {
                next2.SetPrevious(element);
            }

            return(true);
        }
Beispiel #5
0
        /// <summary>
        /// It moves the element one level up in the tree hierarchy.
        /// </summary>
        /// <param name="element">Element to be moved.</param>
        /// <returns>
        /// true if the operation succeeded.
        /// </returns>
        public bool ElementLevelUp(SvgElement element)
        {
            var parent = element.GetParent();

            if (parent == null)
            {
                ErrorMessage = "Root node cannot be moved";
                return(false);
            }

            if (parent.GetParent() == null)
            {
                ErrorMessage = "An element cannot be moved up to the root";
                return(false);
            }

            var next = element.GetNext();

            // the first child of the parent became the next
            parent.SetChild(next);

            if (next != null)
            {
                next.SetPrevious(null);
            }

            // get the last sibling of the parent
            var last = GetLastSibling(parent);

            if (last != null)
            {
                last.SetNext(element);
            }

            element.SetParent(parent.GetParent());
            element.SetPrevious(last);
            element.SetNext(null);

            return(true);
        }
Beispiel #6
0
        private bool IsFirstChild(SvgElement element)
        {
            if ( element.GetParent() == null )
            {
                return false;
            }

            if ( element.GetParent().GetChild() == null )
            {
                return false;
            }

            return (element.GetInternalId() == element.GetParent().GetChild().GetInternalId());
        }
Beispiel #7
0
        private bool DeleteElement(SvgElement element, bool deleteFromParent)
        {
            if ( element == null )
            {
                return false;
            }

            var parent = element.GetParent();
            if ( parent == null )
            {
                // root node cannot be delete!
                ErrorMessage = "root node cannot be delete!";
                return false;
            }

            // set the Next reference of the previous
            if ( element.GetPrevious() != null )
            {
                element.GetPrevious().SetNext(element.GetNext());
            }

            // set the Previous reference of the next
            if ( element.GetNext() != null )
            {
                element.GetNext().SetPrevious(element.GetPrevious());
            }

            // check if the element is the first child
            // the deleteFromParent flag is used to avoid deleting
            // all parent-child relationship. This is used in the Cut
            // operation where the subtree can be pasted
            if ( deleteFromParent )
            {
                if ( IsFirstChild(element) )
                {
                    // set the Child reference of the parent to the next
                    element.GetParent().SetChild(element.GetNext());
                }
            }

            // delete its children
            var child = element.GetChild();

            while ( child != null )
            {
                DeleteElement(child, false);
                child = child.GetNext();
            }

            // delete the element from the colloection
            svgDocumentElements.Remove(element.GetInternalId());

            return true;
        }
Beispiel #8
0
        /// <summary>
        /// It moves the element before its current previous sibling.
        /// </summary>
        /// <param name="element">Element to be moved.</param>
        /// <returns>
        /// true if the operation succeeded.
        /// </returns>
        public bool ElementPositionUp(SvgElement element)
        {
            var parent = element.GetParent();
            if ( parent == null )
            {
                ErrorMessage = "Root node cannot be moved";
                return false;
            }

            if ( IsFirstChild(element) )
            {
                ErrorMessage = "Element is already at the first position";
                return false;
            }

            var next = element.GetNext();
            var previous = element.GetPrevious();
            SvgElement previous2 = null;

            element.SetNext(null);
            element.SetPrevious(null);

            // fix Next
            if ( next != null )
            {
                next.SetPrevious(previous);
            }

            // fix Previous
            if ( previous != null )
            {
                previous.SetNext(next);
                previous2 = previous.GetPrevious();
                previous.SetPrevious(element);

                // check if the Previous is the first child
                if ( IsFirstChild(previous) )
                {
                    // if yes the moved element has to became the new first child
                    if ( previous.GetParent() != null )
                    {
                        previous.GetParent().SetChild(element);
                    }
                }
            }

            // fix Previous/Previous
            if ( previous2 != null )
            {
                previous2.SetNext(element);
            }

            // fix Element
            element.SetNext(previous);
            element.SetPrevious(previous2);

            return true;
        }
Beispiel #9
0
        /// <summary>
        /// It moves the element after its current next sibling.
        /// </summary>
        /// <param name="element">Element to be moved.</param>
        /// <returns>
        /// true if the operation succeeded.
        /// </returns>
        public bool ElementPositionDown(SvgElement element)
        {
            var parent = element.GetParent();
            if ( parent == null )
            {
                ErrorMessage = "Root node cannot be moved";
                return false;
            }

            if ( IsLastSibling(element) )
            {
                ErrorMessage = "Element is already at the last sibling position";
                return false;
            }

            var next = element.GetNext();
            SvgElement next2 = null;
            var previous = element.GetPrevious();

            // fix Next
            if ( next != null )
            {
                next.SetPrevious(element.GetPrevious());
                next2 = next.GetNext();
                next.SetNext(element);
            }

            // fix Previous
            if ( previous != null )
            {
                previous.SetNext(next);
            }

            // fix Element
            if ( IsFirstChild(element) )
            {
                parent.SetChild(next);
            }

            element.SetPrevious(next);
            element.SetNext(next2);

            if ( next2 != null )
            {
                next2.SetPrevious(element);
            }

            return true;
        }
Beispiel #10
0
        /// <summary>
        /// It moves the element one level up in the tree hierarchy.
        /// </summary>
        /// <param name="element">Element to be moved.</param>
        /// <returns>
        /// true if the operation succeeded.
        /// </returns>
        public bool ElementLevelUp(SvgElement element)
        {
            var parent = element.GetParent();
            if ( parent == null )
            {
                ErrorMessage = "Root node cannot be moved";
                return false;
            }

            if ( parent.GetParent() == null )
            {
                ErrorMessage = "An element cannot be moved up to the root";
                return false;
            }

            var next = element.GetNext();

            // the first child of the parent became the next
            parent.SetChild(next);

            if ( next != null )
            {
                next.SetPrevious(null);
            }

            // get the last sibling of the parent
            var last = GetLastSibling(parent);
            if ( last != null )
            {
                last.SetNext(element);
            }

            element.SetParent(parent.GetParent());
            element.SetPrevious(last);
            element.SetNext(null);

            return true;
        }
Beispiel #11
0
        /// <summary>
        /// Load SVG document from a file.
        /// </summary>
        /// <param name="filename">The complete path of a valid SVG file.</param>
        /// <returns>
        ///     True - the file is loaded successfully and it is a valid SVG document
        ///     False - the file cannot be opened or it is not a valid SVG document.
        /// </returns>
        public bool LoadFromFile(string filename)
        {
            if (svgRoot != null)
            {
                svgRoot = null;
                svgDocumentNextInternalId = 1;
                svgDocumentElements.Clear();
            }

            var result = true;

            try
            {
                var reader = new XmlTextReader(filename)
                {
                    WhitespaceHandling = WhitespaceHandling.None,
                    Normalization      = false,
                    XmlResolver        = null,
                    Namespaces         = false
                };

                SvgElement parentElement = null;

                try
                {
                    // parse the file and display each of the nodes.
                    while (reader.Read() && result)
                    {
                        switch (reader.NodeType)
                        {
                        case XmlNodeType.Attribute:
                            break;

                        case XmlNodeType.Element:
                            var element = AddElement(parentElement, reader.Name);

                            if (element != null)
                            {
                                parentElement = element;

                                if (reader.IsEmptyElement)
                                {
                                    if (parentElement != null)
                                    {
                                        parentElement = parentElement.GetParent();
                                    }
                                }

                                var attribute = reader.MoveToFirstAttribute();
                                while (attribute)
                                {
                                    element.SetAttributeValue(reader.Name, reader.Value);

                                    attribute = reader.MoveToNextAttribute();
                                }
                            }

                            break;

                        case XmlNodeType.Text:
                            if (parentElement != null)
                            {
                                parentElement.SetElementValue(reader.Value);
                            }
                            break;

                        case XmlNodeType.CDATA:
                            break;

                        case XmlNodeType.ProcessingInstruction:
                            break;

                        case XmlNodeType.Comment:
                            break;

                        case XmlNodeType.XmlDeclaration:
                            svgDocumentXmlDeclaration = "<?xml " + reader.Value + "?>";
                            break;

                        case XmlNodeType.Document:
                            break;

                        case XmlNodeType.DocumentType:
                        {
                            var sDtd1 = reader.GetAttribute("PUBLIC");
                            var sDtd2 = reader.GetAttribute("SYSTEM");

                            svgDocumentXmlDocumentType = "<!DOCTYPE svg PUBLIC \"" + sDtd1 + "\" \"" + sDtd2 + "\">";
                        }
                        break;

                        case XmlNodeType.EntityReference:
                            break;

                        case XmlNodeType.EndElement:
                            if (parentElement != null)
                            {
                                parentElement = parentElement.GetParent();
                            }
                            break;
                        }         // switch
                    }             // while
                }                 // read try
                catch (XmlException xmle)
                {
                    ErrorMessage =
                        $"{xmle.Message}\r\nLine Number: {xmle.LineNumber.ToString()}\r\nLine Position: {xmle.LinePosition.ToString()}";

                    result = false;
                }
                catch (Exception e)
                {
                    ErrorMessage = e.Message;
                    result       = false;
                }
                finally
                {
                    reader.Close();
                }
            }
            catch
            {
                ErrorMessage = "Unhandled Exception";
                result       = false;
            }

            return(result);
        }