ParseHTML.cs
Upload User: sarage88
Upload Date: 2013-09-22
Package Size: 34k
Code Size: 4k
Development Platform:

C#

  1. using System;
  2. namespace HTML
  3. {
  4.     /// <summary>
  5.     /// Summary description for ParseHTML.
  6.     /// </summary>
  7.     public class ParseHTML : Parse
  8.     {
  9.         public AttributeList GetTag()
  10.         {
  11.             AttributeList tag = new AttributeList();
  12.             tag.Name = m_tag;
  13.             foreach (Attribute x in List)
  14.             {
  15.                 tag.Add((Attribute)x.Clone());
  16.             }
  17.             return tag;
  18.         }
  19.         public String BuildTag()
  20.         {
  21.             String buffer = "<";
  22.             buffer += m_tag;
  23.             int i = 0;
  24.             while (this[i] != null)
  25.             {// has attributes
  26.                 buffer += " ";
  27.                 if (this[i].Value == null)
  28.                 {
  29.                     if (this[i].Delim != 0)
  30.                         buffer += this[i].Delim;
  31.                     buffer += this[i].Name;
  32.                     if (this[i].Delim != 0)
  33.                         buffer += this[i].Delim;
  34.                 }
  35.                 else
  36.                 {
  37.                     buffer += this[i].Name;
  38.                     if (this[i].Value != null)
  39.                     {
  40.                         buffer += "=";
  41.                         if (this[i].Delim != 0)
  42.                             buffer += this[i].Delim;
  43.                         buffer += this[i].Value;
  44.                         if (this[i].Delim != 0)
  45.                             buffer += this[i].Delim;
  46.                     }
  47.                 }
  48.                 i++;
  49.             }
  50.             buffer += ">";
  51.             return buffer;
  52.         }
  53.         protected void ParseTag()
  54.         {
  55.             m_tag = "";
  56.             Clear();
  57.             // Is it a comment?
  58.             if ((GetCurrentChar() == '!') &&
  59.               (GetCurrentChar(1) == '-') &&
  60.               (GetCurrentChar(2) == '-'))
  61.             {
  62.                 while (!Eof())
  63.                 {
  64.                     if ((GetCurrentChar() == '-') &&
  65.                       (GetCurrentChar(1) == '-') &&
  66.                       (GetCurrentChar(2) == '>'))
  67.                         break;
  68.                     if (GetCurrentChar() != 'r')
  69.                         m_tag += GetCurrentChar();
  70.                     Advance();
  71.                 }
  72.                 m_tag += "--";
  73.                 Advance();
  74.                 Advance();
  75.                 Advance();
  76.                 ParseDelim = (char)0;
  77.                 return;
  78.             }
  79.             // Find the tag name
  80.             while (!Eof())
  81.             {
  82.                 if (IsWhiteSpace(GetCurrentChar()) ||
  83.                                  (GetCurrentChar() == '>'))
  84.                     break;
  85.                 m_tag += GetCurrentChar();
  86.                 Advance();
  87.             }
  88.             EatWhiteSpace();
  89.             // Get the attributes
  90.             while (GetCurrentChar() != '>')
  91.             {
  92.                 ParseName = "";
  93.                 ParseValue = "";
  94.                 ParseDelim = (char)0;
  95.                 ParseAttributeName();
  96.                 if (GetCurrentChar() == '>')
  97.                 {
  98.                     AddAttribute();
  99.                     break;
  100.                 }
  101.                 // Get the value(if any)
  102.                 ParseAttributeValue();
  103.                 AddAttribute();
  104.             }
  105.             Advance();
  106.         }
  107.         public char Parse()
  108.         {
  109.             if (GetCurrentChar() == '<')
  110.             {
  111.                 Advance();
  112.                 char ch = char.ToUpper(GetCurrentChar());
  113.                 if ((ch >= 'A') && (ch <= 'Z') || (ch == '!') || (ch == '/'))
  114.                 {
  115.                     ParseTag();
  116.                     return (char)0;
  117.                 }
  118.                 else return (AdvanceCurrentChar());
  119.             }
  120.             else return (AdvanceCurrentChar());
  121.         }
  122.     }
  123. }