KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > interfaces > InterfaceLoader


1 /***
2  * Fractal ADL Parser
3  * Copyright (C) 2002-2004 France Telecom R&D
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: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.adl.interfaces;
25
26 import org.objectweb.fractal.adl.ADLException;
27 import org.objectweb.fractal.adl.Definition;
28 import org.objectweb.fractal.adl.AbstractLoader;
29 import org.objectweb.fractal.adl.Node;
30 import org.objectweb.fractal.adl.components.Component;
31 import org.objectweb.fractal.adl.components.ComponentContainer;
32
33 import java.util.HashSet JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36
37 /**
38  * A {@link org.objectweb.fractal.adl.Loader} to check {@link Interface} nodes
39  * in definitions. This loader checks that interface names are locally unique.
40  */

41
42 public class InterfaceLoader extends AbstractLoader {
43   
44   // --------------------------------------------------------------------------
45
// Implementation of the Loader interface
46
// --------------------------------------------------------------------------
47

48   public Definition load (final String JavaDoc name, final Map JavaDoc context)
49     throws ADLException
50   {
51     Definition d = clientLoader.load(name, context);
52     checkNode(d);
53     return d;
54   }
55
56   // --------------------------------------------------------------------------
57
// Checking methods
58
// --------------------------------------------------------------------------
59

60   private void checkNode (final Object JavaDoc node) throws ADLException {
61     if (node instanceof InterfaceContainer) {
62       checkInterfaceContainer((InterfaceContainer)node);
63     }
64     if (node instanceof ComponentContainer) {
65       Component[] comps = ((ComponentContainer)node).getComponents();
66       for (int i = 0; i < comps.length; i++) {
67         checkNode(comps[i]);
68       }
69     }
70   }
71
72   private void checkInterfaceContainer (final InterfaceContainer container)
73     throws ADLException
74   {
75     Set JavaDoc names = new HashSet JavaDoc();
76     Interface[] itfs = container.getInterfaces();
77     for (int i = 0; i < itfs.length; i++) {
78       Interface itf = itfs[i];
79       if (itf.getName() == null) {
80         throw new ADLException("Interface name missing", (Node)itf);
81       }
82       if (names.contains(itf.getName())) {
83         throw new ADLException(
84           "Duplicate interface name '" + itf.getName() + "'", (Node)itf);
85       } else {
86         names.add(itf.getName());
87       }
88     }
89   }
90 }
91
Popular Tags