📄 htmldtd.java
字号:
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xerces" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, International * Business Machines, Inc., http://www.apache.org. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */// Aug 21, 2000:// Fixed bug in isElement and made HTMLdtd public.// Contributed by Eric SCHAEFFER" <eschaeffer@posterconseil.com>package org.jasig.portal.serialize;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.util.Collections;import java.util.HashMap;import java.util.Map;/** * 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.3 $ $Date: 2004/01/10 01:45:00 $ * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a> */public final class HTMLdtd{ /** * Public identifier for HTML document type. */ public static final String HTMLPublicId = "-//W3C//DTD HTML 4.0//EN"; /** * System identifier for HTML document type. */ public static final String HTMLSystemId = "http://www.w3.org/TR/WD-html-in-xml/DTD/xhtml1-strict.dtd"; /** * Public identifier for XHTML document type. */ public static final String XHTMLPublicId = "-//W3C//DTD XHTML 1.0 Strict//EN"; /** * System identifier for XHTML document type. */ public static final String XHTMLSystemId = "http://www.w3.org/TR/WD-html-in-xml/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 Map _byChar; /** * Table of entity name to value mapping. Entities are held as strings, * character references as <TT>Character</TT> objects. */ private static Map _byName; private static Map _boolAttrs; /** * Holds element definitions. */ private static Map _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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -