1 16 package org.apache.ws.jaxme.generator.sg.impl; 17 18 import javax.xml.bind.Element; 19 import javax.xml.namespace.QName ; 20 21 import org.apache.ws.jaxme.JMElement; 22 import org.apache.ws.jaxme.generator.sg.ComplexTypeSG; 23 import org.apache.ws.jaxme.generator.sg.Context; 24 import org.apache.ws.jaxme.generator.sg.ObjectSG; 25 import org.apache.ws.jaxme.generator.sg.ObjectSGChain; 26 import org.apache.ws.jaxme.generator.sg.SGFactory; 27 import org.apache.ws.jaxme.generator.sg.SchemaSG; 28 import org.apache.ws.jaxme.generator.sg.TypeSG; 29 import org.apache.ws.jaxme.js.JavaField; 30 import org.apache.ws.jaxme.js.JavaMethod; 31 import org.apache.ws.jaxme.js.JavaQName; 32 import org.apache.ws.jaxme.js.JavaSource; 33 import org.apache.ws.jaxme.js.JavaSourceFactory; 34 import org.apache.ws.jaxme.logging.Logger; 35 import org.apache.ws.jaxme.logging.LoggerAccess; 36 import org.apache.ws.jaxme.xs.XSAny; 37 import org.apache.ws.jaxme.xs.XSAttribute; 38 import org.apache.ws.jaxme.xs.XSElement; 39 import org.apache.ws.jaxme.xs.XSObject; 40 import org.apache.ws.jaxme.xs.XSSimpleContentType; 41 import org.apache.ws.jaxme.xs.XSType; 42 import org.apache.ws.jaxme.xs.xml.XsQName; 43 import org.xml.sax.Locator ; 44 import org.xml.sax.SAXException ; 45 46 47 50 public class JAXBObjectSG extends JAXBSGItem implements ObjectSGChain { 51 private final static Logger log = LoggerAccess.getLogger(JAXBObjectSG.class); 52 private final XSType type; 53 private final TypeSG typeSG; 54 private final XsQName name; 55 private final Context classContext; 56 private final boolean global; 57 58 61 public JAXBObjectSG(SGFactory pFactory, SchemaSG pSchema, XSAttribute pAttribute, 62 Context pClassContext) throws SAXException { 63 this(pFactory, pSchema, (XSObject) pAttribute, pClassContext); 64 } 65 66 69 public JAXBObjectSG(SGFactory pFactory, SchemaSG pSchema, XSElement pElement) throws SAXException { 70 this(pFactory, pSchema, (XSObject) pElement, null); 71 } 72 73 76 public JAXBObjectSG(SGFactory pFactory, SchemaSG pSchema, XSElement pElement, 77 Context pClassContext) throws SAXException { 78 this(pFactory, pSchema, (XSObject) pElement, pClassContext); 79 } 80 81 84 private JAXBObjectSG(SGFactory pFactory, SchemaSG pSchema, XSObject pObject, 85 Context pClassContext) throws SAXException { 86 super(pFactory, pSchema, pObject); 87 final String mName = "<init>(XSObject,Context)"; 88 boolean isClassGlobal; 89 if (pObject instanceof XSElement) { 90 XSElement element = (XSElement) pObject; 91 log.finest(mName, "->", new Object []{element.getName(), pClassContext}); 92 type = element.getType(); 93 name = element.getName(); 94 global = element.isGlobal(); 95 isClassGlobal = !type.isSimple() && (type.isGlobal() || global); 96 } else { 97 throw new IllegalStateException ("Unknown object type: " + pObject.getClass().getName()); 98 } 99 100 Context myClassContext; 101 final boolean useTypesContext = pClassContext != null; 102 if (useTypesContext) { 103 myClassContext = pClassContext; 104 } else { 105 myClassContext = new GlobalContext(name, pObject, null, null, pSchema); 106 } 107 108 if (isClassGlobal) { 109 if (type.isGlobal()) { 110 typeSG = pFactory.getTypeSG(type); 111 } else { 112 typeSG = pFactory.getTypeSG(type, name); 113 } 114 } else { 115 typeSG = pFactory.getTypeSG(type, myClassContext, name); 116 } 117 118 if (useTypesContext) { 119 if (typeSG.isComplex()) { 120 classContext = typeSG.getComplexTypeSG().getClassContext(); 121 } else { 122 classContext = pClassContext; 123 } 124 } else if (typeSG.isComplex()) { 125 classContext = myClassContext; 126 Context tctx = typeSG.getComplexTypeSG().getClassContext(); 127 AbstractContext ctx = (AbstractContext) classContext; 128 ctx.setPMName(tctx.getPMName()); 129 ctx.setXMLSerializerName(tctx.getXMLSerializerName()); 130 ctx.setXMLValidatorName(tctx.getXMLValidatorName()); 131 } else { 132 classContext = null; 133 } 134 log.finest(mName, "<-", new Object []{typeSG, classContext}); 135 } 136 137 141 public JAXBObjectSG(SGFactory pFactory, SchemaSG pSchema, TypeSG pComplexType, 142 XSSimpleContentType pContainer, XSType pType) throws SAXException { 143 super(pFactory, pSchema, pType); 144 global = false; 145 type = pType; 146 name = null; 147 classContext = pComplexType.getComplexTypeSG().getClassContext(); 148 typeSG = pFactory.getTypeSG(type, classContext, null); 149 } 150 151 154 public JAXBObjectSG(SGFactory pFactory, SchemaSG pSchema, XSAny pAny) { 155 super(pFactory, pSchema, pAny); 156 type = null; 157 name = null; 158 classContext = null; 159 typeSG = null; 160 global = false; 161 } 162 163 public void init(ObjectSG pController) throws SAXException { 164 } 165 166 public TypeSG getTypeSG(ObjectSG pController) { 167 if (typeSG == null) { 168 throw new NullPointerException ("ObjectSG not initialized"); 169 } 170 return typeSG; 171 } 172 173 public Locator getLocator(ObjectSG pController) { return getLocator(); } 174 public SGFactory getFactory(ObjectSG pController) { return getFactory(); } 175 public SchemaSG getSchema(ObjectSG pController) { return getSchema(); } 176 public Context getClassContext(ObjectSG pController) { return classContext; } 177 public XsQName getName(ObjectSG pController) { 178 if (name == null) { 179 throw new IllegalStateException ("The content object of a complex type with simple content doesn't have an XML Schema name."); 180 } 181 return name; 182 } 183 184 public JavaSource getXMLInterface(ObjectSG pController) throws SAXException { 185 final String mName = "getXMLInterface"; 186 log.finest(mName, "->", pController.getName()); 187 if (!pController.getTypeSG().isComplex()) { 188 log.finest(mName, "<-", "null"); 189 return null; 190 } 191 192 JavaQName xmlInterfaceName = pController.getClassContext().getXMLInterfaceName(); 193 JavaSourceFactory jsf = getSchema().getJavaSourceFactory(); 194 JavaSource js = jsf.newJavaSource(xmlInterfaceName, JavaSource.PUBLIC); 195 js.setType(JavaSource.INTERFACE); 196 js.addExtends(Element.class); 197 198 TypeSG myTypeSG = pController.getTypeSG(); 199 ComplexTypeSG complexTypeSG = myTypeSG.getComplexTypeSG(); 200 if (myTypeSG.isGlobalClass()) { 201 js.addExtends(complexTypeSG.getClassContext().getXMLInterfaceName()); 202 } 203 log.finest(mName, "<-", xmlInterfaceName); 204 return js; 205 } 206 207 public JavaSource getXMLImplementation(ObjectSG pController) throws SAXException { 208 final String mName = "getXMLImplementation"; 209 log.finest(mName, "->", pController.getName()); 210 if (!pController.getTypeSG().isComplex()) { 211 log.finest(mName, "<-", "null"); 212 return null; 213 } 214 215 JavaQName xmlImplementationName = pController.getClassContext().getXMLImplementationName(); 216 JavaSourceFactory jsf = getSchema().getJavaSourceFactory(); 217 JavaSource js = jsf.newJavaSource(xmlImplementationName, JavaSource.PUBLIC); 218 SerializableSG.makeSerializable(pController.getSchema(), js); 219 js.addImplements(pController.getClassContext().getXMLInterfaceName()); 220 js.addImplements(JMElement.class); 221 222 TypeSG myTypeSG = pController.getTypeSG(); 223 ComplexTypeSG complexTypeSG = myTypeSG.getComplexTypeSG(); 224 if (myTypeSG.isGlobalClass()) { 225 js.addExtends(complexTypeSG.getClassContext().getXMLImplementationName()); 226 } 227 228 JavaField myName = js.newJavaField("__qName", QName .class, JavaSource.PRIVATE); 229 myName.setStatic(true); 230 myName.setFinal(true); 231 XsQName qName = pController.getName(); 232 String prefix = qName.getPrefix(); 233 if (prefix == null) { 234 myName.addLine("new ", QName .class, "(", JavaSource.getQuoted(qName.getNamespaceURI()), 235 ", ", JavaSource.getQuoted(qName.getLocalName()), ")"); 236 } else { 237 myName.addLine("new ", QName .class, "(", JavaSource.getQuoted(qName.getNamespaceURI()), 238 ", ", JavaSource.getQuoted(qName.getLocalName()), ", ", 239 JavaSource.getQuoted(prefix), ")"); 240 } 241 242 JavaMethod getQName = js.newJavaMethod("getQName", QName .class, JavaSource.PUBLIC); 243 getQName.addLine("return ", myName, ";"); 244 245 return js; 246 } 247 248 public JavaSource getXMLSerializer(ObjectSG pController) throws SAXException { 249 final String mName = "getXMLSerializer"; 250 log.finest(mName, "->", pController.getName()); 251 TypeSG myTypeSG = pController.getTypeSG(); 252 JavaSource result = myTypeSG.getComplexTypeSG().getXMLSerializer(); 253 log.finest(mName, "<-", result.getQName()); 254 return result; 255 } 256 257 public JavaSource getXMLHandler(ObjectSG pController) throws SAXException { 258 final String mName = "getXMLHandler"; 259 log.finest(mName, "->", pController.getName()); 260 TypeSG myTypeSG = pController.getTypeSG(); 261 if (!myTypeSG.isComplex()) { 262 log.finest(mName, "<-", null); 263 return null; 264 } else if (myTypeSG.isGlobalClass()) { 265 return null; 266 } else { 267 JavaQName xmlHandlerName = pController.getClassContext().getXMLHandlerName(); 268 return myTypeSG.getComplexTypeSG().getXMLHandler(xmlHandlerName); 269 } 270 } 271 272 public void generate(ObjectSG pController) throws SAXException { 273 final String mName = "generate"; 274 log.finest(mName, "->", pController.getName()); 275 pController.getXMLInterface(); 276 pController.getXMLImplementation(); 277 278 TypeSG myTypeSG = pController.getTypeSG(); 279 if (myTypeSG.isGlobalClass() && !myTypeSG.isGlobalType()) { 280 myTypeSG.generate(); 281 } 282 log.finest(mName, "<-"); 283 } 284 285 public boolean isGlobal(ObjectSG pController) throws SAXException { 286 return global; 287 } 288 } 289 | Popular Tags |