KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > types > TypeLoader


1 /***
2  * Fractal ADL Parser
3  * Copyright (C) 2002-2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.adl.types;
25
26 import java.util.Map JavaDoc;
27
28 import org.objectweb.fractal.adl.ADLException;
29 import org.objectweb.fractal.adl.Definition;
30 import org.objectweb.fractal.adl.AbstractLoader;
31 import org.objectweb.fractal.adl.Node;
32 import org.objectweb.fractal.adl.components.Component;
33 import org.objectweb.fractal.adl.components.ComponentContainer;
34 import org.objectweb.fractal.adl.components.ComponentDefinition;
35 import org.objectweb.fractal.adl.interfaces.Interface;
36 import org.objectweb.fractal.adl.interfaces.InterfaceContainer;
37
38 /**
39  * A {@link org.objectweb.fractal.adl.Loader} to check {@link TypeInterface}
40  * nodes in definitions. This loader checks that the Java interfaces specified
41  * in these nodes exist.
42  */

43
44 public class TypeLoader extends AbstractLoader {
45   
46   // --------------------------------------------------------------------------
47
// Implementation of the Loader interface
48
// --------------------------------------------------------------------------
49

50   public Definition load (final String JavaDoc name, final Map JavaDoc context)
51     throws ADLException
52   {
53     Definition d = clientLoader.load(name, context);
54     boolean extend = false;
55     if (d instanceof ComponentDefinition) {
56       extend = ((ComponentDefinition)d).getExtends() != null;
57     }
58     checkNode(d, extend, context);
59     return d;
60   }
61
62   // --------------------------------------------------------------------------
63
// Checking methods
64
// --------------------------------------------------------------------------
65

66   private void checkNode (
67     final Object JavaDoc node,
68     final boolean extend,
69     final Map JavaDoc context) throws ADLException
70   {
71     if (node instanceof InterfaceContainer) {
72       checkInterfaceContainer((InterfaceContainer)node, extend, context);
73     }
74     if (node instanceof ComponentContainer) {
75       Component[] comps = ((ComponentContainer)node).getComponents();
76       for (int i = 0; i < comps.length; i++) {
77         checkNode(comps[i], extend, context);
78       }
79     }
80   }
81
82   private void checkInterfaceContainer (
83     final InterfaceContainer container,
84     final boolean extend,
85     final Map JavaDoc context) throws ADLException
86   {
87     Interface[] itfs = container.getInterfaces();
88     for (int i = 0; i < itfs.length; i++) {
89       Interface itf = itfs[i];
90       if (itf instanceof TypeInterface) {
91         String JavaDoc signature = ((TypeInterface)itf).getSignature();
92         if (signature == null) {
93           if (!extend) {
94             throw new ADLException("Signature missing", (Node)itf);
95           }
96         } else {
97           try {
98             getClassLoader(context).loadClass(signature);
99           } catch (ClassNotFoundException JavaDoc e) {
100             throw new ADLException(
101               "Invalid signature '" + signature + "'", (Node)itf, e);
102           }
103         }
104         String JavaDoc role = ((TypeInterface)itf).getRole();
105         if (role == null) {
106           if (!extend) {
107             throw new ADLException("Role missing", (Node)itf);
108           }
109         } else {
110           if (!role.equals("client") && !role.equals("server")) {
111             throw new ADLException("Invalid role '" + role + "'", (Node)itf);
112           }
113         }
114         String JavaDoc contingency = ((TypeInterface)itf).getContingency();
115         if (contingency != null) {
116           if (!contingency.equals("mandatory") && !contingency.equals("optional")) {
117             throw new ADLException("Invalid contingency '" + contingency + "'", (Node)itf);
118           }
119         }
120         String JavaDoc cardinality = ((TypeInterface)itf).getCardinality();
121         if (cardinality != null) {
122           if (!cardinality.equals("singleton") && !cardinality.equals("collection")) {
123             throw new ADLException("Invalid cardinality '" + cardinality + "'", (Node)itf);
124           }
125         }
126       }
127     }
128   }
129 }
130
Popular Tags