示例#1
0
        /// <summary>
        /// Opens an SVG document from the specified <see cref="Stream"/> and adds the specified entities.
        /// </summary>
        /// <param name="stream">The <see cref="Stream"/> containing the SVG document to open.</param>
        /// <param name="entities">Custom entity definitions.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="stream"/> parameter cannot be <c>null</c>.</exception>
        public static T Open <T>(Stream stream, Dictionary <string, string> entities) where T : SvgDocument, new()
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            // Don't close the stream via a dispose: that is the client's job.
            var reader = new SvgTextReader(stream, entities);

            reader.XmlResolver        = new SvgDtdResolver();
            reader.WhitespaceHandling = WhitespaceHandling.None;
            return(Open <T>(reader));
        }
示例#2
0
        /// <summary>
        /// Attempts to create an SVG document from the specified string data.
        /// </summary>
        /// <param name="svg">The SVG data.</param>
        public static T FromSvg <T>(string svg) where T : SvgDocument, new()
        {
            if (string.IsNullOrEmpty(svg))
            {
                throw new ArgumentNullException("svg");
            }

            using (var strReader = new System.IO.StringReader(svg)) {
                var reader = new SvgTextReader(strReader, null);
                reader.XmlResolver        = new SvgDtdResolver();
                reader.WhitespaceHandling = WhitespaceHandling.None;
                return(Open <T>(reader));
            }
        }
示例#3
0
        /// <summary>
        /// Opens an SVG document from the specified <see cref="Stream"/> and adds the specified entities.
        /// </summary>
        /// <param name="stream">The <see cref="Stream"/> containing the SVG document to open.</param>
        /// <param name="entities">Custom entity definitions.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="stream"/> parameter cannot be <c>null</c>.</exception>
        public static T Open <T>(Stream stream, Dictionary <string, string> entities) where T : SvgDocument, new()
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            // Don't close the stream via a dispose: that is the client's job.
            var reader = new SvgTextReader(stream, entities)
            {
                XmlResolver        = new SvgDtdResolver(),
                WhitespaceHandling = WhitespaceHandling.Significant,
                DtdProcessing      = SvgDocument.DisableDtdProcessing ? DtdProcessing.Ignore : DtdProcessing.Parse,
            };

            return(Open <T>(reader));
        }
示例#4
0
        /// <summary>
        /// Attempts to create an SVG document from the specified string data.
        /// </summary>
        /// <param name="svg">The SVG data.</param>
        public static T FromSvg <T>(string svg, bool ignoreStyles = false) where T : SvgDocument, new()
        {
            if (string.IsNullOrEmpty(svg))
            {
                throw new ArgumentNullException("svg");
            }

            using (var strReader = new System.IO.StringReader(svg))
            {
                var reader = new SvgTextReader(strReader, null)
                {
                    XmlResolver        = new SvgDtdResolver(),
                    WhitespaceHandling = WhitespaceHandling.None
                };
                return(Open <T>(reader, ignoreStyles: ignoreStyles));
            }
        }
示例#5
0
        /// <summary>
        /// Attempts to create an SVG document from the specified string data.
        /// </summary>
        /// <param name="svg">The SVG data.</param>
        public static T FromSvg <T>(string svg) where T : SvgDocument, new()
        {
            if (string.IsNullOrEmpty(svg))
            {
                throw new ArgumentNullException("svg");
            }

            using (var strReader = new System.IO.StringReader(svg))
            {
                var reader = new SvgTextReader(strReader, null)
                {
                    XmlResolver        = new SvgDtdResolver(),
                    WhitespaceHandling = WhitespaceHandling.Significant,
                    DtdProcessing      = SvgDocument.DisableDtdProcessing ? DtdProcessing.Ignore : DtdProcessing.Parse,
                };
                return(Open <T>(reader));
            }
        }
示例#6
0
        /// <summary>
        /// Opens an SVG document from the specified <see cref="Stream"/> and adds the specified entities.
        /// </summary>
        /// <param name="stream">The <see cref="Stream"/> containing the SVG document to open.</param>
        /// <param name="entities">Custom entity definitions.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="stream"/> parameter cannot be <c>null</c>.</exception>
        public static T Open <T>(Stream stream, Dictionary <string, string> entities) where T : SvgDocument, new()
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            //Trace.TraceInformation("Begin Read");

            using (var reader = new SvgTextReader(stream, entities))
            {
                var        elementStack = new Stack <SvgElement>();
                var        value        = new StringBuilder();
                bool       elementEmpty;
                SvgElement element = null;
                SvgElement parent;
                T          svgDocument = null;
                reader.XmlResolver        = new SvgDtdResolver();
                reader.WhitespaceHandling = WhitespaceHandling.None;

                while (reader.Read())
                {
                    try
                    {
                        switch (reader.NodeType)
                        {
                        case XmlNodeType.Element:
                            // Does this element have a value or children
                            // (Must do this check here before we progress to another node)
                            elementEmpty = reader.IsEmptyElement;
                            // Create element
                            if (elementStack.Count > 0)
                            {
                                element = SvgElementFactory.CreateElement(reader, svgDocument);
                            }
                            else
                            {
                                svgDocument = SvgElementFactory.CreateDocument <T>(reader);
                                element     = svgDocument;
                            }

                            if (element == null)
                            {
                                continue;
                            }

                            // Add to the parents children
                            if (elementStack.Count > 0)
                            {
                                parent = elementStack.Peek();
                                parent.Children.Add(element);
                            }

                            // Push element into stack
                            elementStack.Push(element);

                            // Need to process if the element is empty
                            if (elementEmpty)
                            {
                                goto case XmlNodeType.EndElement;
                            }

                            break;

                        case XmlNodeType.EndElement:
                            // Skip if no element was created and is not the closing tag for the last
                            // known element
                            if (element == null && reader.LocalName != elementStack.Peek().ElementName)
                            {
                                continue;
                            }
                            // Pop the element out of the stack
                            element = elementStack.Pop();

                            if (value.Length > 0)
                            {
                                element.Content = value.ToString();
                                // Reset content value for new element
                                value = new StringBuilder();
                            }
                            break;

                        case XmlNodeType.CDATA:
                        case XmlNodeType.Text:
                            value.Append(reader.Value);
                            break;
                        }
                    }
                    catch (Exception exc)
                    {
                        Trace.TraceError(exc.Message);
                    }
                }

                //Trace.TraceInformation("End Read");
                return(svgDocument);
            }
        }
示例#7
0
        /// <summary>
        /// Given the SVG/XML fragment return a fully populated SVG node.  The returned node is not added to the given document
        /// </summary>
        /// <param name="document">The document context to parse the in content in</param>
        /// <param name="fragment">The SVG/XML formatted string to parse</param>
        /// <param name="entities">Optional dictionary to resolve entities. May be null.</param>
        /// <returns></returns>
        public SvgElement[] ParseFragment(SvgDocument document, string fragment, Dictionary <string, string> entities)
        {
            NameTable           nt    = new NameTable();
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);

            nsmgr.AddNamespace("svg", svgNS);

            XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);

            using (var reader = new SvgTextReader(fragment, XmlNodeType.Element, context, entities))
            {
                var        elements     = new List <SvgElement>();
                var        elementStack = new Stack <SvgElement>();
                var        value        = new StringBuilder();
                bool       elementEmpty;
                SvgElement element = null;
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.Element:
                        // Does this element have a value or children
                        // (Must do this check here before we progress to another node)
                        elementEmpty = reader.IsEmptyElement;
                        // Create element
                        element = CreateElement(reader, document);

                        // Add to the parents children
                        if (elementStack.Count > 0)
                        {
                            var parent = elementStack.Peek();
                            if (parent != null && element != null)
                            {
                                parent.Children.Add(element);
                            }
                        }
                        else
                        {
                            elements.Add(element);
                        }

                        // Push element into stack
                        elementStack.Push(element);

                        // Need to process if the element is empty
                        if (elementEmpty)
                        {
                            goto case XmlNodeType.EndElement;
                        }

                        break;

                    case XmlNodeType.EndElement:

                        // Pop the element out of the stack
                        element = elementStack.Pop();

                        if (value.Length > 0 && element != null)
                        {
                            element.Content = value.ToString();
                            // Reset content value for new element
                            value.Clear();
                        }
                        break;

                    case XmlNodeType.CDATA:
                    case XmlNodeType.Text:
                        value.Append(reader.Value);
                        break;
                    }
                }
                return(elements.ToArray());
            }
        }