htmlpolicy.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 704 行 · 第 1/2 页

JAVA
704
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the *   Free SoftwareFoundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.xml;import com.caucho.util.CharBuffer;import com.caucho.util.CharCursor;import com.caucho.util.CharScanner;import com.caucho.util.IntMap;import com.caucho.util.StringCharCursor;import org.w3c.dom.Element;import java.io.IOException;/** * Policy for parsing an HTML file. */class HtmlPolicy extends Policy {  static final int DOCUMENT = 1;  static final int COMMENT = DOCUMENT + 1;  static final int TEXT = COMMENT + 1;  static final int JSP = TEXT + 1;  static final int WHITESPACE = JSP + 1;  static final int HTML = WHITESPACE + 1;  static final int HEAD = HTML + 1;  static final int TITLE = HEAD + 1;  static final int ISINDEX = TITLE + 1;  static final int BASE = ISINDEX + 1;  static final int SCRIPT = BASE + 1;  static final int STYLE = SCRIPT + 1;  static final int META = STYLE + 1;  static final int LINK = META + 1;  static final int OBJECT = LINK + 1;  static final int BODY = OBJECT + 1;  static final int BASEFONT = BODY + 1;  static final int BR = BASEFONT + 1;  static final int AREA = BR + 1;  static final int IMG = AREA + 1;  static final int PARAM = IMG + 1;  static final int HR = PARAM + 1;  static final int INPUT = HR + 1;  static final int P = INPUT + 1;  static final int DT = P + 1;  static final int DD = DT + 1;  static final int LI = DD + 1;  static final int OPTION = LI + 1;  static final int TABLE = OPTION + 1;  static final int CAPTION = TABLE + 1;  static final int THEAD = CAPTION + 1;  static final int TFOOT = THEAD + 1;  static final int COL = TFOOT + 1;  static final int COLGROUP = COL + 1;  static final int TBODY = COLGROUP + 1;  static final int TR = TBODY + 1;  static final int TD = TR + 1;  static final int TH = TD + 1;  static final int FRAME = TH + 1;  static final int FRAMESET = FRAME + 1;  static final int BLOCK = FRAMESET + 1;  static final int INLINE = BLOCK + 1;  static IntMap names;  static IntMap cbNames;    static QName htmlName = new QName(null, "html", null);  static QName headName = new QName(null, "head", null);  static QName bodyName = new QName(null, "body", null);  boolean toLower = true;  boolean isJsp = false;  boolean autoHtml = false;  boolean hasBody = false;  boolean autoHead = false;    CharBuffer cb = new CharBuffer();  public void init()  {    toLower = true;    isJsp = false;    autoHtml = false;    hasBody = false;    autoHead = false;  }  /**   * When true, HTML parsing normalizes HTML tags to lower case.   */  public void setToLower(boolean toLower)  {    this.toLower = toLower;  }  /**   * When true, treat text before HTML specially.   */  public void setJsp(boolean isJsp)  {    this.isJsp = isJsp;  }  /**   * Return the normalized name.   *   * @param tag the raw name in the XML file.   *   * @return the normalized name.   */  QName getName(CharBuffer tag)  {    if (! toLower)      return super.getName(tag);        cb.clear();    cb.append(tag);    cb.toLowerCase();    int name = cbNames.get(cb);    if (name >= 0)      return super.getName(cb);    else      return super.getName(tag);  }  QName getAttributeName(CharBuffer eltName, CharBuffer source)  {    if (! toLower)      return super.getName(source);        cb.clear();    cb.append(eltName);    cb.toLowerCase();    int name = cbNames.get(cb);    if (name < 0)      return super.getName(source);    else {      source.toLowerCase();      return super.getName(source);    }  }  /**   * Returns the appropriate action when opening a HTML tag.   *   * @param parser the XML parser   * @param node the parent node   * @param next the next child   * @return the action code   */  int openAction(XmlParser parser, QName node, QName next)    throws XmlParseException  {    String nodeName = node == null ? "#document" : node.getName();    String nextName = next.getName();    int nextCode = names.get(nextName);    switch (names.get(nodeName)) {    case DOCUMENT:      switch (nextCode) {      case HTML:	return PUSH;      case COMMENT:        return PUSH;      case HEAD: case TITLE: case ISINDEX: case BASE: case SCRIPT:       case STYLE: case META: case LINK: case OBJECT:	opt = htmlName;	return PUSH_OPT;      case WHITESPACE:        return IGNORE;      case JSP:        return PUSH;      default:        if (autoHtml)          return PUSH;                autoHtml = true;	opt = htmlName;	return PUSH_OPT;      }    case HTML:      switch (nextCode) {      case HTML:	return ERROR;      case HEAD:      case COMMENT:      case FRAMESET:	return PUSH;              case BODY:        hasBody = true;	return PUSH;      case TITLE: case ISINDEX: case BASE: case SCRIPT:       case STYLE: case META: case LINK: case OBJECT:	opt = headName;        autoHead = true;	return PUSH_OPT;      case WHITESPACE:        return PUSH;      case JSP:        return PUSH;      default:        if (hasBody)          return PUSH;                hasBody = true;	opt = bodyName;	return PUSH_OPT;      }    case HEAD:      switch (nextCode) {      case META:	// checkMetaEncoding((Element) next);	return PUSH_EMPTY;      case LINK: case ISINDEX: case BASE: 	return PUSH_EMPTY;              case SCRIPT: case STYLE:        return PUSH_VERBATIM;              case TITLE:      case OBJECT:	return PUSH;      case WHITESPACE:        return PUSH;              case JSP:      case TEXT:        if (autoHead)          return POP;        else          return PUSH;      default:	return POP;      }    case LI:      switch (nextCode) {      case LI:	return POP;      case BASEFONT: case BR: case AREA: case LINK: case IMG: case PARAM:       case HR: case INPUT: case COL: case FRAME: case ISINDEX:       case BASE: case META:	return PUSH_EMPTY;      case SCRIPT: case STYLE:        return PUSH_VERBATIM;      default:	return PUSH;      }    case OPTION:      switch (nextCode) {      case WHITESPACE:      case TEXT:        return PUSH;      default:	return POP;      }    case DD:      switch (nextCode) {      case DD: case DT:	return POP;      case BASEFONT: case BR: case AREA: case LINK: case IMG: case PARAM:       case HR: case INPUT: case COL: case FRAME: case ISINDEX:       case BASE: case META:	return PUSH_EMPTY;      case SCRIPT: case STYLE:        return PUSH_VERBATIM;      default:	return PUSH;      }    case THEAD: case TFOOT: case COLGROUP:      switch (nextCode) {      case THEAD: case TFOOT: case TBODY: case COLGROUP: case COL:	return POP;      case BASEFONT: case BR: case AREA: case LINK: case IMG: case PARAM:       case HR: case INPUT: case FRAME: case ISINDEX:       case BASE: case META:	return PUSH_EMPTY;      case SCRIPT: case STYLE:        return PUSH_VERBATIM;      default:	return PUSH;      }    case TR:      switch (nextCode) {      case THEAD: case TFOOT: case TBODY: case COLGROUP: case COL: case TR:	return POP;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?