htmlmodule.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 546 行
JAVA
546 行
/* * 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 Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.quercus.lib;import com.caucho.quercus.QuercusModuleException;import com.caucho.quercus.annotation.Optional;import com.caucho.quercus.env.*;import com.caucho.quercus.lib.regexp.RegexpModule;import com.caucho.quercus.module.AbstractQuercusModule;import com.caucho.util.L10N;import java.io.IOException;import java.io.Reader;import java.io.StringReader;import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Iterator;import java.util.Map;/** * PHP functions implementing html code. */public class HtmlModule extends AbstractQuercusModule { private static final L10N L = new L10N(HtmlModule.class); public static final int HTML_SPECIALCHARS = 0; public static final int HTML_ENTITIES = 1; public static final int ENT_HTML_QUOTE_NONE = 0; public static final int ENT_HTML_QUOTE_SINGLE = 1; public static final int ENT_HTML_QUOTE_DOUBLE = 2; public static final int ENT_COMPAT = ENT_HTML_QUOTE_DOUBLE; public static final int ENT_QUOTES = ENT_HTML_QUOTE_SINGLE | ENT_HTML_QUOTE_DOUBLE; public static final int ENT_NOQUOTES = ENT_HTML_QUOTE_NONE; private final static ArrayValue HTML_SPECIALCHARS_ARRAY = new ConstArrayValue(); private final static ArrayValue HTML_ENTITIES_ARRAY = new ConstArrayValue(); private static ArrayValueImpl HTML_ENTITIES_ARRAY_UNICODE; private static ArrayValueImpl HTML_SPECIALCHARS_ARRAY_UNICODE; public HtmlModule() { } private static ConstArrayValue toUnicodeArray(Env env, ArrayValue array) { ConstArrayValue copy = new ConstArrayValue(); Iterator<Map.Entry<Value,Value>> iter = array.getIterator(env); while (iter.hasNext()) { Map.Entry<Value,Value> entry = iter.next(); Value key = entry.getKey(); Value value = entry.getValue(); if (key.isString()) key = key.toUnicodeValue(env); if (value.isString()) value = value.toUnicodeValue(env); copy.put(key, value); } return copy; } /** * Returns HTML translation tables. */ public Value get_html_translation_table(Env env, @Optional("HTML_SPECIALCHARS") int table, @Optional("ENT_COMPAT") int quoteStyle) { Value result; if (! env.isUnicodeSemantics()) { if (table == HTML_ENTITIES) result = HTML_ENTITIES_ARRAY.copy(); else result = HTML_SPECIALCHARS_ARRAY.copy(); } else { if (table == HTML_ENTITIES) { if (HTML_ENTITIES_ARRAY_UNICODE == null) HTML_ENTITIES_ARRAY_UNICODE = toUnicodeArray(env, HTML_ENTITIES_ARRAY); result = HTML_ENTITIES_ARRAY_UNICODE.copy(); } else { if (HTML_SPECIALCHARS_ARRAY_UNICODE == null) HTML_SPECIALCHARS_ARRAY_UNICODE = toUnicodeArray(env, HTML_SPECIALCHARS_ARRAY); result = HTML_SPECIALCHARS_ARRAY_UNICODE.copy(); } } if ((quoteStyle & ENT_HTML_QUOTE_SINGLE) != 0) result.put(env.createString('\''), env.createString("'")); if ((quoteStyle & ENT_HTML_QUOTE_DOUBLE) != 0) result.put(env.createString('"'), env.createString(""")); return result; } /* * Converts escaped HTML entities back to characters. * * @param str escaped string * @param quoteStyle optional quote style used */ public static StringValue htmlspecialchars_decode(Env env, StringValue str, @Optional("ENT_COMPAT") int quoteStyle) { int len = str.length(); StringValue sb = str.createStringBuilder(len * 4 / 5); for (int i = 0; i < len; i++) { char ch = str.charAt(i); if (ch != '&') { sb.append(ch); continue; } switch (str.charAt(i + 1)) { case 'a': sb.append('&'); if (i + 4 < len && str.charAt(i + 2) == 'm' && str.charAt(i + 3) == 'p' && str.charAt(i + 4) == ';') { i += 4; } break; case 'q': if ((quoteStyle & ENT_HTML_QUOTE_DOUBLE) != 0 && i + 5 < len && str.charAt(i + 2) == 'u' && str.charAt(i + 3) == 'o' && str.charAt(i + 4) == 't' && str.charAt(i + 5) == ';') { i += 5; sb.append('"'); } else sb.append('&'); break; case '#': if ((quoteStyle & ENT_HTML_QUOTE_SINGLE) != 0 && i + 5 < len && str.charAt(i + 2) == '0' && str.charAt(i + 3) == '3' && str.charAt(i + 4) == '9' && str.charAt(i + 5) == ';') { i += 5; sb.append('\''); } else sb.append('&'); break; case 'l': if (i + 3 < len && str.charAt(i + 2) == 't' && str.charAt(i + 3) == ';') { i += 3; sb.append('<'); } else sb.append('&'); break; case 'g': if (i + 3 < len && str.charAt(i + 2) == 't' && str.charAt(i + 3) == ';') { i += 3; sb.append('>'); } else sb.append('&'); break; default: sb.append('&'); } } return sb; } /** * Escapes HTML * * @param env the calling environment * @param string the string to be trimmed * @param quoteStyleV optional quote style * @param charsetV optional charset style * @return the trimmed string */ public static Value htmlspecialchars(Env env, StringValue string, @Optional("ENT_COMPAT") int quoteStyle, @Optional String charset) { int len = string.length(); StringValue sb = string.createStringBuilder(len * 5 / 4); for (int i = 0; i < len; i++) { char ch = string.charAt(i); switch (ch) { case '&': sb.append("&"); break; case '"': if ((quoteStyle & ENT_HTML_QUOTE_DOUBLE) != 0) sb.append("""); else sb.append(ch); break; case '\'': if ((quoteStyle & ENT_HTML_QUOTE_SINGLE) != 0) sb.append("'"); else sb.append(ch); break; case '<': sb.append("<"); break; case '>': sb.append(">"); break; default: sb.append(ch); break; } } return sb; } /** * Escapes HTML * * @param env the calling environment * @param stringV the string to be trimmed * @param quoteStyleV optional quote style * @param charsetV optional charset style * @return the trimmed string */ public static Value htmlentities(Env env, StringValue string, @Optional("ENT_COMPAT") int quoteStyle, @Optional String charset) { if (charset == null || charset.length() == 0) charset = "ISO-8859-1"; Reader reader; try { reader = string.toReader(charset); } catch (UnsupportedEncodingException e) { env.warning(e); reader = new StringReader(string.toString()); } StringValue sb = string.createStringBuilder(string.length() * 5 / 4); ArrayValue entitiesArray; if (env.isUnicodeSemantics()) { if (HTML_ENTITIES_ARRAY_UNICODE == null) HTML_ENTITIES_ARRAY_UNICODE = toUnicodeArray(env, HTML_ENTITIES_ARRAY); entitiesArray = HTML_ENTITIES_ARRAY_UNICODE; } else { entitiesArray = HTML_ENTITIES_ARRAY; } int ch; try { while ((ch = reader.read()) >= 0) { StringValue chV = env.createString((char) ch); Value entity = entitiesArray.get(chV); if (entity.isNull()) entity = chV; if (ch == '"') { if ((quoteStyle & ENT_HTML_QUOTE_DOUBLE) != 0) { entity = env.createString("""); } else entity = chV; } else if (ch == '\'') { if ((quoteStyle & ENT_HTML_QUOTE_SINGLE) != 0) { entity = env.createString("'"); } else entity = chV; } sb.append(entity); } } catch (IOException e) { throw new QuercusModuleException(e); } return sb; } /** * Escapes HTML * * @param string the string to be trimmed * @param quoteStyle optional quote style * @param charset optional charset style * @return the trimmed string */ public static StringValue html_entity_decode(Env env, StringValue string, @Optional int quoteStyle, @Optional String charset) { if (string.length() == 0) return env.getEmptyString(); Iterator<Map.Entry<Value,Value>> iter; if (env.isUnicodeSemantics()) iter = HTML_ENTITIES_ARRAY_UNICODE.getIterator(env); else iter = HTML_ENTITIES_ARRAY.getIterator(env); while (iter.hasNext()) { Map.Entry<Value,Value> entry = iter.next(); StringValue key = entry.getKey().toStringValue(); StringValue value = entry.getValue().toStringValue(); string = RegexpModule.ereg_replace(env, value, key, string).toStringValue(); } return string; } /** * Replaces newlines with HTML breaks. * * @param env the calling environment */ public static Value nl2br(Env env, StringValue string) { int strLen = string.length(); StringValue sb = string.createStringBuilder(strLen * 5 / 4); for (int i = 0; i < strLen; i++) { char ch = string.charAt(i); if (ch == '\n') { sb.append("<br />\n"); } else if (ch == '\r') { if (i + 1 < strLen && string.charAt(i + 1) == '\n') { sb.append("<br />\r\n"); i++; } else { sb.append("<br />\r"); } } else { sb.append(ch); } } return sb; } private static void entity(int ch, String entity) { // XXX: i18n and optimize static variables usuage HTML_ENTITIES_ARRAY.put("" + (char) ch, entity); } static { HTML_SPECIALCHARS_ARRAY.put("<", "<"); HTML_SPECIALCHARS_ARRAY.put(">", ">"); HTML_SPECIALCHARS_ARRAY.put("&", "&"); entity('<', "<"); entity('>', ">"); entity('&', "&"); entity(160, " "); entity(161, "¡"); entity(162, "¢"); entity(163, "£"); entity(164, "¤"); entity(165, "¥"); entity(166, "¦"); entity(167, "§"); entity(168, "¨"); entity(169, "©"); entity(170, "ª"); entity(171, "«"); entity(172, "¬"); entity(173, "­"); entity(174, "®"); entity(175, "¯"); entity(176, "°"); entity(177, "±"); entity(178, "²"); entity(179, "³"); entity(180, "´"); entity(181, "µ"); entity(182, "¶"); entity(183, "·"); entity(184, "¸"); entity(185, "¹"); entity(186, "º"); entity(187, "»"); entity(188, "¼"); entity(189, "½"); entity(190, "¾"); entity(191, "¿"); entity(192, "À"); entity(193, "Á"); entity(194, "Â"); entity(195, "Ã"); entity(196, "Ä"); entity(197, "Å"); entity(198, "Æ"); entity(199, "Ç"); entity(200, "È"); entity(201, "É"); entity(202, "Ê"); entity(203, "Ë"); entity(204, "Ì"); entity(205, "Í"); entity(206, "Î"); entity(207, "Ï"); entity(208, "Ð"); entity(209, "Ñ"); entity(210, "Ò"); entity(211, "Ó"); entity(212, "Ô"); entity(213, "Õ"); entity(214, "Ö"); entity(215, "×"); entity(216, "Ø"); entity(217, "Ù"); entity(218, "Ú"); entity(219, "Û"); entity(220, "Ü"); entity(221, "Ý"); entity(222, "Þ"); entity(223, "ß"); entity(224, "à"); entity(225, "á"); entity(226, "â"); entity(227, "ã"); entity(228, "ä"); entity(229, "å"); entity(230, "æ"); entity(231, "ç"); entity(232, "è"); entity(233, "é"); entity(234, "ê"); entity(235, "ë"); entity(236, "ì"); entity(237, "í"); entity(238, "î"); entity(239, "ï"); entity(240, "ð"); entity(241, "ñ"); entity(242, "ò"); entity(243, "ó"); entity(244, "ô"); entity(245, "õ"); entity(246, "ö"); entity(247, "÷"); entity(248, "ø"); entity(249, "ù"); entity(250, "ú"); entity(251, "û"); entity(252, "ü"); entity(253, "ý"); entity(254, "þ"); entity(255, "ÿ"); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?