1 17 package org.apache.ws.jaxme.generator.sg.impl; 18 19 import java.util.List ; 20 21 import org.apache.ws.jaxme.generator.sg.AttributeSG; 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.SchemaSG; 27 import org.apache.ws.jaxme.generator.sg.SimpleTypeSG; 28 import org.apache.ws.jaxme.generator.sg.TypeSG; 29 import org.apache.ws.jaxme.generator.util.JavaNamer; 30 import org.apache.ws.jaxme.js.DirectAccessible; 31 import org.apache.ws.jaxme.js.JavaField; 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.xs.XSAttribute; 39 import org.apache.ws.jaxme.xs.XSElement; 40 import org.apache.ws.jaxme.xs.XSObject; 41 import org.apache.ws.jaxme.xs.XSType; 42 import org.apache.ws.jaxme.xs.jaxb.JAXBProperty; 43 import org.apache.ws.jaxme.xs.jaxb.JAXBPropertyOwner; 44 import org.xml.sax.SAXException ; 45 46 50 public class JAXBPropertySG implements PropertySGChain { 51 private final String propertyName; 52 private final String fieldName; 53 private final String collectionType; 54 private final boolean generateIsSetMethod; 55 private final String defaultValue; 56 private final TypeSG typeSG; 57 58 protected JAXBPropertySG(String pDefaultPropertyName, SchemaSG pSchema, XSObject pXSObject, 59 String pDefaultValue, TypeSG pTypeSG) { 60 typeSG = pTypeSG; 61 defaultValue = pDefaultValue; 62 String myPropertyName = null; 63 boolean myGeneratedIsSetMethod = pSchema.isGeneratingIsSetMethod(); 64 String myCollectionType = null; 65 if (pXSObject instanceof JAXBPropertyOwner) { 66 JAXBPropertyOwner jaxbPropertyOwner = (JAXBPropertyOwner) pXSObject; 67 JAXBProperty jaxbProperty = jaxbPropertyOwner.getJAXBProperty(); 68 if (jaxbProperty != null) { 69 myPropertyName = jaxbProperty.getName(); 70 myCollectionType = jaxbProperty.getCollectionType(); 71 Boolean jaxbGeneratedIsSetMethod = jaxbProperty.isGenerateIsSetMethod(); 72 if (jaxbGeneratedIsSetMethod != null) { 73 myGeneratedIsSetMethod = jaxbGeneratedIsSetMethod.booleanValue(); 74 } 75 } 76 } 77 78 collectionType = myCollectionType == null ? pSchema.getCollectionType() : myCollectionType; 79 if (myPropertyName == null) { 80 myPropertyName = JavaNamer.convert(pDefaultPropertyName, pSchema); 81 myPropertyName = Character.toLowerCase(myPropertyName.charAt(0)) + myPropertyName.substring(1); 82 } 83 propertyName = myPropertyName; 84 fieldName = "_" + propertyName; 85 generateIsSetMethod = myGeneratedIsSetMethod; 86 } 87 88 protected JAXBPropertySG(AttributeSG pAttribute, XSAttribute pXSAttribute) { 89 this(pAttribute.getName().getLocalName(), pAttribute.getSchema(), pXSAttribute, pXSAttribute.getDefault(), 90 pAttribute.getTypeSG()); 91 } 92 93 protected JAXBPropertySG(ObjectSG pElement, XSElement pXSElement) { 94 this(pElement.getName().getLocalName(), pElement.getSchema(), pXSElement, pXSElement.getDefault(), 95 pElement.getTypeSG()); 96 } 97 98 protected JAXBPropertySG(TypeSG pComplexType, XSType pType) throws SAXException { 99 this("value", pComplexType.getSchema(), pType, null, 100 pComplexType.getComplexTypeSG().getSimpleContentSG().getContentTypeSG()); 101 } 102 103 public void init(PropertySG pController) throws SAXException {} 104 105 public boolean hasIsSetMethod(PropertySG pController) { return generateIsSetMethod; } 106 public String getCollectionType(PropertySG pController) { return collectionType; } 107 108 public String getXMLFieldName(PropertySG pController) throws SAXException { 109 return fieldName; 110 } 111 112 public String getPropertyName(PropertySG pController) throws SAXException { 113 return propertyName; 114 } 115 116 public String getXMLGetMethodName(PropertySG pController) throws SAXException { 117 String prefix; 118 if (typeSG != null && !typeSG.isComplex() && 119 typeSG.getSimpleTypeSG().getRuntimeType().equals(JavaQNameImpl.BOOLEAN)) { 120 prefix = "is"; 121 } else { 122 prefix = "get"; 123 } 124 String propName = pController.getPropertyName(); 125 String methodName = prefix + Character.toUpperCase(propName.charAt(0)) + propName.substring(1); 126 if (methodName.equals("getClass")) { 127 throw new SAXException ("Method name getClass() conflicts with java.lang.Object.getClass(), use jaxb:property to customize the property name."); 128 } 129 return methodName; 130 } 131 132 public String getXMLSetMethodName(PropertySG pController) throws SAXException { 133 String propName = pController.getPropertyName(); 134 return "set" + Character.toUpperCase(propName.charAt(0)) + propName.substring(1); 135 } 136 137 public String getXMLIsSetMethodName(PropertySG pController) throws SAXException { 138 String propName = pController.getPropertyName(); 139 return hasIsSetMethod(pController) ? 140 "isSet" + Character.toUpperCase(propName.charAt(0)) + propName.substring(1) : null; 141 } 142 143 public JavaField getXMLField(PropertySG pController, JavaSource pSource) throws SAXException { 144 return typeSG.getXMLField(pSource, pController.getXMLFieldName(), defaultValue); 145 } 146 147 public JavaMethod getXMLGetMethod(PropertySG pController, JavaSource pSource) throws SAXException { 148 return typeSG.getXMLGetMethod(pSource, pController.getXMLFieldName(), 149 pController.getXMLGetMethodName()); 150 } 151 152 public JavaMethod getXMLSetMethod(PropertySG pController, JavaSource pSource) throws SAXException { 153 String propName = pController.getPropertyName(); 154 String pName = "p" + Character.toUpperCase(propName.charAt(0)) + propName.substring(1); 155 return typeSG.getXMLSetMethod(pSource, pController.getXMLFieldName(), 156 pName, pController.getXMLSetMethodName(), 157 pController.hasIsSetMethod()); 158 } 159 160 public JavaMethod getXMLIsSetMethod(PropertySG pController, JavaSource pSource) throws SAXException { 161 return typeSG.getXMLIsSetMethod(pSource, pController.getXMLFieldName(), 162 pController.getXMLIsSetMethodName()); 163 } 164 165 public void forAllNonNullValues(PropertySG pController, JavaMethod pMethod, 166 DirectAccessible pElement, SGlet pSGlet) throws SAXException { 167 boolean hasIsSetMethod = pController.hasIsSetMethod(); 168 if (hasIsSetMethod) { 169 if (pElement == null) { 170 pMethod.addIf(pController.getXMLIsSetMethodName(), "()"); 171 } else { 172 pMethod.addIf(pElement, ".", pController.getXMLIsSetMethodName(), "()"); 173 } 174 if (typeSG.isComplex()) { 175 pSGlet.generate(pMethod, pController.getValue(pElement)); 176 } else { 177 typeSG.getSimpleTypeSG().forAllValues(pMethod, pController.getValue(pElement), pSGlet); 178 } 179 pMethod.addEndIf(); 180 } else { 181 if (typeSG.isComplex()) { 182 LocalJavaField f = pMethod.newJavaField(typeSG.getComplexTypeSG().getClassContext().getXMLInterfaceName()); 183 f.addLine(pController.getValue(pElement)); 184 pMethod.addIf(f, " != null"); 185 pSGlet.generate(pMethod, pController.getValue(pElement)); 186 pMethod.addEndIf(); 187 } else { 188 typeSG.getSimpleTypeSG().forAllNonNullValues(pMethod, pController.getValue(pElement), pSGlet); 189 } 190 } 191 } 192 public void forAllValues(PropertySG pController, JavaMethod pMethod, 193 DirectAccessible pElement, SGlet pSGlet) throws SAXException { 194 SimpleTypeSG simpleTypeSG = typeSG.getSimpleTypeSG(); 195 simpleTypeSG.forAllValues(pMethod, pController.getValue(pElement), pSGlet); 196 } 197 198 public Object getValue(PropertySG pController, DirectAccessible pElement) throws SAXException { 199 if (pElement == null) { 200 return new Object []{pController.getXMLGetMethodName(), "()"}; 201 } else { 202 return new Object []{pElement, ".", pController.getXMLGetMethodName(), "()"}; 203 } 204 } 205 206 public void generate(PropertySG pController, JavaSource pSource) throws SAXException { 207 if (typeSG != null && !typeSG.isGlobalType() && !typeSG.isGlobalClass() && pSource.isInterface()) { 208 typeSG.generate(pSource); 209 } 210 pController.getXMLField(pSource); 211 pController.getXMLGetMethod(pSource); 212 pController.getXMLSetMethod(pSource); 213 if (hasIsSetMethod(pController)) { 214 pController.getXMLIsSetMethod(pSource); 215 } 216 } 217 218 public void setValue(PropertySG pController, JavaMethod pMethod, 219 DirectAccessible pElement, Object pValue, JavaQName pType) 220 throws SAXException { 221 if (pType != null) { 222 pValue = new Object []{"((", pType, ") ", pValue, ")"}; 223 } 224 if (typeSG.isComplex() || !typeSG.getSimpleTypeSG().isList()) { 225 if (pElement == null) { 226 pMethod.addLine(pController.getXMLSetMethodName(), "(", pValue, ");"); 227 } else { 228 pMethod.addLine(pElement, ".", pController.getXMLSetMethodName(), "(", pValue, ");"); 229 } 230 } else { 231 LocalJavaField list = pMethod.newJavaField(List .class); 232 list.addLine(pController.getValue(pElement)); 233 pMethod.addLine(list, ".clear();"); 234 pMethod.addLine(list, ".addAll(", pValue, ");"); 235 } 236 } 237 238 public void addValue(PropertySG pController, JavaMethod pMethod, DirectAccessible pElement, TypedValue pValue, JavaQName pType) throws SAXException { 239 pController.setValue(pMethod, pElement, pValue, pType); 240 } 241 } 242 | Popular Tags |