Home | History | Annotate | Download | only in util
      1 package org.testng.util;
      2 
      3 import org.testng.collections.Lists;
      4 import org.testng.collections.Maps;
      5 
      6 import java.util.List;
      7 import java.util.Map;
      8 
      9 public class Strings {
     10   public static boolean isNullOrEmpty(String string) {
     11     return string == null || string.length() == 0; // string.isEmpty() in Java 6
     12   }
     13 
     14   private static List<String> ESCAPE_HTML_LIST = Lists.newArrayList(
     15     "&", "&amp;",
     16     "<", "&lt;",
     17     ">", "&gt;"
     18   );
     19 
     20   private static final Map<String, String> ESCAPE_HTML_MAP = Maps.newLinkedHashMap();
     21 
     22   static {
     23     for (int i = 0; i < ESCAPE_HTML_LIST.size(); i += 2) {
     24       ESCAPE_HTML_MAP.put(ESCAPE_HTML_LIST.get(i), ESCAPE_HTML_LIST.get(i + 1));
     25     }
     26   }
     27 
     28   public static String escapeHtml(String text) {
     29     String result = text;
     30     for (Map.Entry<String, String> entry : ESCAPE_HTML_MAP.entrySet()) {
     31       result = result.replace(entry.getKey(), entry.getValue());
     32     }
     33     return result;
     34   }
     35 
     36   public static void main(String[] args) {
     37     System.out.println(escapeHtml("10 < 20 && 30 > 20"));
     38   }
     39 }
     40