KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > types > StaticFractalTypeBuilder


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.types;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.WeakHashMap JavaDoc;
29
30 /**
31  * A Fractal based, static implementation of the {@link TypeBuilder} interface.
32  * This implementation produces Java code that uses the Fractal API to create
33  * types.
34  */

35
36 public class StaticFractalTypeBuilder implements TypeBuilder {
37
38   private Map JavaDoc itfCounters = new WeakHashMap JavaDoc();
39   
40   private Map JavaDoc compCounters = new WeakHashMap JavaDoc();
41   
42   // --------------------------------------------------------------------------
43
// Implementation of the TypeBuilder interface
44
// --------------------------------------------------------------------------
45

46   public Object JavaDoc createInterfaceType (
47     final String JavaDoc name,
48     final String JavaDoc signature,
49     final String JavaDoc role,
50     final String JavaDoc contingency,
51     final String JavaDoc cardinality,
52     final Object JavaDoc context) throws Exception JavaDoc
53   {
54     Integer JavaDoc i = (Integer JavaDoc)itfCounters.get(context);
55     if (i == null) {
56       i = new Integer JavaDoc(0);
57     }
58     itfCounters.put(context, new Integer JavaDoc(i.intValue() + 1));
59     String JavaDoc id = "IT" + i;
60     
61     PrintWriter JavaDoc pw = (PrintWriter JavaDoc)((Map JavaDoc)context).get("printwriter");
62     pw.print("InterfaceType ");
63     pw.print(id);
64     pw.print(" = typeFactory.createFcItfType(\"");
65     pw.print(name);
66     pw.print("\", \"");
67     pw.print(signature);
68     pw.print("\", ");
69     pw.print("client".equals(role));
70     pw.print(", ");
71     pw.print("optional".equals(contingency));
72     pw.print(", ");
73     pw.print("collection".equals(cardinality));
74     pw.println(");");
75     
76     return id;
77   }
78
79   public Object JavaDoc createComponentType (
80     final String JavaDoc name,
81     final Object JavaDoc[] interfaceTypes,
82     final Object JavaDoc context) throws Exception JavaDoc
83   {
84     Integer JavaDoc i = (Integer JavaDoc)compCounters.get(context);
85     if (i == null) {
86       i = new Integer JavaDoc(0);
87     }
88     compCounters.put(context, new Integer JavaDoc(i.intValue() + 1));
89     String JavaDoc id = "CT" + i;
90     
91     PrintWriter JavaDoc pw = (PrintWriter JavaDoc)((Map JavaDoc)context).get("printwriter");
92     pw.print("ComponentType ");
93     pw.print(id);
94     pw.print(" = typeFactory.createFcType(new InterfaceType [] { ");
95     for (int j = 0; j < interfaceTypes.length; ++j) {
96       if (j > 0) {
97         pw.print(", ");
98       }
99       pw.print(interfaceTypes[j]);
100     }
101     pw.println(" });");
102     return id;
103   }
104 }
105
Popular Tags