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
107 public void addDirectory(URL directoryUrl) {
108 try{
109 registerDirectories(directoryUrl);
110 }catch(GateException ge){
111 throw new GateRuntimeException(ge);
112 }
113 }
115
116 public Set getDirectories() {
117 return Collections.unmodifiableSet(directories);
118 }
120
126 public void registerDirectories() throws GateException {
127 }
135
141 public void registerDirectories(URL directoryUrl) throws GateException {
142
143 String urlName = directoryUrl.toExternalForm();
145 if(urlName.toLowerCase().endsWith("creole.xml")) {
146 throw new GateException(
147 "CREOLE directory URLs should point to the parent location of " +
148 "the creole.xml file, not the file itself; bad URL was: " + urlName
149 );
150 }
151 String separator = "/";
153 if(!urlName.endsWith(separator)){
154 urlName += separator;
155 try{
156 directoryUrl = new URL(urlName);
157 }catch(MalformedURLException mue){
158 throw new GateRuntimeException(mue);
159 }
160 }
161 URL directoryXmlFileUrl = directoryUrl;
163 try {
164 directoryXmlFileUrl = new URL(urlName + "creole.xml");
165 } catch(MalformedURLException e) {
166 throw(new GateException("bad creole.xml URL, based on " + urlName));
167 }
168
169 if(directories.add(directoryUrl)){
172 try {
174 parseDirectory(directoryXmlFileUrl.openStream(), directoryUrl,
175 directoryXmlFileUrl);
176 System.out.println("CREOLE plugin loaded: " + directoryUrl);
177 } catch(IOException e) {
178 directories.remove(directoryUrl);
180 throw(new GateException("couldn't open creole.xml: " + e.toString()));
181 }
182 Gate.addKnownPlugin(directoryUrl);
184 }
185 }
187
192 protected void parseDirectory(InputStream directoryStream, URL directoryUrl,
193 URL creoleFileUrl)
194 throws GateException
195 {
196 try {
199 DefaultHandler handler = new CreoleXmlHandler(this, directoryUrl,
200 creoleFileUrl);
201 parser.parse(directoryStream, handler);
202 if(DEBUG) {
203 Out.prln(
204 "done parsing " +
205 ((directoryUrl == null) ? "null" : directoryUrl.toString())
206 );
207 }
208 } catch (IOException e) {
209 throw(new GateException(e));
210 } catch (SAXException se) {
211 if(DEBUG) se.printStackTrace(Err.getPrintWriter());
212 throw(new GateException(se));
213 }
214
215 }
217
221 public void registerBuiltins() throws GateException {
222
223 try {
224 URL creoleFileURL = Gate.class.getResource(Files.getResourcePath() +
225 "/creole/creole.xml");
226 parseDirectory(creoleFileURL.openStream(),
227 Gate.class.getResource(Files.getResourcePath() +
228 "/creole"),
229 creoleFileURL);
230 } catch(IOException e) {
231 if (DEBUG) Out.println(e);
232 throw(new GateException(e));
233 }
234 }
236
243 public File createCreoleDirectoryFile(File directoryFile, Set jarFileNames)
244 {
245 throw new LazyProgrammerException();
252 }
254
258 public Object put(Object key, Object value) {
259 ResourceData rd = (ResourceData) value;
260
261 Class resClass = null;
263 try {
264 resClass = rd.getResourceClass();
265 } catch(ClassNotFoundException e) {
266 throw new GateRuntimeException(
267 "Couldn't get resource class from the resource data:" + e
268 );
269 }
270
271 if(LanguageResource.class.isAssignableFrom(resClass)) {
273 if(DEBUG) Out.prln("LR: " + resClass);
274 if(lrTypes == null) lrTypes = new HashSet(); lrTypes.add(rd.getClassName());
276 } else if(ProcessingResource.class.isAssignableFrom(resClass)) {
277 if(DEBUG) {
278 Out.prln("PR: " + resClass);
279 }
282 if(prTypes == null) prTypes = new HashSet(); prTypes.add(rd.getClassName());
284 } else if(VisualResource.class.isAssignableFrom(resClass)) {
285 if(DEBUG) Out.prln("VR: " + resClass);
286 if(vrTypes == null) vrTypes = new LinkedList(); if(!vrTypes.contains(rd.getClassName())) vrTypes.add(rd.getClassName());
289 }else if(Controller.class.isAssignableFrom(resClass)) {
290 if(DEBUG) Out.prln("Controller: " + resClass);
291 if(controllerTypes == null) controllerTypes = new HashSet(); controllerTypes.add(rd.getClassName());
293 }
294
295 if(rd.isTool()) {
297 if(toolTypes == null) toolTypes = new HashSet(); toolTypes.add(rd.getClassName());
299 }
300
301 return super.put(key, value);
302 }
304
308 public void removeDirectory(URL directory){
309 if(directories.remove(directory)){
310 DirectoryInfo dInfo = (DirectoryInfo)Gate.getDirectoryInfo(directory);
311 if(dInfo != null){
312 Iterator resIter = dInfo.getResourceInfoList().iterator();
313 while(resIter.hasNext()){
314 ResourceInfo rInfo = (ResourceInfo)resIter.next();
315 remove(rInfo.getResourceClassName());
316 }
317 }
318 System.out.println("CREOLE plugin unloaded: " + directory);
319 }
320 }
321
322
325 public Object remove(Object key) {
326 ResourceData rd = (ResourceData) get(key);
327 if(rd == null) return null;
328
329 if(DEBUG) {
330 Out.prln(key);
331 Out.prln(rd);
332 }
333 if(LanguageResource.class.isAssignableFrom(rd.getClass()))
334 lrTypes.remove(rd.getClassName());
335 else if(ProcessingResource.class.isAssignableFrom(rd.getClass()))
336 prTypes.remove(rd.getClassName());
337 else if(VisualResource.class.isAssignableFrom(rd.getClass()))
338 vrTypes.remove(rd.getClassName());
339
340 if(rd.isTool())
342 toolTypes.remove(rd.getClassName());
343
344 return super.remove(key);
345 }
347
350 public void clear() {
351 lrTypes.clear();
352 prTypes.clear();
353 vrTypes.clear();
354 toolTypes.clear();
355 directories.clear();
356 super.clear();
357 }
359
360 public Set getLrTypes() { return Collections.unmodifiableSet(lrTypes);}
361
362
363 public Set getPrTypes() { return Collections.unmodifiableSet(prTypes);}
364
365
366 public Set getVrTypes() { return Collections.unmodifiableSet(new HashSet(vrTypes));}
367
368
369 public Set getControllerTypes() {
370 return Collections.unmodifiableSet(controllerTypes);
371 }
372
373
374 public Set getToolTypes() { return Collections.unmodifiableSet(toolTypes);}
375
376
377 public List getLrInstances() {
378 Set lrTypeSet = getLrTypes();
379 List instances = new ArrayList();
380
381 Iterator iter = lrTypeSet.iterator();
382 while(iter.hasNext()) {
383 String type = (String) iter.next();
384 instances.addAll(getLrInstances(type));
385 } return Collections.unmodifiableList(instances);
387 }
389
390 public List getPrInstances() {
391 Set prTypeSet = getPrTypes();
392 List instances = new ArrayList();
393
394 Iterator iter = prTypeSet.iterator();
395 while(iter.hasNext()) {
396 String type = (String) iter.next();
397 instances.addAll(getPrInstances(type));
398 }
400 return Collections.unmodifiableList(instances);
401 }
403
404 public List getVrInstances() {
405 Set vrTypeSet = getVrTypes();
406 List instances = new ArrayList();
407
408 Iterator iter = vrTypeSet.iterator();
409 while(iter.hasNext()) {
410 String type = (String) iter.next();
411 instances.addAll(getVrInstances(type));
412 }
414 return Collections.unmodifiableList(instances);
415 }
417
418 public List getLrInstances(String resourceTypeName) {
419 ResourceData resData = (ResourceData) get(resourceTypeName);
420 if(resData == null)
421 return Collections.unmodifiableList(new ArrayList());
422
423 return Collections.unmodifiableList(resData.getInstantiations());
424 }
426
427 public List getPrInstances(String resourceTypeName) {
428 ResourceData resData = (ResourceData) get(resourceTypeName);
429 if(resData == null)
430 return Collections.unmodifiableList(new ArrayList());
431
432 return Collections.unmodifiableList(resData.getInstantiations());
433 }
435
436 public List getVrInstances(String resourceTypeName) {
437 ResourceData resData = (ResourceData) get(resourceTypeName);
438 if(resData == null)
439 return Collections.unmodifiableList(new ArrayList());
440
441 return Collections.unmodifiableList(resData.getInstantiations());
442 }
444
445 public List getPublicLrInstances() {
446 return Collections.unmodifiableList(getPublics(getLrInstances()));
447 }
449
450 public List getPublicPrInstances() {
451 return Collections.unmodifiableList(getPublics(getPrInstances()));
452 }
454
455 public List getPublicVrInstances() {
456 return Collections.unmodifiableList(getPublics(getVrInstances()));
457 }
459
460 public List getPublicLrTypes() {
461 return Collections.unmodifiableList(getPublicTypes(getLrTypes()));
462 }
464
465 public List getPublicPrTypes() {
466 return Collections.unmodifiableList(getPublicTypes(getPrTypes()));
467 }
469
470 public List getPublicVrTypes() {
471 return Collections.unmodifiableList(getPublicTypes(getVrTypes()));
472 }
474
475 public List getPublicControllerTypes() {
476 return Collections.unmodifiableList(getPublicTypes(getControllerTypes()));
477 }
479
480
484 public List getAllInstances(String type) throws GateException{
485 Iterator typesIter = keySet().iterator();
486 List res = new ArrayList();
487 Class targetClass;
488 try{
489 targetClass = Gate.getClassLoader().loadClass(type);
490 }catch(ClassNotFoundException cnfe){
491 throw new GateException("Invalid type " + type);
492 }
493 while(typesIter.hasNext()){
494 String aType = (String)typesIter.next();
495 Class aClass;
496 try{
497 aClass = Gate.getClassLoader().loadClass(aType);
498 if(targetClass.isAssignableFrom(aClass)){
499 Iterator newInstancesIter = ((ResourceData)get(aType)).
501 getInstantiations().iterator();
502 while(newInstancesIter.hasNext()){
503 Resource instance = (Resource)newInstancesIter.next();
504 if(!Gate.getHiddenAttribute(instance.getFeatures())){
505 res.add(instance);
506 }
507 }
508 }
509 }catch(ClassNotFoundException cnfe){
510 throw new LuckyException(
511 "A type registered in the creole register does not exist in the VM!");
512 }
513
514 }
516 return res;
517 }
518
519
528 public List getLargeVRsForResource(String resourceClassName){
529 return getVRsForResource(resourceClassName, ResourceData.LARGE_GUI);
530 }
532
541 public List getSmallVRsForResource(String resourceClassName){
542 return getVRsForResource(resourceClassName, ResourceData.SMALL_GUI);
543 }
545
555 private List getVRsForResource(String resourceClassName, int guiType){
556 if (resourceClassName == null)
558 return Collections.unmodifiableList(new ArrayList());
559 Class resourceClass = null;
561 GateClassLoader classLoader = Gate.getClassLoader();
562 try{
563 resourceClass = classLoader.loadClass(resourceClassName);
564 } catch (ClassNotFoundException ex){
565 throw new GateRuntimeException(
566 "Couldn't get resource class from the resource name:" + ex
567 );
568 } LinkedList responseList = new LinkedList();
570 String defaultVR = null;
571 Iterator vrIterator = vrTypes.iterator();
574 while (vrIterator.hasNext()){
575 String vrClassName = (String) vrIterator.next();
576 ResourceData vrResourceData = (ResourceData) this.get(vrClassName);
577 if (vrResourceData == null)
578 throw new GateRuntimeException(
579 "Couldn't get resource data for VR called " + vrClassName
580 );
581 if (vrResourceData.getGuiType() == guiType){
582 String resourceDisplayed = vrResourceData.getResourceDisplayed();
583 if (resourceDisplayed != null){
584 Class resourceDisplayedClass = null;
585 try{
586 resourceDisplayedClass = classLoader.loadClass(resourceDisplayed);
587 } catch (ClassNotFoundException ex){
588 throw new GateRuntimeException(
589 "Couldn't get resource class from the resource name :" +
590 resourceDisplayed + " " +ex );
591 } if (resourceDisplayedClass.isAssignableFrom(resourceClass)){
593 responseList.add(vrClassName);
594 if (vrResourceData.isMainView()){
595 defaultVR = vrClassName;
596 } } } } } if (defaultVR != null){
602 responseList.remove(defaultVR);
603 responseList.addFirst(defaultVR);
604 } return Collections.unmodifiableList(responseList);
606 }
608
614 public List getAnnotationVRs(){
615 LinkedList responseList = new LinkedList();
616 String defaultVR = null;
617 Iterator vrIterator = vrTypes.iterator();
618 while (vrIterator.hasNext()){
619 String vrClassName = (String) vrIterator.next();
620 ResourceData vrResourceData = (ResourceData) this.get(vrClassName);
621 if (vrResourceData == null)
622 throw new GateRuntimeException(
623 "Couldn't get resource data for VR called " + vrClassName
624 );
625 Class vrResourceClass = null;
626 try{
627 vrResourceClass = vrResourceData.getResourceClass();
628 } catch(ClassNotFoundException ex){
629 throw new GateRuntimeException(
630 "Couldn't create a class object for VR called " + vrClassName
631 );
632 } if ( vrResourceData.getGuiType() == ResourceData.NULL_GUI &&
635 vrResourceData.getAnnotationTypeDisplayed() == null &&
636 vrResourceData.getResourceDisplayed() == null &&
637 gate.creole.AnnotationVisualResource.class.
638 isAssignableFrom(vrResourceClass)){
639
640 responseList.add(vrClassName);
641 if (vrResourceData.isMainView())
642 defaultVR = vrClassName;
643 } } if (defaultVR != null){
646 responseList.remove(defaultVR);
647 responseList.addFirst(defaultVR);
648 } return Collections.unmodifiableList(responseList);
650 }
652
657 public List getAnnotationVRs(String annotationType){
658 if (annotationType == null)
659 return Collections.unmodifiableList(new ArrayList());
660 LinkedList responseList = new LinkedList();
661 String defaultVR = null;
662 Iterator vrIterator = vrTypes.iterator();
663 while (vrIterator.hasNext()){
664 String vrClassName = (String) vrIterator.next();
665 ResourceData vrResourceData = (ResourceData) this.get(vrClassName);
666 if (vrResourceData == null)
667 throw new GateRuntimeException(
668 "Couldn't get resource data for VR called " + vrClassName
669 );
670 Class vrResourceClass = null;
671 try{
672 vrResourceClass = vrResourceData.getResourceClass();
673 } catch(ClassNotFoundException ex){
674 throw new GateRuntimeException(
675 "Couldn't create a class object for VR called " + vrClassName
676 );
677 } if ( vrResourceData.getGuiType() == ResourceData.NULL_GUI &&
680 vrResourceData.getAnnotationTypeDisplayed() != null &&
681 gate.creole.AnnotationVisualResource.class.
682 isAssignableFrom(vrResourceClass)){
683
684 String annotationTypeDisplayed =
685 vrResourceData.getAnnotationTypeDisplayed();
686 if (annotationTypeDisplayed.equals(annotationType)){
687 responseList.add(vrClassName);
688 if (vrResourceData.isMainView())
689 defaultVR = vrClassName;
690 } } } if (defaultVR != null){
694 responseList.remove(defaultVR);
695 responseList.addFirst(defaultVR);
696 } return Collections.unmodifiableList(responseList);
698 }
700
703 public void setResourceName(Resource res, String newName){
704 String oldName = res.getName();
705 res.setName(newName);
706 fireResourceRenamed(res, oldName, newName);
707 }
708
709
710
714 public List getVREnabledAnnotationTypes(){
715 LinkedList responseList = new LinkedList();
716 Iterator vrIterator = vrTypes.iterator();
717 while (vrIterator.hasNext()){
718 String vrClassName = (String) vrIterator.next();
719 ResourceData vrResourceData = (ResourceData) this.get(vrClassName);
720 if (vrResourceData == null)
721 throw new GateRuntimeException(
722 "Couldn't get resource data for VR called " + vrClassName
723 );
724 if ( vrResourceData.getGuiType() == ResourceData.NULL_GUI &&
726 vrResourceData.getAnnotationTypeDisplayed() != null ){
727
728 String annotationTypeDisplayed =
729 vrResourceData.getAnnotationTypeDisplayed();
730 responseList.add(annotationTypeDisplayed);
731 } } return Collections.unmodifiableList(responseList);
734 }
736
737
738
739 protected List getPublics(List instances) {
740 Iterator iter = instances.iterator();
741 List publics = new ArrayList();
742
743 while(iter.hasNext()) {
746 Resource res = (Resource) iter.next();
747 ResourceData rd = (ResourceData) get(res.getClass().getName());
748 if(! rd.isPrivate()) publics.add(res);
749 }
750
751 return Collections.unmodifiableList(publics);
752 }
754
755 protected List getPublicTypes(Collection types){
756 Iterator iter = types.iterator();
757 List publics = new ArrayList();
758 while(iter.hasNext()){
759 String oneType = (String)iter.next();
760 ResourceData rData = (ResourceData)get(oneType);
761 if(rData != null && !rData.isPrivate()) publics.add(oneType);
762 }
763 return Collections.unmodifiableList(publics);
764 }
766 public synchronized void removeCreoleListener(CreoleListener l) {
767 if (creoleListeners != null && creoleListeners.contains(l)) {
768 Vector v = (Vector) creoleListeners.clone();
769 v.removeElement(l);
770 creoleListeners = v;
771 }
772 }
773
774 public synchronized void addCreoleListener(CreoleListener l) {
775 Vector v = creoleListeners == null ? new Vector(2) : (Vector) creoleListeners.clone();
776 if (!v.contains(l)) {
777 v.addElement(l);
778 creoleListeners = v;
779 }
780 }
782
786
787
793
798
803
804
805 protected Set lrTypes;
806
807
808 protected Set prTypes;
809
810
811 protected List vrTypes;
812
813
814 protected Set controllerTypes;
815
816
817 protected Set toolTypes;
818
819 private transient Vector creoleListeners;
820 protected void fireResourceLoaded(CreoleEvent e) {
821 if (creoleListeners != null) {
822 Vector listeners = creoleListeners;
823 int count = listeners.size();
824 for (int i = 0; i < count; i++) {
825 ((CreoleListener) listeners.elementAt(i)).resourceLoaded(e);
826 }
827 }
828 }
829
830 protected void fireResourceUnloaded(CreoleEvent e) {
831 if (creoleListeners != null) {
832 Vector listeners = creoleListeners;
833 int count = listeners.size();
834 for (int i = 0; i < count; i++) {
835 ((CreoleListener) listeners.elementAt(i)).resourceUnloaded(e);
836 }
837 }
838 }
839
840 protected void fireResourceRenamed(Resource res, String oldName,
841 String newName) {
842 if (creoleListeners != null) {
843 Vector listeners = creoleListeners;
844 int count = listeners.size();
845 for (int i = 0; i < count; i++) {
846 ((CreoleListener) listeners.elementAt(i)).resourceRenamed(res,
847 oldName,
848 newName);
849 }
850 }
851 }
852
853 protected void fireDatastoreOpened(CreoleEvent e) {
854 if (creoleListeners != null) {
855 Vector listeners = creoleListeners;
856 int count = listeners.size();
857 for (int i = 0; i < count; i++) {
858 ((CreoleListener) listeners.elementAt(i)).datastoreOpened(e);
859 }
860 }
861 }
862 protected void fireDatastoreCreated(CreoleEvent e) {
863 if (creoleListeners != null) {
864 Vector listeners = creoleListeners;
865 int count = listeners.size();
866 for (int i = 0; i < count; i++) {
867 ((CreoleListener) listeners.elementAt(i)).datastoreCreated(e);
868 }
869 }
870 }
871
872 protected void fireDatastoreClosed(CreoleEvent e) {
873 if (creoleListeners != null) {
874 Vector listeners = creoleListeners;
875 int count = listeners.size();
876 for (int i = 0; i < count; i++) {
877 ((CreoleListener) listeners.elementAt(i)).datastoreClosed(e);
878 }
879 }
880 }
881
882 public void resourceLoaded(CreoleEvent e) {
883 fireResourceLoaded(e);
884 }
885
886 public void resourceUnloaded(CreoleEvent e) {
887 fireResourceUnloaded(e);
888 }
889
890 public void resourceRenamed(Resource resource, String oldName,
891 String newName){
892 fireResourceRenamed(resource, oldName, newName);
893 }
894
895 public void datastoreOpened(CreoleEvent e) {
896 fireDatastoreOpened(e);
897 }
898
899 public void datastoreCreated(CreoleEvent e) {
900 fireDatastoreCreated(e);
901 }
902
903 public void datastoreClosed(CreoleEvent e) {
904 fireDatastoreClosed(e);
905 }
906
907
908 }