KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > jar > adl > FileLoader


1 /*
2  * Copyright area
3  */

4
5 package org.objectweb.fractal.jar.adl;
6
7 import java.util.HashSet JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.Set JavaDoc;
10
11 import org.objectweb.fractal.adl.ADLException;
12 import org.objectweb.fractal.adl.AbstractLoader;
13 import org.objectweb.fractal.adl.Definition;
14 import org.objectweb.fractal.adl.Node;
15 import org.objectweb.fractal.adl.attributes.Attributes;
16 import org.objectweb.fractal.adl.attributes.AttributesContainer;
17 import org.objectweb.fractal.adl.components.Component;
18 import org.objectweb.fractal.adl.components.ComponentContainer;
19 import org.objectweb.fractal.adl.implementations.Implementation;
20 import org.objectweb.fractal.adl.implementations.ImplementationContainer;
21 import org.objectweb.fractal.adl.interfaces.Interface;
22 import org.objectweb.fractal.adl.interfaces.InterfaceContainer;
23 import org.objectweb.fractal.adl.types.TypeInterface;
24
25 public class FileLoader extends AbstractLoader {
26   
27   // --------------------------------------------------------------------------
28
// Implementation of the Loader interface
29
// --------------------------------------------------------------------------
30

31   public Definition load (final String JavaDoc name, final Map JavaDoc context)
32     throws ADLException
33   {
34     Definition d = clientLoader.load(name, context);
35     checkNode((Node)d);
36     return d;
37   }
38   
39   // --------------------------------------------------------------------------
40
// Checking methods
41
// --------------------------------------------------------------------------
42

43   private void checkNode (Node node) throws ADLException {
44     Set JavaDoc classes = new HashSet JavaDoc();
45     Set JavaDoc files = new HashSet JavaDoc();
46     getClasses(node, classes);
47     getFiles(node, files);
48     classes.removeAll(files);
49     if (classes.size() > 0) {
50       throw new ADLException(
51         classes.iterator().next() +
52         " is missing in the component's files list", node);
53     }
54     if (node instanceof ComponentContainer) {
55       Component[] comps = ((ComponentContainer)node).getComponents();
56       for (int i = 0; i < comps.length; i++) {
57         checkNode((Node)comps[i]);
58       }
59     }
60   }
61   
62   private void getClasses (Node n, Set JavaDoc s) {
63     if (n instanceof InterfaceContainer) {
64       Interface[] itfs = ((InterfaceContainer)n).getInterfaces();
65       for (int i = 0; i < itfs.length; ++i) {
66         if (itfs[i] instanceof TypeInterface) {
67           addFile(((TypeInterface)itfs[i]).getSignature(), s);
68         }
69       }
70     }
71     if (n instanceof AttributesContainer) {
72       Attributes a = ((AttributesContainer)n).getAttributes();
73       if (a != null && a.getSignature() != null) {
74         addFile(a.getSignature(), s);
75       }
76     }
77     if (n instanceof ImplementationContainer) {
78       Implementation i = ((ImplementationContainer)n).getImplementation();
79       if (i != null) {
80         addFile(i.getClassName(), s);
81       }
82     }
83   }
84   
85   private static void addFile (String JavaDoc f, Set JavaDoc s) {
86     if (!f.startsWith("java") && !f.startsWith("org/objectweb/fractal/api")) {
87       s.add(f.replace('.', '/') + ".class");
88     }
89   }
90
91   private void getFiles (Node n, Set JavaDoc s) {
92     if (n instanceof FileContainer) {
93       File[] files = ((FileContainer)n).getFiles();
94       for (int i = 0; i < files.length; ++i) {
95         s.add(files[i].getName());
96       }
97     }
98   }
99 }
100
Popular Tags