ResourceData.java
001 /*
002  *  ResourceData.java
003  *
004  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
005  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006  *
007  *  This file is part of GATE (see http://gate.ac.uk/), and is free
008  *  software, licenced under the GNU Library General Public License,
009  *  Version 2, June 1991 (in the distribution as file licence.html,
010  *  and also available at http://gate.ac.uk/gate/licence.html).
011  *
012  *  Hamish Cunningham, 1/Sept/2000
013  *
014  *  $Id: ResourceData.java 13188 2010-11-04 13:38:49Z ian_roberts $
015  */
016 
017 package gate.creole;
018 
019 import java.io.Serializable;
020 import java.net.URL;
021 import java.util.ArrayList;
022 import java.util.Collections;
023 import java.util.List;
024 import java.util.concurrent.CopyOnWriteArrayList;
025 
026 import gate.*;
027 import gate.util.*;
028 
029 /** Models an individual CREOLE resource metadata, plus configuration data,
030   * plus the instantiations of the resource current within the system.
031   * Some metadata elements are used by GATE to load resources, or index
032   * the members of the CREOLE register; some are used during resource
033   * parameterisation and initialisation.
034   * Metadata elements which are used by the CREOLE registration and loading
035   * mechanisms are properties of ResourceData implementations and have their
036   * own get/set methods. Other metadata elements are made features of the
037   * ResourceData. So, for example, if you add an element "FunkyElementThaing"
038   * to the metadata of a resource, this will be made a feature of that
039   * resource's ResourceData.
040   @see CreoleRegister
041   */
042 public class ResourceData extends AbstractFeatureBearer implements Serializable
043 {
044 
045   /** Debug flag */
046   protected static final boolean DEBUG = false;
047 
048   protected static final String DEFAULT_LR_ICON = "lr";
049   protected static final String DEFAULT_PR_ICON = "pr";
050   protected static final String DEFAULT_OTHER_ICON = "application";
051   /** Construction */
052   public ResourceData() {  }// ResourceData
053 
054   /** String representation */
055   public String toString() {
056     int noInst = (instantiationStack == null0: instantiationStack.size();
057 /*
058     int noSmallViews = (smallViews == null) ? 0: smallViews.size();
059     int noViews = (views == null) ? 0: views.size();
060 */
061     StringBuffer s = new StringBuffer(
062       "ResourceDataImpl, name=" + name + "; className=" + className +
063       "; jarFileName=" + jarFileName + "; jarFileUrl=" + jarFileUrl +
064       "; xmlFileName=" + xmlFileName + "; xmlFileUrl=" + xmlFileUrl +
065       "; isAutoLoading=" + autoLoading + "; numberInstances=" + noInst +
066       "; isPrivate=" + priv +"; isTool="+ tool +
067       "; validityMessage=" + validityMessage +
068       "; interfaceName=" + interfaceName +
069       "; guiType=" + guiType +
070       "; mainViewer=" + isMainView +
071       "; resourceDisplayed=" + resourceDisplayed +
072       "; annotationTypeDisplayed=" + annotationTypeDisplayed +
073       "; parameterList=" + parameterList +
074       "; features=" + features
075     );
076     return s.toString();
077   // toString
078 
079   /** Equality: two resource data objects are the same if they have the
080     * same name
081     */
082   public boolean equals(Object other) {
083     if(name.equals(((ResourceDataother).getName()))
084       return true;
085     return false;
086   // equals
087 
088   /** Hashing, based on the name field of the object */
089   public int hashCode() {
090     return name.hashCode();
091   // hashCode
092 
093   /** The name of the resource */
094   protected String name;
095 
096   /** Set method for the resource name */
097   public void setName(String name) { this.name = name; }
098 
099   /** Get method for the resource name */
100   public String getName() { return name; }
101 
102   /** Location of an icon for the resource */
103   protected String icon;
104 
105   /** Set method for the resource icon */
106   public void setIcon(String icon) { this.icon = icon; }
107 
108   /** Get method for the resource icon */
109   public String getIcon() { 
110     //if icon not set try and guess it
111     if(icon == null){
112       icon = guessIcon();
113     }
114     return icon; 
115   }
116 
117   /**
118    * Makes the best attempt of guessing an appropriate icon for this resource 
119    * type based on whether it is a Language Resource, a Processing Resource, or
120    * something else.
121    @return a String representing the file name for most appropriate icon for 
122    * this resource type.
123    */
124   protected String guessIcon(){
125     //if no class set we can't do any guessing
126     if(className == nullreturn DEFAULT_OTHER_ICON;
127     if(resourceClass == nullreturn DEFAULT_OTHER_ICON;
128     if(LanguageResource.class.isAssignableFrom(resourceClass)) 
129       return DEFAULT_LR_ICON;
130     if(ProcessingResource.class.isAssignableFrom(resourceClass)) 
131       return DEFAULT_PR_ICON;
132     return DEFAULT_OTHER_ICON;
133   }
134   
135   /** The stack of instantiations */
136   protected List<Resource> instantiationStack = new CopyOnWriteArrayList<Resource>();
137   
138   /**
139    * Unmodifiable view of the instantiation stack, returned by
140    * getInstantiations to ensure that the only way to modify the list is
141    * through the add/removeInstantiation methods of this class.
142    */
143   protected List<Resource> unmodifiableInstantiationStack =
144           Collections.unmodifiableList(instantiationStack);
145 
146   /** This list contains all instances loaded from creole.xml with
147    *  AUTOINSTANCE tag.
148    *  
149    *  @deprecated No longer necessary as we don't use weak references
150    *  for the instantiations.
151    */
152   @Deprecated
153   protected List<Resource> persistantInstantiationList = new ArrayList<Resource>();
154 
155   /** Get the list of instantiations of resources */
156   public List<Resource> getInstantiations() {
157     return unmodifiableInstantiationStack;
158   // getInstantiations
159 
160   /** Add an instantiation of the resource to the register of these */
161   public void addInstantiation(Resource resource) {
162     instantiationStack.add(0, resource);
163   // addInstantiation
164 
165   /** This method makes a certain resource persistent by adding it into a
166     * persistantInstantiationList. It is used especially with AUTOINSTANCE tag
167     * in creole xml.
168     
169     @deprecated No longer needed as we don't use weak references in the
170     * instantiations stack.  Left for compatibility as a no-op.
171     */
172   @Deprecated
173   public void makeInstantiationPersistant(Resource resource) {
174     //persistantInstantiationList.add(resource);
175   // makeInstantiationPersistant
176 
177   /**
178    * Remove an instantiation of the resource from the register of these.
179    @return true if the given instance was contained in the register,
180    *         false otherwise (i.e. the instance had already been removed).
181    */
182   public boolean removeInstantiation(Resource resource) {
183     return instantiationStack.remove(resource);
184     //persistantInstantiationList.remove(resource);
185   // removeInstantiation
186 
187   /**
188    * Bump an instantiation to the top of the instantiation stack
189    
190    @deprecated This operation is no longer supported, and does nothing.
191    */
192   @Deprecated
193   public void bumpInstantiation(Resource resource) {
194     // do nothing
195   // bumpInstantiation
196 
197   /** The class name of the resource */
198   protected String className;
199 
200   /** Set method for the resource class name */
201   public void setClassName(String className) { this.className = className; }
202 
203   /** Get method for the resource class name */
204   public String getClassName() { return className; }
205 
206   /** The interface name of the resource */
207   protected String interfaceName;
208 
209   /** Set method for the resource interface name */
210   public void setInterfaceName(String interfaceName) {
211     this.interfaceName = interfaceName;
212   // setInterfaceName
213 
214   /** Get method for the resource interface name */
215   public String getInterfaceName() { return interfaceName; }
216 
217   /** The class of the resource */
218   protected Class<? extends Resource> resourceClass;
219 
220   /** Set method for the resource class */
221   public void setResourceClass(Class<? extends Resource> resourceClass) {
222     this.resourceClass = resourceClass;
223   // setResourceClass
224 
225   /** Get method for the resource class. Asks the GATE class loader
226     * to load it, if it is not already present.
227     */
228   public Class<? extends Resource> getResourceClass() throws ClassNotFoundException {
229     if(resourceClass == null) {
230       GateClassLoader classLoader = Gate.getClassLoader();
231       resourceClass = classLoader.loadClass(className).asSubclass(Resource.class);
232     }
233 
234     return resourceClass;
235   // getResourceClass
236 
237   /** The jar file name of the resource */
238   protected String jarFileName;
239 
240   /** Set method for the resource jar file name */
241   public void setJarFileName(String jarFileName) {
242     this.jarFileName = jarFileName;
243   // setJarFileName
244 
245   /** Get method for the resource jar file name */
246   public String getJarFileName() { return jarFileName; }
247 
248   /** The jar file URL of the resource */
249   protected URL jarFileUrl;
250 
251   /** Set method for the resource jar file URL */
252   public void setJarFileUrl(URL jarFileUrl) { this.jarFileUrl = jarFileUrl; }
253 
254   /** Get method for the resource jar file URL */
255   public URL getJarFileUrl() { return jarFileUrl; }
256 
257   /** The xml file name of the resource */
258   protected String xmlFileName;
259 
260   /** The xml file URL of the resource */
261   protected URL xmlFileUrl;
262 
263   /** Set the URL to the creole.xml file that defines this resource */
264   public void setXmlFileUrl(URL xmlFileUrl) { this.xmlFileUrl = xmlFileUrl; }
265 
266   /** Get the URL to the creole.xml file that defines this resource */
267   public URL getXmlFileUrl() { return xmlFileUrl; }
268 
269   /** The comment string */
270   protected String comment;
271 
272   /** Get method for the resource comment */
273   public String getComment() { return comment; }
274 
275   /** Set method for the resource comment */
276   public void setComment(String comment) { this.comment = comment; }
277 
278   /** The helpURL string */
279   protected String helpURL;
280   
281   /** Get method for the resource helpURL */
282   public String getHelpURL() { return helpURL; }
283   
284   /** Set method for the resource helpURL */
285   public void setHelpURL(String helpURL) { this.helpURL = helpURL; }
286   
287   /** The set of parameter lists */
288   protected ParameterList parameterList = new ParameterList();
289 
290   /** Set the parameter list */
291   public void setParameterList(ParameterList parameterList) {
292     this.parameterList = parameterList;
293   // addParameterList
294 
295   /** Get the parameter list */
296   public ParameterList getParameterList() { return parameterList; }
297 
298   /** Autoloading flag */
299   protected boolean autoLoading;
300 
301   /** Set method for resource autoloading flag */
302   public void setAutoLoading(boolean autoLoading) {
303     this.autoLoading = autoLoading;
304   // setAutoLoading
305 
306   /** Is the resource autoloading? */
307   public boolean isAutoLoading() { return autoLoading; }
308 
309   /** Private flag */
310   protected boolean priv = false;
311 
312   /** Set method for resource private flag */
313   public void setPrivate(boolean priv) {
314     this.priv = priv;
315   // setPrivate
316 
317   /** Is the resource private? */
318   public boolean isPrivate() { return priv; }
319 
320   /** Tool flag */
321   protected boolean tool = false;
322 
323   /** Set method for resource tool flag */
324   public void setTool(boolean tool) {
325     this.tool = tool;
326   // setTool
327 
328   /** Is the resource a tool? */
329   public boolean isTool() { return tool; }
330   /** Is this a valid resource data configuration? If not, leave an
331     * error message that can be returned by <TT>getValidityMessage()</TT>.
332     */
333   public boolean isValid() {
334     boolean valid = true;
335     validityMessage = "";
336 //******************************
337 // here should check that the resource has all mandatory elements,
338 // e.g. class name, and non-presence of runtime params on LRs and VRs etc.
339 //******************************
340     if(getClassName() == null || getClassName().length() == 0){
341       validityMessage += "No class name provided for the resource!";
342       valid = false;
343     }
344     if(getName() == null || getName().length() == 0){
345       //no name provided.
346       setName(className.substring(className.lastIndexOf('.'1));
347     }
348     return valid;
349   // isValid()
350 
351   /** Status message set by isValid() */
352   protected String validityMessage = "";
353 
354   /** Get validity statues message. */
355   public String getValidityMessage() { return validityMessage; }
356 
357   /////////////////////////////////////////////////////
358   // Fields added for GUI element
359   /////////////////////////////////////////////////////
360   /** This type indicates that the resource is not a GUI */
361   public static final int NULL_GUI = 0;
362   /**This type indicates that the resource goes into the large area of GATE GUI*/
363   public static final int LARGE_GUI = 1;
364   /**This type indicates that the resource goes into the small area of GATE GUI*/
365   public static final int SMALL_GUI = 2;
366   /** A filed which can have one of the 3 predefined values. See above.*/
367   protected int guiType = NULL_GUI;
368   /** Whether or not this viewer will be the default one*/
369   protected boolean isMainView = false;
370   /** The full class name of the resource displayed by this viewer.*/
371   protected String resourceDisplayed = null;
372   /** The full type name of the annotation displayed by this viewer.*/
373   protected String annotationTypeDisplayed = null;
374   /** A simple mutator for guiType field*/
375   public void setGuiType(int aGuiType){guiType = aGuiType;}
376   /** A simple accessor for guiType field*/
377   public int getGuiType(){return guiType;}
378   /** A simple mutator for isMainView field*/
379   public void setIsMainView(boolean mainView){isMainView = mainView;}
380   /** A simple accessor for isMainView field*/
381   public boolean isMainView(){return isMainView;}
382   /** A simple mutator for resourceDisplayed field*/
383   public void setResourceDisplayed(String aResourceDisplayed){
384     resourceDisplayed = aResourceDisplayed;
385   }// setResourceDisplayed
386   /** A simple accessor for resourceDisplayed field*/
387   public String getResourceDisplayed(){return resourceDisplayed;}
388   /** A simple mutator for annotationTypeDisplayed field*/
389   public void setAnnotationTypeDisplayed(String anAnnotationTypeDisplayed){
390     annotationTypeDisplayed = anAnnotationTypeDisplayed;
391   }// setAnnotationTypeDisplayed
392   /** A simple accessor for annotationTypeDisplayed field*/
393   public String getAnnotationTypeDisplayed(){return annotationTypeDisplayed;}
394 // ResourceData