Skip to content

Commit

Permalink
Fix/Add: Locator (Handle JarURLConnection and ..)
Browse files Browse the repository at this point in the history
new: 'public static String getRelativeOf(URL baseLocation, String relativeFile)',
     capable of handling a JAR file/url.

Using File based relative locator, allowing better utilization in code:
  old public static String getRelativeOf(String absoluteFileLocation, String relativeFile)
  new public static String getRelativeOf(File baseLocation, String relativeFile)
  • Loading branch information
sgothel committed Jun 8, 2011
1 parent dcc1ca2 commit 32f2f32
Showing 1 changed file with 59 additions and 16 deletions.
75 changes: 59 additions & 16 deletions src/jogl/classes/com/jogamp/opengl/util/Locator.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ private Locator() {}
* @see #getResource(String, ClassLoader)
*/
public static URL getResource(Class context, String path) {
if(null == path) {
return null;
}
ClassLoader contextCL = (null!=context)?context.getClassLoader():null;
URL url = getResource(path, contextCL);
if (url == null && null!=context) {
Expand All @@ -73,49 +76,89 @@ public static URL getResource(Class context, String path) {
* @see File#File(String)
*/
public static URL getResource(String path, ClassLoader cl) {
if(null == path) {
return null;
}
URL url = null;
if (cl != null) {
url = cl.getResource(path);
} else {
if(!urlExists(url)) {
url = null;
}
}
if(null == url) {
url = ClassLoader.getSystemResource(path);
if(!urlExists(url)) {
url = null;
}
}
if(!urlExists(url)) {
url = null;
if(null == url) {
try {
url = new URL(path);
if(!urlExists(url)) {
url = null;
}
} catch (MalformedURLException e) { }
}
if(!urlExists(url)) {
url = null;
if(null == url) {
try {
File file = new File(path);
if(file.exists()) {
url = file.toURL();
} else {
}
} catch (MalformedURLException e) {}
}
return url;
}

/**
* Generates a path for the 'relativeFile' relative to the 'absoluteFileLocation'
* Generates a path for the 'relativeFile' relative to the 'baseLocation'.
*
* @param baseLocation denotes a directory
* @param relativeFile denotes a relative file to the baseLocation
*/
public static String getRelativeOf(String absoluteFileLocation, String relativeFile) {
File file = new File(absoluteFileLocation);
file = file.getParentFile();
while (file != null && relativeFile.startsWith("../")) {
file = file.getParentFile();
public static String getRelativeOf(File baseLocation, String relativeFile) {
if(null == relativeFile) {
return null;
}

while (baseLocation != null && relativeFile.startsWith("../")) {
baseLocation = baseLocation.getParentFile();
relativeFile = relativeFile.substring(3);
}
if (file != null) {
String res = new File(file, relativeFile).getPath();
if (baseLocation != null) {
final File file = new File(baseLocation, relativeFile);
// Handle things on Windows
return res.replace('\\', '/');
} else {
return relativeFile;
return file.getPath().replace('\\', '/');
}
return null;
}

/**
* Generates a path for the 'relativeFile' relative to the 'baseLocation'.
*
* @param baseLocation denotes a URL to a file
* @param relativeFile denotes a relative file to the baseLocation's parent directory
*/
public static String getRelativeOf(URL baseLocation, String relativeFile) {
String urlPath = baseLocation.getPath();

if ( baseLocation.toString().startsWith("jar") ) {
JarURLConnection jarConnection;
try {
jarConnection = (JarURLConnection) baseLocation.openConnection();
urlPath = jarConnection.getEntryName();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

// Try relative path first
return getRelativeOf(new File(urlPath).getParentFile(), relativeFile);
}

/**
* Returns true, if the url exists,
* trying to open a connection.
Expand Down

0 comments on commit 32f2f32

Please sign in to comment.