1
15
16 package gate.creole;
17
18 import java.io.*;
19 import java.net.MalformedURLException;
20 import java.net.URL;
21 import java.util.*;
22
23 import javax.swing.Icon;
24 import javax.xml.parsers.*;
25
26 import org.jdom.*;
27 import org.jdom.input.SAXBuilder;
28 import org.xml.sax.SAXException;
29 import org.xml.sax.helpers.DefaultHandler;
30
31 import gate.*;
32 import gate.Gate.DirectoryInfo;
33 import gate.Gate.ResourceInfo;
34 import gate.event.CreoleEvent;
35 import gate.event.CreoleListener;
36 import gate.gui.MainFrame;
37 import gate.swing.XJTable;
38 import gate.util.*;
39
40
41
52 public class CreoleRegisterImpl extends HashMap
53 implements CreoleRegister, CreoleListener
54 {
55
56 protected static final boolean DEBUG = false;
57
58
59 protected Set directories;
60
61
62 protected transient SAXParser parser = null;
63
64
68 public CreoleRegisterImpl() throws GateException {
69
70 directories = new HashSet();
72 lrTypes = new HashSet();
73 prTypes = new HashSet();
74 vrTypes = new LinkedList();
75 toolTypes = new HashSet();
76
77 try {
79 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
81
82 saxParserFactory.setValidating(false);
85 saxParserFactory.setNamespaceAware(true);
87
88 parser = saxParserFactory.newSAXParser();
90
91 } catch (SAXException e) {
92 if(DEBUG) Out.println(e);
93 throw(new GateException(e));
94 } catch (ParserConfigurationException e) {
95 if(DEBUG) Out.println(e);
96 throw(new GateException(e));
97 }
98
99 }
101
104 public void addDirectory(URL directoryUrl) {
105 directories.add(directoryUrl);
106 }
108
109 public Set getDirectories() {
110 return Collections.unmodifiableSet(directories);
111 }
113
119 public void registerDirectories() throws GateException {
120 Iterator iter = directories.iterator();
121
122 while(iter.hasNext()) {
123 URL directoryUrl = (URL) iter.next();
124 registerDirectories(directoryUrl);
125 }
126 }
128
134 public void registerDirectories(URL directoryUrl) throws GateException {
135
136 String urlName = directoryUrl.toExternalForm();
138 if(urlName.toLowerCase().endsWith("creole.xml")) {
139 throw new GateException(
140 "CREOLE directory URLs should point to the parent location of " +
141 "the creole.xml file, not the file itself; bad URL was: " + urlName
142 );
143 }
144
145 URL directoryXmlFileUrl = directoryUrl;
147 String separator = "/";
148 if(urlName.endsWith(separator)) separator = "";
149 try {
150 directoryXmlFileUrl = new URL(urlName + separator + "creole.xml");
151 } catch(MalformedURLException e) {
152 throw(new GateException("bad creole.xml URL, based on " + urlName));
153 }
154
155 directories.add(directoryUrl);
158 try {
160 parseDirectory(directoryXmlFileUrl.openStream(), directoryUrl,
161 directoryXmlFileUrl);
162 System.out.println("CREOLE plugin loaded: " + directoryUrl);
163 } catch(IOException e) {
164 directories.remove(directoryUrl);
166 throw(new GateException("couldn't open creole.xml: " + e.toString()));
167 }
168 Gate.addKnownPlugin(directoryUrl);
170 }
172
177 protected void parseDirectory(InputStream directoryStream, URL directoryUrl,
178 URL creoleFileUrl)
179 throws GateException
180 {
181 try {
184 DefaultHandler handler = new CreoleXmlHandler(this, directoryUrl,
185 creoleFileUrl);
186 parser.parse(directoryStream, handler);
187 if(DEBUG) {
188 Out.prln(
189 "done parsing " +
190 ((directoryUrl == null) ? "null" : directoryUrl.toString())
191 );
192 }
193 } catch (IOException e) {
194 throw(new GateException(e));
195 } catch (SAXException se) {
196 if(DEBUG) se.printStackTrace(Err.getPrintWriter());
197 throw(new GateException(se));
198 }
199
200 }
202
206 public void registerBuiltins() throws GateException {
207
208 try {
209 URL creoleFileURL = Gate.class.getResource(Files.getResourcePath() +
210 "/creole/creole.xml");
211 parseDirectory(creoleFileURL.openStream(),
212 Gate.class.getResource(Files.getResourcePath() +
213 "/creole"),
214 creoleFileURL);
215 } catch(IOException e) {
216 if (DEBUG) Out.println(e);
217 throw(new GateException(e));
218 }
219 }
221
228 public File createCreoleDirectoryFile(File directoryFile, Set jarFileNames)
229 {
230 throw new LazyProgrammerException();
237 }
239
243 public Object put(Object key, Object value) {
244 ResourceData rd = (ResourceData) value;
245
246 Class resClass = null;
248 try {
249 resClass = rd.getResourceClass();
250 } catch(ClassNotFoundException e) {
251 throw new GateRuntimeException(
252 "Couldn't get resource class from the resource data:" + e
253 );
254 }
255
256 if(LanguageResource.class.isAssignableFrom(resClass)) {
258 if(DEBUG) Out.prln("LR: " + resClass);
259 if(lrTypes == null) lrTypes = new HashSet(); lrTypes.add(rd.getClassName());
261 } else if(ProcessingResource.class.isAssignableFrom(resClass)) {
262 if(DEBUG) {
263 Out.prln("PR: " + resClass);
264 }
267 if(prTypes == null) prTypes = new HashSet(); prTypes.add(rd.getClassName());
269 } else if(VisualResource.class.isAssignableFrom(resClass)) {
270 if(DEBUG) Out.prln("VR: " + resClass);
271 if(vrTypes == null) vrTypes = new LinkedList(); if(!vrTypes.contains(rd.getClassName())) vrTypes.add(rd.getClassName());
274 }else if(Controller.class.isAssignableFrom(resClass)) {
275 if(DEBUG) Out.prln("Controller: " + resClass);
276 if(controllerTypes == null) controllerTypes = new HashSet(); controllerTypes.add(rd.getClassName());
278 }
279
280 if(rd.isTool()) {
282 if(toolTypes == null) toolTypes = new HashSet(); toolTypes.add(rd.getClassName());
284 }
285
286 return super.put(key, value);
287 }
289
293 public void removeDirectory(URL directory){
294 directories.remove(directory);
295 DirectoryInfo dInfo = (DirectoryInfo)Gate.getDirectoryInfo(directory);
296 if(dInfo != null){
297 Iterator resIter = dInfo.getResourceInfoList().iterator();
298 while(resIter.hasNext()){
299 ResourceInfo rInfo = (ResourceInfo)resIter.next();
300 remove(rInfo.getResourceClassName());
301 }
302 System.out.println("CREOLE plugin unloaded: " + directory);
303 }
304
305 }
306
307
310 public Object remove(Object key) {
311 ResourceData rd = (ResourceData) get(key);
312 if(rd == null) return null;
313
314 if(DEBUG) {
315 Out.prln(key);
316 Out.prln(rd);
317 }
318 if(LanguageResource.class.isAssignableFrom(rd.getClass()))
319 lrTypes.remove(rd.getClassName());
320 else if(ProcessingResource.class.isAssignableFrom(rd.getClass()))
321 prTypes.remove(rd.getClassName());
322 else if(VisualResource.class.isAssignableFrom(rd.getClass()))
323 vrTypes.remove(rd.getClassName());
324
325 if(rd.isTool())
327 toolTypes.remove(rd.getClassName());
328
329 return super.remove(key);
330 }
332
335 public void clear() {
336 lrTypes.clear();
337 prTypes.clear();
338 vrTypes.clear();
339 toolTypes.clear();
340 directories.clear();
341 super.clear();
342 }
344
345 public Set getLrTypes() { return Collections.unmodifiableSet(lrTypes);}
346
347
348 public Set getPrTypes() { return Collections.unmodifiableSet(prTypes);}
349
350
351 public Set getVrTypes() { return Collections.unmodifiableSet(new HashSet(vrTypes));}
352
353
354 public Set getControllerTypes() {
355 return Collections.unmodifiableSet(controllerTypes);
356 }
357
358
359 public Set getToolTypes() { return Collections.unmodifiableSet(toolTypes);}
360
361
362 public List getLrInstances() {
363 Set lrTypeSet = getLrTypes();
364 List instances = new ArrayList();
365
366 Iterator iter = lrTypeSet.iterator();
367 while(iter.hasNext()) {
368 String type = (String) iter.next();
369 instances.addAll(getLrInstances(type));
370 } return Collections.unmodifiableList(instances);
372 }
374
375 public List getPrInstances() {
376 Set prTypeSet = getPrTypes();
377 List instances = new ArrayList();
378
379 Iterator iter = prTypeSet.iterator();
380 while(iter.hasNext()) {
381 String type = (String) iter.next();
382 instances.addAll(getPrInstances(type));
383 }
385 return Collections.unmodifiableList(instances);
386 }
388
389 public List getVrInstances() {
390 Set vrTypeSet = getVrTypes();
391 List instances = new ArrayList();
392
393 Iterator iter = vrTypeSet.iterator();
394 while(iter.hasNext()) {
395 String type = (String) iter.next();
396 instances.addAll(getVrInstances(type));
397 }
399 return Collections.unmodifiableList(instances);
400 }
402
403 public List getLrInstances(String resourceTypeName) {
404 ResourceData resData = (ResourceData) get(resourceTypeName);
405 if(resData == null)
406 return Collections.unmodifiableList(new ArrayList());
407
408 return Collections.unmodifiableList(resData.getInstantiations());
409 }
411
412 public List getPrInstances(String resourceTypeName) {
413 ResourceData resData = (ResourceData) get(resourceTypeName);
414 if(resData == null)
415 return Collections.unmodifiableList(new ArrayList());
416
417 return Collections.unmodifiableList(resData.getInstantiations());
418 }
420
421 public List getVrInstances(String resourceTypeName) {
422 ResourceData resData = (ResourceData) get(resourceTypeName);
423 if(resData == null)
424 return Collections.unmodifiableList(new ArrayList());
425
426 return Collections.unmodifiableList(resData.getInstantiations());
427 }
429
430 public List getPublicLrInstances() {
431 return Collections.unmodifiableList(getPublics(getLrInstances()));
432 }
434
435 public List getPublicPrInstances() {
436 return Collections.unmodifiableList(getPublics(getPrInstances()));
437 }
439
440 public List getPublicVrInstances() {
441 return Collections.unmodifiableList(getPublics(getVrInstances()));
442 }
444
445 public List getPublicLrTypes() {
446 return Collections.unmodifiableList(getPublicTypes(getLrTypes()));
447 }
449
450 public List getPublicPrTypes() {
451 return Collections.unmodifiableList(getPublicTypes(getPrTypes()));
452 }
454
455 public List getPublicVrTypes() {
456 return Collections.unmodifiableList(getPublicTypes(getVrTypes()));
457 }
459
460 public List getPublicControllerTypes() {
461 return Collections.unmodifiableList(getPublicTypes(getControllerTypes()));
462 }
464
465
469 public List getAllInstances(String type) throws GateException{
470 Iterator typesIter = keySet().iterator();
471 List res = new ArrayList();
472 Class targetClass;
473 try{
474 targetClass = Gate.getClassLoader().loadClass(type);
475 }catch(ClassNotFoundException cnfe){
476 throw new GateException("Invalid type " + type);
477 }
478 while(typesIter.hasNext()){
479 String aType = (String)typesIter.next();
480 Class aClass;
481 try{
482 aClass = Gate.getClassLoader().loadClass(aType);
483 if(targetClass.isAssignableFrom(aClass)){
484 Iterator newInstancesIter = ((ResourceData)get(aType)).
486 getInstantiations().iterator();
487 while(newInstancesIter.hasNext()){
488 Resource instance = (Resource)newInstancesIter.next();
489 if(!Gate.getHiddenAttribute(instance.getFeatures())){
490 res.add(instance);
491 }
492 }
493 }
494 }catch(ClassNotFoundException cnfe){
495 throw new LuckyException(
496 "A type registered in the creole register does not exist in the VM!");
497 }
498
499 }
501 return res;
502 }
503
504
513 public List getLargeVRsForResource(String resourceClassName){
514 return getVRsForResource(resourceClassName, ResourceData.LARGE_GUI);
515 }
517
526 public List getSmallVRsForResource(String resourceClassName){
527 return getVRsForResource(resourceClassName, ResourceData.SMALL_GUI);
528 }
530
540 private List getVRsForResource(String resourceClassName, int guiType){
541 if (resourceClassName == null)
543 return Collections.unmodifiableList(new ArrayList());
544 Class resourceClass = null;
546 GateClassLoader classLoader = Gate.getClassLoader();
547 try{
548 resourceClass = classLoader.loadClass(resourceClassName);
549 } catch (ClassNotFoundException ex){
550 throw new GateRuntimeException(
551 "Couldn't get resource class from the resource name:" + ex
552 );
553 } LinkedList responseList = new LinkedList();
555 String defaultVR = null;
556 Iterator vrIterator = vrTypes.iterator();
559 while (vrIterator.hasNext()){
560 String vrClassName = (String) vrIterator.next();
561 ResourceData vrResourceData = (ResourceData) this.get(vrClassName);
562 if (vrResourceData == null)
563 throw new GateRuntimeException(
564 "Couldn't get resource data for VR called " + vrClassName
565 );
566 if (vrResourceData.getGuiType() == guiType){
567 String resourceDisplayed = vrResourceData.getResourceDisplayed();
568 if (resourceDisplayed != null){
569 Class resourceDisplayedClass = null;
570 try{
571 resourceDisplayedClass = classLoader.loadClass(resourceDisplayed);
572 } catch (ClassNotFoundException ex){
573 throw new GateRuntimeException(
574 "Couldn't get resource class from the resource name :" +
575 resourceDisplayed + " " +ex );
576 } if (resourceDisplayedClass.isAssignableFrom(resourceClass)){
578 responseList.add(vrClassName);
579 if (vrResourceData.isMainView()){
580 defaultVR = vrClassName;
581 } } } } } if (defaultVR != null){
587 responseList.remove(defaultVR);
588 responseList.addFirst(defaultVR);
589 } return Collections.unmodifiableList(responseList);
591 }
593
599 public List getAnnotationVRs(){
600 LinkedList responseList = new LinkedList();
601 String defaultVR = null;
602 Iterator vrIterator = vrTypes.iterator();
603 while (vrIterator.hasNext()){
604 String vrClassName = (String) vrIterator.next();
605 ResourceData vrResourceData = (ResourceData) this.get(vrClassName);
606 if (vrResourceData == null)
607 throw new GateRuntimeException(
608 "Couldn't get resource data for VR called " + vrClassName
609 );
610 Class vrResourceClass = null;
611 try{
612 vrResourceClass = vrResourceData.getResourceClass();
613 } catch(ClassNotFoundException ex){
614 throw new GateRuntimeException(
615 "Couldn't create a class object for VR called " + vrClassName
616 );
617 } if ( vrResourceData.getGuiType() == ResourceData.NULL_GUI &&
620 vrResourceData.getAnnotationTypeDisplayed() == null &&
621 vrResourceData.getResourceDisplayed() == null &&
622 gate.creole.AnnotationVisualResource.class.
623 isAssignableFrom(vrResourceClass)){
624
625 responseList.add(vrClassName);
626 if (vrResourceData.isMainView())
627 defaultVR = vrClassName;
628 } } if (defaultVR != null){
631 responseList.remove(defaultVR);
632 responseList.addFirst(defaultVR);
633 } return Collections.unmodifiableList(responseList);
635 }
637
642 public List getAnnotationVRs(String annotationType){
643 if (annotationType == null)
644 return Collections.unmodifiableList(new ArrayList());
645 LinkedList responseList = new LinkedList();
646 String defaultVR = null;
647 Iterator vrIterator = vrTypes.iterator();
648 while (vrIterator.hasNext()){
649 String vrClassName = (String) vrIterator.next();
650 ResourceData vrResourceData = (ResourceData) this.get(vrClassName);
651 if (vrResourceData == null)
652 throw new GateRuntimeException(
653 "Couldn't get resource data for VR called " + vrClassName
654 );
655 Class vrResourceClass = null;
656 try{
657 vrResourceClass = vrResourceData.getResourceClass();
658 } catch(ClassNotFoundException ex){
659 throw new GateRuntimeException(
660 "Couldn't create a class object for VR called " + vrClassName
661 );
662 } if ( vrResourceData.getGuiType() == ResourceData.NULL_GUI &&
665 vrResourceData.getAnnotationTypeDisplayed() != null &&
666 gate.creole.AnnotationVisualResource.class.
667 isAssignableFrom(vrResourceClass)){
668
669 String annotationTypeDisplayed =
670 vrResourceData.getAnnotationTypeDisplayed();
671 if (annotationTypeDisplayed.equals(annotationType)){
672 responseList.add(vrClassName);
673 if (vrResourceData.isMainView())
674 defaultVR = vrClassName;
675 } } } if (defaultVR != null){
679 responseList.remove(defaultVR);
680 responseList.addFirst(defaultVR);
681 } return Collections.unmodifiableList(responseList);
683 }
685
688 public void setResourceName(Resource res, String newName){
689 String oldName = res.getName();
690 res.setName(newName);
691 fireResourceRenamed(res, oldName, newName);
692 }
693
694
695
699 public List getVREnabledAnnotationTypes(){
700 LinkedList responseList = new LinkedList();
701 Iterator vrIterator = vrTypes.iterator();
702 while (vrIterator.hasNext()){
703 String vrClassName = (String) vrIterator.next();
704 ResourceData vrResourceData = (ResourceData) this.get(vrClassName);
705 if (vrResourceData == null)
706 throw new GateRuntimeException(
707 "Couldn't get resource data for VR called " + vrClassName
708 );
709 if ( vrResourceData.getGuiType() == ResourceData.NULL_GUI &&
711 vrResourceData.getAnnotationTypeDisplayed() != null ){
712
713 String annotationTypeDisplayed =
714 vrResourceData.getAnnotationTypeDisplayed();
715 responseList.add(annotationTypeDisplayed);
716 } } return Collections.unmodifiableList(responseList);
719 }
721
722
723
724 protected List getPublics(List instances) {
725 Iterator iter = instances.iterator();
726 List publics = new ArrayList();
727
728 while(iter.hasNext()) {
731 Resource res = (Resource) iter.next();
732 ResourceData rd = (ResourceData) get(res.getClass().getName());
733 if(! rd.isPrivate()) publics.add(res);
734 }
735
736 return Collections.unmodifiableList(publics);
737 }
739
740 protected List getPublicTypes(Collection types){
741 Iterator iter = types.iterator();
742 List publics = new ArrayList();
743 while(iter.hasNext()){
744 String oneType = (String)iter.next();
745 ResourceData rData = (ResourceData)get(oneType);
746 if(rData != null && !rData.isPrivate()) publics.add(oneType);
747 }
748 return Collections.unmodifiableList(publics);
749 }
751 public synchronized void removeCreoleListener(CreoleListener l) {
752 if (creoleListeners != null && creoleListeners.contains(l)) {
753 Vector v = (Vector) creoleListeners.clone();
754 v.removeElement(l);
755 creoleListeners = v;
756 }
757 }
758
759 public synchronized void addCreoleListener(CreoleListener l) {
760 Vector v = creoleListeners == null ? new Vector(2) : (Vector) creoleListeners.clone();
761 if (!v.contains(l)) {
762 v.addElement(l);
763 creoleListeners = v;
764 }
765 }
767
771
772
778
783
788
789
790 protected Set lrTypes;
791
792
793 protected Set prTypes;
794
795
796 protected List vrTypes;
797
798
799 protected Set controllerTypes;
800
801
802 protected Set toolTypes;
803
804 private transient Vector creoleListeners;
805 protected void fireResourceLoaded(CreoleEvent e) {
806 if (creoleListeners != null) {
807 Vector listeners = creoleListeners;
808 int count = listeners.size();
809 for (int i = 0; i < count; i++) {
810 ((CreoleListener) listeners.elementAt(i)).resourceLoaded(e);
811 }
812 }
813 }
814
815 protected void fireResourceUnloaded(CreoleEvent e) {
816 if (creoleListeners != null) {
817 Vector listeners = creoleListeners;
818 int count = listeners.size();
819 for (int i = 0; i < count; i++) {
820 ((CreoleListener) listeners.elementAt(i)).resourceUnloaded(e);
821 }
822 }
823 }
824
825 protected void fireResourceRenamed(Resource res, String oldName,
826 String newName) {
827 if (creoleListeners != null) {
828 Vector listeners = creoleListeners;
829 int count = listeners.size();
830 for (int i = 0; i < count; i++) {
831 ((CreoleListener) listeners.elementAt(i)).resourceRenamed(res,
832 oldName,
833 newName);
834 }
835 }
836 }
837
838 protected void fireDatastoreOpened(CreoleEvent e) {
839 if (creoleListeners != null) {
840 Vector listeners = creoleListeners;
841 int count = listeners.size();
842 for (int i = 0; i < count; i++) {
843 ((CreoleListener) listeners.elementAt(i)).datastoreOpened(e);
844 }
845 }
846 }
847 protected void fireDatastoreCreated(CreoleEvent e) {
848 if (creoleListeners != null) {
849 Vector listeners = creoleListeners;
850 int count = listeners.size();
851 for (int i = 0; i < count; i++) {
852 ((CreoleListener) listeners.elementAt(i)).datastoreCreated(e);
853 }
854 }
855 }
856
857 protected void fireDatastoreClosed(CreoleEvent e) {
858 if (creoleListeners != null) {
859 Vector listeners = creoleListeners;
860 int count = listeners.size();
861 for (int i = 0; i < count; i++) {
862 ((CreoleListener) listeners.elementAt(i)).datastoreClosed(e);
863 }
864 }
865 }
866
867 public void resourceLoaded(CreoleEvent e) {
868 fireResourceLoaded(e);
869 }
870
871 public void resourceUnloaded(CreoleEvent e) {
872 fireResourceUnloaded(e);
873 }
874
875 public void resourceRenamed(Resource resource, String oldName,
876 String newName){
877 fireResourceRenamed(resource, oldName, newName);
878 }
879
880 public void datastoreOpened(CreoleEvent e) {
881 fireDatastoreOpened(e);
882 }
883
884 public void datastoreCreated(CreoleEvent e) {
885 fireDatastoreCreated(e);
886 }
887
888 public void datastoreClosed(CreoleEvent e) {
889 fireDatastoreClosed(e);
890 }
891
892
893 }