KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > impl > tayloredBased > AssociationImplementationGenerator


1 /**
2  * copyright 2002 2003 Laboratoire d'Informatique Paris 6 (LIP6)
3  *
4  * This file is part of ModFact.
5  *
6  * ModFact is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * at your option) any later version.
10  *
11  * ModFact is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with ModFact; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20 package org.objectweb.modfact.jmi.impl.tayloredBased;
21
22 import org.objectweb.modfact.jmi.helper.*;
23 import org.objectweb.modfact.jmi.logging.ModFactLogger;
24
25
26 import javax.jmi.model.*;
27
28 import org.objectweb.modfact.jmi.generator.PrintGenerator;
29
30
31 /**
32  * @author Xavier
33  *
34  */

35 public class AssociationImplementationGenerator extends PrintGenerator {
36     Association[] input;
37
38     ModFactLogger logger;
39
40     /**
41      * Set Input
42      */

43     public void setInput(ModelElement[] elt) {
44         input = new Association[elt.length];
45         for (int i = 0; i < input.length; i++) {
46             input[i] = (Association) elt[i];
47         }
48     }
49
50     /**
51      * Set Trace
52      */

53     public void setLogger(ModFactLogger log) {
54         logger = log;
55     }
56
57
58     Association association;
59     AssociationEnd[] ends;
60     String JavaDoc classEnd0;
61     String JavaDoc classEnd1;
62     String JavaDoc end0;
63     String JavaDoc end1;
64     
65     public void generate() {
66         association = input[0];
67         out.println("package " +ImplHelper.implPrefix +JMIProvider.shortQualifierOf(association) +";");
68         out.println("import " +JMIProvider.qualifierOf(association) +".*;");
69         out.println("import org.objectweb.modfact.jmi.reflect.*;");
70         out.println(
71             "public class "
72                 + JMIProvider.jmiAssociationName(association)
73                 + "Impl extends RefAssociationImpl "
74                 + " implements "
75                 + JMIProvider.jmiAssociationQualifiedName(association)
76                 + " { \n" );
77         
78         ends = MofHelper.associationEndsOfAssociation(association);
79         classEnd0 = JMIProvider.jmiClassifierQualifiedName(ends[0].getType());
80         classEnd1 = JMIProvider.jmiClassifierQualifiedName(ends[1].getType());
81         end0 = ends[0].getName();
82         end1 = ends[1].getName();
83         
84         existsTemplate();
85         addTemplate();
86         removeTemplate();
87         queryTemplate(ends[0], end0, classEnd0, end1, classEnd1);
88         queryTemplate(ends[1], end1, classEnd1, end0, classEnd0);
89                                             
90         out.println("}");
91         out.flush();
92     }
93     
94     
95     void queryTemplate(AssociationEnd end, String JavaDoc endName, String JavaDoc classEndName,
96             String JavaDoc otherEndName, String JavaDoc otherClassEndName) {
97         //if associationEnd0 is single valued and isNavigable
98
if (end.isNavigable()
99             && isSingleValued(end) ) {
100             out.println(
101                 "\tpublic "
102                     + classEndName
103                     + " get"
104                     + JMIProvider.jmiFormat1(endName)
105                     + "("
106                     + otherClassEndName
107                     + " "
108                     + otherEndName
109                     + ") throws javax.jmi.reflect.JmiException { ");
110
111             out.println("\t\tjava.util.Collection c =refQuery(\""+otherEndName +"\"," +otherEndName +");");
112             out.println("\t\tif(c.isEmpty()) return null;");
113             out.println("\t\treturn (" +classEndName +") c.iterator().next();");
114             out.println("\t } ");
115         }
116         
117         //if associationEnd0 is multivalued, and isNavigable
118
if (end.isNavigable()
119             && !isSingleValued(end) ) {
120             out.println(
121                 "\tpublic "
122                     +(end.getMultiplicity().isOrdered()?
123                         "java.util.List" : "java.util.Collection"
124                      )
125                     +" get"
126                     + JMIProvider.jmiFormat1(endName)
127                     + "("
128                     + otherClassEndName
129                     + " "
130                     + "end"
131                     + ") throws javax.jmi.reflect.JmiException { ");
132
133             out.println("\t\treturn (java.util.List) refQuery(\""+otherEndName +"\"," +"end"+");");
134             out.println("\t } ");
135         }
136     }
137     
138     
139     void addTemplate() {
140         // if associationEnd0 and associationEnd1 isChangeable
141
if (ends[0].isChangeable() && ends[1].isChangeable()) {
142                   out.println(
143                       "\tpublic boolean add("
144                           + classEnd0
145                           + " "
146                           + "end0"
147                           + ","
148                           + classEnd1
149                           + " "
150                           + "end1"
151                           + ") throws javax.jmi.reflect.JmiException { ");
152                   out.println("\t\treturn refAddLink("+"end0"+","+"end1"+");");
153                   out.println("\t } ");
154               }
155     }
156     
157     void existsTemplate() {
158         out.println(
159             "\tpublic boolean exists("
160                 + classEnd0
161                 + " "
162                 + "end0"
163                 + ","
164                 + classEnd1
165                 + " "
166                 + "end1"
167                 + ") throws javax.jmi.reflect.JmiException { ");
168                 
169         out.println("\t\treturn refLinkExists("+ "end0"+","+"end1"+");");
170         out.println("\t} ");
171     }
172     
173     void removeTemplate() {
174         out.println(
175             "\tpublic boolean remove("
176                 + classEnd0
177                 + " "
178                 + "end0"
179                 + ","
180                 + classEnd1
181                 + " "
182                 + "end1"
183                 + ") throws javax.jmi.reflect.JmiException "
184                 //+", javax.jmi.reflect.NotFoundException"
185
+" { " );
186                 
187         out.println("\t\treturn refRemoveLink("+"end0"+","+"end1"+");");
188         out.println("\t } ");
189     }
190     
191     
192
193     boolean isSingleValued(AssociationEnd end) {
194       return end.getMultiplicity().getUpper() == 1;
195     }
196
197 }
198
Popular Tags