1 package org.herac.tuxguitar.gui.util; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.InputStream; 7 import java.io.InputStreamReader; 8 import java.net.URL; 9 import java.net.URLDecoder; 10 import java.util.ArrayList; 11 import java.util.Enumeration; 12 import java.util.List; 13 import java.util.Vector; 14 15 import org.eclipse.swt.graphics.Image; 16 import org.eclipse.swt.graphics.ImageData; 17 import org.herac.tuxguitar.gui.TuxGuitar; 18 import org.herac.tuxguitar.gui.system.config.TGConfigKeys; 19 import org.herac.tuxguitar.util.TGClassLoader; 20 import org.herac.tuxguitar.util.TGLibraryLoader; 21 import org.herac.tuxguitar.util.TGVersion; 22 23 public class TGFileUtils { 24 25 private static final String TG_CONFIG_PATH = "tuxguitar.config.path"; 26 private static final String TG_SHARE_PATH = "tuxguitar.share.path"; 27 private static final String TG_CLASS_PATH = "tuxguitar.class.path"; 28 private static final String TG_LIBRARY_PATH = "tuxguitar.library.path"; 29 private static final String TG_LIBRARY_PREFIX = "tuxguitar.library.prefix"; 30 private static final String TG_LIBRARY_EXTENSION = "tuxguitar.library.extension"; 31 32 public static final String PATH_USER_CONFIG = getUserConfigDir(); 33 public static final String PATH_USER_PLUGINS_CONFIG = getUserPluginsConfigDir(); 34 public static final String[] TG_STATIC_SHARED_PATHS = getStaticSharedPaths(); 35 getResourceAsStream(String resource)36 public static InputStream getResourceAsStream(String resource) { 37 try { 38 if(TG_STATIC_SHARED_PATHS != null){ 39 for( int i = 0; i < TG_STATIC_SHARED_PATHS.length ; i ++ ){ 40 File file = new File(TG_STATIC_SHARED_PATHS[i] + File.separator + resource); 41 if( file.exists() ){ 42 return new FileInputStream( file ); 43 } 44 } 45 } 46 return TGClassLoader.instance().getClassLoader().getResourceAsStream(resource); 47 }catch(Throwable throwable){ 48 throwable.printStackTrace(); 49 } 50 return null; 51 } 52 getResourceUrl(String resource)53 public static URL getResourceUrl(String resource) { 54 try { 55 if(TG_STATIC_SHARED_PATHS != null){ 56 for( int i = 0; i < TG_STATIC_SHARED_PATHS.length ; i ++ ){ 57 File file = new File(TG_STATIC_SHARED_PATHS[i] + File.separator + resource); 58 if( file.exists() ){ 59 return file.toURI().toURL(); 60 } 61 } 62 } 63 return TGClassLoader.instance().getClassLoader().getResource(resource); 64 }catch(Throwable throwable){ 65 throwable.printStackTrace(); 66 } 67 return null; 68 } 69 getResourceUrls(String resource)70 public static Enumeration getResourceUrls(String resource) { 71 try { 72 Vector vector = new Vector(); 73 if(TG_STATIC_SHARED_PATHS != null){ 74 for( int i = 0; i < TG_STATIC_SHARED_PATHS.length ; i ++ ){ 75 File file = new File(TG_STATIC_SHARED_PATHS[i] + File.separator + resource); 76 if( file.exists() ){ 77 vector.addElement( file.toURI().toURL() ); 78 } 79 } 80 } 81 Enumeration resources = TGClassLoader.instance().getClassLoader().getResources(resource); 82 while( resources.hasMoreElements() ){ 83 URL url = (URL)resources.nextElement(); 84 if( !vector.contains(url) ){ 85 vector.addElement( url ); 86 } 87 } 88 return vector.elements(); 89 }catch(Throwable throwable){ 90 throwable.printStackTrace(); 91 } 92 return null; 93 } 94 getResourcePath(String resource)95 private static String getResourcePath(String resource) { 96 try { 97 if(TG_STATIC_SHARED_PATHS != null){ 98 for( int i = 0; i < TG_STATIC_SHARED_PATHS.length ; i ++ ){ 99 File file = new File(TG_STATIC_SHARED_PATHS[i] + File.separator + resource); 100 if( file.exists() ){ 101 return file.getAbsolutePath() + File.separator; 102 } 103 } 104 } 105 URL url = TGClassLoader.instance().getClassLoader().getResource(resource); 106 if(url != null){ 107 return new File(URLDecoder.decode(url.getPath(), "UTF-8")).getAbsolutePath() + File.separator; 108 } 109 }catch(Throwable throwable){ 110 throwable.printStackTrace(); 111 } 112 return null; 113 } 114 loadClasspath()115 public static void loadClasspath(){ 116 String plugins = getResourcePath("plugins"); 117 if(plugins != null){ 118 TGClassLoader.instance().addPaths(new File(plugins)); 119 } 120 121 String custompath = System.getProperty(TG_CLASS_PATH); 122 if(custompath != null){ 123 String[] paths = custompath.split(File.pathSeparator); 124 for(int i = 0; i < paths.length; i++){ 125 TGClassLoader.instance().addPaths(new File(paths[i])); 126 } 127 } 128 } 129 loadLibraries()130 public static void loadLibraries(){ 131 String libraryPath = System.getProperty(TG_LIBRARY_PATH); 132 if(libraryPath != null){ 133 String[] libraryPaths = libraryPath.split(File.pathSeparator); 134 String libraryPrefix = System.getProperty(TG_LIBRARY_PREFIX); 135 String libraryExtension = System.getProperty(TG_LIBRARY_EXTENSION); 136 for(int i = 0; i < libraryPaths.length; i++){ 137 TGLibraryLoader.instance().loadLibraries(new File(libraryPaths[i]),libraryPrefix,libraryExtension); 138 } 139 } 140 } 141 getFileNames( String resource )142 public static String[] getFileNames( String resource ){ 143 try { 144 String path = getResourcePath(resource); 145 if( path != null ){ 146 File file = new File( path ); 147 if(file.exists() && file.isDirectory()){ 148 return file.list(); 149 } 150 } 151 InputStream stream = getResourceAsStream(resource + "/list.properties" ); 152 if( stream != null ){ 153 BufferedReader reader = new BufferedReader( new InputStreamReader(stream) ); 154 List fileNameList = new ArrayList(); 155 String fileName = null; 156 while( (fileName = reader.readLine()) != null ){ 157 fileNameList.add( fileName ); 158 } 159 String[] fileNames = new String[ fileNameList.size() ]; 160 for (int i = 0 ; i < fileNames.length ; i ++ ){ 161 fileNames[ i ] = (String)fileNameList.get( i ); 162 } 163 return fileNames; 164 } 165 }catch(Throwable throwable){ 166 throwable.printStackTrace(); 167 } 168 return null; 169 } 170 loadImage(String name)171 public static Image loadImage(String name){ 172 return loadImage(TuxGuitar.instance().getConfig().getStringConfigValue(TGConfigKeys.SKIN),name); 173 } 174 loadImage(String skin,String name)175 public static Image loadImage(String skin,String name){ 176 try{ 177 InputStream stream = getResourceAsStream("skins/" + skin + "/" + name); 178 if(stream != null){ 179 return new Image(TuxGuitar.instance().getDisplay(),new ImageData(stream)); 180 } 181 System.err.println(name + ": not found"); 182 }catch(Throwable throwable){ 183 throwable.printStackTrace(); 184 } 185 return new Image(TuxGuitar.instance().getDisplay(),16,16); 186 } 187 isLocalFile(URL url)188 public static boolean isLocalFile(URL url){ 189 try { 190 if(url.getProtocol().equals( new File(url.getFile()).toURI().toURL().getProtocol() ) ){ 191 return true; 192 } 193 }catch(Throwable throwable){ 194 throwable.printStackTrace(); 195 } 196 return false; 197 } 198 getUserConfigDir()199 private static String getUserConfigDir(){ 200 // Look for the system property 201 String configPath = System.getProperty(TG_CONFIG_PATH); 202 203 // Default System User Home 204 if(configPath == null){ 205 configPath = ( (System.getProperty("user.home") + File.separator + ".tuxguitar-" + TGVersion.CURRENT.getVersion()) ) ; 206 } 207 208 // Check if the path exists 209 File file = new File(configPath); 210 if(!file.exists()){ 211 file.mkdirs(); 212 } 213 return configPath; 214 } 215 getUserPluginsConfigDir()216 private static String getUserPluginsConfigDir(){ 217 String configPluginsPath = (getUserConfigDir() + File.separator + "plugins"); 218 219 //Check if the path exists 220 File file = new File(configPluginsPath); 221 if(!file.exists()){ 222 file.mkdirs(); 223 } 224 225 return configPluginsPath; 226 } 227 getStaticSharedPaths()228 private static String[] getStaticSharedPaths(){ 229 String staticSharedPaths = System.getProperty(TG_SHARE_PATH); 230 if( staticSharedPaths != null ){ 231 return staticSharedPaths.split(File.pathSeparator); 232 } 233 return null; 234 } 235 } 236