1 16 package com.google.gwt.util.tools; 17 18 import java.io.ByteArrayOutputStream ; 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.FileNotFoundException ; 22 import java.io.FileWriter ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import java.io.RandomAccessFile ; 27 import java.io.Reader ; 28 import java.io.Writer ; 29 import java.net.URI ; 30 import java.net.URL ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.Set ; 34 35 38 public final class Utility { 39 40 private static String sDevJarName = null; 41 private static String sInstallPath = null; 42 43 47 public static void close(InputStream is) { 48 try { 49 if (is != null) { 50 is.close(); 51 } 52 } catch (IOException e) { 53 } 54 } 55 56 60 public static void close(OutputStream os) { 61 try { 62 if (os != null) { 63 os.close(); 64 } 65 66 } catch (IOException e) { 67 } 68 } 69 70 74 public static void close(RandomAccessFile f) { 75 if (f != null) { 76 try { 77 f.close(); 78 } catch (IOException e) { 79 } 80 } 81 } 82 83 87 public static void close(Reader reader) { 88 try { 89 if (reader != null) { 90 reader.close(); 91 } 92 } catch (IOException e) { 93 } 94 } 95 96 100 public static void close(Writer writer) { 101 try { 102 if (writer != null) { 103 writer.close(); 104 } 105 } catch (IOException e) { 106 } 107 } 108 109 117 public static File createNormalFile(File parent, String fileName, 118 boolean overwrite, boolean ignore) throws IOException { 119 File file = new File (parent, fileName); 120 if (file.createNewFile()) { 121 System.out.println("Created file " + file); 122 return file; 123 } 124 125 if (!file.exists() || file.isDirectory()) { 126 throw new IOException (file.getPath() + " : could not create normal file."); 127 } 128 129 if (ignore) { 130 System.out.println(file + " already exists; skipping"); 131 return null; 132 } 133 134 if (!overwrite) { 135 throw new IOException ( 136 file.getPath() 137 + " : already exists; please remove it or use the -overwrite or -ignore option."); 138 } 139 140 System.out.println("Overwriting existing file " + file); 141 return file; 142 } 143 144 public static String getDevJarName() { 145 if (sDevJarName == null) { 146 computeInstallationPath(); 147 } 148 return sDevJarName; 149 } 150 151 158 public static File getDirectory(File parent, String dirName, boolean create) 159 throws IOException { 160 File dir = new File (parent, dirName); 161 boolean alreadyExisted = dir.exists(); 162 163 if (create) { 164 dir.mkdirs(); 165 } 166 167 if (!dir.exists() || !dir.isDirectory()) { 168 if (create) { 169 throw new IOException (dir.getPath() + " : could not create directory."); 170 } else { 171 throw new IOException (dir.getPath() + " : could not find directory."); 172 } 173 } 174 175 if (create && !alreadyExisted) { 176 System.out.println("Created directory " + dir); 177 } 178 179 return dir; 180 } 181 182 188 public static File getDirectory(String dirPath, boolean create) 189 throws IOException { 190 return getDirectory(null, dirPath, create); 191 } 192 193 202 public static String getFileFromClassPath(String partialPath) 203 throws IOException { 204 InputStream in = Utility.class.getClassLoader().getResourceAsStream( 205 partialPath); 206 try { 207 if (in == null) { 208 throw new FileNotFoundException (partialPath); 209 } 210 ByteArrayOutputStream os = new ByteArrayOutputStream (); 211 int ch; 212 while ((ch = in.read()) != -1) { 213 os.write(ch); 214 } 215 return new String (os.toByteArray(), "UTF-8"); 216 } finally { 217 close(in); 218 } 219 } 220 221 public static String getInstallPath() { 222 if (sInstallPath == null) { 223 computeInstallationPath(); 224 } 225 return sInstallPath; 226 } 227 228 public static void streamOut(File file, OutputStream out, int bufferSize) 229 throws IOException { 230 FileInputStream fis = null; 231 try { 232 fis = new FileInputStream (file); 233 streamOut(fis, out, bufferSize); 234 } finally { 235 com.google.gwt.util.tools.Utility.close(fis); 236 } 237 } 238 239 public static void streamOut(InputStream in, OutputStream out, int bufferSize) 240 throws IOException { 241 assert (bufferSize >= 0); 242 243 byte[] buffer = new byte[bufferSize]; 244 int bytesRead = 0; 245 while (true) { 246 bytesRead = in.read(buffer); 247 if (bytesRead >= 0) { 248 out.write(buffer, 0, bytesRead); 250 } else { 251 return; 253 } 254 } 255 } 256 257 public static void writeTemplateFile(File file, String contents, 258 Map replacements) throws IOException { 259 260 String replacedContents = contents; 261 Set entries = replacements.entrySet(); 262 for (Iterator iter = entries.iterator(); iter.hasNext();) { 263 Map.Entry entry = (Map.Entry ) iter.next(); 264 String replaceThis = (String ) entry.getKey(); 265 String withThis = (String ) entry.getValue(); 266 withThis = withThis.replaceAll("\\\\", "\\\\\\\\"); 267 withThis = withThis.replaceAll("\\$", "\\\\\\$"); 268 replacedContents = replacedContents.replaceAll(replaceThis, withThis); 269 } 270 271 FileWriter fw = new FileWriter (file); 272 fw.write(replacedContents); 273 close(fw); 274 } 275 276 private static void computeInstallationPath() { 277 try { 278 String override = System.getProperty("gwt.devjar"); 279 if (override == null) { 280 String partialPath = Utility.class.getName().replace('.', '/').concat( 281 ".class"); 282 URL url = Utility.class.getClassLoader().getResource(partialPath); 283 if (url != null && "jar".equals(url.getProtocol())) { 284 String path = url.toString(); 285 String jarPath = path.substring(path.indexOf("file:"), 286 path.lastIndexOf('!')); 287 File devJarFile = new File (URI.create(jarPath)); 288 if (!devJarFile.isFile()) { 289 throw new IOException ("Could not find jar file; " 290 + devJarFile.getCanonicalPath() 291 + " does not appear to be a valid file"); 292 } 293 sDevJarName = devJarFile.getName(); 294 295 String dirPath = jarPath.substring(0, jarPath.lastIndexOf('/') + 1); 296 File installDirFile = new File (URI.create(dirPath)); 297 if (!installDirFile.isDirectory()) { 298 throw new IOException ("Could not find installation directory; " 299 + installDirFile.getCanonicalPath() 300 + " does not appear to be a valid directory"); 301 } 302 303 sInstallPath = installDirFile.getCanonicalPath().replace( 304 File.separatorChar, '/'); 305 } else { 306 throw new IOException ( 307 "Cannot determine installation directory; apparently not running from a jar"); 308 } 309 } else { 310 override = override.replace('\\', '/'); 311 int pos = override.lastIndexOf('/'); 312 if (pos < 0) { 313 sInstallPath = ""; 314 sDevJarName = override; 315 } else { 316 sInstallPath = override.substring(0, pos); 317 sDevJarName = override.substring(pos + 1); 318 } 319 } 320 } catch (IOException e) { 321 throw new RuntimeException ( 322 "Installation problem detected, please reinstall GWT", e); 323 } 324 } 325 326 } 327 | Popular Tags |