1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.objectweb.fractal.cecilia.adl.interfaces;
26
27 import static org.objectweb.fractal.cecilia.adl.interfaces.InterfaceDecorationUtil.setFlattenedCollectionCardinality;
28 import static org.objectweb.fractal.cecilia.adl.interfaces.InterfaceDecorationUtil.setFlattenedCollectionName;
29
30 import java.util.Map;
31
32 import org.objectweb.fractal.adl.ADLException;
33 import org.objectweb.fractal.adl.AbstractLoader;
34 import org.objectweb.fractal.adl.Definition;
35 import org.objectweb.fractal.adl.Node;
36 import org.objectweb.fractal.adl.NodeUtil;
37 import org.objectweb.fractal.adl.components.Component;
38 import org.objectweb.fractal.adl.components.ComponentContainer;
39 import org.objectweb.fractal.adl.interfaces.Interface;
40 import org.objectweb.fractal.adl.interfaces.InterfaceContainer;
41 import org.objectweb.fractal.adl.types.TypeInterface;
42
43
44
45
46
47
48
49 public class BoundedCollectionInterfaceFlattenerLoader extends AbstractLoader {
50
51
52
53
54
55 public Definition load(final String name, final Map<Object, Object> context)
56 throws ADLException {
57 final Definition d = clientLoader.load(name, context);
58 checkNode(d, context);
59 return d;
60 }
61
62 private void checkNode(final Object node, final Map<Object, Object> context)
63 throws ADLException {
64 if (node instanceof InterfaceContainer) {
65 checkInterfaceContainer((InterfaceContainer) node);
66 }
67 if (node instanceof ComponentContainer) {
68 for (final Component comp : ((ComponentContainer) node).getComponents()) {
69 checkNode(comp, context);
70 }
71 }
72 }
73
74 private void checkInterfaceContainer(final InterfaceContainer container)
75 throws ADLException {
76 final Interface[] itfs = container.getInterfaces();
77 if (itfs != null) {
78 for (final Interface itf : itfs) {
79 NodeUtil.castNodeError(itf, TypeInterface.class);
80
81 final String cardinalityStr = ((TypeInterface) itf).getCardinality();
82
83
84 if (cardinalityStr == null) continue;
85 try {
86 final Integer cardinality = Integer.valueOf(cardinalityStr);
87 if (cardinality > 0) {
88 final String itfName = itf.getName();
89
90
91 for (int i = 1; i <= cardinality; i++) {
92 final Node newItf = NodeUtil.cloneNode(itf);
93 NodeUtil.castNodeError(newItf, TypeInterface.class);
94
95 setFlattenedCollectionName((Interface) newItf, itfName);
96 setFlattenedCollectionCardinality((Interface) newItf, cardinality);
97 ((TypeInterface) newItf).setName(itfName + i);
98 ((TypeInterface) newItf)
99 .setCardinality(TypeInterface.SINGLETON_CARDINALITY);
100
101 container.addInterface((Interface) newItf);
102 }
103
104
105 container.removeInterface(itf);
106 }
107 } catch (final NumberFormatException nfe) {
108
109 }
110 }
111 }
112 }
113 }