KickJava   Java API By Example, From Geeks To Geeks.

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


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  */

17 package org.apache.ws.jaxme.generator.sg.impl;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.apache.ws.jaxme.generator.SchemaReader;
23 import org.apache.ws.jaxme.generator.sg.AttributeSG;
24 import org.apache.ws.jaxme.generator.sg.AttributeSGChain;
25 import org.apache.ws.jaxme.generator.sg.ComplexContentSG;
26 import org.apache.ws.jaxme.generator.sg.ComplexContentSGChain;
27 import org.apache.ws.jaxme.generator.sg.ComplexTypeSG;
28 import org.apache.ws.jaxme.generator.sg.ComplexTypeSGChain;
29 import org.apache.ws.jaxme.generator.sg.Context;
30 import org.apache.ws.jaxme.generator.sg.GroupSG;
31 import org.apache.ws.jaxme.generator.sg.SimpleContentSG;
32 import org.apache.ws.jaxme.generator.sg.SimpleContentSGChain;
33 import org.apache.ws.jaxme.generator.sg.TypeSG;
34 import org.apache.ws.jaxme.generator.sg.impl.ccsg.AllHandlerSG;
35 import org.apache.ws.jaxme.generator.sg.impl.ccsg.BeanGeneratingVisitor;
36 import org.apache.ws.jaxme.generator.sg.impl.ccsg.ChoiceHandlerSG;
37 import org.apache.ws.jaxme.generator.sg.impl.ccsg.DriverGeneratingVisitor;
38 import org.apache.ws.jaxme.generator.sg.impl.ccsg.EmptyElementHandlerSG;
39 import org.apache.ws.jaxme.generator.sg.impl.ccsg.HandlerSG;
40 import org.apache.ws.jaxme.generator.sg.impl.ccsg.ParticleWalker;
41 import org.apache.ws.jaxme.generator.sg.impl.ccsg.SequenceHandlerSG;
42 import org.apache.ws.jaxme.generator.sg.impl.ccsg.SimpleContentHandlerSG;
43 import org.apache.ws.jaxme.impl.JMSAXDriver;
44 import org.apache.ws.jaxme.impl.JMSAXElementParser;
45 import org.apache.ws.jaxme.js.JavaQName;
46 import org.apache.ws.jaxme.js.JavaSource;
47 import org.apache.ws.jaxme.js.JavaSourceFactory;
48 import org.apache.ws.jaxme.logging.Logger;
49 import org.apache.ws.jaxme.logging.LoggerAccess;
50 import org.apache.ws.jaxme.xs.XSAttributable;
51 import org.apache.ws.jaxme.xs.XSAttribute;
52 import org.apache.ws.jaxme.xs.XSComplexType;
53 import org.apache.ws.jaxme.xs.XSType;
54 import org.apache.ws.jaxme.xs.XSWildcard;
55 import org.xml.sax.Locator JavaDoc;
56 import org.xml.sax.SAXException JavaDoc;
57 import org.xml.sax.SAXParseException JavaDoc;
58
59
60 /**
61  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
62  * @author <a HREF="mailto:iasandcb@tmax.co.kr">Ias</a>
63  */

64 public class JAXBComplexTypeSG implements ComplexTypeSGChain {
65     private final static Logger log = LoggerAccess.getLogger(JAXBComplexTypeSG.class);
66     private final TypeSG typeSG;
67     private final XSType xsType;
68     private final boolean hasSimpleContent;
69     private final Context classContext;
70     private SimpleContentSG simpleContentSG;
71     private ComplexContentSG complexContentSG;
72     private AttributeSG[] attributes;
73     
74     protected JAXBComplexTypeSG(TypeSG pTypeSG, XSType pType) throws SAXException JavaDoc {
75         final String JavaDoc mName = "<init>(XSType)";
76         log.finest(mName, "->", new Object JavaDoc[]{pTypeSG, pType});
77         typeSG = pTypeSG;
78         xsType = pType;
79         XSComplexType complexType= pType.getComplexType();
80         hasSimpleContent = complexType.hasSimpleContent();
81         String JavaDoc suffix = pTypeSG.isGlobalType() ? null : "Type";
82         classContext = new GlobalContext(pTypeSG.getName(), pType, null, suffix, pTypeSG.getSchema());
83         log.finest(mName, "<-", classContext);
84     }
85     
86     /** <p>Constructor for a local type, which is embedded into the enclosing
87      * <code>pContext</code>.</p>
88      */

89     protected JAXBComplexTypeSG(TypeSG pTypeSG, XSType pType, Context pContext) throws SAXException JavaDoc {
90         final String JavaDoc mName = "<init>(XSType)";
91         log.finest(mName, "->", new Object JavaDoc[]{pTypeSG, pType, pContext});
92         typeSG = pTypeSG;
93         xsType = pType;
94         XSComplexType complexType= pType.getComplexType();
95         hasSimpleContent = complexType.hasSimpleContent();
96         classContext = new LocalContext(pContext, pTypeSG.getName().getLocalName(), pType, null, "Type",
97                 pTypeSG.getSchema());
98         log.finest(mName, "<-", classContext);
99     }
100     
101     public Object JavaDoc newAttributeSG(ComplexTypeSG pController, XSAttribute pAttribute) throws SAXException JavaDoc {
102         return new JAXBAttributeSG(typeSG.getSchema(), pAttribute, classContext);
103     }
104
105     public Object JavaDoc newAttributeSG(ComplexTypeSG pController, XSWildcard pWildcard) throws SAXException JavaDoc {
106         return new JAXBAttributeSG(typeSG.getSchema(), pWildcard, classContext);
107     }
108
109     private AttributeSG[] initAttributes(ComplexTypeSG pController) throws SAXException JavaDoc {
110         XSAttributable[] xsAttributes = xsType.getComplexType().getAttributes();
111         List JavaDoc attributeList = new ArrayList JavaDoc();
112         for (int i = 0; i < xsAttributes.length; i++) {
113             AttributeSGChain attrChain;
114             if (xsAttributes[i] instanceof XSAttribute) {
115                 XSAttribute attr = (XSAttribute) xsAttributes[i];
116                 attrChain = (AttributeSGChain) pController.newAttributeSG(attr);
117             } else if (xsAttributes[i] instanceof XSWildcard) {
118                 boolean isSupportingExtensions = false;
119                 SchemaReader schemaReader = pController.getTypeSG().getFactory().getGenerator().getSchemaReader();
120                 if (schemaReader instanceof JAXBSchemaReader) {
121                     isSupportingExtensions = ((JAXBSchemaReader) schemaReader).isSupportingExtensions();
122                 }
123                 if (isSupportingExtensions) {
124                     XSWildcard wildcard = (XSWildcard) xsAttributes[i];
125                     attrChain = (AttributeSGChain) pController.newAttributeSG(wildcard);
126                 } else {
127                     throw new SAXParseException JavaDoc("Extensions must be enabled to support wildcard attributes (JAXB 1.0, App. E.2.1.1)",
128                             ((XSWildcard) xsAttributes[i]).getLocator());
129                 }
130             } else {
131                 throw new IllegalStateException JavaDoc("Unknown attribute type: " + xsAttributes[i].getClass().getName());
132             }
133             AttributeSG attrSG = new AttributeSGImpl(attrChain);
134             attrSG.init();
135             attributeList.add(attrSG);
136         }
137         return (AttributeSG[]) attributeList.toArray(new AttributeSG[attributeList.size()]);
138     }
139     
140     public void addAttributeSG(ComplexTypeSG pController, AttributeSG pAttribute) throws SAXException JavaDoc {
141         AttributeSG[] result = new AttributeSG[attributes.length+1];
142         System.arraycopy(attributes, 0, result, 0, attributes.length);
143         result[attributes.length] = pAttribute;
144         attributes = result;
145         pAttribute.init();
146     }
147     
148     public void init(ComplexTypeSG pController) throws SAXException JavaDoc {
149         attributes = initAttributes(pController);
150         if (pController.hasSimpleContent()) {
151             SimpleContentSGChain chain = (SimpleContentSGChain) pController.newSimpleContentTypeSG();
152             simpleContentSG = new SimpleContentSGImpl(chain);
153             simpleContentSG.init();
154         } else {
155             ComplexContentSGChain chain = (ComplexContentSGChain) pController.newComplexContentTypeSG();
156             complexContentSG = new ComplexContentSGImpl(chain);
157             complexContentSG.init();
158         }
159     }
160
161     public boolean hasSimpleContent(ComplexTypeSG pController) { return hasSimpleContent; }
162     public TypeSG getTypeSG(ComplexTypeSG pController) { return typeSG; }
163     
164     public boolean hasAttributes(ComplexTypeSG pController) {
165         return attributes.length != 0;
166     }
167     
168     public AttributeSG[] getAttributes(ComplexTypeSG pController) {
169         return attributes;
170     }
171     
172     public Context getClassContext(ComplexTypeSG pController) { return classContext; }
173     public Locator JavaDoc getLocator(ComplexTypeSG pController) { return xsType.getLocator(); }
174     
175     public JavaSource getXMLInterface(ComplexTypeSG pController) throws SAXException JavaDoc {
176         final String JavaDoc mName = "getXMLInterface";
177         log.finest(mName, "->");
178         JavaQName qName = pController.getClassContext().getXMLInterfaceName();
179         JavaSourceFactory jsf = pController.getTypeSG().getSchema().getJavaSourceFactory();
180         JavaSource js = jsf.newJavaSource(qName, JavaSource.PUBLIC);
181         js.setType(JavaSource.INTERFACE);
182         createXMLBean(pController, js);
183         log.finest(mName, "<-", js.getQName());
184         return js;
185     }
186
187     private void createXMLBean(ComplexTypeSG pController, JavaSource pJs)
188             throws SAXException JavaDoc {
189         if (pJs.isInterface()) {
190             if (pController.getTypeSG().isExtension()) {
191                 TypeSG extType = pController.getTypeSG().getExtendedType();
192                 if (extType.isComplex()) {
193                     pJs.addExtends(extType.getComplexTypeSG().getClassContext().getXMLInterfaceName());
194                 }
195             }
196         } else {
197             SerializableSG.makeSerializable(pController.getTypeSG().getSchema(), pJs);
198             if (pController.getTypeSG().isExtension()) {
199                 TypeSG extType = pController.getTypeSG().getExtendedType();
200                 if (extType.isComplex()) {
201                     pJs.addExtends(extType.getComplexTypeSG().getClassContext().getXMLImplementationName());
202                 }
203             }
204         }
205         BeanGeneratingVisitor visitor = new BeanGeneratingVisitor(pJs);
206         new ParticleWalker(visitor).walk(pController);
207     }
208
209     public JavaSource getXMLInterface(ComplexTypeSG pController, JavaSource pSource) throws SAXException JavaDoc {
210         final String JavaDoc mName = "getXMLInterface(JavaSource)";
211         log.finest(mName, "->", pSource.getQName());
212         JavaQName qName = pController.getClassContext().getXMLInterfaceName();
213         JavaSource js = pSource.newJavaInnerClass(qName.getClassName(), JavaSource.PUBLIC);
214         js.setType(JavaSource.INTERFACE);
215         createXMLBean(pController, js);
216         log.finest(mName, "<-", js.getQName());
217         return js;
218     }
219     
220     public JavaSource getXMLImplementation(ComplexTypeSG pController) throws SAXException JavaDoc {
221         final String JavaDoc mName = "getXMLImplementation(JavaQName)";
222         log.finest(mName, "->", typeSG.getName());
223         JavaSourceFactory jsf = pController.getTypeSG().getSchema().getJavaSourceFactory();
224         JavaSource js = jsf.newJavaSource(pController.getClassContext().getXMLImplementationName(), JavaSource.PUBLIC);
225         js.addImplements(pController.getClassContext().getXMLInterfaceName());
226         createXMLBean(pController, js);
227         log.finest(mName, "<-", js.getQName());
228         return js;
229     }
230     
231     public JavaSource getXMLImplementation(ComplexTypeSG pController, JavaSource pSource) throws SAXException JavaDoc {
232         final String JavaDoc mName = "getXMLImplementation(JavaQName,JavaSource)";
233         log.finest(mName, "->", pSource.getQName());
234         JavaSource js = pSource.newJavaInnerClass(pController.getClassContext().getXMLImplementationName().getInnerClassName(), JavaSource.PUBLIC);
235         js.setStatic(true);
236         js.addImplements(pController.getClassContext().getXMLInterfaceName());
237         createXMLBean(pController, js);
238         log.finest(mName, "<-", js.getQName());
239         return js;
240     }
241
242     private void createXMLSerializer(ComplexTypeSG pController, JavaSource pSource)
243             throws SAXException JavaDoc {
244         pSource.addImplements(JMSAXDriver.class);
245         DriverGeneratingVisitor visitor = new DriverGeneratingVisitor(pSource);
246         new ParticleWalker(visitor).walk(pController);
247     }
248
249     public JavaSource getXMLSerializer(ComplexTypeSG pController) throws SAXException JavaDoc {
250         final String JavaDoc mName = "getXMLSerializer";
251         log.finest(mName, "->", typeSG.getName());
252         JavaQName xmlSerializerName = pController.getClassContext().getXMLSerializerName();
253         JavaSourceFactory jsf = typeSG.getSchema().getJavaSourceFactory();
254         JavaSource js = jsf.newJavaSource(xmlSerializerName, JavaSource.PUBLIC);
255         createXMLSerializer(pController, js);
256         log.finest(mName, "<-", js.getQName());
257         return js;
258     }
259     
260     public JavaSource getXMLSerializer(ComplexTypeSG pController, JavaSource pSource) throws SAXException JavaDoc {
261         final String JavaDoc mName = "getXMLSerializer(JavaSource)";
262         log.finest(mName, "->", pSource.getQName());
263         JavaSource js = pSource.newJavaInnerClass(pController.getClassContext().getXMLSerializerName().getInnerClassName(), JavaSource.PUBLIC);
264         js.setStatic(true);
265         createXMLSerializer(pController, js);
266         log.finest(mName, "<-", js.getQName());
267         return js;
268     }
269
270     private HandlerSG newHandlerSG(ComplexTypeSG pController,
271                                    JavaSource pJs)
272             throws SAXException JavaDoc {
273         if (pController.hasSimpleContent()) {
274             return new SimpleContentHandlerSG(pController, pJs);
275         } else {
276             ComplexContentSG ccSG = pController.getComplexContentSG();
277             if (ccSG.isEmpty()) {
278                 return new EmptyElementHandlerSG(pController, pJs);
279             } else {
280                 GroupSG group = ccSG.getRootParticle().getGroupSG();
281                 if (group.isSequence()) {
282                     return new SequenceHandlerSG(pController, pJs);
283                 } else if (group.isChoice()) {
284                     return new ChoiceHandlerSG(pController, pJs);
285                 } else if (group.isAll()) {
286                     return new AllHandlerSG(pController, pJs);
287                 } else {
288                     throw new IllegalStateException JavaDoc("Invalid group type");
289                 }
290             }
291         }
292     }
293
294     private void createXMLHandler(ComplexTypeSG pController, JavaSource pJs) throws SAXException JavaDoc {
295         pJs.addExtends(JMSAXElementParser.class);
296         HandlerSG handlerSG = newHandlerSG(pController, pJs);
297         handlerSG.generate();
298     }
299
300     public JavaSource getXMLHandler(ComplexTypeSG pController, JavaQName pQName) throws SAXException JavaDoc {
301         final String JavaDoc mName = "getXMLHandler";
302         log.finest(mName, "->", typeSG.getName());
303         JavaSourceFactory jsf = typeSG.getSchema().getJavaSourceFactory();
304         JavaSource js = jsf.newJavaSource(pQName, JavaSource.PUBLIC);
305         createXMLHandler(pController, js);
306         log.finest(mName, "<-", js.getQName());
307         return js;
308     }
309
310     public JavaSource getXMLHandler(ComplexTypeSG pController, JavaSource pSource) throws SAXException JavaDoc {
311         final String JavaDoc mName = "getXMLHandler(JavaSource)";
312         log.finest(mName, "->", pSource.getQName());
313         JavaSource js = pSource.newJavaInnerClass(pController.getClassContext().getXMLHandlerName().getInnerClassName(), JavaSource.PUBLIC);
314         js.setStatic(true);
315         createXMLHandler(pController, js);
316         log.finest(mName, "<-", js.getQName());
317         return js;
318     }
319     
320     public SimpleContentSG getSimpleContentSG(ComplexTypeSG pController) {
321         if (simpleContentSG == null) {
322             throw new IllegalStateException JavaDoc("This complex type doesn't have simple content.");
323         }
324         return simpleContentSG;
325     }
326     
327     public ComplexContentSG getComplexContentSG(ComplexTypeSG pController) {
328         if (complexContentSG == null) {
329             throw new IllegalStateException JavaDoc("This complex type doesn't have complex content.");
330         }
331         return complexContentSG;
332     }
333
334     public Object JavaDoc newComplexContentTypeSG(ComplexTypeSG pController) throws SAXException JavaDoc {
335         return new JAXBComplexContentTypeSG(pController, xsType);
336     }
337     
338     public Object JavaDoc newSimpleContentTypeSG(ComplexTypeSG pController) throws SAXException JavaDoc {
339         return new JAXBSimpleContentTypeSG(pController, xsType);
340     }
341 }
342
Popular Tags