1 23 24 package org.dbforms.util; 25 26 import org.dbforms.util.external.PrintfFormat; 27 28 import java.io.UnsupportedEncodingException ; 29 30 import java.net.URLDecoder ; 31 import java.net.URLEncoder ; 32 33 import java.text.Format ; 34 35 import javax.servlet.http.HttpServletRequest ; 36 37 38 39 42 public class Util { 43 private static final String REALPATH = "$(SERVLETCONTEXT_REALPATH)"; 44 45 54 public static final boolean isNull(String s) { 55 return ((s == null) || (s.trim() 56 .length() == 0)); 57 } 58 59 60 68 public static final String decode(String s, 69 String enc) 70 throws UnsupportedEncodingException { 71 if (!Util.isNull(s)) { 72 try { 73 s = decCheck(s, enc); 74 } catch (NoSuchMethodError nsme) { 75 s = URLDecoder.decode(s); 76 } 77 } 78 79 return s; 80 } 81 82 83 95 public static final String encode(String s, 96 String enc) 97 throws UnsupportedEncodingException { 98 if (!Util.isNull(s)) { 99 try { 100 s = encCheck(s, enc); 101 } catch (NoSuchMethodError nsme) { 102 s = URLEncoder.encode(s); 103 } 104 } 105 106 return s; 107 } 108 109 110 121 public static final String replaceRealPath(String s, 122 String realpath) { 123 if (!isNull(s) && !isNull(realpath)) { 124 if (realpath.charAt(realpath.length() - 1) != '/') { 127 realpath = realpath + '/'; 128 } 129 130 int i = s.indexOf(REALPATH); 131 132 while (i >= 0) { 133 StringBuffer buf = new StringBuffer (); 134 buf.append(s.substring(0, i)); 135 buf.append(realpath); 136 buf.append(s.substring(i + REALPATH.length() + 1)); 137 s = buf.toString(); 138 i = s.indexOf(REALPATH); 139 } 140 } 141 142 return s; 143 } 144 145 146 153 public static boolean getFalse(String value) { 154 return !Util.getTrue(value); 155 } 156 157 158 165 public static String getPattern(Format f) { 166 if (f instanceof java.text.DecimalFormat ) { 167 return ((java.text.DecimalFormat ) f).toPattern(); 168 } else if (f instanceof java.text.SimpleDateFormat ) { 169 return ((java.text.SimpleDateFormat ) f).toPattern(); 170 } else { 171 return null; 172 } 173 } 174 175 176 183 public static boolean getTrue(String value) { 184 return "true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value); 185 } 186 187 188 198 public static String sprintf(String format, 199 Object [] o) { 200 PrintfFormat printfFormat = new PrintfFormat(format); 202 return printfFormat.sprintf(o); 206 } 207 208 209 225 private static final String decCheck(String s, 226 String enc) 227 throws UnsupportedEncodingException , 228 NoSuchMethodError { 229 if (isNull(enc)) { 230 enc = "UTF-8"; 231 } 232 233 return URLDecoder.decode(s, enc); 234 } 235 236 237 253 private static final String encCheck(String s, 254 String enc) 255 throws UnsupportedEncodingException , 256 NoSuchMethodError { 257 if (isNull(enc)) { 258 enc = "UTF-8"; 259 } 260 261 s = URLEncoder.encode(s, enc); 262 263 return s; 264 } 265 266 public static String getBaseURL(HttpServletRequest request) { 267 StringBuffer buf = new StringBuffer (); 268 buf.append(request.getScheme()); 269 buf.append("://"); 270 buf.append(request.getServerName()); 271 272 int port = request.getServerPort(); 273 if((port!=80) && (port !=443)) { 274 buf.append(":"); 275 buf.append(String.valueOf(port)); 276 } 277 buf.append(request.getContextPath()); 278 buf.append("/"); 279 return buf.toString(); 280 } 281 282 public static String getRequestURL(HttpServletRequest request) { 283 StringBuffer buf = new StringBuffer (); 284 buf.append(request.getScheme()); 285 buf.append("://"); 286 buf.append(request.getServerName()); 287 int port = request.getServerPort(); 288 if ((port != 80) && (port != 443)) { 289 buf.append(":"); 290 buf.append(port); 291 } 292 buf.append(request.getRequestURI()); 293 String query = request.getQueryString(); 294 if (!isNull(query)) { 295 buf.append("?"); 296 buf.append(query); 297 } 298 return buf.toString(); 299 } 300 301 } 302 | Popular Tags |