示例#1
0
        private void TraverseNode(HtmlNode htmlNode)
        {
            if (htmlNode == null || htmlNode.NodeType == HtmlNodeType.Comment)
            {
                return;
            }

            var attributes = new Attributes();

            if (htmlNode.HasAttributes)
            {
                foreach (HtmlAttribute attribute in htmlNode.Attributes)
                {
                    attributes.AddAttribute(null, htmlNode.Name, attribute.Name, null, attribute.Value);
                }
            }

            ContentHandler.StartElement(null, htmlNode.Name, htmlNode.Name, attributes);
            if (htmlNode.NodeType == HtmlNodeType.Text)
            {
                ContentHandler.Characters(htmlNode.InnerText.ToCharArray(), 0, htmlNode.InnerText.Length);
            }
            else if (htmlNode.HasChildNodes)
            {
                foreach (HtmlNode childNode in htmlNode.ChildNodes)
                {
                    TraverseNode(childNode);
                }
            }
            ContentHandler.EndElement(null, htmlNode.Name, htmlNode.Name);
        }
示例#2
0
        /// <exception cref="Org.Xml.Sax.SAXException"/>
        public virtual void AddString(string str)
        {
            int slen = str.Length;

            char[] arr = new char[slen];
            Sharpen.Runtime.GetCharsForString(str, 0, slen, arr, 0);
            contentHandler.Characters(arr, 0, slen);
        }
示例#3
0
 /// <summary>Add a SAX tag with a string inside.</summary>
 /// <param name="contentHandler">the SAX content handler</param>
 /// <param name="tag">the element tag to use</param>
 /// <param name="val">the string to put inside the tag</param>
 /// <exception cref="Org.Xml.Sax.SAXException"/>
 public static void AddSaxString(ContentHandler contentHandler, string tag, string
                                 val)
 {
     contentHandler.StartElement(string.Empty, string.Empty, tag, new AttributesImpl()
                                 );
     char[] c = MangleXmlString(val, false).ToCharArray();
     contentHandler.Characters(c, 0, c.Length);
     contentHandler.EndElement(string.Empty, string.Empty, tag);
 }
示例#4
0
        public void Parse(StreamReader input, ContentHandler handler)
        {
            var buffer = new StringBuilder();

            var isInsideTag = false;
            var isStartTag  = true;

            var lastChar = -1;
            int c;

            while ((c = input.Read()) != -1)
            {
                if ('<' == c)
                {
                    if (isInsideTag)
                    {
                        throw new InvalidFormatException("Did not expect < char!");
                    }

                    if (buffer.ToString().Trim().Length > 0)
                    {
                        handler.Characters(buffer.ToString().Trim());
                    }

                    buffer = new StringBuilder();

                    isInsideTag = true;
                    isStartTag  = true;
                }

                // buffer.AppendCodePoint(c); <-- java
                buffer.Append(char.ConvertFromUtf32(c));


                if ('/' == c && lastChar == '<')
                {
                    isStartTag = false;
                }

                if ('>' == c)
                {
                    if (!isInsideTag)
                    {
                        throw new InvalidFormatException("Did not expect > char!");
                    }

                    if (isStartTag)
                    {
                        handler.StartElement(extractTagName(buffer), getAttributes(buffer));
                    }
                    else
                    {
                        handler.EndElement(extractTagName(buffer));
                    }

                    buffer = new StringBuilder();

                    isInsideTag = false;
                }

                lastChar = c;
            }

            if (isInsideTag)
            {
                throw new InvalidFormatException("Did not find matching > char!");
            }
        }
示例#5
0
        public void Parse(StreamReader input, ContentHandler handler) {
            var buffer = new StringBuilder();

            var isInsideTag = false;
            var isStartTag = true;

            var lastChar = -1;
            int c;
            while ((c = input.Read()) != -1) {
                if ('<' == c) {
                    if (isInsideTag) {
                        throw new InvalidFormatException("Did not expect < char!");
                    }

                    if (buffer.ToString().Trim().Length > 0) {
                        handler.Characters(buffer.ToString().Trim());
                    }

                    buffer = new StringBuilder();

                    isInsideTag = true;
                    isStartTag = true;
                }

                // buffer.AppendCodePoint(c); <-- java
                buffer.Append(char.ConvertFromUtf32(c));


                if ('/' == c && lastChar == '<') {
                    isStartTag = false;
                }

                if ('>' == c) {
                    if (!isInsideTag) {
                        throw new InvalidFormatException("Did not expect > char!");
                    }

                    if (isStartTag) {
                        handler.StartElement(extractTagName(buffer), getAttributes(buffer));
                    } else {
                        handler.EndElement(extractTagName(buffer));
                    }

                    buffer = new StringBuilder();

                    isInsideTag = false;
                }

                lastChar = c;
            }

            if (isInsideTag) {
                throw new InvalidFormatException("Did not find matching > char!");
            }
        }
示例#6
0
        private void TraverseNode(HtmlNode htmlNode)
        {
            if (htmlNode.NodeType == HtmlNodeType.Element)
            {
                var attributes = new Attributes();
                if (htmlNode.HasAttributes)
                {
                    foreach (HtmlAttribute attribute in htmlNode.Attributes)
                    {
                        attributes.AddAttribute(null, htmlNode.Name, attribute.Name, null, attribute.Value);
                    }
                }

                //Debug.WriteLine($"START: {htmlNode.Name}");

                ContentHandler.StartElement(null, htmlNode.Name, htmlNode.Name, attributes);

                if (htmlNode.HasChildNodes)
                {
                    foreach (HtmlNode childNode in htmlNode.ChildNodes)
                    {
                        if (childNode.NodeType == HtmlNodeType.Text)
                        {
                            //Debug.WriteLine(childNode.InnerText);
                            ContentHandler.Characters(childNode.InnerText.ToCharArray(), 0, childNode.InnerText.Length);
                        }

                        TraverseNode(childNode);
                    }
                }


                //Debug.WriteLine($"ENDE: {htmlNode.Name}");

                ContentHandler.EndElement(null, htmlNode.Name, htmlNode.Name);
            }
            else
            {
                if (htmlNode.HasChildNodes)
                {
                    foreach (HtmlNode childNode in htmlNode.ChildNodes)
                    {
                        TraverseNode(childNode);
                    }
                }
            }

            //if (!(htmlNode.NodeType == HtmlNodeType.Comment ||
            //    htmlNode.NodeType == HtmlNodeType.Text ||
            //    htmlNode.NodeType == HtmlNodeType.))
            //{

            //    ContentHandler.StartElement(null, htmlNode.Name, htmlNode.Name, null);

            //    if (htmlNode.HasChildNodes)
            //        foreach (HtmlNode childNode in htmlNode.ChildNodes)
            //            TraverseNode(childNode);

            //    ContentHandler.EndElement(null, htmlNode.Name, htmlNode.Name);
            //}
            //else
            //{
            //    if (htmlNode.HasChildNodes)
            //        foreach (HtmlNode childNode in htmlNode.ChildNodes)
            //            TraverseNode(childNode);
            //}
        }