1 17 package org.apache.ws.jaxme.generator.types; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import org.apache.ws.jaxme.generator.sg.Context; 23 import org.apache.ws.jaxme.generator.sg.SGFactory; 24 import org.apache.ws.jaxme.generator.sg.SGlet; 25 import org.apache.ws.jaxme.generator.sg.SchemaSG; 26 import org.apache.ws.jaxme.generator.sg.SimpleTypeSG; 27 import org.apache.ws.jaxme.generator.sg.TypeSG; 28 import org.apache.ws.jaxme.generator.sg.UnionTypeSG; 29 import org.apache.ws.jaxme.js.DirectAccessible; 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.xs.XSType; 37 import org.apache.ws.jaxme.xs.XSUnionType; 38 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException; 39 import org.apache.ws.jaxme.xs.xml.XsQName; 40 import org.xml.sax.SAXException ; 41 42 45 public class UnionTypeSGImpl extends SimpleTypeSGImpl { 46 public static final JavaQName OBJECT_TYPE = JavaQNameImpl.getInstance(Object .class); 47 private final XSUnionType unionType; 48 private final TypeSG[] memberTypes; 49 private final UnionTypeSG unionTypeSG; 50 51 53 public UnionTypeSGImpl(SGFactory pFactory, SchemaSG pSchema, XSType pType, 54 Context pClassContext, XsQName pName) throws SAXException { 55 super(pFactory, pSchema, pType); 56 unionType = pType.getSimpleType().getUnionType(); 57 XSType[] mTypes = unionType.getMemberTypes(); 58 List members = new ArrayList (); 59 for (int i = 0; i < mTypes.length; i++) { 60 TypeSG typeSG = getFactory().getTypeSG(mTypes[i], pClassContext, pName); 61 typeSG.getSimpleTypeSG().setNullable(true); 62 members.add(typeSG); 63 } 64 memberTypes = (TypeSG[]) members.toArray(new TypeSG[members.size()]); 65 66 unionTypeSG = new UnionTypeSG(){ 67 public TypeSG[] getMemberTypes() { return UnionTypeSGImpl.this.getMemberTypes(); } 68 }; 69 } 70 71 protected TypeSG[] getMemberTypes() { return memberTypes; } 72 public JavaQName getRuntimeType(SimpleTypeSG pController) { return OBJECT_TYPE; } 73 public boolean isUnion(SimpleTypeSG pController) { return true; } 74 75 public TypedValue getCastFromString(SimpleTypeSG pController, String pValue) throws SAXException { 76 Exception e = null; 77 for (int i = 0; i < memberTypes.length; i++) { 78 try { 79 return memberTypes[i].getSimpleTypeSG().getCastFromString(pValue); 80 } catch (Exception ex) { 81 if (i == 0) { 82 e = ex; 83 } 84 } 85 } 86 throw new LocSAXException("Failed to convert value " + pValue, getLocator(), e); 87 } 88 89 public TypedValue getCastFromString(SimpleTypeSG pController, JavaMethod pMethod, Object pValue, 90 Object pData) throws SAXException { 91 DirectAccessible value; 92 if (pValue instanceof DirectAccessible) { 93 value = (DirectAccessible) pValue; 94 } else { 95 LocalJavaField v = pMethod.newJavaField(String .class); 96 v.addLine(pValue); 97 value = v; 98 } 99 100 LocalJavaField result = pMethod.newJavaField(pController.getRuntimeType()); 101 for (int i = 0; i < memberTypes.length; i++) { 102 pMethod.addTry(); 103 pMethod.addLine(result, " = ", memberTypes[i].getSimpleTypeSG().getCastFromString(pMethod, value, pData), ";"); 104 pMethod.addCatch(RuntimeException .class); 105 } 106 pMethod.addThrowNew(IllegalArgumentException .class, 107 JavaSource.getQuoted("Invalid union value: "), " + ", pValue); 108 for (int i = 0; i < memberTypes.length; i++) { 109 pMethod.addEndTry(); 110 } 111 return result; 112 } 113 114 public TypedValue getCastToString(SimpleTypeSG pController, JavaMethod pMethod, Object pValue, 115 DirectAccessible pData) throws SAXException { 116 DirectAccessible value; 117 if (pValue instanceof DirectAccessible) { 118 value = (DirectAccessible) pValue; 119 } else { 120 LocalJavaField v = pMethod.newJavaField(Object .class); 121 v.addLine(pValue); 122 value = v; 123 } 124 125 LocalJavaField result = pMethod.newJavaField(String .class); 126 for (int i = 0; i < memberTypes.length; i++) { 127 JavaQName memberRuntimeType = memberTypes[i].getSimpleTypeSG().getRuntimeType(); 128 pMethod.addIf(i == 0, value, " instanceof ", memberRuntimeType); 129 Object v = OBJECT_TYPE.equals(memberRuntimeType) ? 130 (Object ) value : 131 new Object []{"((", memberRuntimeType, ") ", value, ")"}; 132 pMethod.addLine(result, " = ", memberTypes[i].getSimpleTypeSG().getCastToString(pMethod, v, pData), ";"); 133 } 134 pMethod.addElse(); 135 pMethod.addThrowNew(IllegalStateException .class, 136 JavaSource.getQuoted("Invalid union object type: "), " + ", value, ".getClass().getName()"); 137 pMethod.addEndIf(); 138 139 return result; 140 } 141 142 public UnionTypeSG getUnionType(SimpleTypeSG pController) { return unionTypeSG; } 143 144 public void forAllNonNullValues(SimpleTypeSG pController, JavaMethod pMethod, Object pValue, SGlet pSGlet) throws SAXException { 145 LocalJavaField f = pMethod.newJavaField(OBJECT_TYPE); 146 f.addLine(pValue); 147 pMethod.addIf(f, " != null"); 148 pSGlet.generate(pMethod, pValue); 149 pMethod.addEndIf(); 150 } 151 152 public void forAllValues(SimpleTypeSG pController, JavaMethod pMethod, Object pValue, SGlet pSGlet) throws SAXException { 153 pSGlet.generate(pMethod, pValue); 154 } 155 156 public Object getEqualsCheck(SimpleTypeSG pController, JavaMethod pMethod, Object pValue1, Object pValue2) throws SAXException { 157 throw new IllegalStateException ("Not implemented"); 158 } 159 160 public boolean isCausingParseConversionEvent(SimpleTypeSG pController) { 161 for (int i = 0; i < memberTypes.length; i++) { 162 if (!memberTypes[i].getSimpleTypeSG().isCausingParseConversionEvent()) { 163 return false; 164 } 165 } 166 return true; 167 } 168 } 169 | Popular Tags |