KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > arguments > ArgumentComponentLoader


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.arguments;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 import org.objectweb.fractal.adl.ADLException;
34 import org.objectweb.fractal.adl.Definition;
35 import org.objectweb.fractal.adl.components.ComponentLoader;
36
37 public class ArgumentComponentLoader extends ComponentLoader {
38
39   public Definition load (
40     final List JavaDoc loaded,
41     final String JavaDoc name,
42     final Map JavaDoc context) throws ADLException
43   {
44     String JavaDoc n = name;
45     Map JavaDoc c = context;
46     int i = name.indexOf('(');
47     if (i != -1) {
48       if (!name.endsWith(")")) {
49         throw new ADLException("Syntax error in definition name", null);
50       }
51       
52       n = name.substring(0, i);
53       c = new HashMap JavaDoc();
54       Iterator JavaDoc j = context.keySet().iterator();
55       while (j.hasNext()) {
56         String JavaDoc key = (String JavaDoc)j.next();
57         if (!key.startsWith(" arg ")) {
58           c.put(key, context.get(key));
59         }
60       }
61       
62       String JavaDoc args = name.substring(i + 1, name.length() - 1);
63       StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(args, ",");
64       i = 0;
65       while (st.hasMoreTokens()) {
66         c.put(" arg " + (i++), st.nextToken());
67       }
68     }
69     return super.load(loaded, n, c);
70   }
71
72   public List JavaDoc parseDefinitions (String JavaDoc nameList) {
73     List JavaDoc l = new ArrayList JavaDoc();
74     int c = nameList.lastIndexOf('(');
75     int p = c == -1 ? nameList.lastIndexOf(',') : nameList.lastIndexOf(',', c);
76     while (p != -1) {
77       l.add(0, nameList.substring(p+1));
78       nameList = nameList.substring(0, p);
79       c = nameList.lastIndexOf('(');
80       p = c == -1 ? nameList.lastIndexOf(',') : nameList.lastIndexOf(',', c);
81     }
82     l.add(0, nameList);
83     return l;
84   }
85   
86   public boolean isShared (String JavaDoc definition) {
87     return definition.indexOf('/') != -1 && definition.indexOf('(') == -1;
88   }
89 }
90
Popular Tags