[Arakhnę-Dev] [275] * Add functions in Resources class.

[ Thread Index | Date Index | More arakhne.org/dev Archives ]


Revision: 275
Author:   galland
Date:     2011-08-25 00:03:27 +0200 (Thu, 25 Aug 2011)
Log Message:
-----------
* Add functions in Resources class.

Modified Paths:
--------------
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/Resources.java
    trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ResourcesTest.java

Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/Resources.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/Resources.java	2011-08-24 20:59:29 UTC (rev 274)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/Resources.java	2011-08-24 22:03:27 UTC (rev 275)
@@ -21,6 +21,8 @@
 
 import java.io.InputStream;
 import java.net.URL;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * This utility class provides to load resources according to
@@ -41,12 +43,15 @@
 		URLHandlerUtil.installArakhneHandlers();
 	}
 
+	/** Character used to separate paths on an resource name.
+	 */
+	public static final String NAME_SEPARATOR = "/"; //$NON-NLS-1$
+
 	/**
      * Replies the URL of a resource.
      * <p>
      * You may use Unix-like syntax to write the resource path, ie.
-     * you may use slashs to separate filenames, and may not start the
-     * path with a slash.
+     * you may use slashes to separate filenames.
      *
      * @param path is the absolute path of the resource. 
      * @return the url of the resource or <code>null</code> if the resource was
@@ -60,25 +65,66 @@
      * Replies the URL of a resource.
      * <p>
      * You may use Unix-like syntax to write the resource path, ie.
-     * you may use slashs to separate filenames, and may not start the
-     * path with a slash.
+     * you may use slashes to separate filenames.
+     * <p>
+     * The name of <var>packagename</var> is translated into a resource
+     * path (by replacing the dots by slashes) and the given path
+     * is append to. For example, the two following codes are equivalent:<pre><code>
+     * Resources.getResources(Package.getPackage("org.arakhne.afc"), "/a/b/c/d.png");
+     * Resources.getResources("org/arakhne/afc/a/b/c/d.png");
+     * </code></pre>
      *
-     * @param clazz is the class which is restricting the scope of the search.
+     * @param packagename is the package in which the resource should be located.
      * @param path is the absolute path of the resource. 
      * @return the url of the resource or <code>null</code> if the resource was
      * not found in class paths.
+     * @since 6.2
      */
-    public static URL getResource(Class<?> clazz, String path) {
-    	return getResource(clazz==null? null : clazz.getClassLoader(), path);
+    public static URL getResource(Package packagename, String path) {
+    	if (packagename==null || path==null) return null;
+    	StringBuffer b = new StringBuffer();
+    	b.append(packagename.getName().replaceAll(
+    			Pattern.quote("."), //$NON-NLS-1$
+    			Matcher.quoteReplacement(NAME_SEPARATOR)));
+    	if (!path.startsWith(NAME_SEPARATOR)) {
+    		b.append(NAME_SEPARATOR);
+    	}
+    	b.append(path);
+    	return getResource(packagename.getClass().getClassLoader(), b.toString());
     }
 
    	/**
      * Replies the URL of a resource.
      * <p>
      * You may use Unix-like syntax to write the resource path, ie.
-     * you may use slashs to separate filenames, and may not start the
-     * path with a slash.
+     * you may use slashes to separate filenames.
+     * <p>
+     * The name of <var>classname</var> is translated into a resource
+     * path (by remove the name of the class and replacing the dots by slashes) and the given path
+     * is append to. For example, the two following codes are equivalent:<pre><code>
+     * Resources.getResources(Resources.class, "/a/b/c/d.png");
+     * Resources.getResources("org/arakhne/vmutil/a/b/c/d.png");
+     * </code></pre>
      *
+     * @param classname is located in the package in which the resource should be also located.
+     * @param path is the absolute path of the resource. 
+     * @return the url of the resource or <code>null</code> if the resource was
+     * not found in class paths.
+     */
+    public static URL getResource(Class<?> classname, String path) {
+    	if (classname==null) return null;
+    	URL u = getResource(classname.getPackage(), path);
+    	if (u==null)
+    		u = getResource(classname.getClassLoader(), path);
+    	return u;
+    }
+
+   	/**
+     * Replies the URL of a resource.
+     * <p>
+     * You may use Unix-like syntax to write the resource path, ie.
+     * you may use slashes to separate filenames.
+     *
      * @param classLoader is the research scope.
      * @param path is the absolute path of the resource. 
      * @return the url of the resource or <code>null</code> if the resource was
@@ -109,7 +155,7 @@
      * Replies the input stream of a resource.
      * <p>
      * You may use Unix-like syntax to write the resource path, ie.
-     * you may use slashs to separate filenames, and may not start the
+     * you may use slashes to separate filenames, and may not start the
      * path with a slash.
      *
      * @param path is the absolute path of the resource. 
@@ -120,27 +166,69 @@
     	return getResourceAsStream(ClassLoaderFinder.findClassLoader(), path);
     }
     
-    /**
+   	/**
      * Replies the input stream of a resource.
      * <p>
      * You may use Unix-like syntax to write the resource path, ie.
-     * you may use slashs to separate filenames, and may not start the
-     * path with a slash.
+     * you may use slashes to separate filenames.
+     * <p>
+     * The name of <var>packagename</var> is translated into a resource
+     * path (by replacing the dots by slashes) and the given path
+     * is append to. For example, the two following codes are equivalent:<pre><code>
+     * Resources.getResources(Package.getPackage("org.arakhne.afc"), "/a/b/c/d.png");
+     * Resources.getResources("org/arakhne/afc/a/b/c/d.png");
+     * </code></pre>
      *
-     * @param clazz is the class which is restricting the scope of the search.
+     * @param packagename is the package in which the resource should be located.
      * @param path is the absolute path of the resource. 
      * @return the url of the resource or <code>null</code> if the resource was
      * not found in class paths.
+     * @since 6.2
      */
-    public static InputStream getResourceAsStream(Class<?> clazz, String path) {
-    	return getResourceAsStream(clazz==null ? null : clazz.getClassLoader(), path);
+    public static InputStream getResourceAsStream(Package packagename, String path) {
+    	if (packagename==null || path==null) return null;
+    	StringBuffer b = new StringBuffer();
+    	b.append(packagename.getName().replaceAll(
+    			Pattern.quote("."), //$NON-NLS-1$
+    			Matcher.quoteReplacement(NAME_SEPARATOR)));
+    	if (!path.startsWith(NAME_SEPARATOR)) {
+    		b.append(NAME_SEPARATOR);
+    	}
+    	b.append(path);
+    	return getResourceAsStream(packagename.getClass().getClassLoader(), b.toString());
     }
 
+   	/**
+     * Replies the input stream of a resource.
+     * <p>
+     * You may use Unix-like syntax to write the resource path, ie.
+     * you may use slashes to separate filenames.
+     * <p>
+     * The name of <var>classname</var> is translated into a resource
+     * path (by remove the name of the class and replacing the dots by slashes) and the given path
+     * is append to. For example, the two following codes are equivalent:<pre><code>
+     * Resources.getResources(Resources.class, "/a/b/c/d.png");
+     * Resources.getResources("org/arakhne/vmutil/a/b/c/d.png");
+     * </code></pre>
+     *
+     * @param classname is located in the package in which the resource should be also located.
+     * @param path is the absolute path of the resource. 
+     * @return the url of the resource or <code>null</code> if the resource was
+     * not found in class paths.
+     */
+    public static InputStream getResourceAsStream(Class<?> classname, String path) {
+    	if (classname==null) return null;
+    	InputStream is = getResourceAsStream(classname.getPackage(), path);
+    	if (is==null)
+    		is = getResourceAsStream(classname.getClassLoader(), path);
+    	return is;
+    }
+
     /**
      * Replies the input stream of a resource.
      * <p>
      * You may use Unix-like syntax to write the resource path, ie.
-     * you may use slashs to separate filenames, and may not start the
+     * you may use slashes to separate filenames, and may not start the
      * path with a slash.
      *
      * @param classLoader is the research scope.

Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ResourcesTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ResourcesTest.java	2011-08-24 20:59:29 UTC (rev 274)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ResourcesTest.java	2011-08-24 22:03:27 UTC (rev 275)
@@ -55,21 +55,52 @@
     	URL u2 = Resources.getResource(ResourcesTest.class, "org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
     	assertNotNull(u2);
     	
+    	URL u3 = Resources.getResource(ResourcesTest.class, "test.txt"); //$NON-NLS-1$
+    	assertNotNull(u3);
+
     	assertEquals(u1,u2);
+    	assertEquals(u1,u3);
 
     	assertNull(Resources.getResource((Class<?>)null, null));
     	
     	u1 = Resources.getResource((Class<?>)null, "/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
-    	assertNotNull(u1);
+    	assertNull(u1);
     	
     	u2 = Resources.getResource((Class<?>)null, "org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
-    	assertNotNull(u2);
-    	
-    	assertEquals(u1,u2);
+    	assertNull(u2);
     }
 
    	/**
      */
+    public void testGetResourcePackageString() {
+    	Package p = Package.getPackage("org.arakhne.vmutil"); //$NON-NLS-1$
+    	assertNull(Resources.getResource(ResourcesTest.class, null));
+    	
+    	URL u1 = Resources.getResource(p, "/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+    	assertNull(u1);
+    	
+    	URL u2 = Resources.getResource(p, "org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+    	assertNull(u2);
+    	
+    	URL u3 = Resources.getResource(p, "test.txt"); //$NON-NLS-1$
+    	assertNotNull(u3);
+
+    	URL u4 = Resources.getResource(p, "/test.txt"); //$NON-NLS-1$
+    	assertNotNull(u4);
+    	
+    	assertEquals(u3, u4);
+
+    	assertNull(Resources.getResource((Package)null, null));
+    	
+    	u1 = Resources.getResource((Package)null, "test.txt"); //$NON-NLS-1$
+    	assertNull(u1);
+    	
+    	u2 = Resources.getResource((Package)null, "/test.txt"); //$NON-NLS-1$
+    	assertNull(u2);
+    }
+
+    /**
+     */
     public void testGetResourceClassLoaderString() {
     	assertNull(Resources.getResource(ResourcesTest.class.getClassLoader(), null));    	
     	
@@ -115,17 +146,56 @@
     	is = Resources.getResourceAsStream(ResourcesTest.class, "org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
     	assertNotNull(is);
 
+    	is = Resources.getResourceAsStream(ResourcesTest.class, "/test.txt"); //$NON-NLS-1$
+    	assertNotNull(is);
+
+    	is = Resources.getResourceAsStream(ResourcesTest.class, "test.txt"); //$NON-NLS-1$
+    	assertNotNull(is);
+
     	assertNull(Resources.getResourceAsStream((Class<?>)null, null));
 
     	is = Resources.getResourceAsStream((Class<?>)null, "/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
-    	assertNotNull(is);
+    	assertNull(is);
     	
     	is = Resources.getResourceAsStream((Class<?>)null, "org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
-    	assertNotNull(is);
+    	assertNull(is);
+
+    	is = Resources.getResourceAsStream((Class<?>)null, "test.txt"); //$NON-NLS-1$
+    	assertNull(is);
     }
 
    	/**
      */
+    public void testGetResourceAsStreamPackageString() {
+    	Package p = Package.getPackage("org.arakhne.vmutil"); //$NON-NLS-1$
+    	assertNull(Resources.getResourceAsStream(ResourcesTest.class, null));
+
+    	InputStream is = Resources.getResourceAsStream(p, "/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+    	assertNull(is);
+    	
+    	is = Resources.getResourceAsStream(p, "org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+    	assertNull(is);
+
+    	is = Resources.getResourceAsStream(p, "/test.txt"); //$NON-NLS-1$
+    	assertNotNull(is);
+
+    	is = Resources.getResourceAsStream(p, "test.txt"); //$NON-NLS-1$
+    	assertNotNull(is);
+
+    	assertNull(Resources.getResourceAsStream((Package)null, null));
+
+    	is = Resources.getResourceAsStream((Package)null, "/org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+    	assertNull(is);
+    	
+    	is = Resources.getResourceAsStream((Package)null, "org/arakhne/vmutil/test.txt"); //$NON-NLS-1$
+    	assertNull(is);
+
+    	is = Resources.getResourceAsStream((Package)null, "test.txt"); //$NON-NLS-1$
+    	assertNull(is);
+    }
+
+    /**
+     */
     public void testGetResourceAsStreamClassLoaderString() {
     	assertNull(Resources.getResourceAsStream(ResourcesTest.class.getClassLoader(), null));
     	


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/