htmldtd.java

来自「JAVA 所有包」· Java 代码 · 共 556 行 · 第 1/2 页

JAVA
556
字号
/* * Copyright 1999-2002,2004 The Apache Software Foundation. *  * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *  *      http://www.apache.org/licenses/LICENSE-2.0 *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */// Aug 21, 2000://   Fixed bug in isElement and made HTMLdtd public.//   Contributed by Eric SCHAEFFER" <eschaeffer@posterconseil.com>package com.sun.org.apache.xml.internal.serialize;import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter;import java.io.InputStream;import java.io.InputStreamReader;import java.io.BufferedReader;import java.util.Hashtable;import java.util.Locale;/** * Utility class for accessing information specific to HTML documents. * The HTML DTD is expressed as three utility function groups. Two methods * allow for checking whether an element requires an open tag on printing * ({@link #isEmptyTag}) or on parsing ({@link #isOptionalClosing}). * <P> * Two other methods translate character references from name to value and * from value to name. A small entities resource is loaded into memory the * first time any of these methods is called for fast and efficient access. * * * @version $Revision: 1.2.6.1 $ $Date: 2005/09/09 07:26:14 $ * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a> */public final class HTMLdtd{    /**     * Public identifier for HTML 4.01 (Strict) document type.     */    public static final String HTMLPublicId = "-//W3C//DTD HTML 4.01//EN";    /**     * System identifier for HTML 4.01 (Strict) document type.     */    public static final String HTMLSystemId =        "http://www.w3.org/TR/html4/strict.dtd";    /**     * Public identifier for XHTML 1.0 (Strict) document type.     */    public static final String XHTMLPublicId =        "-//W3C//DTD XHTML 1.0 Strict//EN";    /**     * System identifier for XHTML 1.0 (Strict) document type.     */    public static final String XHTMLSystemId =        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";    /**     * Table of reverse character reference mapping. Character codes are held     * as single-character strings, mapped to their reference name.     */    private static Hashtable        _byChar;    /**     * Table of entity name to value mapping. Entities are held as strings,     * character references as <TT>Character</TT> objects.     */    private static Hashtable        _byName;    private static Hashtable        _boolAttrs;    /**     * Holds element definitions.     */    private static Hashtable        _elemDefs;    /**     * Locates the HTML entities file that is loaded upon initialization.     * This file is a resource loaded with the default class loader.     */    private static final String     ENTITIES_RESOURCE = "HTMLEntities.res";    /**     * Only opening tag should be printed.     */    private static final int ONLY_OPENING = 0x0001;    /**     * Element contains element content only.     */    private static final int ELEM_CONTENT = 0x0002;    /**     * Element preserve spaces.     */    private static final int PRESERVE     = 0x0004;    /**     * Optional closing tag.     */    private static final int OPT_CLOSING  = 0x0008;    /**     * Element is empty (also means only opening tag)     */    private static final int EMPTY        = 0x0010 | ONLY_OPENING;    /**     * Allowed to appear in head.     */    private static final int ALLOWED_HEAD = 0x0020;    /**     * When opened, closes P.     */    private static final int CLOSE_P      = 0x0040;    /**     * When opened, closes DD or DT.     */    private static final int CLOSE_DD_DT  = 0x0080;    /**     * When opened, closes itself.     */    private static final int CLOSE_SELF   = 0x0100;    /**     * When opened, closes another table section.     */    private static final int CLOSE_TABLE  = 0x0200;    /**     * When opened, closes TH or TD.     */    private static final int CLOSE_TH_TD  = 0x04000;    /**     * Returns true if element is declared to be empty. HTML elements are     * defines as empty in the DTD, not by the document syntax.     *     * @param tagName The element tag name (upper case)     * @return True if element is empty     */    public static boolean isEmptyTag( String tagName )    {        return isElement( tagName, EMPTY );    }    /**     * Returns true if element is declared to have element content.     * Whitespaces appearing inside element content will be ignored,     * other text will simply report an error.     *     * @param tagName The element tag name (upper case)     * @return True if element content     */    public static boolean isElementContent( String tagName )    {        return isElement( tagName, ELEM_CONTENT );    }    /**     * Returns true if element's textual contents preserves spaces.     * This only applies to PRE and TEXTAREA, all other HTML elements     * do not preserve space.     *     * @param tagName The element tag name (upper case)     * @return True if element's text content preserves spaces     */    public static boolean isPreserveSpace( String tagName )    {        return isElement( tagName, PRESERVE );    }    /**     * Returns true if element's closing tag is optional and need not     * exist. An error will not be reported for such elements if they     * are not closed. For example, <tt>LI</tt> is most often not closed.     *     * @param tagName The element tag name (upper case)     * @return True if closing tag implied     */    public static boolean isOptionalClosing( String tagName )    {        return isElement( tagName, OPT_CLOSING );    }    /**     * Returns true if element's closing tag is generally not printed.     * For example, <tt>LI</tt> should not print the closing tag.     *     * @param tagName The element tag name (upper case)     * @return True if only opening tag should be printed     */    public static boolean isOnlyOpening( String tagName )    {        return isElement( tagName, ONLY_OPENING );    }    /**     * Returns true if the opening of one element (<tt>tagName</tt>) implies     * the closing of another open element (<tt>openTag</tt>). For example,     * every opening <tt>LI</tt> will close the previously open <tt>LI</tt>,     * and every opening <tt>BODY</tt> will close the previously open <tt>HEAD</tt>.     *     * @param tagName The newly opened element     * @param openTag The already opened element     * @return True if closing tag closes opening tag     */    public static boolean isClosing( String tagName, String openTag )    {        // Several elements are defined as closing the HEAD        if ( openTag.equalsIgnoreCase( "HEAD" ) )            return ! isElement( tagName, ALLOWED_HEAD );        // P closes iteself        if ( openTag.equalsIgnoreCase( "P" ) )            return isElement( tagName, CLOSE_P );        // DT closes DD, DD closes DT        if ( openTag.equalsIgnoreCase( "DT" ) || openTag.equalsIgnoreCase( "DD" ) )            return isElement( tagName, CLOSE_DD_DT );        // LI and OPTION close themselves        if ( openTag.equalsIgnoreCase( "LI" ) || openTag.equalsIgnoreCase( "OPTION" ) )            return isElement( tagName, CLOSE_SELF );        // Each of these table sections closes all the others        if ( openTag.equalsIgnoreCase( "THEAD" ) || openTag.equalsIgnoreCase( "TFOOT" ) ||             openTag.equalsIgnoreCase( "TBODY" ) || openTag.equalsIgnoreCase( "TR" ) ||             openTag.equalsIgnoreCase( "COLGROUP" ) )            return isElement( tagName, CLOSE_TABLE );        // TD closes TH and TH closes TD        if ( openTag.equalsIgnoreCase( "TH" ) || openTag.equalsIgnoreCase( "TD" ) )            return isElement( tagName, CLOSE_TH_TD );        return false;    }    /**     * Returns true if the specified attribute it a URI and should be     * escaped appropriately. In HTML URIs are escaped differently     * than normal attributes.     *     * @param tagName The element's tag name

⌨️ 快捷键说明

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