Java HTML Decode htmlDecode(String s)

Here you can find the source of htmlDecode(String s)

Description

html Decode

License

Open Source License

Return

decoded text, ready to be printed as text replaces &, ", ', <, > and all whitespace

Declaration

public static String htmlDecode(String s) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 BSI Business Systems Integration AG.
 * 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://from  w  ww. jav  a  2  s  . c o  m
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

public class Main {
    /**
     * @return decoded text, ready to be printed as text
     *         <xmp>replaces &, ", ', <, > and all whitespace</xmp>
     */
    public static String htmlDecode(String s) {
        if (s == null || s.length() == 0) {
            return s;
        }

        s = s.replace("&nbsp;", " ");
        s = s.replace("&quot;", "\"");
        s = s.replace("&apos;", "'");
        s = s.replace("&#39;", "'");
        s = s.replace("&lt;", "<");
        s = s.replace("&gt;", ">");
        s = s.replace("&amp;", "&");

        // whitespace patterns
        String zeroOrMoreWhitespaces = "\\s*?";
        String oneOrMoreWhitespaces = "\\s+?";

        // replace <br/> by \n
        s = s.replaceAll(
                "<" + zeroOrMoreWhitespaces + "br" + zeroOrMoreWhitespaces + "/" + zeroOrMoreWhitespaces + ">",
                "\n");
        // replace HTML-tabs by \t
        s = s.replaceAll("<" + zeroOrMoreWhitespaces + "span" + oneOrMoreWhitespaces + "style"
                + zeroOrMoreWhitespaces + "=" + zeroOrMoreWhitespaces + "\"white-space:pre\""
                + zeroOrMoreWhitespaces + ">&#9;<" + zeroOrMoreWhitespaces + "/" + zeroOrMoreWhitespaces + "span"
                + zeroOrMoreWhitespaces + ">", "\t");

        return s;
    }

    public static int length(String s) {
        if (s == null) {
            return 0;
        } else {
            return s.length();
        }
    }

    /**
     * replace plain text without using regex
     */
    public static String replace(String s, String sOld, String sNew) {
        sNew = (sNew == null ? "" : sNew);
        if (s == null || sOld == null) {
            return s;
        }
        return s.replace(sOld, sNew);
    }
}

Related

  1. HTMLDecode(String s)
  2. htmlDecode(String strSrc)
  3. htmlDecoder(String content)
  4. htmlEntityDecode(String s)
  5. htmlEntityDecodeSingle(String s)