1 17 package org.apache.ws.jaxme.generator.sg.impl; 18 19 import java.io.Serializable ; 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 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 ; 46 47 48 51 public class EnumerationSG extends SimpleTypeSGChainImpl { 52 private class EnumValue { 53 private final String value; 54 private final String name; 55 public EnumValue(String pValue, String pName) { 56 value = pValue; 57 name = pName; 58 } 59 public String getName() { return name; } 60 public String getValue() { return value; } 61 } 62 63 private final JavaQName qName; 64 private final EnumValue[] values; 65 66 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 enums = new ArrayList (); 75 for (int i = 0; i < enumerations.length; i++) { 76 XSEnumeration en = enumerations[i]; 77 String name = null; 78 String 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 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 sb = new StringBuffer (); 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 pValue, Object pData) { 144 return new TypedValueImpl(new Object []{qName, ".fromString(", pValue, ")"}, qName); 145 } 146 147 public TypedValue getCastFromString(SimpleTypeSG pController, String 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 []{qName, ".", values[i].getName()}, qName); 151 } 152 } 153 return super.getCastFromString(pController, pValue); 154 } 155 156 public TypedValue getCastToString(SimpleTypeSG pController, JavaMethod pMethod, Object pValue, DirectAccessible pData) { 157 return new TypedValueImpl(new Object []{pValue, ".toString()"}, String .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 .class); 168 169 JavaField name = js.newJavaField("name", String .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 .class, JavaSource.PRIVATE); 174 lexicalValue.setFinal(true); 175 176 List instanceParams = new ArrayList (); 177 for (int i = 0; i < values.length; i++) { 178 JavaField _f = js.newJavaField("_" + values[i].getName(), String .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 []{"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 .class, "pName"); 199 DirectAccessible pValue = con.addParam(super.getRuntimeType(pController), "pValue"); 200 DirectAccessible pLexicalValue = con.addParam(String .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 .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 .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 []{instances, "[", i, "].value"}, pValue)); 223 fromValueMethod.addLine("return ", instances, "[", i, "];"); 224 fromValueMethod.addEndIf(); 225 fromValueMethod.addEndFor(); 226 fromValueMethod.addThrowNew(IllegalArgumentException .class, JavaSource.getQuoted("Invalid value: "), 227 " + ", pValue); 228 229 JavaMethod fromNameMethod = js.newJavaMethod("fromName", qName, JavaSource.PUBLIC); 230 pName = fromNameMethod.addParam(String .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 .class, JavaSource.getQuoted("Invalid name: "), 238 " + ", pName); 239 240 JavaMethod fromStringMethod = js.newJavaMethod("fromString", qName, JavaSource.PUBLIC); 241 pValue = fromStringMethod.addParam(String .class, "pValue"); 242 fromStringMethod.setStatic(true); 243 fromStringMethod.addLine("return ", fromValueMethod, "(", 244 super.getCastFromString(pController, fromStringMethod, pValue, null), ");"); 245 246 if (js.isImplementing(Serializable .class)) { 247 JavaMethod readResolveMethod = js.newJavaMethod("readResolve", Object .class, JavaSource.PRIVATE); 248 readResolveMethod.addLine("return ", fromValueMethod, "(", value, ");"); 249 } 250 return js; 251 } 252 253 public void forAllNonNullValues(SimpleTypeSG pController, JavaMethod pMethod, Object 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 |