1
15 package gate.creole;
16
17 import java.io.*;
18 import java.lang.reflect.Method;
19 import java.util.*;
20
21 import gate.util.*;
22
23
24
29
30 public class BootStrap {
31
32
33 protected static final String oldResource = "creole/bootstrap/";
35
36
37 protected static final String nameProject = "Template";
38
39
40 protected static final String newLine = Strings.getNl();
41
42
46 protected Map names = null;
47
48 protected Map oldNames = null;
49
50
51 protected List listMethodsResource = null;
52
53
54 protected List listPackages;
55
56
57 protected Set allPackages = null;
58
59
60 protected Map fields = null;
61
62
63 protected char cbuffer[] = null;
64
65
66 protected final static int BUFF_SIZE = 65000;
67
68
69 public BootStrap() {
70
71 names = new HashMap();
72
73 oldNames = new HashMap();
74
75 listMethodsResource = new ArrayList();
76
77 listPackages = new ArrayList();
78
79 cbuffer = new char[BUFF_SIZE];
80
81 allPackages = new HashSet();
82
83 fields = new HashMap();
84 }
85
86
89 public String changeKeyValue ( String text, Map map ){
90
91 Set keys = map.keySet();
92 Iterator iteratorKeys = keys.iterator();
93 while (iteratorKeys.hasNext()) {
94 String key = (String) iteratorKeys.next();
95 String value = (String)map.get(key);
96 text = text.replaceAll(key,value);
97 } return text;
99 }
101
103 public String determineTypePackage(String text) {
104
105 int index = text.lastIndexOf(".");
107 int ind = text.lastIndexOf(";");
108 String type = new String();
109 String packageName = new String();
110
111 if (index != -1){
112 if (ind != -1) {
114 type = text.substring(index+1,text.length()-1)+"[]";
115 packageName = (text.substring(2,index))+".*";
116 }
117 else {
118 packageName = (text.substring(0,index))+".*";
119 type = text.substring(index+1,text.length());
120 }
121 if ((!allPackages.contains(packageName))&&
123 (packageName.compareTo("java.lang.*")!=0))
124 allPackages.add(packageName);
125 } else {type = text;}
126
127 return type;
128 }
129
130
133 public String getInterfacesAndClass (String typeResource, Set interfacesList)
134 throws ClassNotFoundException {
135
136 String abstractClass = null;
137 String interfacesAndClass = null;
139 Class currentClass = null;
141
142 if (typeResource.equals("ProcessingResource")) {
144 abstractClass = "AbstractProcessingResource";
145 } else if (typeResource.equals("VisualResource")) {
146 abstractClass = "AbstractVisualResource";
147 } else if (typeResource.equals("LanguageResource")) {
148 abstractClass = "AbstractLanguageResource";}
149
150 interfacesAndClass = " extends " + abstractClass;
151
152 List methodsInterfaces = new ArrayList();
155 if (interfacesList!=null) {
156 interfacesAndClass = interfacesAndClass+ newLine+ " implements ";
157 Iterator iter = interfacesList.iterator();
158 while (iter.hasNext()) {
159 String nameInterface =(String)iter.next();
160 String nameClass = null;
161 int index = nameInterface.lastIndexOf(".");
162 if (index != -1) {
163 currentClass = Class.forName(nameInterface);
164 nameClass = nameInterface.substring(index+1,nameInterface.length());
165 } else {
166 nameClass = nameInterface;
167 currentClass = Class.forName("gate."+nameInterface);
168 }
170 if (!allPackages.contains(currentClass.getPackage())){
172 allPackages.add(currentClass.getPackage().getName()+".*");
173 }
174
175 interfacesAndClass = interfacesAndClass + nameClass + ", ";
176
177 methodsInterfaces = featuresClass(currentClass,methodsInterfaces);
178 } }
181 if (!interfacesList.contains("gate."+typeResource))
183 interfacesAndClass = interfacesAndClass + typeResource;
184 else if (interfacesAndClass.endsWith(", "))
185 interfacesAndClass = interfacesAndClass.substring
186 (0,interfacesAndClass.length()-2);
187
188 List methodsClassExtend = new ArrayList();
190 Class currentClassExtend = Class.forName("gate.creole."+abstractClass);
191 methodsClassExtend = featuresClass(currentClassExtend, methodsClassExtend);
192
193 getMethodsAndFields(methodsClassExtend,methodsInterfaces);
195
196 return interfacesAndClass;
197 }
199
200 public List featuresClass (Class currentClass, List methodsList){
201
202 Method[] listMethodsCurrentClass = currentClass.getMethods();
204 for (int i=0;i<listMethodsCurrentClass.length;i++) {
205 FeatureMethod featureMethod = new FeatureMethod();
206 featureMethod.setNameMethod(listMethodsCurrentClass[i].getName());
207 featureMethod.setValueReturn(
208 listMethodsCurrentClass[i].getReturnType().getName());
209
210 Class[] parameters = (Class[])(
211 listMethodsCurrentClass[i].getParameterTypes());
212 Class[] exceptions = (Class[])(
213 listMethodsCurrentClass[i].getExceptionTypes());
214
215 List aux = new ArrayList();
217 for (int k=0;k<parameters.length;k++)
218 aux.add(parameters[k].getName());
219 featureMethod.setParameterTypes(aux);
220
221 aux = new ArrayList();
223 for (int k=0;k<exceptions.length;k++)
224 aux.add(exceptions[k].getName());
225 featureMethod.setExceptionTypes(aux);
226
227 if (!methodsList.contains(featureMethod)){
228 methodsList.add(featureMethod);
229 }
230 } return methodsList;
232 }
234
240 public void getMethodsAndFields(List methodsExtendList,
241 List methodsInterfacesList){
242
245 int j = 0;
246 for (int i=0;i<methodsInterfacesList.size();i++) {
247 FeatureMethod featureMethod = (FeatureMethod)methodsInterfacesList.get(i);
248 if (methodsExtendList.contains(featureMethod) == false) {
249 String nameMethod = (String)(featureMethod.getNameMethod());
251
252 List valTypes = (List)(featureMethod.getParameterTypes());
254
255 String typeReturn = determineTypePackage(
257 (String)(featureMethod.getValueReturn()));
258
259 List valException = (List)(featureMethod.getExceptionTypes());
261
262 String declaration = "public "+ typeReturn +" "+
263 nameMethod +"(";
264 if (valTypes.size() == 0)
266 declaration = declaration+")";
267 else
268 for (int k=0;k<valTypes.size();k++) {
269 String type = (String)valTypes.get(k);
270 if (type.endsWith("[]"))
271 declaration = declaration +
272 determineTypePackage(type.substring(0,type.length()-2)) +
273 " parameter"+ k;
274 else
275 declaration = declaration +
276 determineTypePackage((String)valTypes.get(k)) +
277 " parameter"+ k;
278
279 if (k==valTypes.size()-1)
280 declaration = declaration + ")";
281 else
282 declaration = declaration + ", ";
283
284 }
286 if (valException.size() == 0) {
288 if (!typeReturn.equals("void")){
289 if (!typeReturn.endsWith("[]"))
290 declaration = declaration + "{ " + "return "+
291 typeReturn.toLowerCase()+ j + "; }";
292 else
293 declaration = declaration + "{ " + "return "+
294 typeReturn.toLowerCase().substring(
295 0,typeReturn.length()-2)+ j + "; }";
296
297 fields.put(new Integer(j),typeReturn);
298 j =j+1;
299 }
300 else {declaration = declaration+" {}" ;}
301 } else {
303 declaration = declaration + newLine+ " throws ";
304 for (int k=0;k<valException.size();k++) {
305 declaration = declaration + determineTypePackage((String)
306 valException.get(k));
307
308 if (k == valException.size()-1) {
309 if (!typeReturn.equals("void")){
310 if (!typeReturn.endsWith("[]"))
311 declaration = declaration + "{ " + "return "+
312 typeReturn.toLowerCase()+ j+"; }";
313 else
314 declaration = declaration + "{ " + "return "+
315 typeReturn.toLowerCase().substring(
316 0,typeReturn.length()-2)+ j + "; }";
317
318 fields.put(new Integer(j),typeReturn);
319 j=j+1;
320 }
321 else
322 declaration = declaration+" {}" ;
323 } else
324 declaration = declaration + ", ";
325
326 } }
329 listMethodsResource.add(declaration);
331
332 } }
335 }
337
340 public String displayMethodsAndFields(List methods, Map fields) {
341
342 String methodsFields = "";
343
344 Iterator iterator = listMethodsResource.iterator();
346 while (iterator.hasNext()) {
347 methodsFields = methodsFields + newLine + (String)iterator.next()+newLine;
348 }
349
350 Iterator iter = fields.keySet().iterator();
352 int i=0;
353 while (iter.hasNext()) {
354 Integer index = (Integer)iter.next();
355 String type = (String)fields.get(index);
356 if (type.endsWith("[]"))
357 methodsFields = methodsFields + newLine + "protected " + type +" " +
358 type.substring(0,type.length()-2).toLowerCase() +
359 index.toString() +";";
360
361 else
362 methodsFields = methodsFields + newLine + "protected " + type +" " +
363 type.toLowerCase() + index.toString() +";";
364 i+=1;
365 }
366 return methodsFields;
367 }
369
370
371 public Map createNames ( String packageName,
372 String resourceName,
373 String className,
374 String stringPackages,
375 String interfacesAndClass) {
376
377 String packages = namesPackages(allPackages);
379
380 Calendar calendar = Calendar.getInstance();
382 int month = calendar.get(Calendar.MONTH)+1;
383 int year = calendar.get(Calendar.YEAR);
384 int day = calendar.get(Calendar.DAY_OF_MONTH);
385 String date = day+"/"+month+"/"+year;
386 String user = System.getProperty("user.name");
387
388 names.put(nameProject,resourceName);
391 names.put(nameProject.toUpperCase(),resourceName.toUpperCase());
392 names.put(nameProject.toLowerCase(),resourceName.toLowerCase());
393 names.put("___CLASSNAME___",className);
394 names.put("___INTERFACES___",interfacesAndClass);
395 names.put("___CONTENT___",
396 displayMethodsAndFields(listMethodsResource,fields));
397 names.put("___DATE___",date);
398 names.put("___AUTHOR___",user);
399 names.put("___ALLPACKAGES___",stringPackages);
402 names.put("___PACKAGE___",packageName);
405 names.put("___PACKAGETOP___",listPackages.get(0));
406 names.put("___RESOURCE___",resourceName);;
407 names.put(
408 "___GATECLASSPATH___",
409 System.getProperty("path.separator") +
410 System.getProperty("java.class.path")
411 );
412
413 if (packages.length() == 0){
414 names.put("import ___packages___.*;", "");
415 } else {
416 names.put("import ___packages___.*;", packages);
417 }
418
419 oldNames.put("___PACKAGE___","template");
420 oldNames.put("___ALLPACKAGES___","template");
421 oldNames.put("___PACKAGETOP___","template");
422
423 return names;
424 }
426
427 public String namesPackages (Set listPackages) {
428 Iterator iterator = listPackages.iterator();
429 String packages = new String();
430 while (iterator.hasNext()) {
431 String currentPackage = (String)iterator.next();
432 if ((!currentPackage.equals("gate.*"))&&
433 (!currentPackage.equals("gate.creole.*"))&&
434 (!currentPackage.equals("gate.util.*"))&&
435 (!currentPackage.equals("java.util.*")))
436 packages = packages + newLine + "import "+ currentPackage+";";
437 } return packages;
439 }
440
441
443 public List determinePath (String packageName)throws IOException {
444 List list = new ArrayList();
445 StringTokenizer token = new StringTokenizer(packageName,".");
446 while (token.hasMoreTokens()) {
448 list.add(token.nextToken());
449 }
450 return list;
452 }
453
454
457 public void verifyInput(String className, String pathNewProject)
458 throws GateException {
459 char[] classNameChars = className.toCharArray();
462 for (int i=0;i<classNameChars.length;i++){
463 Character classNameCharacter = new Character(classNameChars[i]);
464 if (!Character.isLetterOrDigit(classNameChars[i]))
465 throw new GateException("Only letters and digits in the class name");
466 }
467
468 File dir = new File(pathNewProject);
470 if (!dir.isDirectory())
471 throw new GateException("The folder is not a directory");
472 }
473
474
475 public void executableFile(String nameFile)
476 throws IOException,InterruptedException{
477 String osName = System.getProperty("os.name" );
478 if( !osName.startsWith("Windows") ){
479 Runtime rt = Runtime.getRuntime();
480 Process proc = rt.exec("chmod 711 "+nameFile);
481
482 int exitVal = proc.waitFor();
484 if (exitVal!=0)
485 Out.prln("Warning: it is necessary to make executable the "+
486 "following file: " + nameFile);
487 } }
490
501 public void createResource( String resourceName,String packageName,
502 String typeResource,String className,
503 Set interfacesList,String pathNewProject)
504 throws
505 IOException,ClassNotFoundException,
506 GateException,InterruptedException {
507 File newFile = null;
509
510 Properties properties = new Properties();
512
513 String newPathFile = null;
515
516 String oldPathFile = null;
518
519 verifyInput(className,pathNewProject);
521
522 String interfacesAndClass = getInterfacesAndClass (typeResource,
525 interfacesList);
526
527 listPackages = determinePath(packageName);
529
530 if (!pathNewProject.endsWith("/")) pathNewProject = pathNewProject + "/";
532
533 String stringPackages = (String)listPackages.get(0);
535 for (int i=1;i<listPackages.size();i++) {
536 stringPackages = stringPackages + "/"+listPackages.get(i);
537 }
538
539 createNames(packageName,resourceName,className,
541 stringPackages,interfacesAndClass);
542
543 InputStream inputStream = Files.getGateResourceAsStream(oldResource +
545 "file-list.properties");
546
547 properties.load(inputStream);
549
550 inputStream.close();
552
553 String oldDirectories = properties.getProperty("directories");
555 StringTokenizer token = new StringTokenizer(oldDirectories,",");
556 while (token.hasMoreTokens()) {
557 String propPathDirectory = (String)token.nextToken();
558 if (propPathDirectory.endsWith("___ALLPACKAGES___")) {
559 newPathFile =
561 propPathDirectory.substring(0,propPathDirectory.length()-18);
562 newPathFile = changeKeyValue(newPathFile,names);
564 for (int i=0;i<listPackages.size();i++) {
565 newPathFile = newPathFile + "/"+listPackages.get(i);
566 newFile = new File(pathNewProject + newPathFile);
567 newFile.mkdir();
568 } } else {
570 newPathFile = changeKeyValue(propPathDirectory,names);
571 newFile = new File(pathNewProject + newPathFile);
573 newFile.mkdir();
574 } }
577 Enumeration keyProperties = properties.propertyNames();
579 while (keyProperties.hasMoreElements()) {
581 String key = (String)keyProperties.nextElement();
582 if (!key.equals("directories")) {
583 String oldFiles = properties.getProperty(key);
584 token = new StringTokenizer(oldFiles,",");
585 while (token.hasMoreTokens()) {
587 String propPathFiles = (String)token.nextToken();
588 oldPathFile = changeKeyValue(propPathFiles,oldNames);
589
590 newPathFile = changeKeyValue(propPathFiles,names);
592
593 if (newPathFile.endsWith("jav")) newPathFile = newPathFile +"a";
595
596
598 newFile = new File(pathNewProject+newPathFile);
600
601 FileWriter fileWriter = new FileWriter(newFile);
603
604 InputStream currentInputStream =
606 Files.getGateResourceAsStream(oldResource+oldPathFile);
607
608 InputStreamReader inputStreamReader = new InputStreamReader (
609 currentInputStream);
610 int charRead = 0;
611 String text = null;
612 while(
613 (charRead = inputStreamReader.read(cbuffer,0,BUFF_SIZE)) != -1){
614 text = new String (cbuffer,0,charRead);
615 text = changeKeyValue(text,names);
616 fileWriter.write(text ,0,text.length());
617 } inputStreamReader.close();
619 currentInputStream.close();
621 fileWriter.close();
623
624 if (newPathFile.endsWith("configure")||newPathFile.endsWith(".sh"))
626 executableFile(pathNewProject+newPathFile);
627
628 } } }
632 }
634 public static void main(String[] args) {
635 System.out.println(System.getProperty("path.separator"));
636 System.out.println("intre");
637 System.out.println(System.getProperty("java.class.path"));
638 BootStrap bootStrap = new BootStrap();
639 Set interfaces = new HashSet();
640 interfaces.add("gate.Document");
641 interfaces.add("gate.ProcessingResource");
642 try{
643
644 bootStrap.createResource("morph","creole.sheffield.ac.lisa","LanguageResource",
645 "Documente", interfaces, "z:/test");
646 } catch (GateException ge) {
647 ge.printStackTrace(Err.getPrintWriter());
648 } catch (ClassNotFoundException cnfe) {
649 cnfe.printStackTrace(Err.getPrintWriter());
650 } catch (IOException ioe) {
651 ioe.printStackTrace(Err.getPrintWriter());
652 } catch (InterruptedException ie){
653 ie.printStackTrace(Err.getPrintWriter());
654 }
655 }
657 }
659
663 class FeatureMethod {
664
665 protected String nameMethod;
666
667
668 protected String valueReturn;
669
670
671 protected List parameterTypes;
672
673
674 protected List exceptionTypes;
675
676 FeatureMethod() {
677 nameMethod = new String();
678 valueReturn = new String();
679 parameterTypes = new ArrayList();
680 exceptionTypes = new ArrayList();
681 }
682
683 public String getNameMethod() {
684 return nameMethod;
685 }
687 public String getValueReturn() {
688 return valueReturn;
689 }
691 public List getParameterTypes() {
692 return parameterTypes;
693 }
695 public List getExceptionTypes() {
696 return exceptionTypes;
697 }
699 public void setNameMethod(String newNameMethod) {
700 nameMethod = newNameMethod;
701 }
703 public void setValueReturn(String newValueReturn) {
704 valueReturn = newValueReturn;
705 }
707 public void setParameterTypes(List newParameterTypes) {
708 parameterTypes = newParameterTypes;
709 }
711 public void setExceptionTypes(List newExceptionTypes) {
712 exceptionTypes = newExceptionTypes;
713 }
715 public boolean equals(Object obj){
716 if(obj == null)
717 return false;
718 FeatureMethod other;
719 if(obj instanceof FeatureMethod){
720 other = (FeatureMethod) obj;
721 }else return false;
722
723 if((nameMethod == null) ^ (other.getNameMethod() == null))
725 return false;
726 if(nameMethod != null && (!nameMethod.equals(other.getNameMethod())))
727 return false;
728
729 if((valueReturn == null) ^ (other.getValueReturn() == null))
731 return false;
732 if(valueReturn != null && (!valueReturn.equals(other.getValueReturn())))
733 return false;
734
735 if((parameterTypes == null) ^ (other.getParameterTypes() == null))
737 return false;
738 if(parameterTypes != null &&
739 (!parameterTypes.equals(other.getParameterTypes())))
740 return false;
741
742 if((exceptionTypes == null) ^ (other.getExceptionTypes() == null))
744 return false;
745 if(exceptionTypes != null &&
746 (!exceptionTypes.equals(other.getExceptionTypes())))
747 return false;
748 return true;
749 }
751 public int hashCode(){
752 int hashCodeRes = 0;
753 if (nameMethod != null )
754 hashCodeRes ^= nameMethod.hashCode();
755 if (valueReturn != null)
756 hashCodeRes ^= valueReturn.hashCode();
757 if(exceptionTypes != null)
758 hashCodeRes ^= exceptionTypes.hashCode();
759 if(parameterTypes != null)
760 hashCodeRes ^= parameterTypes.hashCode();
761
762 return hashCodeRes;
763 }}
766