Java HTML Filter filterHTML(String input)

Here you can find the source of filterHTML(String input)

Description

filter HTML

License

Open Source License

Declaration

public static String filterHTML(String input) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String filterHTML(String input) {
        String result = (input == null ? "" : input);
        if (input != null && input.length() > 0) {
            StringBuffer filtered = new StringBuffer(input.length());
            char c;
            for (int i = 0; i < input.length(); i++) {
                c = input.charAt(i);// w  w  w .  j a v  a2  s  .co m
                String s = null;
                switch (c) {
                case '<':
                    s = "&lt;";
                    break;
                case '>':
                    s = "&gt;";
                    break;
                case ' ':
                    try {
                        if (input.charAt(i + 1) == ' ')
                            s = "&nbsp;";
                    } catch (Exception e) {
                    }
                    //s="&nbsp;";
                    break;
                /*               case '"':
                              s="&quot;";
                              break;
                           case '&':
                              s="&amp;";
                              break;*/
                }
                if (s != null)
                    filtered.append(s);
                else
                    filtered.append(c);
            }
            result = filtered.substring(0);
            //System.out.println("filterHTML-> input="+input+"  result="+result);
        }
        return result;
    }
}

Related

  1. filterHtml(String body)
  2. filterHTML(String input)
  3. filterHtml(String input)
  4. filterHtml(String s)
  5. filterHTML(String s)