1 package org.apache.ws.jaxme.generator.sg.impl; 2 3 import org.apache.ws.jaxme.generator.sg.ObjectSG; 4 import org.apache.ws.jaxme.generator.sg.PropertySG; 5 import org.apache.ws.jaxme.generator.sg.PropertySGChain; 6 import org.apache.ws.jaxme.generator.sg.SGlet; 7 import org.apache.ws.jaxme.generator.sg.TypeSG; 8 import org.apache.ws.jaxme.js.DirectAccessible; 9 import org.apache.ws.jaxme.js.JavaMethod; 10 import org.apache.ws.jaxme.js.JavaQName; 11 import org.apache.ws.jaxme.js.JavaQNameImpl; 12 import org.apache.ws.jaxme.js.JavaSource; 13 import org.apache.ws.jaxme.js.LocalJavaField; 14 import org.apache.ws.jaxme.js.Parameter; 15 import org.apache.ws.jaxme.js.TypedValue; 16 import org.xml.sax.SAXException ; 17 18 19 22 public class ArrayPropertySG extends MultiplePropertySG { 23 protected ArrayPropertySG(PropertySGChain pBase, ObjectSG pObjectSG, int pMinOccurs, int pMaxOccurs) throws SAXException { 24 super(pBase, pObjectSG, pMinOccurs, pMaxOccurs); 25 } 26 27 private JavaQName getArrayClass() throws SAXException { 28 return JavaQNameImpl.getArray(getInstanceClass()); 29 } 30 31 public JavaMethod getXMLGetMethod(PropertySG pController, JavaSource pSource) throws SAXException { 32 JavaQName arrayClass = getArrayClass(); 33 String fieldName = pController.getXMLFieldName(); 34 String methodName = pController.getXMLGetMethodName(); 35 JavaMethod result = pSource.newJavaMethod(methodName, arrayClass, JavaSource.PUBLIC); 36 if (!pSource.isInterface()) { 37 Object o = new Object []{"new ", arrayClass.getInstanceClass(), 38 "[", fieldName, ".size()]"}; 39 if (isAutoBoxing()) { 40 LocalJavaField res = result.newJavaField(getArrayClass()); 41 res.addLine(o); 42 DirectAccessible i = result.addForList(int.class, fieldName); 43 result.addLine(res, "[", i, "] = ", asPrimitive(i, fieldName), ";"); 44 result.addEndFor(); 45 result.addLine("return ", res, ";"); 46 } else { 47 result.addLine("return (", arrayClass, ") ", fieldName, 48 ".toArray(", o, ");"); 49 } 50 } 51 return result; 52 } 53 54 protected JavaMethod getXMLGetElementMethod(PropertySG pController, JavaSource pSource) throws SAXException { 55 JavaQName instanceClass = getInstanceClass(); 56 String fieldName = pController.getXMLFieldName(); 57 String methodName = pController.getXMLGetMethodName(); 58 JavaMethod result = pSource.newJavaMethod(methodName, instanceClass, JavaSource.PUBLIC); 59 Parameter index = result.addParam(int.class, "pIndex"); 60 if (!pSource.isInterface()) { 61 result.addLine("return ", asPrimitive(index, fieldName), ";"); 62 } 63 return result; 64 } 65 66 protected JavaMethod getXMLGetLengthMethod(PropertySG pController, JavaSource pSource) throws SAXException { 67 String fieldName = pController.getXMLFieldName(); 68 String methodName = pController.getXMLGetMethodName() + "Length"; 69 JavaMethod result = pSource.newJavaMethod(methodName, int.class, JavaSource.PUBLIC); 70 if (!pSource.isInterface()) { 71 result.addLine("return ", fieldName, ".size();"); 72 } 73 return result; 74 } 75 76 private Object asPrimitive(DirectAccessible pIndex, String pFieldName) throws SAXException { 77 return asPrimitive(new Object []{"((", getObjectClass(), ") ", 78 pFieldName, ".get(", pIndex, "))"}); 79 } 80 81 private Object asPrimitive(Object pObject) throws SAXException { 82 if (isAutoBoxing()) { 83 return new Object []{pObject, ".", getInstanceClass().getPrimitiveConversionMethod(), "()"}; 84 } else { 85 return pObject; 86 } 87 } 88 89 public JavaMethod getXMLSetMethod(PropertySG pController, JavaSource pSource) throws SAXException { 90 JavaQName arrayClass = getArrayClass(); 91 String fieldName = pController.getXMLFieldName(); 92 String methodName = pController.getXMLSetMethodName(); 93 JavaMethod result = pSource.newJavaMethod(methodName, "void", JavaSource.PUBLIC); 94 Parameter array = result.addParam(arrayClass, "pValue"); 95 if (!pSource.isInterface()) { 96 result.addLine(fieldName, ".clear();"); 97 result.addIf(array, " != null"); 98 DirectAccessible i = result.addForArray(int.class, array); 99 Object o = new Object []{array, "[", i, "]"}; 100 result.addLine(fieldName, ".add(", asObject(o), ");"); 101 result.addEndFor(); 102 result.addEndIf(); 103 } 104 return result; 105 } 106 107 protected JavaMethod getXMLSetElementMethod(PropertySG pController, JavaSource pSource) throws SAXException { 108 JavaQName instanceClass = getInstanceClass(); 109 String fieldName = pController.getXMLFieldName(); 110 String methodName = pController.getXMLSetMethodName(); 111 JavaMethod result = pSource.newJavaMethod(methodName, "void", JavaSource.PUBLIC); 112 Parameter index = result.addParam(int.class, "pIndex"); 113 Parameter element = result.addParam(instanceClass, "pValue"); 114 if (!pSource.isInterface()) { 115 result.addLine(fieldName, ".set(", index, ", ", asObject(element), ");"); 116 } 117 return result; 118 } 119 120 public void forAllValues(PropertySG pController, JavaMethod pMethod, DirectAccessible pElement, SGlet pSGlet) throws SAXException { 121 LocalJavaField array = pMethod.newJavaField(getArrayClass()); 122 array.addLine(pController.getValue(pElement)); 123 DirectAccessible i = pMethod.addForArray(array); 124 Object v; 125 boolean isCasting = !OBJECT_TYPE.equals(getInstanceClass()); 126 if (isCasting && pSGlet instanceof SGlet.TypedSGlet) { 127 isCasting = ((SGlet.TypedSGlet) pSGlet).isCasting(); 128 } 129 if (isCasting) { 130 v = new Object []{"(", getInstanceClass(), ")", array, "[", i, "]"}; 131 } else { 132 v = new Object []{array, "[", i, "]"}; 133 } 134 TypeSG tSG = getObjectSG().getTypeSG(); 135 if (tSG.isComplex()) { 136 pSGlet.generate(pMethod, v); 137 } else { 138 tSG.getSimpleTypeSG().forAllValues(pMethod, v, pSGlet); 139 } 140 pMethod.addEndFor(); 141 } 142 143 public void forAllNonNullValues(PropertySG pController, JavaMethod pMethod, DirectAccessible pElement, SGlet pSGlet) throws SAXException { 144 LocalJavaField array = pMethod.newJavaField(getArrayClass()); 145 array.addLine(pController.getValue(pElement)); 146 DirectAccessible i = pMethod.addForArray(array); 147 TypeSG typeSG = getObjectSG().getTypeSG(); 148 Object v; 149 boolean isCasting = !OBJECT_TYPE.equals(typeSG.getRuntimeType()); 150 JavaQName qName = typeSG.getRuntimeType(); 151 if (isCasting && pSGlet instanceof SGlet.TypedSGlet) { 152 SGlet.TypedSGlet typedSGlet = (SGlet.TypedSGlet) pSGlet; 153 isCasting = typedSGlet.isCasting(); 154 if (typedSGlet.getType() != null) { 155 qName = typedSGlet.getType(); 156 } 157 } 158 if (isCasting) { 159 v = new Object []{"(", qName, ")", array, "[", i, "]"}; 160 } else { 161 v = new Object []{array, "[", i, "]"}; 162 } 163 if (typeSG.isComplex()) { 164 pSGlet.generate(pMethod, v); 165 } else { 166 typeSG.getSimpleTypeSG().forAllNonNullValues(pMethod, v, pSGlet); 167 } 168 pMethod.addEndFor(); 169 } 170 171 public void generate(PropertySG pController, JavaSource pSource) throws SAXException { 172 super.generate(pController, pSource); 173 getXMLSetElementMethod(pController, pSource); 174 getXMLGetElementMethod(pController, pSource); 175 getXMLGetLengthMethod(pController, pSource); 176 } 177 178 public void setValue(PropertySG pController, JavaMethod pMethod, DirectAccessible pElement, Object pValue, JavaQName pType) 179 throws SAXException { 180 pMethod.addLine(pElement, ".", pController.getXMLSetMethodName(), "(", pValue, ");"); 181 } 182 183 public void addValue(PropertySG pController, JavaMethod pMethod, DirectAccessible pElement, TypedValue pValue, JavaQName pType) throws SAXException { 184 JavaQName arrayClass = getArrayClass(); 185 LocalJavaField f = pMethod.newJavaField(arrayClass); 186 f.addLine(pController.getValue(pElement)); 187 pMethod.addIf(f, " == null || ", f, ".length == 0"); 188 pMethod.addLine(f, " = new ", arrayClass, "{", pValue, "};"); 189 pController.setValue(pMethod, pElement, f, pType); 190 pMethod.addElse(); 191 LocalJavaField g = pMethod.newJavaField(arrayClass); 192 g.addLine("new ", getInstanceClass(), "[", f, ".length + 1]"); 193 pMethod.addLine(System .class, ".arraycopy(", f, ", 0, ", g, ", 0, ", f, ".length);"); 194 pMethod.addLine(g, "[", f, ".length] = ", pValue, ";"); 195 pController.setValue(pMethod, pElement, g, pType); 196 pMethod.addEndIf(); 197 } 198 } 199 | Popular Tags |