KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > uml > transformation > rules > xml > AssociatedClassUtils


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Pierre Carpentier.
23 Contributor(s): Philippe Merle.
24
25 ---------------------------------------------------------------------
26 $Id: AssociatedClassUtils.java,v 1.1 2004/05/26 11:25:35 carpentier Exp $
27 ====================================================================*/

28
29 package org.objectweb.openccm.uml.transformation.rules.xml;
30
31 import ispuml.mdaTransformation.model.ModelContext;
32
33 /**
34  * Utility class to get the associated class of a class.
35  *
36  * @author Pierre Carpentier
37  */

38 public class AssociatedClassUtils {
39
40     /**
41      * Gets the association end of the association linked to the current bean
42      * with the search stereotyped.
43      * If the search stereotype is null or has an empty value, this method
44      * returns the associated classes with associations without stereotype.
45      * @param bean The source bean.
46      * @param associationStereotype The search stereotype of the association.
47      * @param classStereotype The stereotype of the class.
48      * @return The association end if there is one corresponding end,
49      * a list of association ends if there are some corresponding ends,
50      * or null if there is no association linked to the bean with this stereotype.
51      */

52     public Object JavaDoc getAssociatedClass(Object JavaDoc bean, String JavaDoc associationStereotype, String JavaDoc classStereotype, ModelContext context) {
53         org.omg.uml.core.ModelElement modelElement = (org.omg.uml.core.ModelElement) bean;
54         java.util.Iterator JavaDoc it = modelElement.getNamespace().getOwnedElement().iterator();
55         java.util.List JavaDoc list = new java.util.ArrayList JavaDoc();
56         while (it.hasNext()) {
57             Object JavaDoc obj = it.next();
58             if (obj instanceof org.omg.uml.core.Association) {
59                 org.omg.uml.core.Association asso = (org.omg.uml.core.Association) obj;
60                 boolean stereotypeOk = false;
61                 if (asso.getStereotype().size() == 0 && (associationStereotype == null || associationStereotype.length() == 0))
62                     stereotypeOk = true;
63                 else {
64                     java.util.Iterator JavaDoc itSt = asso.getStereotype().iterator();
65                     while (itSt.hasNext()) {
66                         org.omg.uml.core.Stereotype st = (org.omg.uml.core.Stereotype) itSt.next();
67                         if (st.getName().equals(associationStereotype))
68                             stereotypeOk = true;
69                     }
70                 }
71                 if (stereotypeOk) {
72                     java.util.List JavaDoc connections = asso.getConnection();
73                     if (connections.size() == 2) {
74                         org.omg.uml.core.AssociationEnd firstSide = (org.omg.uml.core.AssociationEnd) connections.get(0);
75                         org.omg.uml.core.AssociationEnd secondSide = (org.omg.uml.core.AssociationEnd) connections.get(1);
76                         if (firstSide.getParticipant().refMofId().equals(modelElement.refMofId())) {
77                             list.add(secondSide);
78                         }
79                         if (secondSide.getParticipant().refMofId().equals(modelElement.refMofId())) {
80                             list.add(firstSide);
81                         }
82                     }
83                 }
84             }
85         }
86         if (classStereotype != null && classStereotype.length() > 0) {
87             it = list.iterator();
88             while (it.hasNext()) {
89                 org.omg.uml.core.AssociationEnd assoEnd = (org.omg.uml.core.AssociationEnd)it.next();
90                 if (!context.isStereotyped(assoEnd.getParticipant(), classStereotype))
91                     it.remove();
92             }
93         }
94         if (list.size() == 1)
95             return list.get(0);
96         else
97             return list;
98     }
99     
100     /**
101      * Gets the associated class of the bean with the association
102      * which has a 'associationType' type.
103      * @param bean The source bean.
104      * @param associationType The type of the association.
105      * @return The association end if there is one corresponding end,
106      * a list of association ends if there are some corresponding ends,
107      * or null if there is no association linked to the bean with this stereotype.
108      */

109     public Object JavaDoc getAssociatedClass(Object JavaDoc bean, String JavaDoc associationType, ModelContext context) {
110         java.util.List JavaDoc list = new java.util.ArrayList JavaDoc();
111         String JavaDoc association = associationType;
112         javax.jmi.reflect.RefObject refObject = (javax.jmi.reflect.RefObject) bean;
113         javax.jmi.reflect.RefPackage pck = refObject.refOutermostPackage();
114         int index = association.indexOf(".");
115         while (index != -1) {
116             pck = pck.refPackage(association.substring(0, index));
117             association = association.substring(index+1);
118             index = association.indexOf(".");
119         }
120         javax.jmi.reflect.RefAssociation asso = pck.refAssociation(association);
121
122         java.util.Collection JavaDoc links = asso.refAllLinks();
123         java.util.Iterator JavaDoc linksIt = links.iterator();
124         while (linksIt.hasNext()) {
125             javax.jmi.reflect.RefAssociationLink link = (javax.jmi.reflect.RefAssociationLink)linksIt.next();
126             if (link.refFirstEnd().refMofId().equals(refObject.refMofId()))
127                 list.add(link.refSecondEnd());
128             else if (link.refSecondEnd().refMofId().equals(refObject.refMofId()))
129                 list.add(link.refFirstEnd());
130         }
131         if (list.size() == 1)
132             return list.get(0);
133         else
134             return list;
135     }
136
137 }
138
Popular Tags