/// <summary> /// It saves the current SVG document to a file. /// </summary> /// <param name="sFilename">The complete path of the file.</param> /// <returns> /// true if the file is saved successfully, false otherwise /// </returns> public bool SaveToFile(string sFilename) { ErrH err = new ErrH("SvgDoc", "SaveToFile"); err.LogParameter("sFilename", sFilename); bool bResult = false; StreamWriter sw = null; try { sw = File.CreateText(sFilename); bResult = true; } catch (UnauthorizedAccessException uae) { err.LogException(uae); } catch (DirectoryNotFoundException dnfe) { err.LogException(dnfe); } catch (ArgumentException ae) { err.LogException(ae); } catch { err.LogUnhandledException(); } if (!bResult) { err.LogEnd(false); return(false); } try { sw.Write(GetXML()); sw.Close(); } catch { err.LogUnhandledException(); err.LogEnd(false); return(false); } err.LogEnd(true); return(true); }
/// <summary> /// It moves the element one level up in the tree hierarchy. /// </summary> /// <param name="ele">Element to be moved.</param> /// <returns> /// true if the operation succeeded. /// </returns> public bool ElementLevelUp(SvgElement ele) { ErrH err = new ErrH("SvgDoc", "ElementLevelUp"); err.Log("Element to move " + ele.ElementInfo(), ErrH._LogPriority.Info); SvgElement parent = ele.getParent(); if (parent == null) { err.Log("Root node cannot be moved", ErrH._LogPriority.Info); err.LogEnd(false); return(false); } if (parent.getParent() == null) { err.Log("An element cannot be moved up to the root", ErrH._LogPriority.Info); err.LogEnd(false); return(false); } SvgElement nxt = ele.getNext(); // the first child of the parent became the next parent.setChild(nxt); if (nxt != null) { nxt.setPrevious(null); } // get the last sibling of the parent SvgElement last = GetLastSibling(parent); if (last != null) { last.setNext(ele); } ele.setParent(parent.getParent()); ele.setPrevious(last); ele.setNext(null); return(true); }
/// <summary> /// It adds the new element eleToAdd as the last children of the given parent element. /// </summary> /// <param name="parent">Parent element. If null the element is added under the root.</param> /// <param name="eleToAdd">Element to be added.</param> public void AddElement(SvgElement parent, SvgElement eleToAdd, ref SvgElement last) { ErrH err = new ErrH("SvgDoc", "AddElement"); if (eleToAdd == null || m_root == null) { err.LogEnd(false); return; } SvgElement parentToAdd = m_root; if (parent != null) { parentToAdd = parent; } eleToAdd.setInternalId(m_nNextId++); m_elements.Add(eleToAdd.getInternalId(), eleToAdd); eleToAdd.setParent(parentToAdd); if (parentToAdd.getChild() == null) { // the element is the first child parentToAdd.setChild(eleToAdd); last = eleToAdd; } else { // add the element as the last sibling //SvgElement last = GetLastSibling(parentToAdd.getChild()); if (last != null) { last.setNext(eleToAdd); eleToAdd.setPrevious(last); last = eleToAdd; } else { last = parentToAdd.getChild(); } } err.Log(eleToAdd.ElementInfo(), ErrH._LogPriority.Info); err.LogEnd(true); }
/// <summary> /// It moves the element after its current next sibling. /// </summary> /// <param name="ele">Element to be moved.</param> /// <returns> /// true if the operation succeeded. /// </returns> public bool ElementPositionDown(SvgElement ele) { ErrH err = new ErrH("SvgDoc", "ElementPositionDown"); err.Log("Element to move " + ele.ElementInfo(), ErrH._LogPriority.Info); SvgElement parent = ele.getParent(); if (parent == null) { err.Log("Root node cannot be moved", ErrH._LogPriority.Info); err.LogEnd(false); return(false); } if (IsLastSibling(ele)) { err.Log("Element is already at the last sibling position", ErrH._LogPriority.Info); err.LogEnd(false); return(false); } SvgElement nxt = ele.getNext(); SvgElement nxt2 = null; SvgElement prv = ele.getPrevious(); // fix Next if (nxt != null) { nxt.setPrevious(ele.getPrevious()); nxt2 = nxt.getNext(); nxt.setNext(ele); } // fix Previous if (prv != null) { prv.setNext(nxt); } // fix Element if (IsFirstChild(ele)) { parent.setChild(nxt); } ele.setPrevious(nxt); ele.setNext(nxt2); if (nxt2 != null) { nxt2.setPrevious(ele); } err.Log("Element moved " + ele.ElementInfo(), ErrH._LogPriority.Info); err.LogEnd(true); return(true); }
/// <summary> /// It creates a SVG document reading from a file. /// If a current document exists, it is destroyed. /// </summary> /// <param name="sFilename">The complete path of a valid SVG file.</param> /// <returns> /// true if the file is loaded successfully and it is a valid SVG document, false if the file cannot be open or if it is not /// a valid SVG document. /// </returns> public bool LoadFromFile(string sFilename) { ErrH err = new ErrH("SvgDoc", "LoadFromFile"); err.LogParameter("sFilename", sFilename); if (m_root != null) { m_root = null; m_nNextId = 1; m_elements.Clear(); } bool bResult = true; try { XmlTextReader reader; reader = new XmlTextReader(sFilename); reader.WhitespaceHandling = WhitespaceHandling.None; reader.Normalization = false; reader.XmlResolver = null; reader.Namespaces = false; string tmp; SvgElement eleParent = null; try { // parse the file and display each of the nodes. while (reader.Read() && bResult) { switch (reader.NodeType) { case XmlNodeType.Attribute: tmp = reader.Name; tmp = reader.Value; break; case XmlNodeType.Element: { SvgElement ele = AddElement(eleParent, reader.Name); if (ele == null) { err.Log("Svg element cannot be added. Name: " + reader.Name, ErrH._LogPriority.Warning); bResult = false; } else { eleParent = ele; if (reader.IsEmptyElement) { if (eleParent != null) { eleParent = eleParent.getParent(); } } bool bLoop = reader.MoveToFirstAttribute(); while (bLoop) { ele.SetAttributeValue(reader.Name, reader.Value); bLoop = reader.MoveToNextAttribute(); } } } break; case XmlNodeType.Text: if (eleParent != null) { eleParent.setElementValue(reader.Value); } break; case XmlNodeType.CDATA: err.Log("Unexpected item: " + reader.Value, ErrH._LogPriority.Warning); break; case XmlNodeType.ProcessingInstruction: err.Log("Unexpected item: " + reader.Value, ErrH._LogPriority.Warning); break; case XmlNodeType.Comment: err.Log("Unexpected item: " + reader.Value, ErrH._LogPriority.Warning); break; case XmlNodeType.XmlDeclaration: m_sXmlDeclaration = "<?xml " + reader.Value + "?>"; break; case XmlNodeType.Document: err.Log("Unexpected item: " + reader.Value, ErrH._LogPriority.Warning); break; case XmlNodeType.DocumentType: { string sDTD1; string sDTD2; sDTD1 = reader.GetAttribute("PUBLIC"); sDTD2 = reader.GetAttribute("SYSTEM"); m_sXmlDocType = "<!DOCTYPE svg PUBLIC \"" + sDTD1 + "\" \"" + sDTD2 + "\">"; } break; case XmlNodeType.EntityReference: err.Log("Unexpected item: " + reader.Value, ErrH._LogPriority.Warning); break; case XmlNodeType.EndElement: if (eleParent != null) { eleParent = eleParent.getParent(); } break; } // switch } // while } // read try catch (XmlException xmle) { err.LogException(xmle); err.LogParameter("Line Number", xmle.LineNumber.ToString()); err.LogParameter("Line Position", xmle.LinePosition.ToString()); bResult = false; } catch (Exception e) { err.LogException(e); bResult = false; } finally { reader.Close(); } } catch { err.LogUnhandledException(); bResult = false; } err.LogEnd(bResult); return(bResult); }
/// <summary> /// It moves the element before its current previous sibling. /// </summary> /// <param name="ele">Element to be moved.</param> /// <returns> /// true if the operation succeeded. /// </returns> public bool ElementPositionUp(SvgElement ele) { ErrH err = new ErrH("SvgDoc", "ElementPositionUp"); err.Log("Element to move " + ele.ElementInfo(), ErrH._LogPriority.Info); SvgElement parent = ele.getParent(); if (parent == null) { err.Log("Root node cannot be moved", ErrH._LogPriority.Info); err.LogEnd(false); return(false); } if (IsFirstChild(ele)) { err.Log("Element is already at the first position", ErrH._LogPriority.Info); err.LogEnd(false); return(false); } SvgElement nxt = ele.getNext(); SvgElement prv = ele.getPrevious(); SvgElement prv2 = null; ele.setNext(null); ele.setPrevious(null); // fix Next if (nxt != null) { nxt.setPrevious(prv); } // fix Previous if (prv != null) { prv.setNext(nxt); prv2 = prv.getPrevious(); prv.setPrevious(ele); // check if the Previous is the first child if (IsFirstChild(prv)) { // if yes the moved element has to became the new first child if (prv.getParent() != null) { prv.getParent().setChild(ele); } } } // fix Previous/Previous if (prv2 != null) { prv2.setNext(ele); } // fix Element ele.setNext(prv); ele.setPrevious(prv2); err.Log("Element moved " + ele.ElementInfo(), ErrH._LogPriority.Info); err.LogEnd(true); return(true); }
// ---------- PUBLIC METHODS END // ---------- PRIVATE METHODS private bool DeleteElement(SvgElement ele, bool bDeleteFromParent) { ErrH err = new ErrH("SvgDoc", "DeleteElement"); if (ele == null) { err.LogEnd(false); return(false); } SvgElement parent = ele.getParent(); if (parent == null) { // root node cannot be delete! err.Log("root node cannot be delete!", ErrH._LogPriority.Info); err.LogEnd(false); return(false); } // set the Next reference of the previous if (ele.getPrevious() != null) { ele.getPrevious().setNext(ele.getNext()); } // set the Previous reference of the next if (ele.getNext() != null) { ele.getNext().setPrevious(ele.getPrevious()); } // check if the element is the first child // the bDeleteFromParent flag is used to avoid deleting // all parent-child relationship. This is used in the Cut // operation where the subtree can be pasted if (bDeleteFromParent) { if (IsFirstChild(ele)) { // set the Child reference of the parent to the next ele.getParent().setChild(ele.getNext()); } } // delete its children SvgElement child = ele.getChild(); while (child != null) { DeleteElement(child, false); child = child.getNext(); } // delete the element from the colloection m_elements.Remove(ele.getInternalId()); err.Log(ele.ElementInfo(), ErrH._LogPriority.Info); err.LogEnd(true); return(true); }