KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2005 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.ccsg;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import javax.xml.bind.ValidationEvent;
22
23 import org.apache.ws.jaxme.ValidationEvents;
24 import org.apache.ws.jaxme.generator.sg.ComplexTypeSG;
25 import org.apache.ws.jaxme.generator.sg.GroupSG;
26 import org.apache.ws.jaxme.generator.sg.ParticleSG;
27 import org.apache.ws.jaxme.impl.JMUnmarshallerHandlerImpl;
28 import org.apache.ws.jaxme.js.DirectAccessible;
29 import org.apache.ws.jaxme.js.JavaComment;
30 import org.apache.ws.jaxme.js.JavaField;
31 import org.apache.ws.jaxme.js.JavaMethod;
32 import org.apache.ws.jaxme.js.JavaSource;
33 import org.apache.ws.jaxme.js.LocalJavaField;
34 import org.xml.sax.SAXException JavaDoc;
35
36
37 /** Creates an instance of
38  * {@link org.apache.ws.jaxme.impl.JMSAXElementParser},
39  * or {@link org.apache.ws.jaxme.impl.JMSAXGroupParser},
40  * which parses an "all" group.
41  */

42 public class AllHandlerSG extends GroupHandlerSG {
43     private JavaField childNumField;
44
45     /** Creates a new instance, which generates a handler for
46      * the complex type <code>pTypeSG</code> by adding methods
47      * and fields to the Java class <code>pJs</code>.
48      */

49     public AllHandlerSG(ComplexTypeSG pType, JavaSource pJs)
50             throws SAXException JavaDoc {
51         super(pType, pJs);
52     }
53
54     AllHandlerSG(GroupHandlerSG pOuterHandler, ComplexTypeSG pType,
55                  GroupSG pGroup, JavaSource pJs)
56             throws SAXException JavaDoc {
57         super(pOuterHandler, pType, pGroup, pJs);
58     }
59
60     protected int getState(int pParticleNum) { return pParticleNum; }
61
62     private JavaField getChildNumField() {
63         if (childNumField == null) {
64             childNumField = getJavaSource().newJavaField("__childNum", int.class, JavaSource.PRIVATE);
65             JavaComment jc = childNumField.newComment();
66             jc.addLine("Index of the particle being currently parsed");
67         }
68         return childNumField;
69     }
70
71     protected DirectAccessible getEndElementState() throws SAXException JavaDoc {
72         return getChildNumField();
73     }
74
75     protected void acceptParticle(JavaMethod pJm, int pNum) throws SAXException JavaDoc {
76         pJm.addIf(getStateField(), "[" + pNum, "]");
77         ParticleSG particle = particles[pNum];
78         if (particle.isElement()) {
79             pJm.addLine("getHandler().validationEvent(", ValidationEvent.class, ".WARNING, ",
80                         JavaSource.getQuoted("The element " + particle.getObjectSG().getName() +
81                                              " has already been defined."),
82                         ", ", ValidationEvents.class, ".EVENT_ALL_GROUP_REUSE, null);");
83         } else if (particle.isGroup()) {
84             pJm.addLine("getHandler().validationEvent(", ValidationEvent.class, ".WARNING, ",
85                         JavaSource.getQuoted("The group " + GroupUtil.getGroupName(particle.getGroupSG()) +
86                                              " has already been defined."),
87                         ", ", ValidationEvents.class, ".EVENT_ALL_GROUP_REUSE, null);");
88         } else if (particle.isWildcard()) {
89             throw new IllegalStateException JavaDoc("TODO: Add support for wildcards.");
90         } else {
91             throw new IllegalStateException JavaDoc("Invalid particle type");
92         }
93         pJm.addEndIf();
94         pJm.addLine(getChildNumField(), " = " + pNum + ";");
95         pJm.addLine(getStateField(), "[" + pNum, "] = true;");
96     }
97
98     public JavaMethod newStartElementMethod() throws SAXException JavaDoc {
99         JavaMethod result = super.newStartElementMethod();
100         LocalJavaField unmarshallerHandler = result.newJavaField(JMUnmarshallerHandlerImpl.class);
101         unmarshallerHandler.addLine("getHandler()");
102         handleStartElementStates(unmarshallerHandler, result, 0, particles.length-1);
103         result.addLine("return false;");
104         return result;
105     }
106
107     public JavaMethod newIsFinishedMethod() throws SAXException JavaDoc {
108         JavaMethod result = super.newIsFinishedMethod();
109         List JavaDoc requiredParticles = new ArrayList JavaDoc();
110         for (int i = 0; i < particles.length; i++) {
111             if (isRequiredParticle(particles[i])) {
112                 if (requiredParticles.size() > 0) {
113                     requiredParticles.add(" && ");
114                 }
115                 requiredParticles.add(new Object JavaDoc[]{getStateField(),
116                                                    "[" + i + "]"});
117             }
118         }
119         if (requiredParticles.size() == 0) {
120             result.addLine("return true;");
121         } else {
122             result.addLine("return ", requiredParticles, ";");
123         }
124         return result;
125     }
126
127     protected JavaField newStateField() throws SAXException JavaDoc {
128         JavaField jf = getJavaSource().newJavaField("__state", boolean[].class, JavaSource.PRIVATE);
129         jf.addLine("new ", boolean.class, "[" + particles.length + "]");
130         JavaComment jc = jf.newComment();
131         jc.addLine("This array indicates the state of the group. For any");
132         jc.addLine("possible child, the corresponding boolean value is set,");
133         jc.addLine("if the child is parsed.");
134         jc.addLine("If the same child occurs again, and the childs boolean");
135         jc.addLine("value is true, then an exception is thrown.");
136         jc.addLine("These are the indices, to which the child elements are");
137         jc.addLine("mapped:");
138         for (int i = 0; i < particles.length; i++) {
139             ParticleSG particle = particles[i];
140             if (particle.isGroup()) {
141                 GroupSG group = particle.getGroupSG();
142                 if (group.isGlobal()) {
143                     jc.addLine(" " + i + " = The nested group " + group.getName());
144                 } else {
145                     jc.addLine(" " + i + " = An anonymous nested group.");
146                 }
147             } else if (particle.isElement()) {
148                 jc.addLine(" " + i + " = The child element " + particle.getObjectSG().getName());
149             } else if (particle.isWildcard()) {
150                 throw new IllegalStateException JavaDoc("TODO: Add support for wildcards.");
151             } else {
152                 throw new IllegalStateException JavaDoc("Invalid particle type.");
153             }
154         }
155         return jf;
156     }
157 }
Popular Tags