KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ws.jaxme.generator.sg.impl.ccsg;
2
3 import org.apache.ws.jaxme.generator.sg.ComplexContentSG;
4 import org.apache.ws.jaxme.generator.sg.ComplexTypeSG;
5 import org.apache.ws.jaxme.generator.sg.GroupSG;
6 import org.apache.ws.jaxme.generator.sg.ParticleSG;
7 import org.apache.ws.jaxme.generator.sg.TypeSG;
8 import org.xml.sax.SAXException JavaDoc;
9
10
11 /** A <code>ParticleWalker</code> walks over a complex
12  * elements particles, invoking the given
13  * {@link org.apache.ws.jaxme.generator.sg.impl.ccsg.ParticleVisitor Particle Visitors}
14  * methods for any particle.
15  */

16 public class ParticleWalker {
17     private final ParticleVisitor visitor;
18
19     /** Creates a new instance, walking over the given type.
20      */

21     public ParticleWalker(ParticleVisitor pVisitor) {
22         visitor = pVisitor;
23     }
24
25     /** Walks over the given types particles.
26      * @throws SAXException Walking over the particles failed.
27      */

28     public void walk(ComplexTypeSG pType) throws SAXException JavaDoc {
29         if (pType.hasSimpleContent()) {
30             visitor.simpleContent(pType);
31         } else {
32             ComplexContentSG ccSG = pType.getComplexContentSG();
33             if (ccSG.isEmpty()) {
34                 visitor.emptyType(pType);
35             } else {
36                 visitor.startComplexContent(pType);
37                 walkGroup(ccSG.getRootParticle().getGroupSG());
38                 visitor.endComplexContent(pType);
39             }
40         }
41     }
42
43     /** Walks over the group.
44      * @throws SAXException Walking over the group failed.
45      */

46     private void walkGroup(GroupSG pGroup) throws SAXException JavaDoc {
47         if (pGroup.isSequence()) {
48             visitor.startSequence(pGroup);
49             walkParticles(pGroup);
50             visitor.endSequence(pGroup);
51         } else if (pGroup.isChoice()) {
52             visitor.startChoice(pGroup);
53             walkParticles(pGroup);
54             visitor.endChoice(pGroup);
55         } else if (pGroup.isAll()) {
56             visitor.startAll(pGroup);
57             walkParticles(pGroup);
58             visitor.endAll(pGroup);
59         } else {
60             throw new IllegalStateException JavaDoc("Invalid group type");
61         }
62     }
63
64     /** Walks over the groups particles.
65      * @throws SAXException Walking over the groups particles failed.
66      */

67     private void walkParticles(GroupSG pGroup) throws SAXException JavaDoc {
68         ParticleSG[] particles = pGroup.getParticles();
69         for (int i = 0; i < particles.length; i++) {
70             ParticleSG particle = particles[i];
71             if (particle.isElement()) {
72                 TypeSG type = particle.getObjectSG().getTypeSG();
73                 if (type.isComplex()) {
74                     visitor.complexElementParticle(pGroup, particle);
75                 } else {
76                     visitor.simpleElementParticle(pGroup, particle);
77                 }
78             } else if (particle.isGroup()) {
79                 walkGroup(particle.getGroupSG());
80             } else if (particle.isWildcard()) {
81                 visitor.wildcardParticle(particle);
82             } else {
83                 throw new IllegalStateException JavaDoc("Invalid particle type");
84             }
85         }
86     }
87 }
88
Popular Tags