View Javadoc

1   package org.catacomb.druid.manifest;
2   
3   
4   
5   import org.catacomb.interlish.resource.ResourceRole;
6   import org.catacomb.interlish.resource.Role;
7   import org.catacomb.interlish.structure.Element;
8   import org.catacomb.report.E;
9   import org.catacomb.serial.ElementXMLReader;
10  import org.catacomb.serial.Serializer;
11  import org.catacomb.util.FileUtil;
12  
13  
14  
15  import java.io.File;
16  import java.util.ArrayList;
17  
18  
19  
20  public class DecManifest {
21  
22  
23  
24      public String rootPath; // "org/catacomb/ime" or whatever - gets put back on paths
25      // later;
26  
27      public ArrayList<DecFile> files;
28      public ArrayList<Role> roles;
29  
30  
31  
32      public DecManifest() {
33      }
34  
35  
36      public DecManifest(File basedir, String path) {
37          rootPath = path;
38  
39          init();
40          addFilesFrom(basedir, rootPath);
41      }
42  
43  
44      public ArrayList<DecFile> getFiles() {
45          return files;
46      }
47  
48  
49      public ArrayList<Role> getRoles() {
50          return roles;
51      }
52  
53  
54      public String getRootPath() {
55          return rootPath;
56      }
57  
58  
59  
60      public void init() {
61          files = new ArrayList<DecFile>();
62          roles = new ArrayList<Role>();
63      }
64  
65  
66  
67      void addFilesFrom(File fdir, String psf) {
68          File[] af = fdir.listFiles();
69          for (int i = 0; i < af.length; i++) {
70              File f = af[i];
71  
72              if (f.getName().endsWith(".xml")) {
73  
74                  addFile(psf, f);
75  
76              } else if (f.isDirectory()) {
77  
78                  String psc = psf;
79                  if (psc.length() > 0) {
80                      psc += "/";
81                  }
82                  psc += f.getName();
83  
84                  addFilesFrom(f, psc);
85              }
86          }
87      }
88  
89  
90      private void addFile(String psf, File f) {
91          String fnm = f.getName();
92  
93          DecFile xmf = new DecFile(psf, fnm);
94  
95          String jpath = psf.replaceAll("/", ".");
96  
97          // E.info("adding file " + f);
98          String stxt = FileUtil.readStringFromFile(f);
99  
100         if (stxt.trim().length() > 10) {
101 
102             Element elt = (Element)(ElementXMLReader.deserialize(stxt));
103 
104 
105             if (elt.getName().equals("DeclarationReading")) {
106                 addInfoFrom(elt, jpath);
107 
108             } else {
109                 addResource(elt, xmf);
110             }
111         }
112     }
113 
114 
115 
116     private void addInfoFrom(Element parent, String jpath) {
117         for (Element elt : parent.getElements()) {
118 
119             if (elt.getName().equals("Instantiable")) {
120                 String cls = elt.getAttribute("class");
121                 String prv = elt.getAttribute("provides");
122 
123                 if (cls != null) {
124                     if (prv == null) {
125                         prv = cls.substring(cls.lastIndexOf(".") + 1, cls.length());
126                     }
127                     if (cls.indexOf(".") < 0) {
128                         cls = jpath + "." + cls;
129                     }
130                     roles.add(new ClassRole(cls, "provides", prv));
131 
132                 } else {
133                     E.error("cant extract info from " + elt);
134                 }
135 
136             } else if (elt.getName().equals("Factory")) {
137                 String cls = elt.getAttribute("class");
138                 String val = elt.getAttribute("makes");
139                 if (cls != null && val != null) {
140                     roles.add(new ClassRole(cls, "makes", val));
141                 } else {
142                     E.error("cant extract info from " + elt);
143                 }
144 
145 
146             } else {
147                 E.error("unhandled info element " + elt);
148             }
149         }
150     }
151 
152 
153 
154     private void addResource(Element elt, DecFile xmf) {
155 
156         files.add(xmf);
157 
158         if (false) { // was  if id == Type
159 
160         } else {
161             ArrayList<Element> subelements = new ArrayList<Element>();
162             if (elt.hasElements()) {
163                 subelements.addAll(elt.getElements());
164             }
165 
166             for (Element sub : subelements) {
167                 if (sub.getName().equals("Role")) {
168                     ResourceRole role = new ResourceRole();
169                     role.populateFrom(sub);
170                     role.setResource(xmf.getName());
171 
172                     roles.add(role);
173                 }
174             }
175         }
176     }
177 
178 
179 
180 
181     public static DecManifest rebuildManifest(File ftop) {
182         File fdest = new File(ftop, "DecManifest.xml");
183         FileUtil.writeStringToFile("", fdest);
184         DecManifest xm = new DecManifest(ftop, "");
185 
186         String ser = Serializer.serialize(xm);
187         FileUtil.writeStringToFile(ser, fdest);
188         return xm;
189     }
190 
191 
192 
193 }