KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ws.jaxme.generator.sg.impl.ccsg;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import org.apache.ws.jaxme.generator.sg.AttributeSG;
7 import org.apache.ws.jaxme.generator.sg.ComplexTypeSG;
8 import org.apache.ws.jaxme.generator.sg.GroupSG;
9 import org.apache.ws.jaxme.generator.sg.ObjectSG;
10 import org.apache.ws.jaxme.generator.sg.ParticleSG;
11 import org.apache.ws.jaxme.generator.sg.PropertySG;
12 import org.apache.ws.jaxme.generator.sg.PropertySGChain;
13 import org.apache.ws.jaxme.generator.sg.TypeSG;
14 import org.apache.ws.jaxme.generator.sg.impl.PropertySGChainImpl;
15 import org.apache.ws.jaxme.generator.sg.impl.PropertySGImpl;
16 import org.apache.ws.jaxme.js.JavaField;
17 import org.apache.ws.jaxme.js.JavaMethod;
18 import org.apache.ws.jaxme.js.JavaQName;
19 import org.apache.ws.jaxme.js.JavaSource;
20 import org.xml.sax.SAXException JavaDoc;
21
22
23 /** Implementation of a
24  * {@link org.apache.ws.jaxme.generator.sg.impl.ccsg.ParticleVisitor},
25  * which generates a bean class.
26  */

27 public class BeanGeneratingVisitor extends ParticleVisitorImpl {
28     private final JavaSource js;
29     private JavaMethod mixedContentMethod;
30     private boolean isMixed;
31     private ComplexTypeSG ct;
32
33     /** Creates a new instance generating methods into the
34      * given class.
35      * @param pJs Bean class being generated.
36      */

37     public BeanGeneratingVisitor(JavaSource pJs) {
38         js = pJs;
39     }
40
41     private JavaSource getJavaSource() {
42         return js;
43     }
44
45     private boolean isInheritedAttribute(AttributeSG pAttr, AttributeSG[] pInheritedAttributes) throws SAXException JavaDoc {
46         if (pInheritedAttributes != null) {
47             String JavaDoc p = pAttr.getPropertySG().getPropertyName();
48             for (int i = 0; i < pInheritedAttributes.length; i++) {
49                 if (pInheritedAttributes[i].getPropertySG().getPropertyName().equals(p)) {
50                     return true;
51                 }
52             }
53         }
54         return false;
55     }
56
57     private void generateAttributes(ComplexTypeSG pType) throws SAXException JavaDoc {
58         JavaSource js = getJavaSource();
59         AttributeSG[] myAttributes = pType.getAttributes();
60         AttributeSG[] inheritedAttributes = null;
61         if (pType.getTypeSG().isExtension()) {
62             TypeSG tSG = pType.getTypeSG().getExtendedType();
63             if (tSG.isComplex()) {
64                 inheritedAttributes = tSG.getComplexTypeSG().getAttributes();
65             }
66         }
67         for (int i = 0; i < myAttributes.length; i++) {
68             AttributeSG attr = myAttributes[i];
69             if (!isInheritedAttribute(attr, inheritedAttributes)) {
70                 attr.getPropertySG().generate(js);
71             }
72         }
73     }
74
75     public void emptyType(ComplexTypeSG pType) throws SAXException JavaDoc {
76         generateAttributes(pType);
77     }
78
79     public void simpleContent(ComplexTypeSG pType) throws SAXException JavaDoc {
80         generateAttributes(pType);
81         pType.getSimpleContentSG().getPropertySG().generate(getJavaSource());
82     }
83
84     protected JavaMethod getGetMixedContentMethod() {
85         if (mixedContentMethod == null) {
86             JavaSource js = getJavaSource();
87             JavaMethod jm = js.newJavaMethod("getContent", List JavaDoc.class, JavaSource.PUBLIC);
88             if (!js.isInterface()) {
89                 JavaField mixedContentList = getJavaSource().newJavaField("content", List JavaDoc.class, JavaSource.PRIVATE);
90                 mixedContentList.setFinal(true);
91                 mixedContentList.addLine("new ", ArrayList JavaDoc.class, "()");
92                 jm.addLine("return ", mixedContentList, ";");
93             }
94         }
95         return mixedContentMethod;
96     }
97
98     public void startComplexContent(ComplexTypeSG pType) throws SAXException JavaDoc {
99         pType.getComplexContentSG().getElementParticles(); // Triggers the content verification
100
ct = pType;
101         generateAttributes(pType);
102         if (pType.getComplexContentSG().isMixed()) {
103             isMixed = true;
104             getGetMixedContentMethod();
105         }
106     }
107
108     private boolean isInheritedParticle(ParticleSG pParticle) throws SAXException JavaDoc {
109         String JavaDoc propertyName = pParticle.getPropertySG().getPropertyName();
110         if (ct.getTypeSG().isExtension()) {
111             TypeSG extType = ct.getTypeSG().getExtendedType();
112             if (extType.isComplex() && !extType.getComplexTypeSG().hasSimpleContent()) {
113                 ParticleSG[] inheritedParticles = extType.getComplexTypeSG().getComplexContentSG().getElementParticles();
114                 for (int i = 0; i < inheritedParticles.length; i++) {
115                     if (inheritedParticles[i].getPropertySG().getPropertyName().equals(propertyName)) {
116                         return true;
117                     }
118                 }
119             }
120         }
121         return false;
122     }
123
124     private void elementParticle(GroupSG pGroupSG, ParticleSG pParticle) throws SAXException JavaDoc {
125         if (isInheritedParticle(pParticle)) {
126             return;
127         }
128         final JavaSource pJs = this.js;
129         final PropertySG elementSG = pParticle.getPropertySG();
130         if (isMixed) {
131             JavaQName qName = GroupUtil.getContentClass(pGroupSG, pParticle, pJs.getQName());
132             JavaSource js;
133             if (qName.isInnerClass()) {
134                 js = pJs.newJavaInnerClass(qName.getInnerClassName(), JavaSource.PUBLIC);
135                 if (!pJs.isInterface()) {
136                     js.setStatic(true);
137                 }
138             } else {
139                 js = pJs.getFactory().newJavaSource(qName, JavaSource.PUBLIC);
140             }
141             if (pJs.isInterface()) {
142                 js.setType(JavaSource.INTERFACE);
143             } else {
144                 js.addImplements(GroupUtil.getContentClass(pGroupSG, pParticle, ct.getClassContext().getXMLInterfaceName()));
145             }
146             PropertySGChain chain = ((PropertySGImpl) elementSG).getHeadOfChain();
147             PropertySGChain head = new PropertySGChainImpl(chain){
148                 public String JavaDoc getXMLFieldName(PropertySG pController) throws SAXException JavaDoc {
149                     return "_value";
150                 }
151                 public String JavaDoc getPropertyName(PropertySG pController) throws SAXException JavaDoc {
152                     return "value";
153                 }
154             };
155             PropertySGImpl pSG = new PropertySGImpl(head);
156             pSG.generate(js);
157         } else {
158             elementSG.generate(pJs);
159         }
160
161         ObjectSG oSG = pParticle.getObjectSG();
162         TypeSG typeSG = oSG.getTypeSG();
163         if (!typeSG.isGlobalType() && !typeSG.isGlobalClass() && typeSG.isComplex()) {
164             ComplexTypeSG complexTypeSG = typeSG.getComplexTypeSG();
165             if (pJs.isInterface()) {
166                 complexTypeSG.getXMLInterface(pJs);
167             } else {
168                 complexTypeSG.getXMLImplementation(pJs);
169             }
170         }
171     }
172
173     public void simpleElementParticle(GroupSG pGroup, ParticleSG pParticle) throws SAXException JavaDoc {
174         elementParticle(pGroup, pParticle);
175     }
176
177     public void complexElementParticle(GroupSG pGroup, ParticleSG pParticle) throws SAXException JavaDoc {
178         elementParticle(pGroup, pParticle);
179     }
180
181     public void wildcardParticle(ParticleSG particle) {
182         throw new IllegalStateException JavaDoc("TODO: Add support for wildcards");
183     }
184 }
185
Popular Tags