View Javadoc

1   /***
2    * Cecilia ADL Compiler
3    * Copyright (C) 2009 INRIA
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: fractal@objectweb.org
20   *
21   * Author: Lionel Debroux
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   * A {@link org.objectweb.fractal.adl.Loader} to handle {@link Interface} nodes
45   * that contain fake collection interfaces (convenient one-line declaration for
46   * n..m cardinality collection interfaces). These are flattened to m singleton
47   * interfaces.
48   */
49  public class BoundedCollectionInterfaceFlattenerLoader extends AbstractLoader {
50  
51    // --------------------------------------------------------------------------
52    // Implementation of the Loader interface
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          // if the cardinality is null, pass it.
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              // We have a valid bounded "collection" interface.
90              // Create new singleton nodes, add them to the container.
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             // Remove the bounded "collection" interface.
105             container.removeInterface(itf);
106           }
107         } catch (final NumberFormatException nfe) {
108           // Do nothing, as in this Loader, we want to handle only integers.
109         }
110       }
111     }
112   }
113 }