1 17 package org.apache.ws.jaxme.generator.sg.impl; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import org.apache.ws.jaxme.generator.sg.ObjectSG; 23 import org.apache.ws.jaxme.generator.sg.PropertySG; 24 import org.apache.ws.jaxme.generator.sg.PropertySGChain; 25 import org.apache.ws.jaxme.generator.sg.SGlet; 26 import org.apache.ws.jaxme.generator.sg.TypeSG; 27 import org.apache.ws.jaxme.js.DirectAccessible; 28 import org.apache.ws.jaxme.js.JavaField; 29 import org.apache.ws.jaxme.js.JavaMethod; 30 import org.apache.ws.jaxme.js.JavaQName; 31 import org.apache.ws.jaxme.js.JavaQNameImpl; 32 import org.apache.ws.jaxme.js.JavaSource; 33 import org.apache.ws.jaxme.js.LocalJavaField; 34 import org.apache.ws.jaxme.js.TypedValue; 35 import org.xml.sax.SAXException ; 36 37 38 42 public class MultiplePropertySG extends PropertySGChainImpl { 43 protected static final JavaQName OBJECT_TYPE = JavaQNameImpl.getInstance(Object .class); 44 private final int minOccurs, maxOccurs; 45 private final ObjectSG objectSG; 46 47 49 protected MultiplePropertySG(PropertySGChain pBase, ObjectSG pObjectSG, int pMinOccurs, int pMaxOccurs) { 50 super(pBase); 51 objectSG = pObjectSG; 52 maxOccurs = pMaxOccurs; 53 minOccurs = pMinOccurs; 54 } 55 56 protected int getMinOccurs() { return minOccurs; } 57 protected int getMaxOccurs() { return maxOccurs; } 58 protected ObjectSG getObjectSG() { return objectSG; } 59 protected JavaQName getInstanceClass() throws SAXException { 60 return getObjectSG().getTypeSG().getRuntimeType(); 61 } 62 protected boolean isAutoBoxing() throws SAXException { 63 return getInstanceClass().isPrimitive(); 64 } 65 protected JavaQName getObjectClass() throws SAXException { 66 return getInstanceClass().getObjectType(); 67 } 68 protected Object asObject(Object pObject) throws SAXException { 69 if (isAutoBoxing()) { 70 return new Object []{"new ", getObjectClass(), "(", pObject, ")"}; 71 } else { 72 return pObject; 73 } 74 } 75 76 public JavaField getXMLField(PropertySG pController, JavaSource pSource) throws SAXException { 77 String fieldName = pController.getXMLFieldName(); 78 JavaField result = pSource.newJavaField(fieldName, List .class, JavaSource.PRIVATE); 79 result.addLine("new ", ArrayList .class, "()"); 80 return result; 81 } 82 83 public JavaMethod getXMLSetMethod(PropertySG pController, JavaSource pSource) throws SAXException { 84 return null; 85 } 86 87 public JavaMethod getXMLGetMethod(PropertySG pController, JavaSource pSource) throws SAXException { 88 String fieldName = pController.getXMLFieldName(); 89 String methodName = pController.getXMLGetMethodName(); 90 JavaMethod result = pSource.newJavaMethod(methodName, List .class, JavaSource.PUBLIC); 91 result.addLine("return ", fieldName, ";"); 92 return result; 93 } 94 95 public void forAllValues(PropertySG pController, JavaMethod pMethod, DirectAccessible pElement, SGlet pSGlet) throws SAXException { 96 LocalJavaField list = pMethod.newJavaField(List .class); 97 list.addLine(pController.getValue(pElement)); 98 DirectAccessible i = pMethod.addForList(list); 99 TypeSG typeSG = objectSG.getTypeSG(); 100 Object v; 101 boolean isCasting = !OBJECT_TYPE.equals(typeSG.getRuntimeType()); 102 if (isCasting && pSGlet instanceof SGlet.TypedSGlet) { 103 isCasting = ((SGlet.TypedSGlet) pSGlet).isCasting(); 104 } 105 if (isCasting) { 106 v = new Object []{"(", typeSG.getRuntimeType(), ")", list, ".get(", i, ")"}; 107 } else { 108 v = new Object []{list, ".get(", i, ")"}; 109 } 110 if (typeSG.isComplex()) { 111 pSGlet.generate(pMethod, v); 112 } else { 113 typeSG.getSimpleTypeSG().forAllValues(pMethod, v, pSGlet); 114 } 115 pMethod.addEndFor(); 116 } 117 118 public void forAllNonNullValues(PropertySG pController, JavaMethod pMethod, DirectAccessible pElement, SGlet pSGlet) throws SAXException { 119 LocalJavaField list = pMethod.newJavaField(List .class); 120 list.addLine(pController.getValue(pElement)); 121 DirectAccessible i = pMethod.addForList(list); 122 TypeSG typeSG = objectSG.getTypeSG(); 123 boolean isCasting = !OBJECT_TYPE.equals(typeSG.getRuntimeType()); 124 JavaQName qName = typeSG.getRuntimeType(); 125 if (isCasting && pSGlet instanceof SGlet.TypedSGlet) { 126 SGlet.TypedSGlet typedSGlet = (SGlet.TypedSGlet) pSGlet; 127 isCasting = typedSGlet.isCasting(); 128 if (typedSGlet.getType() != null) { 129 qName = typedSGlet.getType(); 130 } 131 } 132 Object v = new Object []{list, ".get(", i, ")"}; 133 if (isCasting) { 134 if (qName.isPrimitive()) { 135 v = new Object []{"((", qName.getObjectType(), ")", v, ").", qName.getPrimitiveConversionMethod(), "()"}; 136 } else { 137 v = new Object []{"(", qName, ")", v}; 138 } 139 } 140 if (typeSG.isComplex()) { 141 pSGlet.generate(pMethod, v); 142 } else { 143 typeSG.getSimpleTypeSG().forAllNonNullValues(pMethod, v, pSGlet); 144 } 145 pMethod.addEndFor(); 146 } 147 148 public void setValue(PropertySG pController, JavaMethod pMethod, DirectAccessible pElement, Object pValue, JavaQName pType) throws SAXException { 149 if (pType != null) { 150 pValue = new Object []{"(", pType, ") ", pValue}; 151 } 152 LocalJavaField list = pMethod.newJavaField(List .class); 153 list.addLine(pController.getValue(pElement)); 154 pMethod.addLine(list, ".clear();"); 155 pMethod.addLine(list, ".addAll(", pValue, ");"); 156 } 157 158 public void addValue(PropertySG pController, JavaMethod pMethod, DirectAccessible pElement, TypedValue pValue, JavaQName pType) throws SAXException { 159 pMethod.addLine(pController.getValue(pElement), ".add(", asObject(pValue), ");"); 160 } 161 } 162 | Popular Tags |