KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

46     public ChoiceHandlerSG(ComplexTypeSG pType, JavaSource pJs)
47             throws SAXException JavaDoc {
48         super(pType, pJs);
49     }
50
51     ChoiceHandlerSG(GroupHandlerSG pOuterHandler, ComplexTypeSG pType,
52                     GroupSG pGroup, JavaSource pJs)
53             throws SAXException JavaDoc {
54         super(pOuterHandler, pType, pGroup, pJs);
55     }
56
57     protected int getState(int pParticleNum) { return pParticleNum; }
58
59     protected void acceptParticle(JavaMethod pJm, int pNum) throws SAXException JavaDoc {
60         pJm.addIf(getStateField());
61         ParticleSG particle = particles[pNum];
62         pJm.addIf(getChildNumField(), " != " + pNum);
63         pJm.addLine("getHandler().validationEvent(", ValidationEvent.class, ".WARNING, ",
64                     JavaSource.getQuoted("Multiple different particles present in a choive group."),
65                     ", ", ValidationEvents.class, ".EVENT_CHOICE_GROUP_REUSE, null);");
66         if (!particles[pNum].isMultiple()) {
67             pJm.addElse();
68             if (particle.isElement()) {
69                 pJm.addLine("getHandler().validationEvent(", ValidationEvent.class, ".WARNING, ",
70                             JavaSource.getQuoted("The element " + particle.getObjectSG().getName() +
71                                                  " has already been defined."),
72                             ", ", ValidationEvents.class, ".EVENT_CHOICE_GROUP_REUSE, null);");
73             } else if (particle.isGroup()) {
74                 pJm.addLine("getHandler().validationEvent(", ValidationEvent.class, ".WARNING, ",
75                             JavaSource.getQuoted("The group " + GroupUtil.getGroupName(particle.getGroupSG()) +
76                                                  " has already been defined."),
77                             ", ", ValidationEvents.class, ".EVENT_CHOICE_GROUP_REUSE, null);");
78             } else if (particle.isWildcard()) {
79                 throw new IllegalStateException JavaDoc("TODO: Add support for wildcards.");
80             } else {
81                 throw new IllegalStateException JavaDoc("Invalid particle type");
82             }
83         }
84         pJm.addEndIf();
85         pJm.addEndIf();
86         pJm.addLine(getStateField(), " = true;");
87         pJm.addLine(getChildNumField(), " = " + pNum + ";");
88     }
89
90     public JavaMethod newStartElementMethod() throws SAXException JavaDoc {
91         JavaMethod result = super.newStartElementMethod();
92         LocalJavaField unmarshallerHandler = result.newJavaField(JMUnmarshallerHandlerImpl.class);
93         unmarshallerHandler.addLine("getHandler()");
94         handleStartElementStates(unmarshallerHandler, result, 0, particles.length-1);
95         result.addLine("return false;");
96         return result;
97     }
98
99     private JavaField getChildNumField() {
100         if (childNumField == null) {
101             childNumField = getJavaSource().newJavaField("__childNum", int.class, JavaSource.PRIVATE);
102             JavaComment jc = childNumField.newComment();
103             jc.addLine("Index of the particle being currently parsed");
104         }
105         return childNumField;
106     }
107
108     protected DirectAccessible getEndElementState() throws SAXException JavaDoc {
109         return getChildNumField();
110     }
111
112     public JavaMethod newIsFinishedMethod() throws SAXException JavaDoc {
113         JavaMethod result = super.newIsFinishedMethod();
114         Object JavaDoc o = getStateField();
115         for (int i = 0; i < particles.length; i++) {
116             if (!isRequiredParticle(particles[i])) {
117                 o = "true";
118                 break;
119             }
120         }
121         result.addLine("return ", o, ";");
122         return result;
123     }
124
125     protected JavaField newStateField() throws SAXException JavaDoc {
126         JavaField jf = getJavaSource().newJavaField("__state", boolean.class, JavaSource.PRIVATE);
127         JavaComment jc = jf.newComment();
128         jc.addLine("Will be set to true, if the first child is parsed.");
129         jc.addLine("It is an error, if another child is parsed, and the");
130         jc.addLine("fields value is true.");
131         return jf;
132     }
133 }
134
Popular Tags