gate.creole.Parameter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gate-core Show documentation
Show all versions of gate-core Show documentation
GATE - general achitecture for text engineering - is open source
software capable of solving almost any text processing problem. This
artifact enables you to embed the core GATE Embedded with its essential
dependencies. You will able to use the GATE Embedded API and load and
store GATE XML documents. This artifact is the perfect dependency for
CREOLE plugins or for applications that need to customize the GATE
dependencies due to confict with their own dependencies or for lower
footprint.
The newest version!
/*
* Parameter.java
*
* Copyright (c) 1995-2012, The University of Sheffield. See the file
* COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
*
* This file is part of GATE (see http://gate.ac.uk/), and is free
* software, licenced under the GNU Library General Public License,
* Version 2, June 1991 (in the distribution as file licence.html,
* and also available at http://gate.ac.uk/gate/licence.html).
*
* Hamish Cunningham, 15/Oct/2000
*
* $Id: Parameter.java 20035 2017-02-01 03:01:32Z markagreenwood $
*/
package gate.creole;
import gate.Factory;
import gate.FeatureMap;
import gate.Gate;
import gate.Resource;
import gate.util.GateRuntimeException;
import gate.util.Strings;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.SortedSet;
import java.util.StringTokenizer;
import java.util.TreeSet;
/** Models a resource parameter.
*/
public class Parameter implements Serializable
{
private static final long serialVersionUID = 6611664706992065985L;
/**
* Construct a new Parameter for a resource in a given plugin.
* @param plugin the plugin that defines the resource
* this parameter belongs to. This will be used as context when deriving
* default values for the parameters of type URL oe ResourceReference.
*/
public Parameter(Plugin plugin) {
this.plugin = plugin;
}
/** The type name of the parameter */
String typeName;
/** Set the type name for this parameter */
public void setTypeName(String typeName) { this.typeName = typeName; }
/** Get the type name for this parameter */
public String getTypeName() { return typeName; }
/** Is the parameter optional? */
boolean optional = false;
/** Set optionality of this parameter */
public void setOptional(boolean optional) { this.optional = optional; }
/** Is the parameter optional? */
public boolean isOptional() { return optional; }
/** The name of the item's class. If the parameter is a collection then
* we need to know the class of its items in order to create them the
* way we want.
*/
String itemClassName = null;
/** A set of strings representing suffixes for URL params*/
Set suffixes = null;
/**
* Map giving concrete classes that should be used for a parameter
* whose declared type is an interface. This substitution allows a
* resource to specify a parameter of type, e.g. java.util.List,
* and give it a default value in creole.xml. The runtime class of
* the default value will be taken from this map.
*
* Note that for this to work, it must be the case that for every
* key k in this map,
*
* k.isAssignableFrom(substituteClasses.get(k))
*/
static Map, Class>> substituteClasses =
new HashMap, Class>>();
static {
substituteClasses.put(Collection.class, ArrayList.class);
substituteClasses.put(List.class, ArrayList.class);
substituteClasses.put(Set.class, HashSet.class);
substituteClasses.put(SortedSet.class, TreeSet.class);
substituteClasses.put(Queue.class, LinkedList.class);
}
/** Calculate and return the default value for this parameter */
public Object calculateDefaultValue() throws ParameterException {
// if there's no default string and this is a builtin type, return null
if(
defaultValueString == null && typeName != null &&
typeName.startsWith("java.")
)
return null;
return calculateValueFromString(defaultValueString);
} // calculateDefaultValue()
/** Calculate and return the value for this parameter starting from a String
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public Object calculateValueFromString(String stringValue)
throws ParameterException {
//if we have no string we can't construct a value
Object value = null;
// get the Class for the parameter via Class.forName or CREOLE register
Class> paramClass = getParameterClass();
if(substituteClasses.containsKey(paramClass)) {
paramClass = substituteClasses.get(paramClass);
}
// Test if the paramClass is a collection and if it is, try to
// construct the param as a collection of items specified in the
// default string value. If paramClass is an interface type we
// look up its substitute concrete type in the map
// collectionSubstituteClasses and create a value of that type.
if (Collection.class.isAssignableFrom(paramClass) &&
!paramClass.isInterface()){
// Create an collection object belonging to paramClass
Collection> colection = null;
try{
colection = paramClass.asSubclass(Collection.class).getConstructor(new Class>[]{}).
newInstance(new Object[]{});
} catch(Exception ex){
throw new ParameterException("Could not construct an object of type "
+ typeName + " for param " + name +
"\nProblem was: " + ex.toString());
}// End try
// If an itemClassName was specified then try to create objects belonging
// to this class and add them to the collection. Otherwise add the
// string tokens to the collection.
if(itemClassName == null){
// Read the tokens from the default value and try to create items
// belonging to the itemClassName
StringTokenizer strTokenizer = new StringTokenizer(
stringValue,";");
while(strTokenizer.hasMoreTokens()){
String itemStringValue = strTokenizer.nextToken();
((Collection)colection).add(itemStringValue);
}// End while
}else{
Class> itemClass = null;
try{
itemClass = Gate.getClassLoader().loadClass(itemClassName);
}catch(ClassNotFoundException e){
throw new ParameterException("Could not construct a class object for "
+ itemClassName + " for param "+ name +
", with type name="+ typeName);
}// End try
// Read the tokens from the default value and try to create items
// belonging to the itemClassName
StringTokenizer strTokenizer = new StringTokenizer(
stringValue,";");
while(strTokenizer.hasMoreTokens()){
// Read a string item and construct an object belonging to
// itemClassName
String itemStringValue = strTokenizer.nextToken();
Object itemValue = null;
try{
itemValue = itemClass.getConstructor(new Class>[]{String.class}).
newInstance(new Object[]{itemStringValue});
}catch(Exception e){
throw new ParameterException("Could not create an object of " +
itemClassName + " for param name "+ name + ", with type name ="+
typeName);
}// End try
// Add the item value object to the collection
((Collection