KickJava   Java API By Example, From Geeks To Geeks.

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


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

36 public class SequenceHandlerSG extends GroupHandlerSG {
37     /** Creates a new instance, which generates a handler for
38      * the complex type <code>pTypeSG</code> by adding methods
39      * and fields to the Java class <code>pJs</code>.
40      */

41     public SequenceHandlerSG(ComplexTypeSG pTypeSG, JavaSource pJs)
42             throws SAXException JavaDoc {
43         super(pTypeSG, pJs);
44     }
45
46     SequenceHandlerSG(GroupHandlerSG pOuterHandler, ComplexTypeSG pTypeSG,
47                       GroupSG pGroup, JavaSource pJs)
48             throws SAXException JavaDoc {
49         super(pOuterHandler, pTypeSG, pGroup, pJs);
50     }
51
52     protected DirectAccessible getEndElementState() throws SAXException JavaDoc {
53         return getStateField();
54     }
55
56     protected int getState(int pNum) {
57         return pNum+1;
58     }
59
60     private int fromState(int pState) {
61         return pState-1;
62     }
63
64     protected JavaField newStateField() throws SAXException JavaDoc {
65         JavaField jf = getJavaSource().newJavaField("__state", int.class, JavaSource.PRIVATE);
66         JavaComment jc = jf.newComment();
67         jc.addLine("The current state. The following values are valid states:");
68         jc.addLine(" 0 = Before parsing the element");
69         for (int i = 0; i < particles.length; i++) {
70             ParticleSG particle = particles[i];
71             if (particle.isGroup()) {
72                 GroupSG group = particle.getGroupSG();
73                 if (group.isGlobal()) {
74                     jc.addLine(" " + getState(i) + " = While parsing the nested group " + group.getName());
75                 } else {
76                     jc.addLine(" " + getState(i) + " = While parsing the nested group " + GroupUtil.getGroupName(group));
77                 }
78             } else if (particle.isElement()) {
79                 jc.addLine(" " + getState(i) + " = While or after parsing the child element " + particle.getObjectSG().getName());
80             } else if (particle.isWildcard()) {
81                 throw new IllegalStateException JavaDoc("TODO: Add support for wildcards.");
82             } else {
83                 throw new IllegalStateException JavaDoc("Invalid particle type.");
84             }
85         }
86         
87         return jf;
88     }
89
90
91     protected void acceptParticle(JavaMethod pJm, int pNum) throws SAXException JavaDoc {
92         pJm.addLine(getStateField(), " = " + getState(pNum) + ";");
93     }
94
95     public JavaMethod newStartElementMethod() throws SAXException JavaDoc {
96         JavaMethod result = super.newStartElementMethod();
97         LocalJavaField unmarshallerHandler = result.newJavaField(JMUnmarshallerHandlerImpl.class);
98         unmarshallerHandler.addLine("getHandler()");
99         result.addSwitch(getStateField());
100         result.addCase(new Integer JavaDoc(0));
101         handleStartElementStates(unmarshallerHandler,
102                                  result, getFirstValidParticle(0),
103                                  getLastValidParticle(0));
104         result.addBreak();
105         for (int i = 0; i < particles.length; i++) {
106             int state = getState(i);
107             result.addCase(new Integer JavaDoc(state));
108             handleStartElementStates(unmarshallerHandler,
109                                      result, getFirstValidParticle(state),
110                                      getLastValidParticle(state));
111             result.addBreak();
112         }
113         result.addDefault();
114         result.addThrowNew(IllegalStateException JavaDoc.class,
115                            JavaSource.getQuoted("Invalid state: "),
116                            " + ", getStateField());
117         result.addEndSwitch();
118         result.addLine("return false;");
119         return result;
120     }
121
122     /** Assuming, we are currently in state <code>pState</code>,
123      * returns the index of the first valid particle. Returns
124      * -1, if there is no valid particle.
125      */

126     private int getFirstValidParticle(int pState) {
127         if (pState == 0) {
128             return 0;
129         } else {
130             int i = fromState(pState);
131             ParticleSG particle = particles[i];
132             if (particle.isMultiple()) {
133                 return i;
134             } else if (i+1 < particles.length) {
135                 return i+1;
136             } else {
137                 return -1;
138             }
139         }
140     }
141
142     /** Assuming, we are currently in state <code>pState</code>,
143      * returns the index of the last valid particle. Returns
144      * -1, if there is no valid particle.
145      */

146     private int getLastValidParticle(int pState) {
147         int lastParticle;
148         if (pState == 0) {
149             lastParticle = 0;
150         } else {
151             lastParticle = fromState(pState)+1;
152             if (lastParticle >= particles.length) {
153                 return fromState(pState);
154             }
155         }
156         while (lastParticle < particles.length-1) {
157             ParticleSG particle = particles[lastParticle];
158             if (isRequiredParticle(particle)) {
159                 break;
160             } else {
161                 ++lastParticle;
162             }
163         }
164         return lastParticle;
165     }
166
167     public JavaMethod newIsFinishedMethod() throws SAXException JavaDoc {
168         JavaMethod result = super.newIsFinishedMethod();
169         result.addSwitch(getStateField());
170         boolean allOptional = true;
171         for (int i = particles.length-1; i >= 0; i--) {
172             ParticleSG particle = particles[i];
173             result.addCase(new Integer JavaDoc(getState(i)));
174             if (isRequiredParticle(particle)) {
175                 allOptional = false;
176                 break;
177             }
178         }
179         if (allOptional) {
180             result.addCase(new Integer JavaDoc(0));
181         }
182         result.addLine("return true;");
183         result.addDefault();
184         result.addLine("return false;");
185         result.addEndSwitch();
186         return result;
187     }
188 }
Popular Tags