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
22 */
23
24 package org.objectweb.fractal.cecilia.adl.interfaces;
25
26 import static org.objectweb.fractal.adl.NodeUtil.castNodeError;
27
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map;
31
32 import org.objectweb.fractal.adl.ADLException;
33 import org.objectweb.fractal.adl.ComponentVisitor;
34 import org.objectweb.fractal.adl.Node;
35 import org.objectweb.fractal.adl.components.ComponentContainer;
36 import org.objectweb.fractal.adl.interfaces.Interface;
37 import org.objectweb.fractal.adl.interfaces.InterfaceContainer;
38 import org.objectweb.fractal.adl.types.TypeInterface;
39 import org.objectweb.fractal.api.Component;
40 import org.objectweb.fractal.api.factory.InstantiationException;
41 import org.objectweb.fractal.task.core.AbstractTaskFactoryUser;
42 import org.objectweb.fractal.task.core.TaskException;
43
44 /**
45 * Abstract visitor component that creates a task that depends on the interfaces
46 * of the component (if any).
47 */
48 public abstract class AbstractInterfaceVisitor extends AbstractTaskFactoryUser
49 implements
50 ComponentVisitor {
51
52 /**
53 * Creates the task that is returned by this visitor.
54 *
55 * @param container the component that is visited.
56 * @param interfaces the interfaces of the component (contains at least one
57 * element).
58 * @return the created task.
59 * @throws InstantiationException if the instantiation of the task fails.
60 * @throws ADLException if the instantiation of the task fails.
61 */
62 protected abstract Component createTask(ComponentContainer container,
63 List<TypeInterface> interfaces) throws ADLException, TaskException;
64
65 // ---------------------------------------------------------------------------
66 // Implementation of the ComponentVisitor interface
67 // ---------------------------------------------------------------------------
68
69 /**
70 * Visits {@link ComponentContainer} nodes and call
71 * {@link #createTask(ComponentContainer, Interface[]) createTask} if the
72 * given component contains {@link Interface} sub-nodes.
73 */
74 public Component visit(final List<Node> path,
75 final ComponentContainer container, final Map<Object, Object> context)
76 throws ADLException, TaskException {
77 final List<TypeInterface> itfs = new ArrayList<TypeInterface>();
78 if (container instanceof InterfaceContainer) {
79 for (final Interface itf : ((InterfaceContainer) container)
80 .getInterfaces()) {
81 itfs.add(castNodeError(itf, TypeInterface.class));
82 }
83 }
84 return createTask(container, itfs);
85 }
86
87 }