diff options
author | Sven Gothel <[email protected]> | 2011-04-22 05:38:28 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2011-04-22 05:38:28 +0200 |
commit | b3eebc2480bf96df8626d8494692ab5a3b5d88e7 (patch) | |
tree | 08564c255b1a37528847f773bf81b4147745cefb /src/jogl/classes/com/jogamp | |
parent | 7ac7b81d5cf10187aca8c1df85d1cf44fef299d3 (diff) |
Fix/Add: Locator (Handle JarURLConnection and ..)
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)
Diffstat (limited to 'src/jogl/classes/com/jogamp')
-rw-r--r-- | src/jogl/classes/com/jogamp/opengl/util/Locator.java | 75 | ||||
-rw-r--r-- | src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java | 29 |
2 files changed, 76 insertions, 28 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/util/Locator.java b/src/jogl/classes/com/jogamp/opengl/util/Locator.java index 291cd770c..0176b575a 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/Locator.java +++ b/src/jogl/classes/com/jogamp/opengl/util/Locator.java @@ -49,6 +49,9 @@ public class 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) { @@ -73,24 +76,36 @@ public class Locator { * @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) {} } @@ -98,25 +113,53 @@ public class Locator { } /** - * 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. */ diff --git a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java index 1f59318f2..c735de468 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java +++ b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java @@ -290,26 +290,35 @@ public class ShaderCode { } } - public static void readShaderSource(ClassLoader context, String path, URL url, StringBuffer result) { + public static void readShaderSource(Class context, URL url, StringBuffer result) { try { + if(DEBUG_CODE) { + System.err.println("ShaderCode.readShaderSource<0>: "+url); + } BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); String line = null; while ((line = reader.readLine()) != null) { if (line.startsWith("#include ")) { String includeFile = line.substring(9).trim(); + URL nextURL = null; + // Try relative path first - String next = Locator.getRelativeOf(path, includeFile); - URL nextURL = Locator.getResource(next, context); + String next = Locator.getRelativeOf(url, includeFile); + if(null != next) { + nextURL = Locator.getResource(context, next); + } if (nextURL == null) { // Try absolute path - next = includeFile; - nextURL = Locator.getResource(next, context); + nextURL = Locator.getResource(context, includeFile); } if (nextURL == null) { // Fail throw new FileNotFoundException("Can't find include file " + includeFile); } - readShaderSource(context, next, nextURL, result); + if(DEBUG_CODE) { + System.err.println("ShaderCode.readShaderSource<I>: "+url+" + "+includeFile+" := "+nextURL); + } + readShaderSource(context, nextURL, result); } else { result.append(line + "\n"); } @@ -320,16 +329,12 @@ public class ShaderCode { } public static String readShaderSource(Class context, String path) { - ClassLoader contextCL = (null!=context)?context.getClassLoader():null; URL url = Locator.getResource(context, path); if (url == null) { return null; - } - File pf = new File(url.getPath()); - path = pf.getParent() + "/" ; - + } StringBuffer result = new StringBuffer(); - readShaderSource(contextCL, path, url, result); + readShaderSource(context, url, result); return result.toString(); } |