KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > api > AssociationGenerator


1 /**
2  * copyright 2002 2004 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.api;
21
22 import javax.jmi.model.Association;
23 import javax.jmi.model.AssociationEnd;
24 import javax.jmi.model.ModelElement;
25
26 import org.objectweb.modfact.jmi.generator.BracketGenerator;
27 import org.objectweb.modfact.jmi.helper.JMIProvider;
28 import org.objectweb.modfact.jmi.helper.MofHelper;
29 import org.objectweb.modfact.jmi.logging.ModFactLogger;
30
31
32 /**
33  * @author Xavier
34  *
35  */

36 public class AssociationGenerator extends BracketGenerator {
37     Association[] input;
38
39     ModFactLogger logger;
40
41     /**
42      * Set Input
43      */

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

54     public void setLogger(ModFactLogger log) {
55         logger = log;
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         outputln("package " +JMIProvider.qualifierOf(association) +";");
68
69         outputln(
70             "public interface "
71                 + JMIProvider.jmiAssociationName(association)
72                 + " extends javax.jmi.reflect.RefAssociation "
73                 + " { \n" );
74         
75         ends = MofHelper.associationEndsOfAssociation(association);
76         classEnd0 = JMIProvider.jmiClassifierQualifiedName(ends[0].getType());
77         classEnd1 = JMIProvider.jmiClassifierQualifiedName(ends[1].getType());
78         end0 = ends[0].getName();
79         end1 = ends[1].getName();
80         
81         existsTemplate();
82         addTemplate();
83         removeTemplate();
84         queryTemplate(ends[0], end0, classEnd0, end1, classEnd1);
85         queryTemplate(ends[1], end1, classEnd1, end0, classEnd0);
86                                             
87         outputln("}");
88         flushFile();
89     }
90     
91     
92     void queryTemplate(AssociationEnd end, String JavaDoc endName, String JavaDoc classEndName,
93             String JavaDoc otherEndName, String JavaDoc otherClassEndName) {
94         //if associationEnd0 is single valued and isNavigable
95
if (end.isNavigable()
96             && isSingleValued(end) ) {
97             outputln(
98                 "public "
99                     + classEndName
100                     + " get"
101                     + JMIProvider.jmiFormat1(endName)
102                     + "("
103                     + otherClassEndName
104                     + " "
105                     + otherEndName
106                     + ") throws javax.jmi.reflect.JmiException ; ");
107         }
108         
109         //if associationEnd0 is multivalued, and isNavigable
110
if (end.isNavigable()
111             && !isSingleValued(end) ) {
112             outputln(
113                 "public "
114                     +(end.getMultiplicity().isOrdered()?
115                         "java.util.List" : "java.util.Collection"
116                      )
117                     +" get"
118                     + JMIProvider.jmiFormat1(endName)
119                     + "("
120                     + otherClassEndName
121                     + " "
122                     + "end"
123                     + ") throws javax.jmi.reflect.JmiException ; ");
124         }
125     }
126     
127     
128     void addTemplate() {
129         // if associationEnd0 and associationEnd1 isChangeable
130
if (ends[0].isChangeable() && ends[1].isChangeable()) {
131                   outputln(
132                       "public boolean add("
133                           + classEnd0
134                           + " "
135                           + "end0"
136                           + ","
137                           + classEnd1
138                           + " "
139                           + "end1"
140                           + ") throws javax.jmi.reflect.JmiException ; ");
141               }
142     }
143     
144     void existsTemplate() {
145         outputln(
146             "public boolean exists("
147                 + classEnd0
148                 + " "
149                 + "end0"
150                 + ","
151                 + classEnd1
152                 + " "
153                 + "end1"
154                 + ") throws javax.jmi.reflect.JmiException ;");
155     }
156     
157     void removeTemplate() {
158         outputln(
159             "public boolean remove("
160                 + classEnd0
161                 + " "
162                 + "end0"
163                 + ","
164                 + classEnd1
165                 + " "
166                 + "end1"
167                 + ") throws javax.jmi.reflect.JmiException "
168                 //+", javax.jmi.reflect.NotFoundException"
169
+";" );
170     }
171         
172     
173     
174     
175
176     boolean isSingleValued(AssociationEnd end) {
177       return end.getMultiplicity().getUpper() == 1;
178     }
179
180 }
181
Popular Tags