[MOBY-guts] biomoby commit

Martin Senger senger at pub.open-bio.org
Sat Nov 19 18:33:02 UTC 2005


senger
Sat Nov 19 13:33:02 EST 2005
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared
In directory pub.open-bio.org:/tmp/cvs-serv25480/src/main/org/biomoby/shared

Modified Files:
	Utils.java 
Log Message:


moby-live/Java/src/main/org/biomoby/shared Utils.java,1.8,1.9
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/Utils.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/Utils.java	2005/11/10 08:54:32	1.8
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/Utils.java	2005/11/19 18:33:01	1.9
@@ -7,6 +7,9 @@
 
 package org.biomoby.shared;
 
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+
 import java.util.HashSet;
 import java.io.PrintWriter;
 import java.io.BufferedWriter;
@@ -16,7 +19,11 @@
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.io.InputStream;
-
+import java.io.OutputStreamWriter;
+import java.io.ByteArrayOutputStream;
+import java.nio.charset.Charset;
+import java.net.URL;
+import java.net.MalformedURLException;
 
 /**
  * This is a set of several utility methods which may be useful for
@@ -144,17 +151,21 @@
 
     /*************************************************************************
      * Find the resource with the given 'filename', read it and return
-     * it. A resource is some data (images, audio, text, etc) that can be
-     * accessed by class code in a way that is independent of the location
-     * of the code, typicallt such resource file sits anywhere on the CLASSPATH. <p>
+     * it. A resource is some data (images, audio, text, etc) that can
+     * be accessed by class code in a way that is independent of the
+     * location of the code, typically such resource file sits
+     * anywhere on the CLASSPATH. <p>
      *
      * @param filename of a resource is a '/'-separated path name that
      * identifies the resource
+     *
      * @param resourceOwner is any object whose class loader is used
      * to find and get the resource; typically one would put here
      * "this" when calling this method
+     *
      * @return contents of the resource, or null if the resource could
      * not be found
+     *
      * @throws MobyException if resource was found but an error
      * occured during its reading (IO problem, memory problem etc.)
      *************************************************************************/
@@ -193,9 +204,114 @@
     }
 
     /*************************************************************************
+     * Work in progress. <p>
+     *
+     * Slightly richer version of {@link
+     * #readResource(String,Object)}. It reads the resource using
+     * platform default encoding (which may be not what you
+     * want... something to be done better (TBD). <p>
+     *
+     * @return contents of the resource, or null if the resource could
+     * not be found
+     *
+     * @throws IOException if resource was found but an error
+     * occured during its reading (IO problem, memory problem etc.)
+     *************************************************************************/
+    public static String readResource (String path, Class c)
+	throws IOException {
+
+	// path can be empty
+	if (path == null) return null;
+
+	// seems that we are going to read something - so prepare a
+	// default encoding
+	String encoding = new OutputStreamWriter (new ByteArrayOutputStream()).getEncoding();
+	// for 1.5:
+// 	String encoding = Charset.defaultCharset().name();
+
+	// path can be absolute...
+	File file = new File (path);
+	if (file.isAbsolute())
+	    return FileUtils.readFileToString (file, encoding);
+
+	// ...or consider it a resource and load it as a resource of
+	// the given class
+	InputStream is = null;
+	if (c != null) {
+	    is = c.getClassLoader().getResourceAsStream (path);
+	    if (is != null)
+		return IOUtils.toString (is, encoding);
+
+	    // ...or extend the path by the package name of the given
+	    // class
+	    String className = c.getName();
+	    int pkgEndIndex = className.lastIndexOf ('.');
+	    if (pkgEndIndex > 0) {
+		String packageName = className.substring (0, pkgEndIndex);
+		String newPath = packageName.replace ('.', '/') + "/" + path;
+		is = c.getClassLoader().getResourceAsStream (newPath);
+		if (is != null)
+		    return IOUtils.toString (is, encoding);
+	    }
+	}
+
+	// ...or (finally) try some general class loader
+	is = Thread.currentThread().getContextClassLoader().getResourceAsStream (path);
+	if (is != null)
+	    return IOUtils.toString (is, encoding);
+
+	// sorry, I cannot do more
+	return null;
+    }
+
+    /*************************************************************************
+     * Work in progress. <p>
+     *
+     * Similar to {@link #readResource(String,Class)} but return just
+     * an URL of a resource, not the resource itself. <p>
+     *
+     * @return URL of the resource, or null if the resource could not
+     * be found
+     *************************************************************************/
+    public static URL getResourceURL (String path, Class c) {
+
+	// path can be empty
+	if (path == null) return null;
+
+	// path can be absolute...
+	File file = new File (path);
+	if (file.isAbsolute()) {
+	    try {
+		return file.toURI().toURL();
+	    } catch (MalformedURLException e) {
+		return null;
+	    }
+	}
+
+	// ...or consider it a resource of the given class
+	URL url = null;
+	if (c != null) {
+	    url = c.getClassLoader().getResource (path);
+	    if (url != null) return url;
+
+	    // ...or extend the path by the package name of the given class
+	    String className = c.getName();
+	    int pkgEndIndex = className.lastIndexOf ('.');
+	    if (pkgEndIndex > 0) {
+		String packageName = className.substring (0, pkgEndIndex);
+		String newPath = packageName.replace ('.', '/') + "/" + path;
+		url = c.getClassLoader().getResource (newPath);
+		if (url != null) return url;
+	    }
+	}
+
+	// ...or (finally) try some general class loader
+	return Thread.currentThread().getContextClassLoader().getResource (path);
+    }
+
+    /*************************************************************************
      * Return just the last part of a Java class name (after the last
-     * dot). It is useful for displaying purposes (and when data do
-     * not have article names). <p>
+     * dot). It is useful for displaying purposes. <p>
      *
      * @param className whose last part is being looked for
      * @return the last part of 'className', or the whole 'className' if it does




More information about the MOBY-guts mailing list