Beispiel #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);
        }
Beispiel #2
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);
 }
Beispiel #3
0
 /// <summary>Start visitor (initialization)</summary>
 /// <exception cref="System.IO.IOException"/>
 public virtual void Start(int version)
 {
     try
     {
         contentHandler.StartElement(string.Empty, string.Empty, "EDITS_VERSION", new AttributesImpl
                                         ());
         StringBuilder bld = new StringBuilder();
         bld.Append(version);
         AddString(bld.ToString());
         contentHandler.EndElement(string.Empty, string.Empty, "EDITS_VERSION");
     }
     catch (SAXException e)
     {
         throw new IOException("SAX error: " + e.Message);
     }
 }
Beispiel #4
0
        /// <summary>
        /// Create a processor that writes to the file named and may or may not
        /// also output to the screen, as specified.
        /// </summary>
        /// <param name="filename">Name of file to write output to</param>
        /// <param name="printToScreen">Mirror output to screen?</param>
        /// <exception cref="System.IO.IOException"/>
        public XmlEditsVisitor(OutputStream @out)
        {
            this.@out = @out;
            OutputFormat outFormat = new OutputFormat("XML", "UTF-8", true);

            outFormat.SetIndenting(true);
            outFormat.SetIndent(2);
            outFormat.SetDoctype(null, null);
            XMLSerializer serializer = new XMLSerializer(@out, outFormat);

            contentHandler = serializer.AsContentHandler();
            try
            {
                contentHandler.StartDocument();
                contentHandler.StartElement(string.Empty, string.Empty, "EDITS", new AttributesImpl
                                                ());
            }
            catch (SAXException e)
            {
                throw new IOException("SAX error: " + e.Message);
            }
        }
Beispiel #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!");
            }
        }
Beispiel #6
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!");
            }
        }
Beispiel #7
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);
            //}
        }