jfun.yan.xml
Class NutsUtils

java.lang.Object
  extended by jfun.yan.xml.NutsUtils

public class NutsUtils
extends java.lang.Object

A common utility class for various Nuts helper functions.

Author:
Ben Yu

Constructor Summary
NutsUtils()
           
 
Method Summary
static Component asComponent(java.lang.Object val)
          Convert an object to Component.
static java.lang.Object callFunction(NutsFunction nfun, java.util.Map arg_map)
          Call a NutsFunction object with a map of parameter name to argument value.
static java.lang.Object callFunction(NutsFunction nfun, java.lang.Object[] args)
          Call a NutsFunction object with an array of arguments.
static java.lang.String canonicalizeAttributeName(java.lang.String name)
          Canonicalize an attribute name by replacing "-" with "_".
static java.lang.ClassLoader getClassLoader(java.lang.ClassLoader baseloader, java.lang.String classpath, java.io.File basedir)
          To get a ClassLoader object that uses a base ClassLoader object as parent and alternatively searches a classpath if the class or resource is not found in parent.
static java.lang.Object getState(Component c, java.lang.Object key)
          Get a state value by name.
static boolean isValidId(java.lang.String str)
          To determine if a string is a valid id in yan xml config file.
static Component setPossibleProperty(Component c, java.lang.String name, Component valc)
           
static Component setPossiblePropertyValue(Component c, java.lang.String name, java.lang.Object val)
          If a property of the component type exists and the property type is compatible with the property value, the property is set when the component is instantiated.
static Component setState(Component c, java.lang.Object key, java.lang.Object val)
          Add named state to a Component object.
static java.lang.String[] split(java.lang.String str, java.lang.String sep)
          To split a string into an array of sub-strings.
static java.lang.Boolean toBoolean(java.lang.String str)
          To convert a string literal to boolean.
static java.io.File toFile(java.io.File basedir, java.lang.String path)
          Convert a string to a File.
static java.net.URI toUri(java.lang.String str)
          To convert a string literal to a URI object.
static java.net.URL toUrl(java.io.File basedir, java.lang.String str)
          Convert a string to a URL.
static java.net.URL[] toUrls(java.io.File basedir, java.lang.String str)
          Convert a string to an array of URL.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NutsUtils

public NutsUtils()
Method Detail

split

public static java.lang.String[] split(java.lang.String str,
                                       java.lang.String sep)
To split a string into an array of sub-strings.

Parameters:
str - the string to split.
sep - the separator characters.
Returns:
an array of sub-strings who are seperated by one or many of the characters in the sep string.

toBoolean

public static java.lang.Boolean toBoolean(java.lang.String str)
To convert a string literal to boolean.

Parameters:
str - the string literal
Returns:
Boolean object. null if the string is not recognized.

toFile

public static java.io.File toFile(java.io.File basedir,
                                  java.lang.String path)
Convert a string to a File. if the string represents a relative path, it is treated as relative to the basedir.

Parameters:
basedir - the base directory.
path - the string.
Returns:
the File object.

toUrl

public static java.net.URL toUrl(java.io.File basedir,
                                 java.lang.String str)
                          throws java.net.MalformedURLException
Convert a string to a URL. if the string represents a relative path, it is treated as relative to the basedir.

Parameters:
basedir - the base directory.
str - the string.
Returns:
the URL object.
Throws:
java.net.MalformedURLException - if the string represents neither a file nor a URL.

toUri

public static java.net.URI toUri(java.lang.String str)
                          throws java.net.URISyntaxException
To convert a string literal to a URI object.

Parameters:
str - the string literal.
Returns:
the URI object.
Throws:
java.net.URISyntaxException - if the uri syntax is wrong.

toUrls

public static java.net.URL[] toUrls(java.io.File basedir,
                                    java.lang.String str)
                             throws java.net.MalformedURLException
Convert a string to an array of URL. The string contains a series of sub-string seperated by whitespaces or "," or ";"

Parameters:
basedir - the base directory to resolve relative path.
str - the string.
Returns:
the array of URL
Throws:
java.net.MalformedURLException - if any sub-string is not convertible to URL.

isValidId

public static boolean isValidId(java.lang.String str)
To determine if a string is a valid id in yan xml config file.

Parameters:
str - the string.
Returns:
true if valid.

callFunction

public static java.lang.Object callFunction(NutsFunction nfun,
                                            java.lang.Object[] args)
Call a NutsFunction object with an array of arguments.

Parameters:
nfun - the NutsFunction object.
args - the arguments.
Returns:
the result.

callFunction

public static java.lang.Object callFunction(NutsFunction nfun,
                                            java.util.Map arg_map)
Call a NutsFunction object with a map of parameter name to argument value.

Parameters:
nfun - the function.
arg_map - the map of the argument values.
Returns:
the result.

asComponent

public static Component asComponent(java.lang.Object val)
Convert an object to Component. Conversion is automatically done if necessary.

Parameters:
val - the value to be converted to Component.
Returns:
the Component.

canonicalizeAttributeName

public static java.lang.String canonicalizeAttributeName(java.lang.String name)
Canonicalize an attribute name by replacing "-" with "_".

Parameters:
name - the attribute name.
Returns:
the new attribute name, or null if the attribute name is null.

getClassLoader

public static java.lang.ClassLoader getClassLoader(java.lang.ClassLoader baseloader,
                                                   java.lang.String classpath,
                                                   java.io.File basedir)
                                            throws java.net.MalformedURLException
To get a ClassLoader object that uses a base ClassLoader object as parent and alternatively searches a classpath if the class or resource is not found in parent.

Parameters:
baseloader - the base class loader.
classpath - the alternative classpath.
basedir - the base directory used in the classpath.
Returns:
the ClassLoader object.
Throws:
java.net.MalformedURLException - if the classpath is invalid.

setState

public static Component setState(Component c,
                                 java.lang.Object key,
                                 java.lang.Object val)
Add named state to a Component object.

Parameters:
c - the Component object.
key - the key of the state.
val - the value of the state.
Returns:
the new Component object with this state.

getState

public static java.lang.Object getState(Component c,
                                        java.lang.Object key)
Get a state value by name.

Parameters:
c - the Component object to get state from.
key - the state key.
Returns:
the state value.

setPossiblePropertyValue

public static Component setPossiblePropertyValue(Component c,
                                                 java.lang.String name,
                                                 java.lang.Object val)
                                          throws java.beans.IntrospectionException
If a property of the component type exists and the property type is compatible with the property value, the property is set when the component is instantiated.

Parameters:
c - the component.
name - the property name.
val - the property value.
Returns:
the result Component.
Throws:
java.beans.IntrospectionException

setPossibleProperty

public static Component setPossibleProperty(Component c,
                                            java.lang.String name,
                                            Component valc)
                                     throws java.beans.IntrospectionException
Throws:
java.beans.IntrospectionException