1   /*
2    *  ResourceData.java
3    *
4    *  Copyright (c) 1998-2005, The University of Sheffield.
5    *
6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
7    *  software, licenced under the GNU Library General Public License,
8    *  Version 2, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *
11   *  Hamish Cunningham, 1/Sept/2000
12   *
13   *  $Id: ResourceData.java,v 1.22 2006/03/22 11:07:26 valyt Exp $
14   */
15  
16  package gate.creole;
17  
18  import java.io.Serializable;
19  import java.net.URL;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import gate.*;
24  import gate.util.*;
25  
26  /** Models an individual CREOLE resource metadata, plus configuration data,
27    * plus the instantiations of the resource current within the system.
28    * Some metadata elements are used by GATE to load resources, or index
29    * the members of the CREOLE register; some are used during resource
30    * parameterisation and initialisation.
31    * Metadata elements which are used by the CREOLE registration and loading
32    * mechanisms are properties of ResourceData implementations and have their
33    * own get/set methods. Other metadata elements are made features of the
34    * ResourceData. So, for example, if you add an element "FunkyElementThaing"
35    * to the metadata of a resource, this will be made a feature of that
36    * resource's ResourceData.
37    * @see CreoleRegister
38    */
39  public class ResourceData extends AbstractFeatureBearer implements Serializable
40  {
41  
42    /** Debug flag */
43    protected static final boolean DEBUG = false;
44  
45    protected static final String DEFAULT_LR_ICON = "lr.gif";
46    protected static final String DEFAULT_PR_ICON = "pr.gif";
47    protected static final String DEFAULT_OTHER_ICON = "controller.gif";
48    /** Construction */
49    public ResourceData() {  }// ResourceData
50  
51    /** String representation */
52    public String toString() {
53      int noInst = (instantiationStack == null) ? 0: instantiationStack.size();
54  /*
55      int noSmallViews = (smallViews == null) ? 0: smallViews.size();
56      int noViews = (views == null) ? 0: views.size();
57  */
58      StringBuffer s = new StringBuffer(
59        "ResourceDataImpl, name=" + name + "; className=" + className +
60        "; jarFileName=" + jarFileName + "; jarFileUrl=" + jarFileUrl +
61        "; xmlFileName=" + xmlFileName + "; xmlFileUrl=" + xmlFileUrl +
62        "; isAutoLoading=" + autoLoading + "; numberInstances=" + noInst +
63        "; isPrivate=" + priv +"; isTool="+ tool +
64        "; validityMessage=" + validityMessage +
65        "; interfaceName=" + interfaceName +
66        "; guiType=" + guiType +
67        "; mainViewer=" + isMainView +
68        "; resourceDisplayed=" + resourceDisplayed +
69        "; annotationTypeDisplayed=" + annotationTypeDisplayed +
70        "; parameterList=" + parameterList +
71        "; features=" + features
72      );
73      return s.toString();
74    } // toString
75  
76    /** Equality: two resource data objects are the same if they have the
77      * same name
78      */
79    public boolean equals(Object other) {
80      if(name.equals(((ResourceData) other).getName()))
81        return true;
82      return false;
83    } // equals
84  
85    /** Hashing, based on the name field of the object */
86    public int hashCode() {
87      return name.hashCode();
88    } // hashCode
89  
90    /** The name of the resource */
91    protected String name;
92  
93    /** Set method for the resource name */
94    public void setName(String name) { this.name = name; }
95  
96    /** Get method for the resource name */
97    public String getName() { return name; }
98  
99    /** Location of an icon for the resource */
100   protected String icon;
101 
102   /** Set method for the resource icon */
103   public void setIcon(String icon) { this.icon = icon; }
104 
105   /** Get method for the resource icon */
106   public String getIcon() { 
107     //if icon not set try and guess it
108     if(icon == null){
109       icon = guessIcon();
110     }
111     return icon; 
112   }
113 
114   /**
115    * Makes the best attempt of guessing an appropriate icon for this resource 
116    * type based on whether it is a Language Resource, a Processing Resource, or
117    * something else.
118    * @return a String representing the file name for most appropriate icon for 
119    * this resource type.
120    */
121   protected String guessIcon(){
122     //if no class set we can't do any guessing
123     if(className == null) return DEFAULT_OTHER_ICON;
124     if(resourceClass == null) return DEFAULT_OTHER_ICON;
125     if(LanguageResource.class.isAssignableFrom(resourceClass)) 
126       return DEFAULT_LR_ICON;
127     if(ProcessingResource.class.isAssignableFrom(resourceClass)) 
128       return DEFAULT_PR_ICON;
129     return DEFAULT_OTHER_ICON;
130   }
131   
132   /** The stack of instantiations */
133   protected WeakBumpyStack instantiationStack = new WeakBumpyStack();
134 
135   /** This list contains all instances loaded from creole.xml with
136    *  AUTOINSTANCE tag. The idea is that we don't want to loose them from the
137    *  system, because of the WeakBumpyStack
138    */
139   protected List persistantInstantiationList = new ArrayList();
140 
141   /** Get the list of instantiations of resources */
142   public WeakBumpyStack getInstantiations() {
143     return instantiationStack;
144   } // getInstantiations
145 
146   /** Add an instantiation of the resource to the register of these */
147   public void addInstantiation(Resource resource) {
148     instantiationStack.push(resource);
149   } // addInstantiation
150 
151   /** This method makes a certain resource persistent by adding it into a
152     * persistantInstantiationList. It is used especially with AUTOINSTANCE tag
153     * in creole xml.
154     */
155   public void makeInstantiationPersistant(Resource resource) {
156     persistantInstantiationList.add(resource);
157   } // makeInstantiationPersistant
158 
159   /** Remove an instantiation of the resource from the register of these */
160   public void removeInstantiation(Resource resource) {
161     instantiationStack.remove(resource);
162     persistantInstantiationList.remove(resource);
163   } // removeInstantiation
164 
165   /** Bump an instantiation to the top of the instantiation stack */
166   public void bumpInstantiation(Resource resource) {
167     instantiationStack.bump(resource);
168   } // bumpInstantiation
169 
170   /** The class name of the resource */
171   protected String className;
172 
173   /** Set method for the resource class name */
174   public void setClassName(String className) { this.className = className; }
175 
176   /** Get method for the resource class name */
177   public String getClassName() { return className; }
178 
179   /** The interface name of the resource */
180   protected String interfaceName;
181 
182   /** Set method for the resource interface name */
183   public void setInterfaceName(String interfaceName) {
184     this.interfaceName = interfaceName;
185   } // setInterfaceName
186 
187   /** Get method for the resource interface name */
188   public String getInterfaceName() { return interfaceName; }
189 
190   /** The class of the resource */
191   protected Class resourceClass;
192 
193   /** Set method for the resource class */
194   public void setResourceClass(Class resourceClass) {
195     this.resourceClass = resourceClass;
196   } // setResourceClass
197 
198   /** Get method for the resource class. Asks the GATE class loader
199     * to load it, if it is not already present.
200     */
201   public Class getResourceClass() throws ClassNotFoundException {
202     if(resourceClass == null) {
203       GateClassLoader classLoader = Gate.getClassLoader();
204       resourceClass = classLoader.loadClass(className);
205     }
206 
207     return resourceClass;
208   } // getResourceClass
209 
210   /** The jar file name of the resource */
211   protected String jarFileName;
212 
213   /** Set method for the resource jar file name */
214   public void setJarFileName(String jarFileName) {
215     this.jarFileName = jarFileName;
216   } // setJarFileName
217 
218   /** Get method for the resource jar file name */
219   public String getJarFileName() { return jarFileName; }
220 
221   /** The jar file URL of the resource */
222   protected URL jarFileUrl;
223 
224   /** Set method for the resource jar file URL */
225   public void setJarFileUrl(URL jarFileUrl) { this.jarFileUrl = jarFileUrl; }
226 
227   /** Get method for the resource jar file URL */
228   public URL getJarFileUrl() { return jarFileUrl; }
229 
230   /** The xml file name of the resource */
231   protected String xmlFileName;
232 
233   /** The xml file URL of the resource */
234   protected URL xmlFileUrl;
235 
236   /** Set the URL to the creole.xml file that defines this resource */
237   public void setXmlFileUrl(URL xmlFileUrl) { this.xmlFileUrl = xmlFileUrl; }
238 
239   /** Get the URL to the creole.xml file that defines this resource */
240   public URL getXmlFileUrl() { return xmlFileUrl; }
241 
242   /** The comment string */
243   protected String comment;
244 
245   /** Get method for the resource comment */
246   public String getComment() { return comment; }
247 
248   /** Set method for the resource comment */
249   public void setComment(String comment) { this.comment = comment; }
250 
251   /** The set of parameter lists */
252   protected ParameterList parameterList = new ParameterList();
253 
254   /** Set the parameter list */
255   public void setParameterList(ParameterList parameterList) {
256     this.parameterList = parameterList;
257   } // addParameterList
258 
259   /** Get the parameter list */
260   public ParameterList getParameterList() { return parameterList; }
261 
262   /** Autoloading flag */
263   protected boolean autoLoading;
264 
265   /** Set method for resource autoloading flag */
266   public void setAutoLoading(boolean autoLoading) {
267     this.autoLoading = autoLoading;
268   } // setAutoLoading
269 
270   /** Is the resource autoloading? */
271   public boolean isAutoLoading() { return autoLoading; }
272 
273   /** Private flag */
274   protected boolean priv = false;
275 
276   /** Set method for resource private flag */
277   public void setPrivate(boolean priv) {
278     this.priv = priv;
279   } // setPrivate
280 
281   /** Is the resource private? */
282   public boolean isPrivate() { return priv; }
283 
284   /** Tool flag */
285   protected boolean tool = false;
286 
287   /** Set method for resource tool flag */
288   public void setTool(boolean tool) {
289     this.tool = tool;
290   } // setTool
291 
292   /** Is the resource a tool? */
293   public boolean isTool() { return tool; }
294   /** Is this a valid resource data configuration? If not, leave an
295     * error message that can be returned by <TT>getValidityMessage()</TT>.
296     */
297   public boolean isValid() {
298     boolean valid = true;
299 //******************************
300 // here should check that the resource has all mandatory elements,
301 // e.g. class name, and non-presence of runtime params on LRs and VRs etc.
302 //******************************
303     return valid;
304   } // isValid()
305 
306   /** Status message set by isValid() */
307   protected String validityMessage = "";
308 
309   /** Get validity statues message. */
310   public String getValidityMessage() { return validityMessage; }
311 
312   /////////////////////////////////////////////////////
313   // Fields added for GUI element
314   /////////////////////////////////////////////////////
315   /** This type indicates that the resource is not a GUI */
316   public static final int NULL_GUI = 0;
317   /**This type indicates that the resource goes into the large area of GATE GUI*/
318   public static final int LARGE_GUI = 1;
319   /**This type indicates that the resource goes into the small area of GATE GUI*/
320   public static final int SMALL_GUI = 2;
321   /** A filed which can have one of the 3 predefined values. See above.*/
322   protected int guiType = NULL_GUI;
323   /** Whether or not this viewer will be the default one*/
324   protected boolean isMainView = false;
325   /** The full class name of the resource displayed by this viewer.*/
326   protected String resourceDisplayed = null;
327   /** The full type name of the annotation displayed by this viewer.*/
328   protected String annotationTypeDisplayed = null;
329   /** A simple mutator for guiType field*/
330   public void setGuiType(int aGuiType){guiType = aGuiType;}
331   /** A simple accessor for guiType field*/
332   public int getGuiType(){return guiType;}
333   /** A simple mutator for isMainView field*/
334   public void setIsMainView(boolean mainView){isMainView = mainView;}
335   /** A simple accessor for isMainView field*/
336   public boolean isMainView(){return isMainView;}
337   /** A simple mutator for resourceDisplayed field*/
338   public void setResourceDisplayed(String aResourceDisplayed){
339     resourceDisplayed = aResourceDisplayed;
340   }// setResourceDisplayed
341   /** A simple accessor for resourceDisplayed field*/
342   public String getResourceDisplayed(){return resourceDisplayed;}
343   /** A simple mutator for annotationTypeDisplayed field*/
344   public void setAnnotationTypeDisplayed(String anAnnotationTypeDisplayed){
345     annotationTypeDisplayed = anAnnotationTypeDisplayed;
346   }// setAnnotationTypeDisplayed
347   /** A simple accessor for annotationTypeDisplayed field*/
348   public String getAnnotationTypeDisplayed(){return annotationTypeDisplayed;}
349 } // ResourceData
350