KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > generator > types > UnionTypeSGImpl


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.types;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
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 JavaDoc;
41
42 /**
43  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
44  */

45 public class UnionTypeSGImpl extends SimpleTypeSGImpl {
46   public static final JavaQName OBJECT_TYPE = JavaQNameImpl.getInstance(Object JavaDoc.class);
47   private final XSUnionType unionType;
48   private final TypeSG[] memberTypes;
49   private final UnionTypeSG unionTypeSG;
50
51   /** <p>Creates a new instance of UnionTypeSG.</p>
52    */

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 JavaDoc members = new ArrayList JavaDoc();
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 JavaDoc pValue) throws SAXException {
76     Exception JavaDoc e = null;
77     for (int i = 0; i < memberTypes.length; i++) {
78       try {
79         return memberTypes[i].getSimpleTypeSG().getCastFromString(pValue);
80       } catch (Exception JavaDoc 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 JavaDoc pValue,
90                                       Object JavaDoc pData) throws SAXException {
91     DirectAccessible value;
92     if (pValue instanceof DirectAccessible) {
93       value = (DirectAccessible) pValue;
94     } else {
95       LocalJavaField v = pMethod.newJavaField(String JavaDoc.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 JavaDoc.class);
105     }
106     pMethod.addThrowNew(IllegalArgumentException JavaDoc.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 JavaDoc 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 JavaDoc.class);
121       v.addLine(pValue);
122       value = v;
123     }
124
125     LocalJavaField result = pMethod.newJavaField(String JavaDoc.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 JavaDoc v = OBJECT_TYPE.equals(memberRuntimeType) ?
130         (Object JavaDoc) value :
131         new Object JavaDoc[]{"((", memberRuntimeType, ") ", value, ")"};
132       pMethod.addLine(result, " = ", memberTypes[i].getSimpleTypeSG().getCastToString(pMethod, v, pData), ";");
133     }
134     pMethod.addElse();
135     pMethod.addThrowNew(IllegalStateException JavaDoc.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 JavaDoc 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 JavaDoc pValue, SGlet pSGlet) throws SAXException {
153     pSGlet.generate(pMethod, pValue);
154   }
155
156   public Object JavaDoc getEqualsCheck(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue1, Object JavaDoc pValue2) throws SAXException {
157     throw new IllegalStateException JavaDoc("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