首页

关于grinder源码包中定义HTMLDocument、HTMLElement实现HTML文档元素结构原型设计定义源码解读

标签:grinder,HTMLDocument,HTMLElement,HTML文档元素结构     发布时间:2018-05-23   

一、前言

关于grinder源码包定义net.grinder.util.html.HTMLDocument、net.grinder.util.html.HTMLElement文档元素结构类,实现HTML文档元素结构定义渲染,具体参见源码说明

二、源码说明

1.HTMLDocument文档类

package net.grinder.util.html;@b@@b@public final class HTMLDocument extends HTMLElement@b@{@b@  private final HTMLElement m_head;@b@  private final HTMLElement m_body;@b@@b@  public HTMLDocument()@b@  {@b@    super("html");@b@@b@    this.m_head = addElement("head");@b@    this.m_body = addElement("body");@b@  }@b@@b@  public HTMLElement getHead()@b@  {@b@    return this.m_head;@b@  }@b@@b@  public HTMLElement getBody()@b@  {@b@    return this.m_body;@b@  }@b@}

2.HTMLElement文档类

package net.grinder.util.html;@b@@b@import java.util.ArrayList;@b@import java.util.Iterator;@b@import java.util.List;@b@@b@public class HTMLElement extends AbstractHTMLNode@b@{@b@  private final String m_name;@b@  private final List m_children = new ArrayList();@b@@b@  public HTMLElement(String name)@b@  {@b@    this.m_name = name;@b@  }@b@@b@  public HTMLElement()@b@  {@b@    this.m_name = null;@b@  }@b@@b@  public final HTMLElement addElement(String childText)@b@  {@b@    HTMLElement child = new HTMLElement(childText);@b@    this.m_children.add(child);@b@    return child;@b@  }@b@@b@  public final void addText(String text)@b@  {@b@    this.m_children.add(new HTMLText(text));@b@  }@b@@b@  protected final void toStringBuffer(StringBuffer buffer, boolean html)@b@  {@b@    if (this.m_children.size() > 0) {@b@      if ((!(isFragment())) && @b@        (html)) {@b@        buffer.append('<');@b@        buffer.append(this.m_name);@b@        buffer.append(">");@b@      }@b@@b@      Iterator iterator = this.m_children.iterator();@b@@b@      while (iterator.hasNext()) {@b@        ((AbstractHTMLNode)iterator.next()).toStringBuffer(buffer, html);@b@      }@b@@b@      if (!(isFragment()))@b@        if (html) {@b@          buffer.append("</");@b@          buffer.append(this.m_name);@b@          buffer.append(">");@b@        }@b@        else if (isNewLineElement()) {@b@          buffer.append("\n");@b@        }@b@@b@@b@    }@b@    else if (!(isFragment())) {@b@      if (html) {@b@        buffer.append("<");@b@        buffer.append(this.m_name);@b@        buffer.append("/>");@b@      }@b@      else if (isNewLineElement()) {@b@        buffer.append("\n");@b@      }@b@    }@b@  }@b@@b@  private boolean isNewLineElement()@b@  {@b@    return ((this.m_name.equalsIgnoreCase("p")) || (this.m_name.equalsIgnoreCase("br")));@b@  }@b@@b@  private boolean isFragment()@b@  {@b@    return (this.m_name == null);@b@  }@b@}

3.HTMLText类

package net.grinder.util.html;@b@@b@public final class HTMLText extends AbstractHTMLNode@b@{@b@  private final String m_text;@b@@b@  public HTMLText(String text)@b@  {@b@    this.m_text = text;@b@  }@b@@b@  protected void toStringBuffer(StringBuffer buffer, boolean html)@b@  {@b@    buffer.append(this.m_text);@b@  }@b@}

4.AbstractHTMLNode抽象类

package net.grinder.util.html;@b@@b@abstract class AbstractHTMLNode@b@{@b@  protected abstract void toStringBuffer(StringBuffer paramStringBuffer, boolean paramBoolean);@b@@b@  public String toText()@b@  {@b@    StringBuffer result = new StringBuffer();@b@    toStringBuffer(result, false);@b@    return result.toString();@b@  }@b@@b@  public String toHTML()@b@  {@b@    StringBuffer result = new StringBuffer();@b@    toStringBuffer(result, true);@b@    return result.toString();@b@  }@b@@b@  public String toString()@b@  {@b@    return toText();@b@  }@b@}