📄 htmlencoder.java
字号:
/*
* Helma License Notice
*
* The contents of this file are subject to the Helma License
* Version 2.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://adele.helma.org/download/helma/license.txt
*
* http://adele.helma.org/download/helma/license.txt:
*
Copyright (c) 1999-2002 Helma Project. 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. Products derived from this software may not be called "Helma"
or "Hop", nor may "Helma" or "Hop" appear in their name, without
prior written permission of the Helma Project Group. For written
permission, please contact helma@helma.org.
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 HELMA PROJECT 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.
*/
package freecs.util;
import java.util.*;
/**
* This is a utility class to encode special characters and do formatting
* for HTML output.
* @author Hannes Wallnoefer
*/
public final class HtmlEncoder {
// transformation table for characters 128 to 255. These actually fall into two
// groups, put together for efficiency: "Windows" chacacters 128-159 such as
// "smart quotes", which are encoded to valid Unicode entities, and
// valid ISO-8859 caracters 160-255, which are encoded to the symbolic HTML
// entity. Everything >= 256 is encoded to a numeric entity.
//
// for mor on HTML entities see http://www.pemberley.com/janeinfo/latin1.html and
// ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT
//
static final String[] transform = {
"€", // 128
"", // empty string means character is undefined in unicode
"‚",
"ƒ",
"„",
"…",
"†",
"‡",
"ˆ",
"‰",
"Š",
"‹",
"Œ",
"",
"Ž",
"",
"",
"‘",
"’",
"“",
"”",
"•",
"–",
"—",
"˜",
"™",
"š",
"›",
"œ",
"",
"ž",
"Ÿ", // 159
" ", // 160
"¡",
"¢",
"£",
"¤",
"¥",
"¦",
"§",
"¨",
"©",
"ª",
"«",
"¬",
"­",
"®",
"¯",
"°",
"±",
"²",
"³",
"´",
"µ",
"¶",
"·",
"¸",
"¹",
"º",
"»",
"¼",
"½",
"¾",
"¿",
"À",
"Á",
"Â",
"Ã",
"Ä",
"Å",
"Æ",
"Ç",
"È",
"É",
"Ê",
"Ë",
"Ì",
"Í",
"Î",
"Ï",
"Ð",
"Ñ",
"Ò",
"Ó",
"Ô",
"Õ",
"Ö",
"×",
"Ø",
"Ù",
"Ú",
"Û",
"Ü",
"Ý",
"Þ",
"ß",
"à",
"á",
"â",
"ã",
"ä",
"å",
"æ",
"ç",
"è",
"é",
"ê",
"ë",
"ì",
"í",
"î",
"ï",
"ð",
"ñ",
"ò",
"ó",
"ô",
"õ",
"ö",
"÷",
"ø",
"ù",
"ú",
"û",
"ü",
"ý",
"þ",
"ÿ" // 255
};
static final HashSet allTags = new HashSet();
static {
allTags.add("a");
allTags.add("abbr");
allTags.add("acronym");
allTags.add("address");
allTags.add("applet");
allTags.add("area");
allTags.add("b");
allTags.add("base");
allTags.add("basefont");
allTags.add("bdo");
allTags.add("bgsound");
allTags.add("big");
allTags.add("blink");
allTags.add("blockquote");
allTags.add("bq");
allTags.add("body");
allTags.add("br");
allTags.add("button");
allTags.add("caption");
allTags.add("center");
allTags.add("cite");
allTags.add("code");
allTags.add("col");
allTags.add("colgroup");
allTags.add("del");
allTags.add("dfn");
allTags.add("dir");
allTags.add("div");
allTags.add("dl");
allTags.add("dt");
allTags.add("dd");
allTags.add("em");
allTags.add("embed");
allTags.add("fieldset");
allTags.add("font");
allTags.add("form");
allTags.add("frame");
allTags.add("frameset");
allTags.add("h1");
allTags.add("h2");
allTags.add("h3");
allTags.add("h4");
allTags.add("h5");
allTags.add("h6");
allTags.add("head");
allTags.add("html");
allTags.add("hr");
allTags.add("i");
allTags.add("iframe");
allTags.add("img");
allTags.add("input");
allTags.add("ins");
allTags.add("isindex");
allTags.add("kbd");
allTags.add("label");
allTags.add("legend");
allTags.add("li");
allTags.add("link");
allTags.add("listing");
allTags.add("map");
allTags.add("marquee");
allTags.add("menu");
allTags.add("meta");
allTags.add("nobr");
allTags.add("noframes");
allTags.add("noscript");
allTags.add("object");
allTags.add("ol");
allTags.add("option");
allTags.add("optgroup");
allTags.add("p");
allTags.add("param");
allTags.add("plaintext");
allTags.add("pre");
allTags.add("q");
allTags.add("s");
allTags.add("samp");
allTags.add("script");
allTags.add("select");
allTags.add("small");
allTags.add("span");
allTags.add("strike");
allTags.add("strong");
allTags.add("style");
allTags.add("sub");
allTags.add("sup");
allTags.add("table");
allTags.add("tbody");
allTags.add("td");
allTags.add("textarea");
allTags.add("tfoot");
allTags.add("th");
allTags.add("thead");
allTags.add("title");
allTags.add("tr");
allTags.add("tt");
allTags.add("u");
allTags.add("ul");
allTags.add("var");
allTags.add("wbr");
allTags.add("xmp");
}
// HTML block tags need to suppress automatic newline to <br>
// conversion around them to look good. However, they differ
// in how many newlines around them should ignored. These sets
// help to treat each tag right in newline conversion.
static final HashSet internalTags = new HashSet();
static final HashSet blockTags = new HashSet();
static final HashSet semiBlockTags = new HashSet();
static {
// actual block level elements
semiBlockTags.add("address");
semiBlockTags.add("dir");
semiBlockTags.add("div");
semiBlockTags.add("table");
blockTags.add("blockquote");
blockTags.add("center");
blockTags.add("dl");
blockTags.add("fieldset");
blockTags.add("form");
blockTags.add("h1");
blockTags.add("h2");
blockTags.add("h3");
blockTags.add("h4");
blockTags.add("h5");
blockTags.add("h6");
blockTags.add("hr");
blockTags.add("isindex");
blockTags.add("ol");
blockTags.add("p");
blockTags.add("pre");
blockTags.add("ul");
internalTags.add("menu");
internalTags.add("noframes");
internalTags.add("noscript");
/// to be treated as block level elements
semiBlockTags.add("th");
blockTags.add("br");
blockTags.add("dd");
blockTags.add("dt");
blockTags.add("frameset");
blockTags.add("li");
blockTags.add("td");
internalTags.add("tbody");
internalTags.add("tfoot");
internalTags.add("thead");
internalTags.add("tr");
}
// set of tags that are always empty
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -