1 package org.mvnforum.util; 2 import java.sql.Timestamp ; 3 import java.text.SimpleDateFormat ; 4 import java.util.Date ; 5 import java.util.Hashtable ; 6 import net.myvietnam.mvncore.filter.DisableHtmlTagFilter; 7 8 58 61 public class Utils { 62 63 static SimpleDateFormat datetimefmt = new SimpleDateFormat ("yyyy-MM-dd kk:mm:ss"); 64 65 static SimpleDateFormat datefmt = new SimpleDateFormat ("yyyy-MM-dd"); 66 67 static Hashtable str2hex = new Hashtable (); 68 static { 69 str2hex.put("0", new Integer (0)); 70 str2hex.put("1", new Integer (1)); 71 str2hex.put("2", new Integer (2)); 72 str2hex.put("3", new Integer (3)); 73 str2hex.put("4", new Integer (4)); 74 str2hex.put("5", new Integer (5)); 75 str2hex.put("6", new Integer (6)); 76 str2hex.put("7", new Integer (7)); 77 str2hex.put("8", new Integer (8)); 78 str2hex.put("9", new Integer (9)); 79 str2hex.put("A", new Integer (10)); 80 str2hex.put("B", new Integer (11)); 81 str2hex.put("C", new Integer (12)); 82 str2hex.put("D", new Integer (13)); 83 str2hex.put("E", new Integer (14)); 84 str2hex.put("F", new Integer (15)); 85 str2hex.put("a", new Integer (10)); 86 str2hex.put("b", new Integer (11)); 87 str2hex.put("c", new Integer (12)); 88 str2hex.put("d", new Integer (13)); 89 str2hex.put("e", new Integer (14)); 90 str2hex.put("f", new Integer (15)); 91 } 92 93 108 109 public static String wrapit(String text) { 110 StringBuffer strbuf = new StringBuffer (); 111 for (int i = 0; i < text.length(); i++) { 113 if (text.charAt(i) == '\'') 114 strbuf.append("\'\'"); 115 else 116 strbuf.append(text.charAt(i)); 117 } 118 120 String result = strbuf.toString(); 121 result = DisableHtmlTagFilter.filter(result); 122 result = result.replaceAll("(.*):\\)(.*)", "$1\\[:\\)\\]$2"); 125 result = result.replaceAll("(.*):\\((.*)", "$1\\[:\\(\\]$2"); 127 result = result.replaceAll("(.*):D(.*)", "$1\\[:D\\]$2"); 129 result = result.replaceAll("(.*):lol:(.*)", "$1\\[:\\)\\)\\]$2"); 131 result = result.replaceAll("(.*):cry:(.*)", "$1\\[:\\(\\(\\]$2"); 133 result = result.replaceAll("(.*):wink:(.*)", "$1\\[;\\)\\]$2"); 135 result = result.replaceAll("(.*):oops:(.*)", "$1\\[:\">\\]$2"); 137 result = result.replaceAll("(.*):p(.*)", "$1\\[:p\\]$2"); 139 result = result.replaceAll("(.*)8\\)(.*)", "$1\\[B-\\)\\]$2"); 141 result = result.replaceAll("(.*):\\?(.*)", "$1\\[:-/\\]$2"); 143 result = result.replaceAll("(.*):shock:(.*)", "$1\\[:O\\]$2"); 145 result = result.replaceAll("(.*):evil:(.*)", "$1\\[>:\\)\\]$2"); 147 148 result = result.replaceAll("(.*)\\[url\\](.*)\\[/url\\](.*)", "$1\\[url=$2\\]$2\\[/url\\]$3"); 151 result = result.replaceAll("(.*)\\[b:[0-9,abcdef]*\\](.*)\\[/b:[0-9,abcdef]*\\](.*)", "$1\\[b\\]$2\\[/b\\]$3"); 153 result = result.replaceAll("(.*)\\[u:[0-9,abcdef]*\\](.*)\\[/u:[0-9,abcdef]*\\](.*)", "$1\\[u\\]$2\\[/u\\]$3"); 155 result = result.replaceAll("(.*)\\[i:[0-9,abcdef]*\\](.*)\\[/i:[0-9,abcdef]*\\](.*)", "$1\\[i\\]$2\\[/i\\]$3"); 157 result = result.replaceAll("(.*)\\[color=([a-z]*):[0-9,abcdef]*\\](.*)\\[/color:[0-9,abcdef]*\\](.*)", 159 "$1\\[color=$2\\]$3\\[/color\\]$4"); 160 result = result.replaceAll("(.*)\\[quote:[0-9,abcdef]*\\](.*)\\[/quote:[0-9,abcdef]*\\](.*)", "$1\\[quote=\\]$2\\[/quote\\]$3"); 162 result = result.replaceAll("(.*)\\[quote:[0-9,abcdef]*=\\\"(.*)\\\"\\](.*)\\[/quote:[0-9,abcdef]*\\](.*)", "$1\\[quote=\\\"$2\\\"\\]$3\\[/quote\\]$4"); 164 return result; 165 } 166 167 public static String DateTimeFromS(long s) { 168 Date d = new Date (s * 1000); 169 return datetimefmt.format(d); 170 } 171 172 public static String DateFromS(long s) { 173 Date d = new Date (s * 1000); 174 return datefmt.format(d); 175 } 176 177 public static String HexIPtoString(String hexrep) { 178 if (hexrep.length() < 8) 179 return "0.0.0.0"; 180 181 byte[] ipaddr = new byte[8]; 182 for (int i = 0; i < ipaddr.length; i++) { 183 String letter = "" + hexrep.charAt(i); 184 int val = ((Integer ) str2hex.get(letter)).intValue(); 185 ipaddr[i] = (byte) val; 186 } 187 188 String strrep = ""; 189 int A = 0; 190 A |= ipaddr[0]; 191 A = A << 4; 192 A |= ipaddr[1]; 193 int B = 0; 194 B |= ipaddr[2]; 195 B = B << 4; 196 B |= ipaddr[3]; 197 int C = 0; 198 C |= ipaddr[4]; 199 C = C << 4; 200 C |= ipaddr[5]; 201 int D = 0; 202 D |= ipaddr[6]; 203 D = D << 4; 204 D |= ipaddr[7]; 205 206 return "" + A + "." + B + "." + C + "." + D; 207 } 208 209 public static String stripPHPBBQuotes(String in) { 210 boolean err = false; 211 StringBuffer endstr = new StringBuffer (); 212 213 do { 214 int firstquote = in.indexOf("[quote:"); 215 if (firstquote == -1) { 216 err = true; 217 break; 218 } 219 220 int firstclosebrace = in.indexOf(']', firstquote); 221 if (firstclosebrace == -1) { 222 err = true; 223 break; 224 } 225 226 endstr.append(in.substring(0, firstquote)); 227 endstr.append("[quote]"); 228 229 int endquote = in.indexOf("[/quote:"); 230 if (endquote == -1) { 231 err = true; 232 break; 233 } 234 235 int endclosebrace = in.indexOf(']', endquote); 236 if (endclosebrace == -1) { 237 err = true; 238 break; 239 } 240 241 endstr.append(in.substring(firstclosebrace + 1, endquote)); 242 endstr.append("[/quote]"); 243 endstr.append(in.substring(endclosebrace + 1)); 244 } while (false); 245 246 if (err) 247 return in; 248 else 249 return endstr.toString(); 250 } 251 252 public static String getFormatDate (Date date) { 253 return datefmt.format(date); 254 } 255 256 public static Timestamp getTimeStamp (long data) { 257 return new Timestamp (data*1000); 258 } 259 260 public static void main(String [] args) { 261 String phpstr = " BOXO[quote:2845990e65=\"suganthan\"]Help me! This is the string[/quote:2845990e65]"; 262 263 System.out.println(Utils.stripPHPBBQuotes(phpstr)); 264 } 265 } 266 | Popular Tags |