/// <summary> /// It creates a new element copying all attributes from eleToClone; the new /// element is inserted under the parent element provided. /// </summary> /// <param name="parent">Parent element. If null the element is added under the root.</param> /// <param name="eleToClone">Element to be cloned</param> /// <returns></returns> public SvgElement CloneElement(SvgElement parent, SvgElement eleToClone) { // calculate unique id string sOldId = eleToClone.GetAttributeStringValue(SvgAttribute._SvgAttribute.attrCore_Id); string sNewId = sOldId; if (sOldId != "") { int i = 1; // check if it is unique while (GetSvgElement(sNewId) != null) { sNewId = sOldId + "_" + i.ToString(); i++; } } // clone operation SvgElement eleNew = AddElement(parent, eleToClone.getElementName()); eleNew.CloneAttributeList(eleToClone); if (sNewId != "") { eleNew.SetAttributeValue(SvgAttribute._SvgAttribute.attrCore_Id, sNewId); } if (eleToClone.getChild() != null) { eleNew.setChild(CloneElement(eleNew, eleToClone.getChild())); if (eleToClone.getChild().getNext() != null) { eleNew.getChild().setNext(CloneElement(eleNew, eleToClone.getChild().getNext())); } } return(eleNew); }
/// <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); }