Java examples for java.lang:String HTML
Turn any HTML escape entities in the string into characters and return the resulting string.
/*/*from w w w . j a v a2s . c om*/ * Static String formatting and query routines. * Copyright (C) 2001-2005 Stephen Ostermiller * http://ostermiller.org/contact.pl?regarding=Java+Utilities * * This program 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. * * This program 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. See the * GNU General Public License for more details. * * See COPYING.TXT for details. */ //package com.java2s; import java.util.HashMap; public class Main { public static void main(String[] argv) { String s = "java2s.com"; System.out.println(unescapeHTML(s)); } private static HashMap<String, Integer> htmlEntities = new HashMap<String, Integer>(); /** * Turn any HTML escape entities in the string into * characters and return the resulting string. * * @param s String to be unescaped. * @return unescaped String. * @throws NullPointerException if s is null. * * @since 1.00.00 */ public static String unescapeHTML(String s) { StringBuffer result = new StringBuffer(s.length()); int ampInd = s.indexOf("&"); int lastEnd = 0; while (ampInd >= 0) { int nextAmp = s.indexOf("&", ampInd + 1); int nextSemi = s.indexOf(";", ampInd + 1); if (nextSemi != -1 && (nextAmp == -1 || nextSemi < nextAmp)) { int value = -1; String escape = s.substring(ampInd + 1, nextSemi); try { if (escape.startsWith("#")) { value = Integer.parseInt(escape.substring(1), 10); } else { if (htmlEntities.containsKey(escape)) { value = ((Integer) (htmlEntities.get(escape))) .intValue(); } } } catch (NumberFormatException x) { } result.append(s.substring(lastEnd, ampInd)); lastEnd = nextSemi + 1; if (value >= 0 && value <= 0xffff) { result.append((char) value); } else { result.append("&").append(escape).append(";"); } } ampInd = nextAmp; } result.append(s.substring(lastEnd)); return result.toString(); } }