コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <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);
        }