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