Version: 1.1.1

org.biomoby.shared
Class Utils

java.lang.Object
  extended by org.biomoby.shared.Utils

public abstract class Utils
extends Object

This is a set of several utility methods which may be useful for writing both registry and client code. Some methods are specific for Apache Axis framework.

Version:
$Id: Utils.java,v 1.21 2008/03/17 14:29:53 kawas Exp $
Author:
Martin Senger

Constructor Summary
Utils()
           
 
Method Summary
static String checkOrCreateArticleName(String articleName, String className)
          Check or create an article name.
static void createFile(File file, String contents)
          Create a file and fill it with given contents.
static void createFile(File file, StringBuffer contents)
          Create a file and fill it with given contents.
static String format(Object objectToBeFormatted, int indent)
           
static InputStream getInputStream(URL url)
          Gets an InputStream on a URL
static URL getResourceURL(String path, Class c)
          Work in progress.
static String javaEscape(String value)
          Make 'value' a valid Java identifier by trimming it and by replacing "unwanted" characters by underscores.
static String mobyEscape(String value)
          Make sure that an article name does not collide with the member names in the top-level MobyObject.
static String ms2Human(long millis)
          Give back an elapsed time (given in milllis) in a human readable form.
static String pureName(String lsid)
          Return just the last part of the LSID identifier.
static String readResource(String path, Class c)
          Work in progress.
static String readResource(String filename, Object resourceOwner)
          Find the resource with the given 'filename', read it and return it.
static String simpleClassName(String className)
          Return just the last part of a Java class name (after the last dot).
static String stackTraceIfSerious(Throwable e)
          Return a stringified version of the given exception, containing - for more serious errors, such as NullPointerException - also a stack trace.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Utils

public Utils()
Method Detail

format

public static String format(Object objectToBeFormatted,
                            int indent)

createFile

public static void createFile(File file,
                              String contents)
                       throws MobyException
Create a file and fill it with given contents.

Parameters:
file - to be created
contents - what to put in the created file
Throws:
MobyException - if something goes wrong

createFile

public static void createFile(File file,
                              StringBuffer contents)
                       throws MobyException
Create a file and fill it with given contents.

Parameters:
file - to be created
contents - what to put in the created file
Throws:
MobyException - if something goes wrong

ms2Human

public static String ms2Human(long millis)
Give back an elapsed time (given in milllis) in a human readable form.

Parameters:
millis - is a time interval in milliseconds
Returns:
formatted, human-readable, time

pureName

public static String pureName(String lsid)
Return just the last part of the LSID identifier. An example of an LSID identifier as used by and returned from the Moby registry is urn:lsid:biomoby.org:objectclass:object.

Parameters:
lsid - is an input
Returns:
the last part of 'lsid', or the whole 'lsid' if it does not contain any colon (which is a delimiter used by LSID identifiers)

readResource

public static String readResource(String filename,
                                  Object resourceOwner)
                           throws MobyException
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, typically such resource file sits anywhere on the CLASSPATH.

Parameters:
filename - of a resource is a '/'-separated path name that identifies the resource
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
Returns:
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.)

readResource

public static String readResource(String path,
                                  Class c)
                           throws IOException
Work in progress.

Slightly richer version of readResource(String,Object). It reads the resource using platform default encoding (which may be not what you want... something to be done better (TBD).

Returns:
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.)

getResourceURL

public static URL getResourceURL(String path,
                                 Class c)
Work in progress.

Similar to readResource(String,Class) but return just an URL of a resource, not the resource itself.

Returns:
URL of the resource, or null if the resource could not be found

stackTraceIfSerious

public static String stackTraceIfSerious(Throwable e)
Return a stringified version of the given exception, containing - for more serious errors, such as NullPointerException - also a stack trace.

Parameters:
e - to be reported

simpleClassName

public static String simpleClassName(String className)
Return just the last part of a Java class name (after the last dot). It is useful for displaying purposes.

Parameters:
className - whose last part is being looked for
Returns:
the last part of 'className', or the whole 'className' if it does not contain any dots

checkOrCreateArticleName

public static String checkOrCreateArticleName(String articleName,
                                              String className)
Check or create an article name. Biomoby uses term "article name" for naming Biomoby objects by context where they appear. For example, article name must be used for Biomoby objects that are children (members) of other Biomoby objects.

This method checks if the given 'articleName' is not empty - and if it is then it replaces it with the given 'className' (each Biomoby object always has a class name).

It also trims the article name (removes starting and ending whitespaces).

Parameters:
articleName - will be checked, or created if it is empty
className - will be used to create an article name (if necessary)
Returns:
original 'articleName' or a new (created) one

mobyEscape

public static String mobyEscape(String value)
Make sure that an article name does not collide with the member names in the top-level MobyObject. The members in question are 'id', 'name', 'namespace' and 'value'. This does not mean that Biomoby data types are not allowed to have such article names, but that Java code generator must generate slightly different names for its methods in these cases.

I must admit that this replacement is not full-proof: if a data type will have both article names 'value' and 'the_value' then this arrangement breaks...

This method is used at least from two places now (an XML Moby Parser and a MoSeS code generator) - that's why it ended up here in general utilities.

Parameters:
value - will be checked (already expected not to be empty)
Returns:
the same 'value' if nothing wrong with it was found, or a new string resembling the 'value' but having some characters replaced

javaEscape

public static String javaEscape(String value)
Make 'value' a valid Java identifier by trimming it and by replacing "unwanted" characters by underscores.

Parameters:
value - will be checked
Returns:
the same 'value' if nothing wrong with it was found, or a new string resembling the 'value' but having some characters replaced

getInputStream

public static InputStream getInputStream(URL url)
                                  throws MobyException
Gets an InputStream on a URL

Throws:
MobyException

Version: 1.1.1

Submit a bug or feature
Generated: Sat May 29 04:26:35 EDT 2010