KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > generator > sg > impl > EnumerationSG


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.generator.sg.impl;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.apache.ws.jaxme.generator.sg.SGlet;
25 import org.apache.ws.jaxme.generator.sg.SimpleTypeSG;
26 import org.apache.ws.jaxme.generator.sg.SimpleTypeSGChain;
27 import org.apache.ws.jaxme.js.DirectAccessible;
28 import org.apache.ws.jaxme.js.JavaConstructor;
29 import org.apache.ws.jaxme.js.JavaField;
30 import org.apache.ws.jaxme.js.JavaMethod;
31 import org.apache.ws.jaxme.js.JavaQName;
32 import org.apache.ws.jaxme.js.JavaQNameImpl;
33 import org.apache.ws.jaxme.js.JavaSource;
34 import org.apache.ws.jaxme.js.LocalJavaField;
35 import org.apache.ws.jaxme.js.TypedValue;
36 import org.apache.ws.jaxme.js.impl.TypedValueImpl;
37 import org.apache.ws.jaxme.xs.XSEnumeration;
38 import org.apache.ws.jaxme.xs.XSSimpleType;
39 import org.apache.ws.jaxme.xs.XSType;
40 import org.apache.ws.jaxme.xs.jaxb.JAXBEnumeration;
41 import org.apache.ws.jaxme.xs.jaxb.JAXBSimpleType;
42 import org.apache.ws.jaxme.xs.jaxb.JAXBTypesafeEnumClass;
43 import org.apache.ws.jaxme.xs.jaxb.JAXBTypesafeEnumMember;
44 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException;
45 import org.xml.sax.SAXException JavaDoc;
46
47
48 /**
49  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
50  */

51 public class EnumerationSG extends SimpleTypeSGChainImpl {
52   private class EnumValue {
53     private final String JavaDoc value;
54     private final String JavaDoc name;
55     public EnumValue(String JavaDoc pValue, String JavaDoc pName) {
56       value = pValue;
57       name = pName;
58     }
59     public String JavaDoc getName() { return name; }
60     public String JavaDoc getValue() { return value; }
61   }
62
63   private final JavaQName qName;
64   private final EnumValue[] values;
65
66   /** <p>Creates a new instance of EnumerationSG.java.</p>
67    */

68   protected EnumerationSG(SimpleTypeSGChain o, JavaQName pName, XSType pType) throws SAXException {
69     super(o);
70     qName = pName;
71
72     XSSimpleType simpleType = pType.getSimpleType();
73     XSEnumeration[] enumerations = simpleType.getEnumerations();
74     List JavaDoc enums = new ArrayList JavaDoc();
75     for (int i = 0; i < enumerations.length; i++) {
76       XSEnumeration en = enumerations[i];
77       String JavaDoc name = null;
78       String JavaDoc value = en.getValue();
79       if (en instanceof JAXBEnumeration) {
80         JAXBEnumeration jaxbEnumeration = (JAXBEnumeration) en;
81         JAXBTypesafeEnumMember member = jaxbEnumeration.getJAXBTypesafeEnumMember();
82         if (member != null) {
83           name = member.getName();
84         }
85       }
86
87       if (name == null) {
88         if (simpleType instanceof JAXBSimpleType) {
89           JAXBSimpleType jaxbSimpleType = (JAXBSimpleType) simpleType;
90           JAXBTypesafeEnumClass jaxbTypesafeEnumClass = jaxbSimpleType.getJAXBTypesafeEnumClass();
91           if (jaxbTypesafeEnumClass != null) {
92             for (Iterator JavaDoc iter = jaxbTypesafeEnumClass.getTypesafeEnumMember(); iter.hasNext(); ) {
93               JAXBTypesafeEnumMember member = (JAXBTypesafeEnumMember) iter.next();
94               if (value.equals(member.getValue())) {
95                 name = member.getName();
96                 break;
97               }
98             }
99           }
100         }
101
102         if (name == null) {
103           if ("".equals(value)) {
104             name = "EMPTY";
105           } else {
106             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
107             char c = value.charAt(0);
108             int offset;
109             if (Character.isJavaIdentifierStart(c)) {
110               sb.append(Character.toUpperCase(c));
111               offset = 1;
112             } else {
113               sb.append("V");
114               offset = 0;
115             }
116             for (int j = offset; j < value.length(); j++) {
117               c = value.charAt(j);
118               if (Character.isJavaIdentifierPart(c)) {
119                 sb.append(Character.toUpperCase(c));
120               } else {
121                 sb.append('_');
122               }
123             }
124             name = sb.toString();
125           }
126         }
127       }
128
129       for (int j = 0; j < enums.size(); j++) {
130         EnumValue ev = (EnumValue) enums.get(j);
131         if (name.equals(ev.getName())) {
132           throw new LocSAXException("An enumeration value named " + name + " already exists.", en.getLocator());
133         }
134         if (value.equals(ev.getValue())) {
135           throw new LocSAXException("An enumeration value " + value + " already exists.", en.getLocator());
136         }
137       }
138       enums.add(new EnumValue(value, name));
139     }
140     values = (EnumValue[]) enums.toArray(new EnumValue[enums.size()]);
141   }
142
143   public TypedValue getCastFromString(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue, Object JavaDoc pData) {
144     return new TypedValueImpl(new Object JavaDoc[]{qName, ".fromString(", pValue, ")"}, qName);
145   }
146
147   public TypedValue getCastFromString(SimpleTypeSG pController, String JavaDoc pValue) throws SAXException {
148     for (int i = 0; i < values.length; i++) {
149       if (values[i].getValue().equals(pValue)) {
150         return new TypedValueImpl(new Object JavaDoc[]{qName, ".", values[i].getName()}, qName);
151       }
152     }
153     return super.getCastFromString(pController, pValue);
154   }
155
156   public TypedValue getCastToString(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue, DirectAccessible pData) {
157     return new TypedValueImpl(new Object JavaDoc[]{pValue, ".toString()"}, String JavaDoc.class);
158   }
159
160   public JavaQName getRuntimeType(SimpleTypeSG pController) { return qName; }
161
162   public JavaSource getEnumClass(SimpleTypeSG pController) throws SAXException {
163     JavaQName valueType = super.getRuntimeType(pController);
164     JavaQName qNameArray = JavaQNameImpl.getArray(qName);
165
166     JavaSource js = pController.getSchema().getJavaSourceFactory().newJavaSource(qName, JavaSource.PUBLIC);
167     js.addImplements(Serializable JavaDoc.class);
168
169     JavaField name = js.newJavaField("name", String JavaDoc.class, JavaSource.PRIVATE);
170     name.setFinal(true);
171     JavaField value = js.newJavaField("value", valueType, JavaSource.PRIVATE);
172     value.setFinal(true);
173     JavaField lexicalValue = js.newJavaField("lexicalValue", String JavaDoc.class, JavaSource.PRIVATE);
174     lexicalValue.setFinal(true);
175
176     List JavaDoc instanceParams = new ArrayList JavaDoc();
177     for (int i = 0; i < values.length; i++) {
178       JavaField _f = js.newJavaField("_" + values[i].getName(), String JavaDoc.class, JavaSource.PUBLIC);
179       _f.setStatic(true);
180       _f.setFinal(true);
181       _f.addLine(JavaSource.getQuoted(values[i].getName()));
182
183       JavaField f = js.newJavaField(values[i].getName(), qName, JavaSource.PUBLIC);
184       f.addLine("new ", qName, "(", JavaSource.getQuoted(values[i].getName()), ", ",
185                 super.getCastFromString(pController, values[i].getValue()), ", ",
186                 JavaSource.getQuoted(values[i].getValue()), ");");
187       f.setStatic(true);
188       f.setFinal(true);
189       if (!instanceParams.isEmpty()) instanceParams.add(", ");
190       instanceParams.add(f);
191     }
192     JavaField instances = js.newJavaField("instances", qNameArray, JavaSource.PRIVATE);
193     instances.addLine(new Object JavaDoc[]{"new ", qNameArray, "{", instanceParams, "}"});
194     instances.setStatic(true);
195     instances.setFinal(true);
196
197     JavaConstructor con = js.newJavaConstructor(JavaSource.PRIVATE);
198     DirectAccessible pName = con.addParam(String JavaDoc.class, "pName");
199     DirectAccessible pValue = con.addParam(super.getRuntimeType(pController), "pValue");
200     DirectAccessible pLexicalValue = con.addParam(String JavaDoc.class, "pLexicalValue");
201     con.addLine(name, " = ", pName, ";");
202     con.addLine(value, " = ", pValue, ";");
203     con.addLine(lexicalValue, " = ", pLexicalValue, ";");
204
205     JavaMethod toStringMethod = js.newJavaMethod("toString", String JavaDoc.class, JavaSource.PUBLIC);
206     toStringMethod.addLine("return ", lexicalValue, ";");
207
208     JavaMethod getValueMethod = js.newJavaMethod("getValue", valueType, JavaSource.PUBLIC);
209     getValueMethod.addLine("return ", value, ";");
210
211     JavaMethod getNameMethod = js.newJavaMethod("getName", String JavaDoc.class, JavaSource.PUBLIC);
212     getNameMethod.addLine("return ", name, ";");
213
214     JavaMethod getInstancesMethod = js.newJavaMethod("getInstances", qNameArray, JavaSource.PUBLIC);
215     getInstancesMethod.setStatic(true);
216     getInstancesMethod.addLine("return ", instances, ";");
217
218     JavaMethod fromValueMethod = js.newJavaMethod("fromValue", qName, JavaSource.PUBLIC);
219     pValue = fromValueMethod.addParam(valueType, "pValue");
220     fromValueMethod.setStatic(true);
221     DirectAccessible i = fromValueMethod.addForArray(instances);
222     fromValueMethod.addIf(pController.getEqualsCheck(fromValueMethod, new Object JavaDoc[]{instances, "[", i, "].value"}, pValue));
223     fromValueMethod.addLine("return ", instances, "[", i, "];");
224     fromValueMethod.addEndIf();
225     fromValueMethod.addEndFor();
226     fromValueMethod.addThrowNew(IllegalArgumentException JavaDoc.class, JavaSource.getQuoted("Invalid value: "),
227                                 " + ", pValue);
228
229     JavaMethod fromNameMethod = js.newJavaMethod("fromName", qName, JavaSource.PUBLIC);
230     pName = fromNameMethod.addParam(String JavaDoc.class, "pName");
231     fromNameMethod.setStatic(true);
232     i = fromNameMethod.addForArray(instances);
233     fromNameMethod.addIf(instances, "[", i, "].name.equals(", pName, ")");
234     fromNameMethod.addLine("return ", instances, "[", i, "];");
235     fromNameMethod.addEndIf();
236     fromNameMethod.addEndFor();
237     fromNameMethod.addThrowNew(IllegalArgumentException JavaDoc.class, JavaSource.getQuoted("Invalid name: "),
238                                 " + ", pName);
239
240     JavaMethod fromStringMethod = js.newJavaMethod("fromString", qName, JavaSource.PUBLIC);
241     pValue = fromStringMethod.addParam(String JavaDoc.class, "pValue");
242     fromStringMethod.setStatic(true);
243     fromStringMethod.addLine("return ", fromValueMethod, "(",
244                              super.getCastFromString(pController, fromStringMethod, pValue, null), ");");
245
246     if (js.isImplementing(Serializable JavaDoc.class)) {
247       JavaMethod readResolveMethod = js.newJavaMethod("readResolve", Object JavaDoc.class, JavaSource.PRIVATE);
248       readResolveMethod.addLine("return ", fromValueMethod, "(", value, ");");
249     }
250     return js;
251   }
252
253   public void forAllNonNullValues(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue, SGlet pSGlet) throws SAXException {
254     LocalJavaField f = pMethod.newJavaField(qName);
255     f.addLine(pValue);
256     pMethod.addIf(f, " != null");
257     pSGlet.generate(pMethod, f);
258     pMethod.addEndIf();
259   }
260
261   public void generate(SimpleTypeSG pController) throws SAXException {
262     super.generate(pController);
263     getEnumClass(pController);
264   }
265
266   public void generate(SimpleTypeSG pController, JavaSource pSource) throws SAXException {
267     super.generate(pController, pSource);
268     getEnumClass(pController);
269   }
270
271     public boolean isCausingParseConversionEvent(SimpleTypeSG pController) {
272         return true;
273     }
274 }
275
Popular Tags