View Javadoc

1   /***
2    * Cecilia ADL Compiler
3    * Copyright (C) 2006-2008 STMicroelectronics
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: Matthieu Leclercq
22   * Contributor: Alessio Pace
23   */
24  
25  package org.objectweb.fractal.cecilia.adl.interfaces;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.objectweb.fractal.adl.Node;
31  import org.objectweb.fractal.adl.interfaces.Interface;
32  import org.objectweb.fractal.adl.interfaces.InterfaceContainer;
33  import org.objectweb.fractal.cecilia.adl.idl.ast.IDLDefinition;
34  
35  /**
36   * Utility class to manipulate decorations of {@link Interface} node.
37   */
38  public final class InterfaceDecorationUtil {
39  
40    /**
41     * A decoration set on client {@link Interface} nodes to say that binding to
42     * this client interface must not be established statically. The value of this
43     * decoration must be a {@link Boolean}.
44     * 
45     * @see #setNoStaticBinding(Interface, boolean)
46     * @see #isNoStaticBinding(Interface)
47     * @see org.objectweb.fractal.cecilia.adl.bindings.BindingDecorationUtil#STATICALLY_BOUND_DECORATION
48     */
49    public static final String NO_STATIC_BINDING_DECORATION = "no-static-binding";
50  
51    /**
52     * A decoration set on a client {@link Interface} node to indicates to which
53     * server interface it is bound to. The value of this decoration must be an
54     * {@link Interface} node.
55     * 
56     * @see #setBoundTo(Interface, Interface)
57     * @see #getBoundTo(Interface)
58     */
59    public static final String BOUND_TO_DECORATION          = "bound-to";
60  
61    /**
62     * A decoration set on {@link Interface} node to find the component to which
63     * the interface belongs to. The value of this decoration must be an
64     * {@link InterfaceContainer} node.
65     */
66    public static final String CONTAINER_DECORATION         = "container";
67  
68    /**
69     * A decoration set on {@link IDLDefinition} node to add the ordered list of
70     * IDL interfaces along the inheritance path, starting from the IDLDefinition
71     * signature itself.
72     */
73    public static final String INHERITANCE_PATH_DECORATION  = "INHERITANCE_PATH_DECORATION";
74  
75    /**
76     * A decoration set on {@link Interface} node to record the name of the
77     * original, non-flattened a bounded "collection" interface, on singleton
78     * interfaces that were created by flattening a bounded "collection" interface.
79     */
80    public static final String FLATTENED_COLLECTION_NAME  = "flattened-collection-name";
81  
82    /**
83     * A decoration set on {@link Interface} node to record the cardinality of the
84     * original, non-flattened a bounded "collection" interface, on singleton
85     * interfaces that were created by flattening a bounded "collection" interface.
86     */
87    public static final String FLATTENED_COLLECTION_CARDINALITY  = "flattened-collection-cardinality";
88  
89    private InterfaceDecorationUtil() {
90    }
91  
92    /**
93     * Sets the {@link #NO_STATIC_BINDING_DECORATION} decoration to the given node
94     * with the given boolean value.
95     * 
96     * @param itf the interface node to which the decoration is set.
97     * @param b the value of the decoration.
98     * @see #NO_STATIC_BINDING_DECORATION
99     */
100   public static void setNoStaticBinding(final Interface itf, final boolean b) {
101     ((Node) itf).astSetDecoration(NO_STATIC_BINDING_DECORATION, b);
102   }
103 
104   /**
105    * Returns <code>true</code> if the given interface node has a
106    * {@link #NO_STATIC_BINDING_DECORATION} decoration with the <code>true</code>
107    * value.
108    * 
109    * @param itf an interface node.
110    * @return <code>true</code> if the given binding node has a
111    *         {@link #NO_STATIC_BINDING_DECORATION} decoration with the
112    *         <code>true</code> value.
113    * @see #NO_STATIC_BINDING_DECORATION
114    */
115   public static boolean isNoStaticBinding(final Interface itf) {
116     final Boolean b = (Boolean) ((Node) itf)
117         .astGetDecoration(NO_STATIC_BINDING_DECORATION);
118     return b != null && b;
119   }
120 
121   /**
122    * Sets the {@link #BOUND_TO_DECORATION} decoration to the given client
123    * interface node with the given server interface node value.
124    * 
125    * @param clientItf an interface node on which the decoration will be set.
126    * @param serverItf the value of the decoration.
127    * @see #BOUND_TO_DECORATION
128    */
129   public static void setBoundTo(final Interface clientItf,
130       final Interface serverItf) {
131     ((Node) clientItf).astSetDecoration(BOUND_TO_DECORATION, serverItf);
132   }
133 
134   /**
135    * Returns the value of the {@link #BOUND_TO_DECORATION} decoration of the
136    * given client interface node.
137    * 
138    * @param clientItf an interface node.
139    * @return The value of the {@link #BOUND_TO_DECORATION} decoration or
140    *         <code>null</code> if the given node has no such decoration.
141    * @see #BOUND_TO_DECORATION
142    */
143   public static Interface getBoundTo(final Interface clientItf) {
144     return (Interface) ((Node) clientItf).astGetDecoration(BOUND_TO_DECORATION);
145   }
146 
147   /**
148    * Sets the value of the {@link #CONTAINER_DECORATION} decoration to the given
149    * interface container node.
150    * 
151    * @param itf an interface node.
152    * @param itfContainer the value of the decoration.
153    * @see #CONTAINER_DECORATION
154    */
155   public static void setContainer(final Interface itf,
156       final InterfaceContainer itfContainer) {
157     ((Node) itf).astSetDecoration(CONTAINER_DECORATION, itfContainer);
158   }
159 
160   /**
161    * Returns the value of the {@link #CONTAINER_DECORATION} decoration of the
162    * given interface node.
163    * 
164    * @param itf an interface node.
165    * @return The value of the {@link #CONTAINER_DECORATION} decoration or
166    *         <code>null</code> if the given node has no such decoration.
167    * @see #CONTAINER_DECORATION
168    */
169   public static InterfaceContainer getContainer(final Interface itf) {
170     return (InterfaceContainer) ((Node) itf)
171         .astGetDecoration(CONTAINER_DECORATION);
172   }
173 
174   /**
175    * Adds the given signature to the value of the
176    * {@link #INHERITANCE_PATH_DECORATION} decoration.
177    * 
178    * @param node the node on which the decoration is updated
179    * @param signature the signature to be added.
180    */
181   public static void updateInheritancePathDecoration(final IDLDefinition node,
182       final String signature) {
183 
184     List<String> inheritanceList = getInheritancePathDecoration(node);
185 
186     if (inheritanceList == null) {
187       inheritanceList = new ArrayList<String>();
188       ((Node) node).astSetDecoration(INHERITANCE_PATH_DECORATION,
189           inheritanceList);
190     }
191 
192     inheritanceList.add(signature);
193   }
194 
195   /**
196    * Adds the given list of signature to the value of the
197    * {@link #INHERITANCE_PATH_DECORATION} decoration.
198    * 
199    * @param node the node on which the decoration is updated
200    * @param parentInheritancePath the list of signature to be added.
201    */
202   public static void updateInheritancePathDecoration(final IDLDefinition node,
203       final List<String> parentInheritancePath) {
204 
205     List<String> inheritanceList = getInheritancePathDecoration(node);
206 
207     if (inheritanceList == null) {
208       inheritanceList = new ArrayList<String>();
209       ((Node) node).astSetDecoration(INHERITANCE_PATH_DECORATION,
210           inheritanceList);
211     }
212 
213     inheritanceList.addAll(parentInheritancePath);
214   }
215 
216   /**
217    * Returns the value of the {@link #INHERITANCE_PATH_DECORATION} decoration of
218    * the given node.
219    * 
220    * @param node a node
221    * @return the value of the {@link #INHERITANCE_PATH_DECORATION} decoration of
222    *         the given node. May be <code>null</code>.
223    */
224   @SuppressWarnings("unchecked")
225   public static List<String> getInheritancePathDecoration(
226       final IDLDefinition node) {
227     return (List<String>) node.astGetDecoration(INHERITANCE_PATH_DECORATION);
228   }
229 
230   /**
231    * Returns <code>true</code> is the {@link #INHERITANCE_PATH_DECORATION}
232    * decoration is present on the given node.
233    * 
234    * @param node a node
235    * @return <code>true</code> is the {@link #INHERITANCE_PATH_DECORATION}
236    *         decoration is present on the given node.
237    */
238   public static boolean isInheritancePathDecorationPresent(
239       final IDLDefinition node) {
240     return getInheritancePathDecoration(node) != null;
241   }
242 
243   /**
244    * Sets the {@link #FLATTENED_COLLECTION_NAME} decoration on the given node
245    * 
246    * @param itf the interface node on which the decoration is set.
247    * @param name the name of the un-flattened bounded "collection" interface
248    * @see #FLATTENED_COLLECTION_NAME
249    */
250   public static void setFlattenedCollectionName(final Interface itf,
251       final String name) {
252     ((Node) itf).astSetDecoration(FLATTENED_COLLECTION_NAME, name);
253   }
254 
255   /**
256    * Returns the value of the {@link #FLATTENED_COLLECTION_NAME} decoration of the
257    * given interface node.
258    * 
259    * @param itf an interface node.
260    * @return The value of the {@link #FLATTENED_COLLECTION_NAME} decoration or
261    *         <code>null</code> if the given node has no such decoration.
262    */
263   public static String getFlattenedCollectionName(final Interface itf) {
264     return (String) ((Node) itf).astGetDecoration(FLATTENED_COLLECTION_NAME);
265   }
266 
267   /**
268    * Sets the {@link #FLATTENED_COLLECTION_CARDINALITY} decoration on the given node
269    * 
270    * @param itf the interface node on which the decoration is set.
271    * @param cardinality the cardinality of the un-flattened bounded "collection" interface
272    * @see #FLATTENED_COLLECTION_CARDINALITY
273    */
274   public static void setFlattenedCollectionCardinality(final Interface itf,
275       final Integer cardinality) {
276     ((Node) itf).astSetDecoration(FLATTENED_COLLECTION_CARDINALITY, cardinality);
277   }
278 
279   /**
280    * Returns the value of the {@link #FLATTENED_COLLECTION_CARDINALITY} decoration of the
281    * given interface node.
282    * 
283    * @param itf an interface node.
284    * @return The value of the {@link #FLATTENED_COLLECTION_CARDINALITY} decoration or
285    *         <code>null</code> if the given node has no such decoration.
286    */
287   public static Integer getFlattenedCollectionCardinality(final Interface itf) {
288     return (Integer) ((Node) itf).astGetDecoration(FLATTENED_COLLECTION_CARDINALITY);
289   }
290 }