View Javadoc

1   /***
2    * Cecilia ADL Compiler
3    * Copyright (C) 2006-2007 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, Ali Erdem Ozcan
22   */
23  
24  package org.objectweb.fractal.cecilia.adl.types;
25  
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.objectweb.fractal.adl.ADLException;
30  import org.objectweb.fractal.adl.ComponentVisitor;
31  import org.objectweb.fractal.adl.Definition;
32  import org.objectweb.fractal.adl.Node;
33  import org.objectweb.fractal.adl.components.ComponentContainer;
34  import org.objectweb.fractal.api.Component;
35  import org.objectweb.fractal.cecilia.adl.TypeNameProvider;
36  import org.objectweb.fractal.task.core.AbstractTaskFactoryUser;
37  import org.objectweb.fractal.task.core.TaskException;
38  import org.objectweb.fractal.task.core.primitive.annotations.ServerInterface;
39  import org.objectweb.fractal.task.core.primitive.annotations.ServerInterfaces;
40  import org.objectweb.fractal.task.core.primitive.annotations.TaskParameters;
41  
42  /**
43   * Creates a {@link TypeNameProvider} task for the visited component.
44   */
45  public class TypeVisitor extends AbstractTaskFactoryUser
46      implements
47        ComponentVisitor {
48  
49    public Component visit(final List<Node> path, final ComponentContainer node,
50        final Map<Object, Object> context) throws ADLException, TaskException {
51      return taskFactoryItf.newPrimitiveTask(new TypeNameProviderImpl(
52          getAbsoluteName(path, node)), node);
53    }
54  
55    // ---------------------------------------------------------------------------
56    // Utility methods
57    // ---------------------------------------------------------------------------
58  
59    protected String getAbsoluteName(final List<Node> path,
60        final ComponentContainer container) {
61      String realname = getComponentName(container);
62      for (int i = path.size() - 1; i >= 0; i--)
63        realname = getComponentName(path.get(i)) + "/" + realname;
64      return realname;
65    }
66  
67    protected String getComponentName(final Node container) {
68      if (container instanceof Definition)
69        return ((Definition) container).getName();
70      else if (container instanceof org.objectweb.fractal.adl.components.Component)
71        return ((org.objectweb.fractal.adl.components.Component) container)
72            .getName();
73      else
74        throw new IllegalArgumentException(
75            "The given node is neither a Definition nor a Component");
76    }
77  
78    // ---------------------------------------------------------------------------
79    // Task classes
80    // ---------------------------------------------------------------------------
81  
82    /**
83     * Provides the name of the type of the given component.
84     */
85    @TaskParameters("componentNode")
86    @ServerInterfaces(@ServerInterface(name = "type-name-provider", signature = TypeNameProvider.class, record = "role:typeNameProvider, id:%", parameters = "componentNode"))
87    public static class TypeNameProviderImpl implements TypeNameProvider {
88  
89      protected final String typeName;
90      protected final String cTypeName;
91  
92      // -------------------------------------------------------------------------
93      // Constructor
94      // -------------------------------------------------------------------------
95  
96      /**
97       * @param typeName the name provided by this task.
98       */
99      public TypeNameProviderImpl(final String typeName) {
100       this.typeName = typeName;
101       cTypeName = typeName.replace('.', '_').replace('-', '_')
102           .replace('/', '_');
103     }
104 
105     // -------------------------------------------------------------------------
106     // Implementation of the TypeNameProvider interface
107     // -------------------------------------------------------------------------
108 
109     public String getTypeName() {
110       return typeName;
111     }
112 
113     public String getCTypeName() {
114       return cTypeName;
115     }
116   }
117 }