KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
23
24 import org.objectweb.modfact.jmi.helper.*;
25
26
27 /**
28  * @author Xavier
29  */

30 public class EnumerationTypeImplementationGenerator
31             extends EnumerationTypeInterfaceGenerator {
32     
33
34     /**
35      * @see fr.lip6.modfact.generation.Generator#generate()
36      */

37     public void generate() {
38         
39         int d=0;
40         EnumerationType type = input[d];
41         
42         
43 // Package _package = MofHelper.outermostPackageOfModelElement(type);
44
// String packagePrefix = MofHelper.jmiPrefixForPackage(_package);
45
// if (packagePrefix.compareTo("") != 0) {
46
// out.println("package " + packagePrefix + ";");
47
// }
48

49         outputln(
50             "package " + JMIProvider.qualifierOf(type) +";" );
51         
52     //<<ANNOTATION TEMPLATE>>
53
outputln("public class "+type.getName()+"Enum implements " +type.getName()
54                 +" {");
55                 
56         //for each enumeration literal
57
String JavaDoc[] labels = (String JavaDoc[]) type.getLabels().toArray(new String JavaDoc[0]);
58
59         for (int i=0 ; i < labels.length; i++) {
60             outputln("public static final "+type.getName()+"Enum "
61                 +labels[i].toUpperCase()
62                 +" = new "+type.getName()+"Enum(\""+labels[i]+"\");");
63         }
64         
65         outputln("private static final "+ type.getName()+"[] allValues = new "+type.getName()+"[" +labels.length +"];");
66         outputln("private static final java.util.List typeName;");
67         
68         outputln("private final String literalName;");
69         
70         outputln("static {");
71         for (int i=0 ; i < labels.length; i++) {
72             outputln("allValues["+ i +"] = "
73                 +labels[i].toUpperCase()
74                 +";");
75         }
76         outputln("java.util.Vector temp = new java.util.Vector();");
77         //for each part of the fully qualified name
78
ModelElement elem = type;
79         while(true) {
80             if(elem==null) break;
81             outputln("temp.insertElementAt(\""+elem.getName()+"\",0);" );
82             elem = elem.getContainer();
83         }
84         outputln("typeName = java.util.Collections.unmodifiableList(temp);");
85         outputln("}");
86         
87         outputln("public "+type.getName()+"Enum(String literalName) {");
88         outputln("this.literalName = literalName;");
89         outputln("}");
90         
91         outputln("public String toString() {");
92         outputln("return literalName;");
93         outputln("}");
94         
95         outputln("public static " +type.getName() +" forName(String value) {" );
96         outputln("for(int i=0; i<allValues.length; i++) {");
97         outputln("if(allValues[i].toString().equals(value)) return allValues[i];" );
98         outputln("}");
99         outputln("\t" +"return null;" );
100         outputln("}");
101         
102         outputln("public java.util.List refTypeName() {");
103         outputln("return typeName;");
104         outputln("}");
105         
106         outputln("public boolean equals(Object o) {");
107         outputln("if (o==null) {");
108         outputln("return false;");
109         outputln("} else {");
110         output("return ((o instanceof javax.jmi.reflect.RefEnum) && ");
111         output("((javax.jmi.reflect.RefEnum) o).refTypeName().equals(typeName)");
112         outputln("&& ((javax.jmi.reflect.RefEnum) o).toString().equals(literalName));");
113         outputln("}");
114         outputln("}");
115         
116         outputln("}");
117         flushFile();
118     }
119
120 }
121
Popular Tags