package basics.cleanser;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import basics.testing.Test;

public class HtmlCleanser implements Cleanser
{
// | Pattern.MULTILINE
final static Pattern comments1 = Pattern.compile("<!--.*-->", Pattern.CASE_INSENSITIVE
                                  | Pattern.DOTALL);
//final static Pattern comments2 = Pattern.compile("/\\*(.*)\\*/", Pattern.DOTALL);
final static Pattern cdata     = Pattern.compile("<!\\[CDATA\\[.*\\]\\]>", Pattern.CASE_INSENSITIVE
                                  | Pattern.DOTALL);

public String text(String input)
{
   if(input == null)
      return "";
   return removeCData(removeComments(input));
}

public static String removeComments(String code)
{
   code = replace(code, comments1, "");
   //   code = replace(code, comments2, "");
   return code;
}

public static String removeCData(String code)
{
   return replace(code, cdata, "");
}

private static String replace(String input, Pattern p, String r)
{
   Matcher matcher = p.matcher(input);
   return matcher.replaceAll(r);
}
}
