1 2 34 35 package com.icesoft.jsfmeta.util; 36 37 import java.io.File ; 38 import java.io.FileNotFoundException ; 39 import java.net.URL ; 40 41 public class GeneratorUtil { 42 43 private static String WORKING_FOLDER; 44 45 public GeneratorUtil() { 46 } 47 48 static{ 49 String result = "."; 50 try { 51 ClassLoader classLoader = Thread.currentThread() 52 .getContextClassLoader(); 53 URL localUrl = classLoader.getResource("."); 54 if(localUrl != null){ 55 result = convertFileUrlToPath(localUrl); 56 } 57 58 } catch (Exception ex) { 59 ex.printStackTrace(); 60 } 61 WORKING_FOLDER = result; 62 } 63 64 public static String getWorkingFolder(){ 65 return WORKING_FOLDER; 66 } 67 68 public static File getDestFolder(String path) throws FileNotFoundException { 69 File file = new File (path); 70 if(!file.exists()){ 71 if(!file.mkdirs()){ 72 throw new FileNotFoundException (file.getPath()); 73 } 74 } 75 76 return file; 77 } 78 79 86 private static String convertFileUrlToPath(URL url) { 87 String path = url.getPath(); 88 if( url.toExternalForm().startsWith("file:") ) { 89 StringBuffer sb = new StringBuffer ( path.length() ); 90 int pathLength = path.length(); 91 for(int i = 0; i < pathLength;) { 92 char c = path.charAt(i); 93 if( c == '%' ) { 94 if( (i+1) < pathLength && isHexDigit(path.charAt(i+1)) ) { 95 int increment = 2; 96 if( (i+2) < pathLength && isHexDigit(path.charAt(i+2)) ) 97 increment++; 98 try { 99 char unescaped = (char) Integer.parseInt( 100 path.substring(i+1, i+increment), 16); 101 102 sb.append( unescaped ); 103 i += increment; 104 continue; 105 } catch(NumberFormatException nfe) { 106 } 109 } 110 } 111 sb.append( c ); 112 i++; 113 } 114 path = sb.toString(); 115 } 116 return path; 117 } 118 119 private static boolean isHexDigit(char c) { 120 return ( (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ); 121 } 122 } | Popular Tags |