KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > helper > MofHelper


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.helper;
21
22 import javax.jmi.model.*;
23 import java.util.*;
24
25 public class MofHelper {
26     
27     public static MofPackage[] packagesOfPackage(MofPackage p){
28         List l = filterContentsByClass(p, MofPackage.class ,false);
29         return (MofPackage[]) l.toArray(new MofPackage[0]);
30     }
31     
32     public static MofClass[] classesOfPackage(MofPackage p) {
33         List l = filterContentsByClass(p, MofClass.class ,false);
34         return (MofClass[]) l.toArray(new MofClass[0]);
35     }
36     
37     public static Association[] associationsOfPackage(MofPackage p) {
38         List l = filterContentsByClass(p, Association.class ,false);
39         return (Association[]) l.toArray(new Association[0]);
40     }
41     
42     public static Import[] importsOfPackage(MofPackage p) {
43         List l = filterContentsByClass(p, Import.class ,false);
44         return (Import[]) l.toArray(new Import[0]);
45     }
46         
47     public static DataType[] datatypesOf(Namespace ns, boolean considerSupertypes) {
48         List l = filterContentsByClass(ns, DataType.class ,considerSupertypes);
49         return (DataType[]) l.toArray(new DataType[0]);
50     }
51     
52     public static AssociationEnd[] associationEndsOfAssociation(Association a) {
53         List l = filterContentsByClass(a, AssociationEnd.class ,false);
54         return (AssociationEnd[]) l.toArray(new AssociationEnd[0]);
55     }
56
57     
58     public static Constant[] constantsOfClass(MofClass c) {
59         List l = filterContentsByClass(c, Constant.class ,false);
60         return (Constant[]) l.toArray(new Constant[0]);
61     }
62
63     public static Reference[] referencesOfClass(MofClass c, boolean considerSupertypes) {
64         List l = filterContentsByClass(c, Reference.class , considerSupertypes);
65         return (Reference[]) l.toArray(new Reference[0]);
66     }
67     
68     public static Operation[] operationsOfClass(MofClass cl, ScopeKind sk, boolean considerSupertypes) {
69         List l = filterContentsByClass(cl, Operation.class ,considerSupertypes);
70         List l2 = new Vector();
71         Iterator it = l.iterator();
72         while(it.hasNext()) {
73           Operation op = (Operation)it.next();
74           if(op.getScope().equals(sk)) {
75              l2.add(op);
76           }
77         }
78         return (Operation[]) l2.toArray(new Operation[0]);
79     }
80         
81     public static Attribute[] attributesOfClass(
82             MofClass cl, ScopeKind sk, boolean considerSupertypes) {
83                 
84         List l = filterContentsByClass(cl, Attribute.class ,considerSupertypes);
85         List l2 = new Vector();
86         Iterator it = l.iterator();
87         while(it.hasNext()) {
88           Attribute att = (Attribute)it.next();
89           if(att.getScope().equals(sk)) {
90              l2.add(att);
91           }
92         }
93         return (Attribute[]) l2.toArray(new Attribute[0]);
94     }
95     
96     public static StructureField[] structureFieldsOf(StructureType t) {
97         List l = filterContentsByClass(t, StructureField.class ,false);
98         return (StructureField[]) l.toArray(new StructureField[0]);
99     }
100     
101     
102     public static Parameter[] parametersOf(Operation op) {
103         List l = filterContentsByClass(op, Parameter.class ,false);
104         return (Parameter[]) l.toArray(new Parameter[0]);
105     }
106     
107     public static MofException[] exceptionsOf(Operation op) {
108       List l = filterContentsByClass(op, MofException.class ,false);
109       return (MofException[]) l.toArray(new MofException[0]);
110     }
111     
112     
113     
114     public static List filterContentsByClass
115             (Namespace ns, Class JavaDoc c, boolean considerSupertypes) {
116       List r = new Vector();
117       if((ns instanceof GeneralizableElement) && considerSupertypes) {
118            Iterator it = ((GeneralizableElement)ns).getSupertypes().iterator();
119            while(it.hasNext()) {
120              Namespace sup = (Namespace) it.next();
121              Iterator it2 = filterContentsByClass(sup,c,true).iterator();
122              while(it2.hasNext()) {
123                 Object JavaDoc o = it2.next();
124                 if(!r.contains(o)) r.add(o);
125              }
126            }
127       }
128       Iterator it = ns.getContents().iterator();
129       while(it.hasNext()) {
130           Object JavaDoc o = it.next();
131           if(c.isInstance(o)) {
132              if(!r.contains(o)) r.add(o);
133           }
134       }
135       return r;
136     }
137     
138     
139     public static boolean isSinglePrimitive(TypedElement elem) {
140       if(!(elem.getType() instanceof PrimitiveType)) return false;
141       if(elem instanceof StructuralFeature) {
142         StructuralFeature f = (StructuralFeature)elem;
143         if(f.getMultiplicity().getLower()!=1) return false;
144         if(f.getMultiplicity().getUpper()!=1) return false;
145         return true;
146       }
147       if(elem instanceof Parameter) {
148         Parameter p = (Parameter)elem;
149         if(p.getMultiplicity().getLower()!=1) return false;
150         if(p.getMultiplicity().getUpper()!=1) return false;
151         return true;
152       }
153       return true; // for StructureField
154
}
155     
156     /*
157     public static boolean isNullablePrimitive(TypedElement elem) {
158         return (elem.getType() instanceof PrimitiveType)
159               && (elem instanceof StructuralFeature)
160               && ((StructuralFeature)elem).getMultiplicity().getLower()==0
161               && ((StructuralFeature)elem).getMultiplicity().getUpper()==1 ;
162     }
163     */

164
165 }
166
Popular Tags