1 25 package org.jresearch.gossip.util; 26 27 import java.io.UnsupportedEncodingException ; 28 import java.net.URLEncoder ; 29 import java.security.AccessController ; 30 31 import sun.security.action.GetPropertyAction; 32 33 39 public class HtmlCodec { 40 43 public static char[] defSpecSymbols = new char[] { '"', '\'', '\\', ',', 44 '<', '>', '(', ')', '.' }; 45 46 57 public static String encode(final String text) { 58 return encode(text, defSpecSymbols); 59 } 60 61 76 public static String encode(final String text, final String specSymbols) { 77 final char[] cSpecSymbols; 78 79 if ((specSymbols == null) || (specSymbols.length() == 0)) { 80 cSpecSymbols = defSpecSymbols; 81 } else { 82 cSpecSymbols = specSymbols.toCharArray(); 83 } 84 85 return encode(text, cSpecSymbols); 86 } 87 88 104 public static String encode(final String text, char[] specSymbols) { 105 if (text == null) { 106 return null; 107 } 108 109 if ((specSymbols == null) || (specSymbols.length == 0)) { 110 specSymbols = defSpecSymbols; 111 } 112 113 StringBuffer buffer = new StringBuffer (text.length()); 114 char[] cText = text.toCharArray(); 115 116 for (int i = 0; i < cText.length; i++) { 117 char cTextChar = cText[i]; boolean isSpecial = false; 119 120 for (int ss = 0; ss < specSymbols.length; ss++) { 121 if (specSymbols[ss] == cTextChar) { 122 isSpecial = true; 123 buffer.append('&'); 124 buffer.append('#'); 125 buffer.append((int) cTextChar); 126 buffer.append(';'); 127 128 break; } 130 } 131 132 if (!isSpecial) { 133 buffer.append(cTextChar); 134 } 135 } 136 137 return buffer.toString(); 138 } 139 140 148 public static String trim(String string) { 149 return string.replaceAll(" ", " ").replaceAll(" ", " ") 150 .trim(); 151 } 152 153 165 public static String encodeURL(final String url) { 166 try { 167 return URLEncoder.encode(url, (String ) AccessController 168 .doPrivileged(new GetPropertyAction("file.encoding"))); 169 } catch (UnsupportedEncodingException e) { 170 e.printStackTrace(); 171 172 return url; 173 } 174 } 175 } 176 | Popular Tags |