KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > generator > sg > impl > JAXBObjectSG


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.ws.jaxme.generator.sg.impl;
17
18 import javax.xml.bind.Element;
19 import javax.xml.namespace.QName JavaDoc;
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 JavaDoc;
44 import org.xml.sax.SAXException JavaDoc;
45
46
47 /**
48  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
49  */

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   /** <p>Creates a new, local instance of JAXBObjectSG, generating
59    * the given attribute within the given {@link Context}.</p>
60    */

61   public JAXBObjectSG(SGFactory pFactory, SchemaSG pSchema, XSAttribute pAttribute,
62                        Context pClassContext) throws SAXException JavaDoc {
63     this(pFactory, pSchema, (XSObject) pAttribute, pClassContext);
64   }
65
66   /** <p>Creates a new, global instance of JAXBObjectSG, generating
67    * the given element.</p>
68    */

69   public JAXBObjectSG(SGFactory pFactory, SchemaSG pSchema, XSElement pElement) throws SAXException JavaDoc {
70     this(pFactory, pSchema, (XSObject) pElement, null);
71   }
72
73   /** <p>Creates a new, local instance of JAXBObjectSG, generating
74    * the given element within the given {@link Context}.</p>
75    */

76   public JAXBObjectSG(SGFactory pFactory, SchemaSG pSchema, XSElement pElement,
77                        Context pClassContext) throws SAXException JavaDoc {
78     this(pFactory, pSchema, (XSObject) pElement, pClassContext);
79   }
80
81   /** <p>Creates a new, local instance of JAXBObjectSG, which
82    * is generated within the given {@link Context}.</p>
83    */

84   private JAXBObjectSG(SGFactory pFactory, SchemaSG pSchema, XSObject pObject,
85                        Context pClassContext) throws SAXException JavaDoc {
86     super(pFactory, pSchema, pObject);
87     final String JavaDoc mName = "<init>(XSObject,Context)";
88     boolean isClassGlobal;
89     if (pObject instanceof XSElement) {
90       XSElement element = (XSElement) pObject;
91       log.finest(mName, "->", new Object JavaDoc[]{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 JavaDoc("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 JavaDoc[]{typeSG, classContext});
135   }
136
137   /** <p>Creates a new instance of JAXBObjectSG generating the given simple
138    * content <code>pContent</code> of the given complex type
139    * <code>pComplexType</code>.</p>
140    */

141   public JAXBObjectSG(SGFactory pFactory, SchemaSG pSchema, TypeSG pComplexType,
142                        XSSimpleContentType pContainer, XSType pType) throws SAXException JavaDoc {
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   /** <p>Creates a new instance of JAXBObjectSG generating the given
152    * wildcard object.</p>
153    */

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 JavaDoc {
164   }
165
166   public TypeSG getTypeSG(ObjectSG pController) {
167     if (typeSG == null) {
168       throw new NullPointerException JavaDoc("ObjectSG not initialized");
169     }
170     return typeSG;
171   }
172
173   public Locator JavaDoc 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 JavaDoc("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 JavaDoc {
185     final String JavaDoc 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 JavaDoc {
208     final String JavaDoc 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 JavaDoc.class, JavaSource.PRIVATE);
229     myName.setStatic(true);
230     myName.setFinal(true);
231     XsQName qName = pController.getName();
232     String JavaDoc prefix = qName.getPrefix();
233     if (prefix == null) {
234         myName.addLine("new ", QName JavaDoc.class, "(", JavaSource.getQuoted(qName.getNamespaceURI()),
235                        ", ", JavaSource.getQuoted(qName.getLocalName()), ")");
236     } else {
237         myName.addLine("new ", QName JavaDoc.class, "(", JavaSource.getQuoted(qName.getNamespaceURI()),
238                        ", ", JavaSource.getQuoted(qName.getLocalName()), ", ",
239                        JavaSource.getQuoted(prefix), ")");
240     }
241
242     JavaMethod getQName = js.newJavaMethod("getQName", QName JavaDoc.class, JavaSource.PUBLIC);
243     getQName.addLine("return ", myName, ";");
244
245     return js;
246   }
247
248   public JavaSource getXMLSerializer(ObjectSG pController) throws SAXException JavaDoc {
249     final String JavaDoc 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 JavaDoc {
258         final String JavaDoc 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 JavaDoc {
273     final String JavaDoc 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 JavaDoc {
286    return global;
287   }
288 }
289
Popular Tags