KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Made > Tools > InstallToTR


1 /* $Id: InstallToTR.java,v 1.4 2004/05/20 14:23:52 bures Exp $ */
2 package SOFA.SOFAnode.Made.Tools;
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.FileOutputStream JavaDoc;
6
7 import javax.xml.parsers.DocumentBuilder JavaDoc;
8 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
9 import javax.xml.parsers.ParserConfigurationException JavaDoc;
10
11 import org.w3c.dom.Document JavaDoc;
12 import org.w3c.dom.Element JavaDoc;
13 import org.w3c.dom.Node JavaDoc;
14 import org.w3c.dom.NodeList JavaDoc;
15 import org.xml.sax.SAXException JavaDoc;
16
17 /** Install component to "TR".
18   * Arguments:
19   * <ul>
20   * <li>1st argument - ddform file (it was created by CodeGen and completed by developer</li>
21   * <li>next arguments - class files to pack</li>
22   * </ul>
23   * Java properties:
24   * <ul>
25   * <li>file.root - from which directory (inserted) files will be searched
26   * <li>sofa.tr.dir - directory with TR
27   * </ul>
28   *
29   * @author Petr Hnetynka
30   */

31 public class InstallToTR {
32   
33   private static Element JavaDoc dd;
34   private static String JavaDoc version;
35   private static String JavaDoc arch;
36   private static java.util.Hashtable JavaDoc ent;
37   private static java.util.Hashtable JavaDoc fullNamesWithoutVersion;
38   private static java.util.Properties JavaDoc trIndex;
39   private static java.util.Properties JavaDoc implIndex;
40   private static String JavaDoc lastIndex;
41
42   public static void main(String JavaDoc[] argv) {
43     if (argv.length < 2) {
44       System.out.println("too few arguments");
45       System.exit(1);
46     }
47   
48
49     String JavaDoc trDir = System.getProperty("sofa.tr.dir", null);
50     if (trDir == null) {
51       System.out.println("Specify TR directrory (java property \"sofa.tr.dir\")");
52       System.exit(1);
53     }
54     trIndex = new java.util.Properties JavaDoc();
55     implIndex = new java.util.Properties JavaDoc();
56     try {
57       FileInputStream JavaDoc fis = new FileInputStream JavaDoc(trDir+File.separator+"iface"+File.separator+"index");
58       trIndex.load(fis);
59       fis.close();
60
61       fis = new FileInputStream JavaDoc(trDir+File.separator+"impl"+File.separator+"info");
62       implIndex.load(fis);
63       fis.close();
64
65       fis = new FileInputStream JavaDoc(trDir+File.separator+"iface"+File.separator+"last");
66       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
67       int a = fis.read();
68       while (a != -1) {
69         sb.append((char) a);
70         a = fis.read();
71       }
72       fis.close();
73       lastIndex = sb.toString();
74     } catch (java.io.IOException JavaDoc e) {
75       System.out.println("IOException: "+e.getMessage());
76       System.exit(1);
77     }
78     
79     String JavaDoc fileRoot = System.getProperty("file.root", ".");
80     fileRoot += File.separator;
81
82     try {
83       dd = getDDElem(fileRoot+argv[0]);
84     } catch (java.io.IOException JavaDoc e) {
85       System.out.println("IOException: "+e.getMessage());
86       System.exit(1);
87     } catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
88       System.out.println("ParserConfigurationException: "+e.getMessage() );
89       System.exit(1);
90     } catch (org.xml.sax.SAXException JavaDoc e) {
91       System.out.println("SAXException: "+e.getMessage() );
92       System.exit(1);
93     }
94     
95     version = getImplVersion(dd);
96     ent = getCDLEntities(dd);
97     arch = getArchitectureFullName(dd);
98     fullNamesWithoutVersion = getFullNamesWithoutVersion(trIndex);
99
100     // prepare translation table
101
java.util.Hashtable JavaDoc trTable = new java.util.Hashtable JavaDoc();
102     // add interfaces to trTable
103
for (java.util.Enumeration JavaDoc e = ent.keys(); e.hasMoreElements(); ) {
104       String JavaDoc javaName = (String JavaDoc) e.nextElement();
105       String JavaDoc fullName = (String JavaDoc) ent.get(javaName);
106       String JavaDoc specVersion = fullName.substring(fullName.indexOf('?')+1);
107       trTable.put(toVMName(javaName), toVMName(javaName+specVersionToFileName(specVersion)));
108     }
109     // add implementation classes to trTable
110
for (int i=1; i<argv.length; i++) {
111       try {
112         SOFA.Tools.Resolver.ClassInfo info = new SOFA.Tools.Resolver.ClassInfo(argv[i]);
113         trTable.put(info.className(), info.className()+implVersionToFileName(version));
114       } catch (java.io.IOException JavaDoc ex) {
115         System.out.println("IOException: "+ex.getMessage());
116         System.exit(1);
117       }
118     }
119     // create resolver
120
SOFA.Tools.Resolver.Resolver resolver = new SOFA.Tools.Resolver.Resolver(trTable);
121
122     System.out.print("Installing to TR....");
123
124     // copy interfaces
125
for (java.util.Enumeration JavaDoc e = ent.keys(); e.hasMoreElements(); ) {
126       String JavaDoc javaName = (String JavaDoc) e.nextElement();
127       String JavaDoc fullName = (String JavaDoc) ent.get(javaName);
128       String JavaDoc withoutVersion = fullName.substring(0, fullName.indexOf('?'));
129       String JavaDoc specVersion = fullName.substring(fullName.indexOf('?')+1);
130       if (fullNamesWithoutVersion.containsKey(withoutVersion)) {
131         if (trIndex.containsKey(fullName)) {
132           // contains same version -> rewrite
133
String JavaDoc ind = (String JavaDoc) trIndex.get(fullName);
134           File JavaDoc fsource = new File JavaDoc(fileRoot+ classNameToFileName(javaName));
135           if (!fsource.exists()) {
136             System.out.println("Error: file "+fsource.toString()+" does not exist.");
137             System.exit(1);
138           }
139           File JavaDoc fdest = new File JavaDoc(trDir+File.separator+"iface"+File.separator+ind+File.separator+classNameToFileName(javaName));
140           File JavaDoc dir = fdest.getParentFile();
141           if (dir != null)
142             if (! dir.exists())
143               dir.mkdirs();
144           try {
145             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(fsource);
146             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(fdest);
147             int a;
148             while ( (a = fis.read()) != -1)
149               fos.write(a);
150             fis.close();
151             fos.close();
152           } catch (java.io.IOException JavaDoc ex) {
153             System.out.println("IOException: "+ex.getMessage());
154             System.exit(1);
155           }
156           // save augmented class
157
fdest = new File JavaDoc(trDir+File.separator+"classes"+File.separator+classNameToFileName(javaName+specVersionToFileName(specVersion)));
158           dir = fdest.getParentFile();
159           if (dir != null)
160             if (! dir.exists())
161               dir.mkdirs();
162           try {
163             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(fsource);
164             byte [] b = resolver.process(fis);
165             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(fdest);
166             fos.write(b);
167             fis.close();
168             fos.close();
169           } catch (java.io.IOException JavaDoc ex) {
170             System.out.println("IOException: "+ex.getMessage());
171             System.exit(1);
172           }
173         } else { //another version
174
lastIndex = incrementLastIndex(lastIndex);
175           File JavaDoc fsource = new File JavaDoc(fileRoot+ classNameToFileName(javaName));
176           if (!fsource.exists()) {
177             System.out.println("Error: file "+fsource.toString()+" does not exist.");
178             System.exit(1);
179           }
180           File JavaDoc fdest = new File JavaDoc(trDir+File.separator+"iface"+File.separator+lastIndex+File.separator+classNameToFileName(javaName));
181           File JavaDoc dir = fdest.getParentFile();
182           if (dir != null)
183             if (! dir.exists())
184               dir.mkdirs();
185           try {
186             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(fsource);
187             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(fdest);
188             int a;
189             while ( (a = fis.read()) != -1)
190               fos.write(a);
191             fis.close();
192             fos.close();
193           } catch (java.io.IOException JavaDoc ex) {
194             System.out.println("IOException: "+ex.getMessage());
195             System.exit(1);
196           }
197           // save augmented class
198
fdest = new File JavaDoc(trDir+File.separator+"classes"+File.separator+classNameToFileName(javaName+specVersionToFileName(specVersion)));
199           dir = fdest.getParentFile();
200           if (dir != null)
201             if (! dir.exists())
202               dir.mkdirs();
203           try {
204             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(fsource);
205             byte [] b = resolver.process(fis);
206             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(fdest);
207             fos.write(b);
208             fis.close();
209             fos.close();
210           } catch (java.io.IOException JavaDoc ex) {
211             System.out.println("IOException: "+ex.getMessage());
212             System.exit(1);
213           }
214           trIndex.put(fullName, lastIndex);
215         }
216       } else {
217         File JavaDoc fsource = new File JavaDoc(fileRoot+ classNameToFileName(javaName));
218         if (!fsource.exists()) {
219           System.out.println("Error: file "+fsource.toString()+" does not exist.");
220           System.exit(1);
221         }
222         File JavaDoc fdest = new File JavaDoc(trDir+File.separator+"iface"+File.separator+"a"+File.separator+classNameToFileName(javaName));
223         File JavaDoc dir = fdest.getParentFile();
224         if (dir != null)
225           if (! dir.exists())
226             dir.mkdirs();
227         try {
228           FileInputStream JavaDoc fis = new FileInputStream JavaDoc(fsource);
229           FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(fdest);
230           int a;
231           while ( (a = fis.read()) != -1)
232             fos.write(a);
233           fis.close();
234           fos.close();
235         } catch (java.io.IOException JavaDoc ex) {
236           System.out.println("IOException: "+ex.getMessage());
237           System.exit(1);
238         }
239         // save augmented class
240
fdest = new File JavaDoc(trDir+File.separator+"classes"+File.separator+classNameToFileName(javaName+specVersionToFileName(specVersion)));
241         dir = fdest.getParentFile();
242         if (dir != null)
243           if (! dir.exists())
244             dir.mkdirs();
245         try {
246           FileInputStream JavaDoc fis = new FileInputStream JavaDoc(fsource);
247           byte [] b = resolver.process(fis);
248           FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(fdest);
249           fos.write(b);
250           fis.close();
251           fos.close();
252         } catch (java.io.IOException JavaDoc ex) {
253           System.out.println("IOException: "+ex.getMessage());
254           System.exit(1);
255         }
256         trIndex.put(fullName, "a");
257       }
258     }
259
260     // copy implementation files
261
String JavaDoc sdir = fullNameToFileName(arch)+File.separator+version+File.separator;
262     for (int i=1; i<argv.length; i++) {
263       File JavaDoc fsource = new File JavaDoc(fileRoot+ File.separator+argv[i]);
264       if (!fsource.exists()) {
265         System.out.println("Error: file "+fsource.toString()+" does not exist.");
266         System.exit(1);
267       }
268       File JavaDoc fdest = new File JavaDoc(trDir+File.separator+"impl"+File.separator+sdir+argv[i]);
269       File JavaDoc dir = fdest.getParentFile();
270       if (dir != null)
271         if (! dir.exists())
272           dir.mkdirs();
273       try {
274         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(fsource);
275         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(fdest);
276         int a;
277         while ( (a = fis.read()) != -1)
278           fos.write(a);
279         fis.close();
280         fos.close();
281       } catch (java.io.IOException JavaDoc ex) {
282         System.out.println("IOException: "+ex.getMessage());
283         System.exit(1);
284       }
285       // save augmented class
286
fdest = new File JavaDoc(trDir+File.separator+"classes"+File.separator+argv[i].substring(0, argv[i].length()-6)+implVersionToFileName(version)+".class");
287       dir = fdest.getParentFile();
288       if (dir != null)
289         if (! dir.exists())
290           dir.mkdirs();
291       try {
292         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(fsource);
293         byte [] b = resolver.process(fis);
294         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(fdest);
295         fos.write(b);
296         fis.close();
297         fos.close();
298       } catch (java.io.IOException JavaDoc ex) {
299         System.out.println("IOException: "+ex.getMessage());
300         System.exit(1);
301       }
302     }
303     // copy dd
304
File JavaDoc fsource = new File JavaDoc(fileRoot+ File.separator+argv[0]);
305     if (!fsource.exists()) {
306       System.out.println("Error: file "+fsource.toString()+" does not exist.");
307       System.exit(1);
308     }
309     File JavaDoc fdest = new File JavaDoc(trDir+File.separator+"impl"+File.separator+sdir+"index.dc");
310     try {
311       FileInputStream JavaDoc fis = new FileInputStream JavaDoc(fsource);
312       FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(fdest);
313       int a;
314       while ( (a = fis.read()) != -1)
315         fos.write(a);
316       fis.close();
317       fos.close();
318     } catch (java.io.IOException JavaDoc ex) {
319       System.out.println("IOException: "+ex.getMessage());
320       System.exit(1);
321     }
322     implIndex.put(arch+"["+version+"]", "1");
323
324     // save indexes
325
try {
326       FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(trDir+File.separator+"iface"+File.separator+"last");
327       for (int i=0; i<lastIndex.length(); i++) {
328         fos.write((byte)lastIndex.charAt(i));
329       }
330       fos.close();
331       fos = new FileOutputStream JavaDoc(trDir+File.separator+"iface"+File.separator+"index");
332       trIndex.store(fos, null);
333       fos.close();
334       fos = new FileOutputStream JavaDoc(trDir+File.separator+"impl"+File.separator+"info");
335       implIndex.store(fos, null);
336       fos.close();
337     } catch (java.io.IOException JavaDoc ex) {
338       System.out.println("IOException: "+ex.getMessage());
339       System.exit(1);
340     }
341     
342     System.out.println("OK");
343   }
344
345   /** Return document element of DD.
346     *
347     * @param fname filename
348     * @return document element
349     */

350   static Element JavaDoc getDDElem(String JavaDoc fname) throws java.io.IOException JavaDoc, ParserConfigurationException JavaDoc, SAXException JavaDoc {
351     java.io.File JavaDoc file = new java.io.File JavaDoc(fname);
352     if (!file.exists()) {
353       throw new java.io.IOException JavaDoc("File \""+fname+"\" doesn't exist.");
354     }
355
356     DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
357     DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
358     Document JavaDoc document = builder.parse(file);
359     Element JavaDoc element = document.getDocumentElement();
360     element.normalize();
361     return element;
362   }
363
364   static String JavaDoc getImplVersion(Element JavaDoc element) {
365     NodeList JavaDoc nl = element.getChildNodes();
366     for (int i=0; i<nl.getLength(); i++) {
367       Node JavaDoc n = nl.item(i);
368       if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("version")==0)) {
369         return n.getFirstChild().getNodeValue();
370       }
371     }
372     return null;
373   }
374
375   static String JavaDoc getArchitectureFullName(Element JavaDoc element) {
376     NodeList JavaDoc nl = element.getChildNodes();
377     for (int i=0; i<nl.getLength(); i++) {
378       Node JavaDoc n = nl.item(i);
379       if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("architecture_ref")==0)) {
380         String JavaDoc ar = n.getFirstChild().getNodeValue();
381         ar = ar.substring(0,ar.indexOf('?'));
382         return ar;
383       }
384     }
385     return null;
386   }
387
388   static java.util.Hashtable JavaDoc getCDLEntities(Element JavaDoc element) {
389     NodeList JavaDoc nl = element.getChildNodes();
390     for (int i=0; i<nl.getLength(); i++) {
391       Node JavaDoc n = nl.item(i);
392       if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("cdl_entities")==0)) {
393         NodeList JavaDoc nl1 = n.getChildNodes();
394         java.util.Hashtable JavaDoc ret = new java.util.Hashtable JavaDoc();
395         for (int j=0; j<nl1.getLength(); j++) {
396           Node JavaDoc n1 = nl1.item(j);
397           if ((n1.getNodeType() == Node.ELEMENT_NODE) && (n1.getNodeName().compareTo("entity")==0)) {
398             ret.put(((Element JavaDoc)n1).getAttribute("javaname"), ((Element JavaDoc)n1).getAttribute("cdlname"));
399           }
400         }
401         return ret;
402       }
403     }
404     return null;
405   }
406
407   private static java.util.Hashtable JavaDoc getFullNamesWithoutVersion(java.util.Properties JavaDoc prop) {
408     java.util.Hashtable JavaDoc ret = new java.util.Hashtable JavaDoc();
409     for (java.util.Enumeration JavaDoc e = prop.keys() ; e.hasMoreElements() ;) {
410       String JavaDoc k = (String JavaDoc) e.nextElement();
411       k = k.substring(0, k.indexOf('?'));
412       ret.put(k, "");
413     }
414     return ret;
415   }
416
417   private static boolean compareFullNameWithoutVersion(String JavaDoc n1, String JavaDoc n2) {
418     n1 = n1.substring(0, n1.indexOf('?'));
419     n2 = n2.substring(0, n2.indexOf('?'));
420     if (n1.compareTo(n2) == 0)
421       return true;
422     return false;
423   }
424
425   /** From name of class creates name of file (replaces dot to slash).
426     */

427   static String JavaDoc classNameToFileName(String JavaDoc clname) {
428     return clname.replace('.', File.separatorChar) + ".class";
429   }
430
431   /** From full name creates name of file (replaces doble colon to slash).
432     */

433   static String JavaDoc fullNameToFileName(String JavaDoc clname) {
434     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
435     boolean s = true;
436     for (int i=0; i<clname.length(); i++) {
437       if (clname.charAt(i)==':') {
438         if (s) {
439           sb.append(":");
440           s = false;
441         }
442       } else {
443         s = true;
444         sb.append(clname.charAt(i));
445       }
446     }
447 // System.out.println(sb);
448
// return clname.replaceAll("::", String.valueOf(File.separatorChar));
449
return sb.toString().replace(':', File.separatorChar);
450   }
451
452   private static String JavaDoc incrementLastIndex(String JavaDoc last) {
453     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(last);
454     int now = last.length() - 1;
455     while (true) {
456       if (last.charAt(now) != 'z') {
457         sb.setCharAt(now, (char) ((char)last.charAt(now)+(char)1));
458         return sb.toString();
459       }
460       sb.setCharAt(now, 'a');
461       now--;
462       if (now < 0) {
463         sb.append('a');
464         return sb.toString();
465       }
466     }
467   }
468
469   static String JavaDoc specVersionToFileName(String JavaDoc specVersion) {
470     StringBuffer JavaDoc ret = new StringBuffer JavaDoc("_Q_");
471     for (int i=0; i<specVersion.length(); i++) {
472       switch (specVersion.charAt(i)) {
473       case '.':
474         ret.append('_');
475         break;
476       case '!':
477         ret.append("_E_");
478         break;
479       default:
480         ret.append(specVersion.charAt(i));
481       }
482     }
483     return ret.toString();
484   }
485
486   static String JavaDoc toVMName(String JavaDoc name) {
487     StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
488     for (int i=0; i<name.length(); i++) {
489       if (name.charAt(i) == '.')
490         ret.append('/');
491       else
492         ret.append(name.charAt(i));
493     }
494     return ret.toString();
495   }
496
497   static String JavaDoc implVersionToFileName(String JavaDoc implVersion) {
498     StringBuffer JavaDoc ret = new StringBuffer JavaDoc("_V_");
499     for (int i=0; i<implVersion.length(); i++) {
500       switch (implVersion.charAt(i)) {
501       case '.':
502         ret.append('_');
503         break;
504       case '!':
505         ret.append("_E_");
506         break;
507       default:
508         ret.append(implVersion.charAt(i));
509       }
510     }
511     return ret.toString();
512   }
513 }
514
Popular Tags