KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > jar > Package


1 /*
2  * Copyright area
3  */

4
5 package org.objectweb.fractal.jar;
6
7 import java.io.ByteArrayInputStream JavaDoc;
8 import java.io.Serializable JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.objectweb.fractal.adl.Node;
17 import org.objectweb.fractal.adl.components.Component;
18 import org.objectweb.fractal.adl.components.ComponentContainer;
19 import org.objectweb.fractal.adl.components.ComponentDefinition;
20 import org.objectweb.fractal.adl.xml.XMLParser;
21
22 public class Package implements Serializable JavaDoc {
23
24   String JavaDoc name;
25   
26   Map JavaDoc files; // associate name and version to PackageFile files
27

28   List JavaDoc packageDependencies; // list of Package objects
29

30   List JavaDoc dependentPackages; // list of Package objects
31

32   public Package (String JavaDoc name) {
33     this.name = name;
34     this.files = new HashMap JavaDoc();
35     this.packageDependencies = new ArrayList JavaDoc();
36     this.dependentPackages = new ArrayList JavaDoc();
37   }
38   
39   Set JavaDoc computeDependencies () throws Exception JavaDoc {
40     int dash = name.indexOf('-');
41     String JavaDoc n = name.substring(0, dash);
42     String JavaDoc v = name.substring(dash + 1);
43       
44     Set JavaDoc s = new HashSet JavaDoc();
45     getDependencies((PackageFile)files.get(
46       v + '/' + n.replace('.', '/') + ".fractal"), s);
47
48     return s;
49   }
50   
51   private void getDependencies (PackageFile file, Set JavaDoc s) throws Exception JavaDoc {
52     XMLParser p = new XMLParser();
53     Node n = p.parse(new ByteArrayInputStream JavaDoc(file.content), file.name);
54     getDependencies(n, s);
55   }
56   
57   private void getDependencies (Node n, Set JavaDoc s) throws Exception JavaDoc {
58     if (n instanceof ComponentContainer) {
59       Component[] components = ((ComponentContainer)n).getComponents();
60       for (int i = 0; i < components.length; ++i) {
61         getDependencies((Node)components[i], s);
62       }
63     }
64     if (n instanceof ComponentDefinition) {
65       getDependencies(((ComponentDefinition)n).getExtends(), s);
66     }
67     if (n instanceof Component) {
68       getDependencies(((Component)n).getDefinition(), s);
69     }
70   }
71   
72   private void getDependencies (String JavaDoc definitions, Set JavaDoc s) throws Exception JavaDoc {
73     if (definitions == null) {
74       return;
75     }
76     int comma = definitions.indexOf(',');
77     if (comma != -1) {
78       getDependencies(definitions.substring(0, comma), s);
79       getDependencies(definitions.substring(comma + 1), s);
80       return;
81     }
82     int dash = definitions.indexOf('-');
83     String JavaDoc definition = definitions.substring(0, dash);
84     String JavaDoc version = definitions.substring(dash + 1);
85
86     PackageFile file = (PackageFile)files.get(
87       version + '/' + definition.replace('.', '/') + ".fractal");
88     
89     if (file == null) {
90       s.add(definitions);
91     } else {
92       getDependencies(file, s);
93     }
94   }
95   
96   public String JavaDoc toString () {
97     return name;
98   }
99 }
100
Popular Tags