KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ws.jaxme.generator.sg.impl.ccsg;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import javax.xml.namespace.QName JavaDoc;
10
11 import org.apache.ws.jaxme.WildcardAttribute;
12 import org.apache.ws.jaxme.generator.sg.AttributeSG;
13 import org.apache.ws.jaxme.generator.sg.ComplexTypeSG;
14 import org.apache.ws.jaxme.generator.sg.GroupSG;
15 import org.apache.ws.jaxme.generator.sg.ParticleSG;
16 import org.apache.ws.jaxme.generator.sg.SGlet;
17 import org.apache.ws.jaxme.generator.sg.TypeSG;
18 import org.apache.ws.jaxme.impl.JMSAXDriverController;
19 import org.apache.ws.jaxme.js.DirectAccessible;
20 import org.apache.ws.jaxme.js.JavaMethod;
21 import org.apache.ws.jaxme.js.JavaQName;
22 import org.apache.ws.jaxme.js.JavaSource;
23 import org.apache.ws.jaxme.js.LocalJavaField;
24 import org.apache.ws.jaxme.xs.xml.XsQName;
25 import org.xml.sax.SAXException JavaDoc;
26 import org.xml.sax.helpers.AttributesImpl JavaDoc;
27
28
29 /** Implementation of
30  * {@link org.apache.ws.jaxme.generator.sg.impl.ccsg.ParticleVisitor}
31  * for generating the driver class.
32  */

33 public class DriverGeneratingVisitor extends ParticleVisitorImpl {
34     private final JavaSource js;
35
36     /** Creates a new instance, which writes methods into the
37      * given class.
38      */

39     public DriverGeneratingVisitor(JavaSource pJs) {
40         js = pJs;
41     }
42
43     /** This method builds a list of the names, which are being
44      * used in the element. The list is used for generating the
45      * method
46      * {@link org.apache.ws.jaxme.impl.JMSAXDriver#getPreferredPrefix(String)}.
47      * @throws SAXException
48      */

49     protected List JavaDoc getNames(ComplexTypeSG pType) throws SAXException JavaDoc {
50         final List JavaDoc names = new ArrayList JavaDoc();
51         ParticleVisitor visitor = new ParticleVisitorImpl(){
52             private void addAttributes(ComplexTypeSG pType) {
53                 AttributeSG[] myAttributes = pType.getAttributes();
54                 for (int i = 0; i < myAttributes.length; i++) {
55                     XsQName qName = myAttributes[i].getName();
56                     if (qName != null) {
57                         names.add(qName);
58                     }
59                 }
60             }
61             public void emptyType(ComplexTypeSG pType) {
62                 addAttributes(pType);
63             }
64             public void simpleContent(ComplexTypeSG pType) {
65                 addAttributes(pType);
66             }
67             public void startComplexContent(ComplexTypeSG pType) {
68                 addAttributes(pType);
69             }
70             public void simpleElementParticle(GroupSG pGroup, ParticleSG pParticle) throws SAXException JavaDoc {
71                 names.add(pParticle.getObjectSG().getName());
72             }
73             public void complexElementParticle(GroupSG pGroup, ParticleSG pParticle) throws SAXException JavaDoc {
74                 names.add(pParticle.getObjectSG().getName());
75             }
76         };
77         new ParticleWalker(visitor).walk(pType);
78         return names;
79     }
80
81     private JavaMethod newGetPreferredPrefixMethod(ComplexTypeSG pType) throws SAXException JavaDoc {
82         List JavaDoc names = getNames(pType);
83         Map JavaDoc uris = new HashMap JavaDoc();
84         for (Iterator JavaDoc iter = names.iterator(); iter.hasNext(); ) {
85             XsQName qName = (XsQName) iter.next();
86             String JavaDoc prefix = qName.getPrefix();
87             if (prefix != null) {
88                 String JavaDoc uri = qName.getNamespaceURI();
89                 if (uri == null) uri = "";
90                 if (!uris.containsKey(uri)) {
91                     uris.put(uri, prefix);
92                 }
93             }
94         }
95
96         JavaMethod jm = js.newJavaMethod("getPreferredPrefix", String JavaDoc.class, JavaSource.PUBLIC);
97         DirectAccessible pURI = jm.addParam(String JavaDoc.class, "pURI");
98         if (!uris.isEmpty()) {
99             jm.addIf(pURI, " == null");
100             jm.addLine(pURI, " = \"\";");
101             jm.addEndIf();
102             boolean first = true;
103             for (Iterator JavaDoc iter = uris.entrySet().iterator(); iter.hasNext(); ) {
104                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
105                 String JavaDoc uri = (String JavaDoc) entry.getKey();
106                 String JavaDoc prefix = (String JavaDoc) entry.getValue();
107                 jm.addIf(first, pURI, ".equals(", JavaSource.getQuoted(uri), ")");
108                 jm.addLine("return ", JavaSource.getQuoted(prefix), ";");
109                 first = false;
110             }
111             jm.addEndIf();
112         }
113         jm.addLine("return null;");
114         return jm;
115     }
116
117     private JavaMethod newGetAttributesMethod(ComplexTypeSG pType) throws SAXException JavaDoc {
118         JavaMethod jm = js.newJavaMethod("getAttributes", AttributesImpl JavaDoc.class, JavaSource.PUBLIC);
119         final DirectAccessible pController = jm.addParam(JMSAXDriverController.class, "pController");
120         DirectAccessible pObject = jm.addParam(Object JavaDoc.class, "pObject");
121         jm.addThrows(SAXException JavaDoc.class);
122         final LocalJavaField result = jm.newJavaField(AttributesImpl JavaDoc.class);
123         result.addLine("new ", AttributesImpl JavaDoc.class, "()");
124         AttributeSG[] myAttributes = pType.getAttributes();
125         if (myAttributes.length > 0) {
126             JavaQName elementInterface = pType.getClassContext().getXMLInterfaceName();
127             LocalJavaField element = jm.newJavaField(elementInterface);
128             element.addLine("(", elementInterface, ") ", pObject);
129             
130             for (int i = 0; i < myAttributes.length; i++) {
131                 final AttributeSG attribute = myAttributes[i];
132                 if (attribute.isWildcard()) {
133                     LocalJavaField anyAttributes = jm.newJavaField(WildcardAttribute[].class);
134                     anyAttributes.addLine(element, ".", attribute.getPropertySG().getXMLGetMethodName() + "Array", "()");
135                     DirectAccessible index = jm.addForArray(anyAttributes);
136                     LocalJavaField wildcardAttribute = jm.newJavaField(WildcardAttribute.class);
137                     wildcardAttribute.addLine(anyAttributes, "[", index, "]");
138                     LocalJavaField qName = jm.newJavaField(QName JavaDoc.class);
139                     qName.addLine(wildcardAttribute, ".getName()");
140                     LocalJavaField uri = jm.newJavaField(String JavaDoc.class);
141                     uri.addLine(qName, ".getNamespaceURI()");
142                     LocalJavaField localPart = jm.newJavaField(String JavaDoc.class);
143                     localPart.addLine(qName, ".getLocalPart()");
144                     jm.addLine(result, ".addAttribute(", uri, ", ", localPart,
145                                ", ", pController, ".getAttrQName(this, ", uri, ", ", localPart,
146                                "), \"CDATA\", ", wildcardAttribute, ".getValue());");
147                     jm.addEndFor();
148                 } else {
149                     SGlet sgLet = new SGlet(){
150                         public void generate(JavaMethod pMethod, Object JavaDoc pValue) throws SAXException JavaDoc {
151                             String JavaDoc uri = JavaSource.getQuoted(attribute.getName().getNamespaceURI());
152                             String JavaDoc localName = JavaSource.getQuoted(attribute.getName().getLocalName());
153                             pMethod.addLine(result, ".addAttribute(", uri, ", ", localName,
154                                             ", ", pController, ".getAttrQName(this, ", uri, ", ", localName, "), \"CDATA\", ",
155                                             attribute.getTypeSG().getSimpleTypeSG().getCastToString(pMethod, pValue, pController), ");");
156                         }
157                     };
158                     attribute.forAllNonNullValues(jm, element, sgLet);
159                 }
160             }
161         }
162         jm.addLine("return ", result, ";");
163         return jm;
164     }
165
166     private JavaMethod newMarshalChildsMethod(ComplexTypeSG pType) throws SAXException JavaDoc {
167         MarshalChildsMethodGeneratingVisitor visitor = new MarshalChildsMethodGeneratingVisitor(js);
168         new ParticleWalker(visitor).walk(pType);
169         return visitor.getMethod();
170     }
171
172     private void generate(ComplexTypeSG pType) throws SAXException JavaDoc {
173         newGetAttributesMethod(pType);
174         newGetPreferredPrefixMethod(pType);
175         newMarshalChildsMethod(pType);
176     }
177
178     public void emptyType(ComplexTypeSG pType) throws SAXException JavaDoc {
179         generate(pType);
180     }
181
182     public void simpleContent(ComplexTypeSG pType) throws SAXException JavaDoc {
183         generate(pType);
184     }
185
186     public void startComplexContent(ComplexTypeSG pType) throws SAXException JavaDoc {
187         generate(pType);
188     }
189
190     public void complexElementParticle(GroupSG pGroup, ParticleSG pParticle) throws SAXException JavaDoc {
191         TypeSG tSG = pParticle.getObjectSG().getTypeSG();
192         if (tSG.isComplex() && !tSG.isGlobalClass()) {
193             tSG.getComplexTypeSG().getXMLSerializer(js);
194         }
195     }
196 }
197
Popular Tags