KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  *
3  */

4 package org.apache.ws.jaxme.generator.sg.impl.ccsg;
5
6 import java.util.HashSet JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Set JavaDoc;
9
10 import org.apache.ws.jaxme.generator.sg.ComplexTypeSG;
11 import org.apache.ws.jaxme.generator.sg.Context;
12 import org.apache.ws.jaxme.generator.sg.GroupSG;
13 import org.apache.ws.jaxme.generator.sg.ObjectSG;
14 import org.apache.ws.jaxme.generator.sg.ParticleSG;
15 import org.apache.ws.jaxme.generator.sg.SGlet;
16 import org.apache.ws.jaxme.generator.sg.SimpleContentSG;
17 import org.apache.ws.jaxme.impl.JMSAXDriver;
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.js.Parameter;
25 import org.xml.sax.ContentHandler JavaDoc;
26 import org.xml.sax.SAXException JavaDoc;
27
28
29 class MarshalChildsMethodGeneratingVisitor extends ParticleVisitorImpl {
30     private final Parameter element, controller, handler;
31     private ComplexTypeSG ctSG;
32     private LocalJavaField castedElement;
33     private final JavaMethod jm;
34     private boolean isMixed;
35     private Set JavaDoc mixedTypesMap;
36     private LocalJavaField object;
37
38     MarshalChildsMethodGeneratingVisitor(JavaSource pJs) {
39         jm = pJs.newJavaMethod("marshalChilds", void.class, JavaSource.PUBLIC);
40         jm.addThrows(SAXException JavaDoc.class);
41         controller = jm.addParam(JMSAXDriverController.class, "pController");
42         handler = jm.addParam(ContentHandler JavaDoc.class, "pHandler");
43         element = jm.addParam(Object JavaDoc.class, "pObject");
44     }
45
46     public void simpleContent(ComplexTypeSG pType) throws SAXException JavaDoc {
47         JavaQName elementInterface = pType.getClassContext().getXMLInterfaceName();
48         castedElement = jm.newJavaField(elementInterface);
49         castedElement.addLine("(", elementInterface, ") ", element);
50         LocalJavaField chars = jm.newJavaField(String JavaDoc.class);
51         SimpleContentSG simpleContent = pType.getSimpleContentSG();
52         Object JavaDoc value = simpleContent.getPropertySG().getValue(castedElement);
53         chars.addLine(simpleContent.getContentTypeSG().getSimpleTypeSG().getCastToString(jm, value, controller));
54         jm.addIf(chars, " != null && ", chars, ".length() > 0");
55         LocalJavaField charArray = jm.newJavaField(char[].class);
56         charArray.addLine(chars, ".toCharArray()");
57         jm.addLine(handler, ".characters(", charArray, ", 0, ", charArray, ".length);");
58         jm.addEndIf();
59     }
60
61     public void startComplexContent(ComplexTypeSG pType) throws SAXException JavaDoc {
62         ctSG = pType;
63         JavaQName elementInterface = pType.getClassContext().getXMLInterfaceName();
64         castedElement = jm.newJavaField(elementInterface);
65         castedElement.addLine("(", elementInterface, ") ", element);
66         isMixed = pType.getComplexContentSG().isMixed();
67         if (isMixed) {
68             LocalJavaField list = jm.newJavaField(List JavaDoc.class);
69             list.addLine(castedElement, ".getContent()");
70             DirectAccessible i = jm.addForList(list);
71             object = jm.newJavaField(Object JavaDoc.class);
72             object.addLine(list, ".get(", i, ")");
73             mixedTypesMap = new HashSet JavaDoc();
74             jm.addIf(object, " instanceof ", String JavaDoc.class);
75             LocalJavaField s = jm.newJavaField(String JavaDoc.class);
76             s.addLine("(", String JavaDoc.class, ") ", object);
77             jm.addLine(handler, ".characters(", s,
78                        ".toCharArray(), 0, ", s, ".length());");
79         }
80     }
81
82     public void endComplexContent(ComplexTypeSG pType) throws SAXException JavaDoc {
83         if (isMixed) {
84             jm.addElse();
85             jm.addThrowNew(IllegalStateException JavaDoc.class,
86                            JavaSource.getQuoted("Invalid type: "),
87                            " + ", object, ".getClass().getName()");
88             jm.addEndIf();
89             jm.addEndFor();
90         }
91     }
92
93     private void marshalSimpleValue(ObjectSG pObjectSG, JavaMethod pMethod, Object JavaDoc pValue) throws SAXException JavaDoc {
94         Object JavaDoc value = pObjectSG.getTypeSG().getSimpleTypeSG().getCastToString(pMethod, pValue, controller);
95         pMethod.addLine(controller, ".marshalSimpleChild(this, ",
96                 JavaSource.getQuoted(pObjectSG.getName().getNamespaceURI()), ", ",
97                 JavaSource.getQuoted(pObjectSG.getName().getLocalName()), ", ",
98                 value, ");");
99     }
100
101     public void simpleElementParticle(GroupSG pGroup, ParticleSG pParticle) throws SAXException JavaDoc {
102         final ObjectSG oSG = pParticle.getObjectSG();
103         if (isMixed) {
104             JavaQName interfaceName = ctSG.getClassContext().getXMLInterfaceName();
105             JavaQName qName = GroupUtil.getContentClass(pGroup, pParticle, interfaceName);
106             if (mixedTypesMap.contains(qName)) {
107                 return;
108             }
109             mixedTypesMap.add(qName);
110             jm.addElseIf(object, " instanceof ", qName);
111             Object JavaDoc value = new Object JavaDoc[]{"((", qName, ") ", object, ").getValue()"};
112             marshalSimpleValue(oSG, jm, value);
113         } else {
114             SGlet sgLet = new SGlet(){
115                 public void generate(JavaMethod pMethod, Object JavaDoc pValue) throws SAXException JavaDoc {
116                     marshalSimpleValue(oSG, pMethod, pValue);
117                 }
118             };
119             pParticle.getPropertySG().forAllNonNullValues(jm, castedElement, sgLet);
120         }
121     }
122
123     private void marshalComplexValue(ObjectSG pObjectSG, JavaMethod pMethod,
124                                      Object JavaDoc pValue) throws SAXException JavaDoc {
125         Context cc = pObjectSG.getTypeSG().getComplexTypeSG().getClassContext();
126         JavaQName serializerClass = cc.getXMLSerializerName();
127         LocalJavaField driver = pMethod.newJavaField(JMSAXDriver.class);
128         if (pObjectSG.getTypeSG().isGlobalClass()) {
129             JavaQName elementInterface;
130             if (pObjectSG.isGlobal()) {
131                 elementInterface = pObjectSG.getClassContext().getXMLInterfaceName();
132             } else {
133                 elementInterface = cc.getXMLInterfaceName();
134             }
135             driver.addLine(controller,
136                     ".getJMMarshaller().getJAXBContextImpl().getManagerS(",
137                     elementInterface, ".class).getDriver()");
138         } else {
139             driver.addLine("new ", serializerClass, "();");
140         }
141         pMethod.addLine(controller, ".marshal(", driver,
142                 ", ", JavaSource.getQuoted(pObjectSG.getName().getNamespaceURI()),
143                 ", ", JavaSource.getQuoted(pObjectSG.getName().getLocalName()),
144                 ", ", pValue, ");");
145     }
146
147     public void complexElementParticle(GroupSG pGroup, ParticleSG pParticle) throws SAXException JavaDoc {
148         final ObjectSG oSG = pParticle.getObjectSG();
149         if (isMixed) {
150             JavaQName qName = oSG.getTypeSG().getComplexTypeSG().getClassContext().getXMLInterfaceName();
151             if (mixedTypesMap.contains(qName)) {
152                 return;
153             }
154             mixedTypesMap.add(qName);
155             jm.addElseIf(object, " instanceof ", qName);
156             Object JavaDoc value = new Object JavaDoc[]{"((", qName, ") ", object, ")"};
157             marshalComplexValue(oSG, jm, value);
158         } else {
159             SGlet sgLet = new SGlet(){
160                 public void generate(JavaMethod pMethod, Object JavaDoc pValue) throws SAXException JavaDoc {
161                     marshalComplexValue(oSG, pMethod, pValue);
162                 }
163             };
164             pParticle.getPropertySG().forAllNonNullValues(jm, castedElement, sgLet);
165         }
166     }
167
168     JavaMethod getMethod() { return jm; }
169 }
Popular Tags