/// <summary> /// It creates a new empty SVG document that contains just the root element. /// If a current document exists, it is destroyed. /// </summary> /// <returns> /// The root element of the SVG document. /// </returns> public SvgRoot CreateNewDocument() { if (m_root != null) { m_root = null; m_nNextId = 1; m_elements.Clear(); } m_root = new SvgRoot(this); m_root.setInternalId(m_nNextId++); m_elements.Add(m_root.getInternalId(), m_root); m_sXmlDeclaration = "<?xml version=\"1.0\"?>"; m_sXmlDocType = "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">"; m_root.SetAttributeValue(SvgAttribute._SvgAttribute.attrSvg_XmlNs, "http://www.w3.org/2000/svg"); m_root.SetAttributeValue(SvgAttribute._SvgAttribute.attrSvg_Version, "1.1"); return(m_root); }
/// <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); }
// ---------- PRIVATE PROPERTIES END // ---------- PUBLIC METHODS /// <summary> /// Constructor. /// </summary> public SvgDoc() { m_root = null; m_nNextId = 1; m_elements = new Hashtable(); }
/// <summary> /// It creates a new element according to the element name provided /// and it adds the new element 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="sName">SVG element name.</param> /// <returns>The new created element.</returns> public SvgElement AddElement(SvgElement parent, string sName) { SvgElement eleToReturn = null; if (sName == "svg") { m_root = new SvgRoot(this); m_root.setInternalId(m_nNextId++); m_elements.Add(m_root.getInternalId(), m_root); eleToReturn = m_root; } else if (sName == "desc") { eleToReturn = AddDesc(parent); } else if (sName == "text") { eleToReturn = AddText(parent); } else if (sName == "g") { eleToReturn = AddGroup(parent); } else if (sName == "rect") { eleToReturn = AddRect(parent); } else if (sName == "circle") { eleToReturn = AddCircle(parent); } else if (sName == "ellipse") { eleToReturn = AddEllipse(parent); } else if (sName == "line") { eleToReturn = AddLine(parent); } else if (sName == "path") { eleToReturn = AddPath(parent); } else if (sName == "polygon") { eleToReturn = AddPolygon(parent); } else if (sName == "image") { eleToReturn = AddImage(parent); } else { if (parent != null) { eleToReturn = AddUnsupported(parent, sName); } } return(eleToReturn); }