View Javadoc

    1   package sharin.util;
    2
    3   import java.io.UnsupportedEncodingException;
    4   import java.net.URLDecoder;
    5   import java.net.URLEncoder;
    6
    7   public class StringUtils {
    8
    9       public static String decodeUrl(String s, String enc) {
    10
    11          try {
    12              return URLDecoder.decode(s, enc);
    13
    14          } catch (UnsupportedEncodingException e) {
    15              throw new RuntimeException(e);
    16          }
    17      }
    18
    19      public static String encodeUrl(String s, String enc) {
    20
    21          try {
    22              return URLEncoder.encode(s, enc);
    23
    24          } catch (UnsupportedEncodingException e) {
    25              throw new RuntimeException(e);
    26          }
    27      }
    28
    29      public static String escapeXml(String s) {
    30          StringBuilder builder = new StringBuilder();
    31
    32          for (int i = 0; i < s.length(); i++) {
    33              char ch = s.charAt(i);
    34
    35              switch (ch) {
    36              case '"':
    37                  builder.append("&quot;");
    38                  break;
    39
    40              case '&':
    41                  builder.append("&amp;");
    42                  break;
    43
    44              case '<':
    45                  builder.append("&lt;");
    46                  break;
    47
    48              case '>':
    49                  builder.append("&gt;");
    50                  break;
    51
    52              case '\'':
    53                  builder.append("&apos;");
    54                  break;
    55
    56              default:
    57                  builder.append(ch);
    58                  break;
    59              }
    60          }
    61
    62          return builder.toString();
    63      }
    64  }