1 17 package org.apache.ws.jaxme.generator.types; 18 19 import java.util.ArrayList ; 20 import java.util.Arrays ; 21 import java.util.List ; 22 import java.util.StringTokenizer ; 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 ; 43 44 45 48 public class ListTypeSGImpl extends SimpleTypeSGImpl { 49 public static final JavaQName LIST_TYPE = JavaQNameImpl.getInstance(List .class); 50 public static final JavaQName ARRAYLIST_TYPE = JavaQNameImpl.getInstance(ArrayList .class); 51 52 private class InnerListTypeSG implements ListTypeSG { 53 private final Long 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 getLength() { return length; } 61 public Long getMaxLength() { return maxLength; } 62 public Long 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 76 public ListTypeSGImpl(SGFactory pFactory, SchemaSG pSchemaSG, XSType pType, 77 Context pClassContext, XsQName pName) throws SAXException { 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 { 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 pValue) throws SAXException { 106 List list = new ArrayList (); 107 for (StringTokenizer st = new StringTokenizer (pValue); st.hasMoreElements(); ) { 108 if (list.size() > 0) { 109 list.add(", "); 110 } 111 list.add(itemType.getSimpleTypeSG().getCastFromString(st.nextToken())); 112 } 113 114 Object result = new Object []{"new ", itemType.getSimpleTypeSG().getRuntimeType(), "[]{", list, "}"}; 115 String 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 []{"(new ", myListType, "(", Arrays .class, ".asList(", result, ")))"}, myListType); 121 } 122 } 123 124 public TypedValue getCastFromString(SimpleTypeSG pController, JavaMethod pMethod, Object pValue, Object pData) 125 throws SAXException { 126 LocalJavaField list = pMethod.newJavaField(LIST_TYPE); 127 String collectionType = pController.getCollectionType(); 128 list.addLine("new ", ("indexed".equals(collectionType) ? 129 ARRAYLIST_TYPE : JavaQNameImpl.getInstance(collectionType, true)), "()"); 130 DirectAccessible st = pMethod.addForEnumeration(StringTokenizer .class, new Object []{"new ", StringTokenizer .class, "(", pValue, ")"}); 131 pMethod.addLine(list, ".add(", itemType.getSimpleTypeSG().getCastFromString(pMethod, new Object []{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 []{"((", 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 pValue, DirectAccessible pData) 146 throws SAXException { 147 String 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 .class); 158 sb.addLine("new ", StringBuffer .class, "()"); 159 Object v; 160 DirectAccessible loopVar; 161 if ("indexed".equals(collectionType)) { 162 loopVar = pMethod.addForArray(value); 163 v = new Object []{value, "[", loopVar, "]"}; 164 } else { 165 loopVar = pMethod.addForList(value); 166 v = new Object []{"(", 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 []{sb, ".toString()"}, String .class); 174 } 175 176 public void forAllNonNullValues(SimpleTypeSG pController, JavaMethod pMethod, Object pValue, SGlet pSGlet) throws SAXException { 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 pValue, SGlet pSGlet) throws SAXException { 185 pSGlet.generate(pMethod, pValue); 186 } 187 188 public Object getEqualsCheck(SimpleTypeSG pController, JavaMethod pMethod, Object pValue1, Object pValue2) throws SAXException { 189 throw new IllegalStateException ("Not implemented"); 190 } 191 192 public Object getInitialValue(SimpleTypeSG pController, JavaSource p0) throws SAXException { 193 String s = pController.getCollectionType(); 194 JavaQName listClass = "indexed".equals(s) ? ARRAYLIST_TYPE : JavaQNameImpl.getInstance(s, true); 195 return new Object []{"new ", listClass, "()"}; 196 } 197 198 public boolean isCausingParseConversionEvent(SimpleTypeSG pController) { 199 return itemType.getSimpleTypeSG().isCausingParseConversionEvent(); 200 } 201 } 202 | Popular Tags |