| /******************************************************************************* |
| * Copyright (c) 2006 Sybase, Inc. and others. |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Sybase, Inc. - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.jst.pagedesigner.utils; |
| |
| import java.io.IOException; |
| import java.io.Writer; |
| import java.util.Enumeration; |
| import java.util.Hashtable; |
| |
| import org.eclipse.jst.jsf.common.ui.internal.logging.Logger; |
| import org.eclipse.jst.pagedesigner.PDPlugin; |
| |
| /** |
| * entity map support. |
| * |
| * @author mengbo |
| */ |
| public class HTMLSpecialCharHelper { |
| private static Logger _log = PDPlugin |
| .getLogger(HTMLSpecialCharHelper.class); |
| |
| private static Hashtable _table; |
| |
| private static Hashtable _reverse; |
| static { |
| _table = new Hashtable(256); |
| |
| _table.put(Integer.valueOf(34), """); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(38), "&"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(60), "<"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(62), ">"); //$NON-NLS-1$ |
| |
| _table.put(Integer.valueOf(160), " "); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(161), "¡"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(162), "¢"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(163), "£"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(164), "¤"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(165), "¥"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(166), "¦"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(167), "§"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(168), "¨"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(169), "©"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(170), "ª"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(171), "«"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(172), "¬"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(173), "­"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(174), "®"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(175), "¯"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(176), "°"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(177), "±"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(178), "²"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(179), "³"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(180), "´"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(181), "µ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(182), "¶"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(183), "·"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(184), "¸"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(185), "¹"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(186), "º"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(187), "»"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(188), "¼"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(189), "½"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(190), "¾"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(191), "¿"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(192), "À"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(193), "Á"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(194), "Â"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(195), "Ã"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(196), "Ä"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(197), "Å"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(198), "Æ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(199), "Ç"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(200), "È"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(201), "É"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(202), "Ê"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(203), "Ë"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(204), "Ì"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(205), "Í"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(206), "Î"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(207), "Ï"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(208), "Ð"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(209), "Ñ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(210), "Ò"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(211), "Ó"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(212), "Ô"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(213), "Õ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(214), "Ö"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(215), "×"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(216), "Ø"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(217), "Ù"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(218), "Ú"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(219), "Û"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(220), "Ü"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(221), "Ý"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(222), "Þ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(223), "ß"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(224), "à"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(225), "á"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(226), "â"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(227), "ã"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(228), "ä"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(229), "å"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(230), "æ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(231), "ç"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(232), "è"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(233), "é"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(234), "ê"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(235), "ë"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(236), "ì"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(237), "í"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(238), "î"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(239), "ï"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(240), "ð"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(241), "ñ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(242), "ò"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(243), "ó"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(244), "ô"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(245), "õ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(246), "ö"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(247), "÷"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(248), "ø"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(249), "ù"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(250), "ú"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(251), "û"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(252), "ü"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(253), "ý"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(254), "þ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(255), "ÿ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(402), "ƒ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(913), "Α"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(914), "Β"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(915), "Γ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(916), "Δ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(917), "Ε"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(918), "Ζ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(919), "Η"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(920), "Θ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(921), "Ι"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(922), "Κ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(923), "Λ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(924), "Μ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(925), "Ν"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(926), "Ξ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(927), "Ο"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(928), "Π"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(929), "Ρ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(931), "Σ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(932), "Τ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(933), "Υ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(934), "Φ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(935), "Χ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(936), "Ψ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(937), "Ω"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(945), "α"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(946), "β"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(947), "γ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(948), "δ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(949), "ε"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(950), "ζ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(951), "η"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(952), "θ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(953), "ι"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(954), "κ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(955), "λ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(956), "μ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(957), "ν"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(958), "ξ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(959), "ο"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(960), "π"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(961), "ρ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(962), "ς"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(963), "σ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(964), "τ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(965), "υ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(966), "φ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(967), "χ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(968), "ψ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(969), "ω"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(977), "ϑ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(978), "ϒ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(982), "ϖ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8226), "•"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8230), "…"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8242), "′"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8243), "″"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8254), "‾"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8260), "⁄"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8472), "℘"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8465), "ℑ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8476), "ℜ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8482), "™"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8501), "ℵ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8592), "←"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8593), "↑"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8594), "→"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8595), "↓"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8596), "↔"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8629), "↵"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8656), "⇐"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8657), "⇑"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8658), "⇒"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8659), "⇓"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8660), "⇔"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8704), "∀"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8706), "∂"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8707), "∃"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8709), "∅"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8711), "∇"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8712), "∈"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8713), "∉"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8715), "∋"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8719), "∏"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8722), "∑"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8722), "−"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8727), "∗"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8730), "√"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8733), "∝"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8734), "∞"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8736), "∠"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8869), "∧"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8870), "∨"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8745), "∩"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8746), "∪"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8747), "∫"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8756), "∴"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8764), "∼"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8773), "≅"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8773), "≈"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8800), "≠"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8801), "≡"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8804), "≤"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8805), "≥"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8834), "⊂"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8835), "⊃"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8836), "⊄"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8838), "⊆"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8839), "⊇"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8853), "⊕"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8855), "⊗"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8869), "⊥"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8901), "⋅"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8968), "⌈"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8969), "⌉"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8970), "⌊"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8971), "⌋"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(9001), "⟨"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(9002), "⟩"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(9674), "◊"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(9824), "♠"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(9827), "♣"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(9829), "♥"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(9830), "♦"); //$NON-NLS-1$ |
| |
| _table.put(Integer.valueOf(338), "Œ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(339), "œ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(352), "Š"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(353), "š"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(376), "Ÿ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(710), "ˆ"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(732), "˜"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8194), " "); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8195), " "); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8201), " "); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8204), "‌"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8205), "‍"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8206), "‎"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8207), "‏"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8211), "–"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(151), "—"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8216), "‘"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8217), "’"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8218), "‚"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8220), "“"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8221), "”"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8222), "„"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8224), "†"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8225), "‡"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8240), "‰"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8249), "‹"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8250), "›"); //$NON-NLS-1$ |
| _table.put(Integer.valueOf(8364), "€"); //$NON-NLS-1$ |
| |
| _reverse = new Hashtable(256); |
| for (Enumeration e = _table.keys(); e.hasMoreElements();) { |
| Object key = e.nextElement(); |
| String value = (String) _table.get(key); |
| _reverse.put(value, key); |
| // also support without the training ';' |
| _reverse.put(value.substring(0, value.length() - 1), key); |
| } |
| } |
| |
| /** |
| * @param ch |
| * @return if not in the special list |
| */ |
| public static String getSpecial(int ch) { |
| return (String) _table.get(Integer.valueOf(ch)); |
| } |
| |
| /** |
| * @param str |
| * @return the code value corresponding to the string or null |
| * if string is unknown |
| */ |
| public static int getSpecial(String str) { |
| Integer result = (Integer) _reverse.get(str); |
| if (result == null) { |
| return -1; |
| } |
| return result.intValue(); |
| } |
| |
| /** |
| * @param str |
| * @param start |
| * @param end |
| * @param writer |
| * @throws IOException |
| */ |
| public static void encode(String str, int start, int end, Writer writer) |
| throws IOException { |
| for (int i = start; i < end; i++) { |
| char ch = str.charAt(i); |
| String special = getSpecial(ch); |
| if (special != null) { |
| writer.write(special); |
| } else { |
| if ((ch & 0xff) != 0) { |
| writer.write("&#"); //$NON-NLS-1$ |
| writer.write(Integer.toString(ch)); |
| writer.write(";"); //$NON-NLS-1$ |
| } else { |
| writer.write(ch); |
| } |
| } |
| } |
| } |
| |
| /** |
| * @param str |
| * @param result |
| * @return the encoded string buffer |
| */ |
| public static StringBuffer encode(String str, StringBuffer result) { |
| return encode(str, 0, str.length(), result); |
| } |
| |
| /** |
| * @param str |
| * @param start |
| * @param end |
| * @param result |
| * @return the encoded string buffer |
| */ |
| public static StringBuffer encode(String str, int start, int end, |
| StringBuffer result) { |
| for (int i = start; i < end; i++) { |
| char ch = str.charAt(i); |
| String special = getSpecial(ch); |
| if (special != null) { |
| result.append(special); |
| } else { |
| if ((ch & 0xff00) != 0) { |
| result.append("&#"); //$NON-NLS-1$ |
| result.append(Integer.toString(ch)); |
| result.append(";"); //$NON-NLS-1$ |
| } else { |
| result.append(ch); |
| } |
| } |
| } |
| return result; |
| } |
| |
| /** |
| * @param str |
| * @param buffer |
| * @return the decoded string buffer |
| * @throws RuntimeException |
| */ |
| public static StringBuffer decode(String str, StringBuffer buffer) |
| throws RuntimeException { |
| return decode(str, 0, str.length(), buffer); |
| } |
| |
| /** |
| * @param str |
| * @param start |
| * @param end |
| * @param buffer |
| * @return the decoded string buffer |
| * @throws RuntimeException |
| */ |
| public static StringBuffer decode(String str, int start, int end, |
| StringBuffer buffer) throws RuntimeException { |
| int pos = start; |
| do { |
| char ch = str.charAt(pos); |
| if (ch == '&') { |
| int stop = str.indexOf(';', pos + 1); |
| if (stop < 0 || stop >= end) { |
| _log.error("HTMLSpecialCharHelper.3"); //$NON-NLS-1$ |
| throw new RuntimeException("HTMLSpecialCharHelper.2"); //$NON-NLS-1$ |
| } |
| String sp = str.substring(pos, stop + 1); |
| int special = getSpecial(sp); |
| if (special != -1) { |
| buffer.append((char) special); |
| } else { |
| ch = sp.charAt(1); |
| if (ch != '#') |
| throw new RuntimeException("HTMLSpecialCharHelper.1"); //$NON-NLS-1$ |
| try { |
| buffer.append((char) Integer.parseInt(sp.substring(2, |
| sp.length() - 1))); |
| } catch (NumberFormatException ex) { |
| _log.info("HTMLSpecialCharHelper.0", ex); //$NON-NLS-1$ |
| throw new RuntimeException("illegal: " + sp); //$NON-NLS-1$ |
| } |
| } |
| pos = stop + 1; |
| } else { |
| buffer.append(ch); |
| pos++; |
| } |
| } while (pos < end); |
| return buffer; |
| } |
| |
| /** |
| * @param entityRef |
| * @return the code for the decoded entity reference |
| */ |
| public static int decodeEntity(String entityRef) { |
| Integer result = (Integer) _reverse.get(entityRef); |
| if (result != null) { |
| return result.intValue(); |
| } |
| if (entityRef.length() >= 2 && entityRef.charAt(1) == '#') { |
| String s = entityRef.substring(2); |
| if (s.endsWith(";")) { //$NON-NLS-1$ |
| s = s.substring(0, s.length() - 1); |
| } |
| try { |
| return Integer.parseInt(s); |
| } catch (Exception ex) { |
| // ignore |
| } |
| } |
| return -1; |
| } |
| } |
| |
| // FIXME: will it better to use Character instead of Integer ? (yang) |