1 package dinamica; 2 3 import java.io.BufferedReader ; 4 import java.io.File ; 5 import java.io.FileOutputStream ; 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.io.InputStreamReader ; 9 import java.io.PrintWriter ; 10 import java.text.DecimalFormat ; 11 import java.text.NumberFormat ; 12 import java.text.SimpleDateFormat ; 13 import java.util.Locale ; 14 import java.net.*; 15 16 17 29 public class StringUtil 30 { 31 32 40 public static String replace(String source, String pattern, String newText) 41 { 42 43 if (pattern == null || pattern.length() == 0 ) 44 return source; 45 46 StringBuffer buf = new StringBuffer (2*source.length()); 47 48 int previndex=0; 49 int index=0; 50 int flen = pattern.length(); 51 while (true) { 52 index = source.indexOf(pattern, previndex); 53 if (index == -1) { 54 buf.append(source.substring(previndex)); 55 break; 56 } 57 buf.append( source.substring(previndex, index)).append( newText ); 58 previndex = index + flen; 59 } 60 return buf.toString(); 61 62 } 63 64 71 public static String formatDate(java.util.Date d, String format) throws Throwable 72 { 73 SimpleDateFormat f = new SimpleDateFormat (); 74 f.applyPattern(format); 75 return f.format(d); 76 } 77 78 86 public static String formatDate(java.util.Date d, String format, Locale loc) throws Throwable 87 { 88 SimpleDateFormat f = new SimpleDateFormat (format, loc); 89 return f.format(d); 90 } 91 92 106 public static java.util.Date getDateObject(String dateValue, String dateFormat) throws Throwable 107 { 108 SimpleDateFormat x = new SimpleDateFormat (dateFormat); 109 x.setLenient(false); 110 return x.parse(dateValue); 111 } 112 113 120 public static String formatNumber(Object value, String numberFormat) throws Throwable 121 { 122 DecimalFormat fmt = (DecimalFormat ) NumberFormat.getInstance(); 123 fmt.applyPattern(numberFormat); 124 return fmt.format(value); 125 } 126 127 135 public static String formatNumber(Object value, String numberFormat, Locale loc) throws Throwable 136 { 137 DecimalFormat fmt = (DecimalFormat ) NumberFormat.getInstance(loc); 138 fmt.applyPattern(numberFormat); 139 return fmt.format(value); 140 } 141 142 149 public static String [] split(String s, String separator) 150 { 151 separator = "\\" + separator; 152 return s.split(separator); 153 } 154 155 161 public static String getResource(javax.servlet.ServletContext ctx, String path) throws Throwable 162 { 163 return getResource(ctx, path, System.getProperty("file.encoding", "ISO8859_1")); 164 } 165 166 172 public static synchronized void saveMessage(String path, String message) 173 { 174 175 FileOutputStream fos = null; 176 PrintWriter pw = null; 177 178 try { 179 fos = new FileOutputStream (new File (path), true); 180 pw = new PrintWriter (fos, false); 181 pw.println(message); 182 } 183 catch (IOException e) { 184 185 try 186 { 187 String d = StringUtil.formatDate(new java.util.Date (), "yyyy-MM-dd HH:mm:ss"); 188 System.err.println("ERROR [dinamica.StringUtil.saveMessage@" + d + "]: " + e.getMessage()); 189 } 190 catch (Throwable e1) 191 { 192 } 193 } 194 finally { 195 196 try { 197 if (pw != null) 198 pw.close(); 199 if (fos != null) 200 fos.close(); 201 } 202 catch (IOException e) { 203 } 204 205 } 206 } 207 208 217 public static String httpGet(String url, boolean logStdout) throws Throwable 218 { 219 220 final int bufferSize = 4096; 221 BufferedReader br = null; 222 HttpURLConnection urlc = null; 223 StringBuffer buffer = new StringBuffer (); 224 URL page = new URL(url); 225 226 try 227 { 228 229 if (logStdout) 230 System.err.println("Waiting for reply...:" + url); 231 232 urlc = (HttpURLConnection)page.openConnection(); 233 urlc.setUseCaches(false); 234 235 if (logStdout) 236 { 237 System.err.println("Content-type = " + urlc.getContentType()); 238 System.err.println("Content-length = " + urlc.getContentLength()); 239 System.err.println("Response-code = " + urlc.getResponseCode()); 240 System.err.println("Response-message = " + urlc.getResponseMessage()); 241 } 242 243 int retCode = urlc.getResponseCode(); 244 String retMsg = urlc.getResponseMessage(); 245 if (retCode>=400) 246 throw new Throwable ("HTTP Error: " + retCode + " - " + retMsg + " - URL:" + url); 247 248 br = new BufferedReader (new InputStreamReader (urlc.getInputStream()), bufferSize); 249 char buf[] = new char[bufferSize]; 250 int bytesRead = 0; 251 252 while (bytesRead!=-1) 253 { 254 bytesRead = br.read(buf); 255 if (bytesRead>0) 256 buffer.append(buf,0,bytesRead); 257 } 258 259 if (logStdout) 260 { 261 System.err.println("Document received."); 262 } 263 264 return buffer.toString(); 265 } 266 catch (Throwable e) 267 { 268 throw e; 269 } 270 finally 271 { 272 if (br != null) 273 br.close(); 274 275 if (urlc!=null) 276 urlc.disconnect(); 277 } 278 279 } 280 281 291 public static String getResource(javax.servlet.ServletContext ctx, String path, String encoding) throws Throwable 292 { 293 294 StringBuffer buf = new StringBuffer (5000); 295 byte[] data = new byte[5000]; 296 297 InputStream in = null; 298 299 in = ctx.getResourceAsStream(path); 300 301 try 302 { 303 if (in!=null) 304 { 305 while (true) 306 { 307 int len = in.read(data); 308 if (len!=-1) 309 { 310 buf.append( new String (data, 0, len, encoding) ); 311 } 312 else 313 { 314 break; 315 } 316 } 317 318 return buf.toString(); 319 320 } 321 else 322 { 323 throw new Throwable ("Invalid path to resource: " + path); 324 } 325 326 } 327 catch (Throwable e) 328 { 329 throw e; 330 } 331 finally 332 { 333 if (in!=null) 334 { 335 try{ 336 in.close(); 337 } catch (Exception e){} 338 } 339 } 340 341 } 342 343 } 344 | Popular Tags |