KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 import org.apache.ws.jaxme.generator.sg.Context;
25 import org.apache.ws.jaxme.generator.sg.ListTypeSG;
26 import org.apache.ws.jaxme.generator.sg.SGFactory;
27 import org.apache.ws.jaxme.generator.sg.SGlet;
28 import org.apache.ws.jaxme.generator.sg.SchemaSG;
29 import org.apache.ws.jaxme.generator.sg.SimpleTypeSG;
30 import org.apache.ws.jaxme.generator.sg.TypeSG;
31 import org.apache.ws.jaxme.js.DirectAccessible;
32 import org.apache.ws.jaxme.js.JavaMethod;
33 import org.apache.ws.jaxme.js.JavaQName;
34 import org.apache.ws.jaxme.js.JavaQNameImpl;
35 import org.apache.ws.jaxme.js.JavaSource;
36 import org.apache.ws.jaxme.js.LocalJavaField;
37 import org.apache.ws.jaxme.js.TypedValue;
38 import org.apache.ws.jaxme.js.impl.TypedValueImpl;
39 import org.apache.ws.jaxme.xs.XSListType;
40 import org.apache.ws.jaxme.xs.XSType;
41 import org.apache.ws.jaxme.xs.xml.XsQName;
42 import org.xml.sax.SAXException JavaDoc;
43
44
45 /**
46  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
47  */

48 public class ListTypeSGImpl extends SimpleTypeSGImpl {
49   public static final JavaQName LIST_TYPE = JavaQNameImpl.getInstance(List JavaDoc.class);
50   public static final JavaQName ARRAYLIST_TYPE = JavaQNameImpl.getInstance(ArrayList JavaDoc.class);
51
52   private class InnerListTypeSG implements ListTypeSG {
53     private final Long JavaDoc length, minLength, maxLength;
54     public InnerListTypeSG(XSListType pListType) {
55       length = pListType.getLength();
56       maxLength = pListType.getMaxLength();
57       minLength = pListType.getMinLength();
58     }
59     public TypeSG getItemType() { return ListTypeSGImpl.this.getItemType(); }
60     public Long JavaDoc getLength() { return length; }
61     public Long JavaDoc getMaxLength() { return maxLength; }
62     public Long JavaDoc getMinLength() { return minLength; }
63   }
64
65   private final XSListType listType;
66   private final ListTypeSG listTypeSG;
67   private TypeSG itemType;
68   private final Context classContext;
69   private final XsQName name;
70   public boolean hasSetMethod(SimpleTypeSG pController) { return "indexed".equals(pController.getCollectionType()); }
71
72   public boolean isList(SimpleTypeSG pController) { return true; }
73
74   /** <p>Creates a new instance of ListTypeSG in the given {@link Context}.</p>
75    */

76   public ListTypeSGImpl(SGFactory pFactory, SchemaSG pSchemaSG, XSType pType,
77                          Context pClassContext, XsQName pName) throws SAXException JavaDoc {
78     super(pFactory, pSchemaSG, pType);
79     name = pName;
80     classContext = pClassContext;
81     listType = pType.getSimpleType().getListType();
82     listTypeSG = new InnerListTypeSG(listType);
83   }
84
85   public void init(SimpleTypeSG pController) throws SAXException JavaDoc {
86     itemType = getFactory().getTypeSG(listType.getItemType(), classContext, name);
87   }
88
89   protected TypeSG getItemType() {
90     return itemType;
91   }
92
93   public ListTypeSG getListType(SimpleTypeSG pController) {
94     return listTypeSG;
95   }
96
97   public JavaQName getRuntimeType(SimpleTypeSG pController) {
98     if ("indexed".equals(pController.getCollectionType())) {
99       return JavaQNameImpl.getArray(itemType.getSimpleTypeSG().getRuntimeType());
100     } else {
101       return LIST_TYPE;
102     }
103   }
104
105   public TypedValue getCastFromString(SimpleTypeSG pController, String JavaDoc pValue) throws SAXException JavaDoc {
106     List JavaDoc list = new ArrayList JavaDoc();
107     for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(pValue); st.hasMoreElements(); ) {
108       if (list.size() > 0) {
109         list.add(", ");
110       }
111       list.add(itemType.getSimpleTypeSG().getCastFromString(st.nextToken()));
112     }
113
114     Object JavaDoc result = new Object JavaDoc[]{"new ", itemType.getSimpleTypeSG().getRuntimeType(), "[]{", list, "}"};
115     String JavaDoc collectionType = pController.getCollectionType();
116     if ("indexed".equals(collectionType)) {
117       return new TypedValueImpl(result, itemType.getSimpleTypeSG().getRuntimeType());
118     } else {
119       JavaQName myListType = JavaQNameImpl.getInstance(collectionType, true);
120       return new TypedValueImpl(new Object JavaDoc[]{"(new ", myListType, "(", Arrays JavaDoc.class, ".asList(", result, ")))"}, myListType);
121     }
122   }
123
124   public TypedValue getCastFromString(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue, Object JavaDoc pData)
125       throws SAXException JavaDoc {
126     LocalJavaField list = pMethod.newJavaField(LIST_TYPE);
127     String JavaDoc collectionType = pController.getCollectionType();
128     list.addLine("new ", ("indexed".equals(collectionType) ?
129                           ARRAYLIST_TYPE : JavaQNameImpl.getInstance(collectionType, true)), "()");
130     DirectAccessible st = pMethod.addForEnumeration(StringTokenizer JavaDoc.class, new Object JavaDoc[]{"new ", StringTokenizer JavaDoc.class, "(", pValue, ")"});
131     pMethod.addLine(list, ".add(", itemType.getSimpleTypeSG().getCastFromString(pMethod, new Object JavaDoc[]{st, ".nextToken()"}, pData),
132                     ");");
133     pMethod.addEndFor();
134
135     if ("indexed".equals(collectionType)) {
136       JavaQName iType = itemType.getSimpleTypeSG().getRuntimeType();
137       return new TypedValueImpl(new Object JavaDoc[]{"((", iType, ") ", list, ".toArray(new ", iType, "[", list, ".size()]))"},
138                                 itemType.getSimpleTypeSG().getRuntimeType());
139     } else {
140         JavaQName myListType = JavaQNameImpl.getInstance(collectionType, true);
141         return new TypedValueImpl(list, myListType);
142     }
143   }
144
145   public TypedValue getCastToString(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue, DirectAccessible pData)
146       throws SAXException JavaDoc {
147     String JavaDoc collectionType = pController.getCollectionType();
148     DirectAccessible value;
149     if (pValue instanceof DirectAccessible) {
150       value = (DirectAccessible) pValue;
151     } else {
152       LocalJavaField v = pMethod.newJavaField(pController.getRuntimeType());
153       v.addLine(pValue);
154       value = v;
155     }
156
157     LocalJavaField sb = pMethod.newJavaField(StringBuffer JavaDoc.class);
158     sb.addLine("new ", StringBuffer JavaDoc.class, "()");
159     Object JavaDoc v;
160     DirectAccessible loopVar;
161     if ("indexed".equals(collectionType)) {
162       loopVar = pMethod.addForArray(value);
163       v = new Object JavaDoc[]{value, "[", loopVar, "]"};
164     } else {
165       loopVar = pMethod.addForList(value);
166       v = new Object JavaDoc[]{"(", itemType.getSimpleTypeSG().getRuntimeType(), ") ", value, ".get(", loopVar, ")"};
167     }
168     pMethod.addIf(loopVar, " > 0");
169     pMethod.addLine(sb, ".append(' ');");
170     pMethod.addEndIf();
171     pMethod.addLine(sb, ".append(", itemType.getSimpleTypeSG().getCastToString(pMethod, v, pData), ");");
172     pMethod.addEndFor();
173     return new TypedValueImpl(new Object JavaDoc[]{sb, ".toString()"}, String JavaDoc.class);
174   }
175
176   public void forAllNonNullValues(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue, SGlet pSGlet) throws SAXException JavaDoc {
177     LocalJavaField f = pMethod.newJavaField(LIST_TYPE);
178     f.addLine(pValue);
179     pMethod.addIf(f, " != null");
180     pSGlet.generate(pMethod, f);
181     pMethod.addEndIf();
182   }
183
184   public void forAllValues(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue, SGlet pSGlet) throws SAXException JavaDoc {
185     pSGlet.generate(pMethod, pValue);
186   }
187
188   public Object JavaDoc getEqualsCheck(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue1, Object JavaDoc pValue2) throws SAXException JavaDoc {
189     throw new IllegalStateException JavaDoc("Not implemented");
190   }
191
192   public Object JavaDoc getInitialValue(SimpleTypeSG pController, JavaSource p0) throws SAXException JavaDoc {
193     String JavaDoc s = pController.getCollectionType();
194     JavaQName listClass = "indexed".equals(s) ? ARRAYLIST_TYPE : JavaQNameImpl.getInstance(s, true);
195     return new Object JavaDoc[]{"new ", listClass, "()"};
196   }
197
198     public boolean isCausingParseConversionEvent(SimpleTypeSG pController) {
199         return itemType.getSimpleTypeSG().isCausingParseConversionEvent();
200     }
201 }
202
Popular Tags