KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2003, 2004 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;
17
18 import org.apache.ws.jaxme.generator.sg.Context;
19 import org.apache.ws.jaxme.generator.sg.GroupSG;
20 import org.apache.ws.jaxme.generator.sg.ObjectSG;
21 import org.apache.ws.jaxme.generator.sg.ParticleSG;
22 import org.apache.ws.jaxme.generator.sg.ParticleSGChain;
23 import org.apache.ws.jaxme.generator.sg.PropertySG;
24 import org.apache.ws.jaxme.generator.sg.PropertySGChain;
25 import org.apache.ws.jaxme.generator.sg.SGFactory;
26 import org.apache.ws.jaxme.generator.sg.SGlet;
27 import org.apache.ws.jaxme.generator.sg.TypeSG;
28 import org.apache.ws.jaxme.js.DirectAccessible;
29 import org.apache.ws.jaxme.js.JavaMethod;
30 import org.apache.ws.jaxme.xs.XSAny;
31 import org.apache.ws.jaxme.xs.XSElement;
32 import org.apache.ws.jaxme.xs.XSParticle;
33 import org.xml.sax.Locator JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35
36
37 /**
38  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
39  */

40 public class JAXBParticleSG implements ParticleSGChain {
41     private final int minOccurs, maxOccurs;
42     private final XSParticle.Type type;
43     private PropertySG propertySG;
44     private final GroupSG groupSG;
45     private final ObjectSG objectSG;
46     private final Locator JavaDoc locator;
47     private XSElement element;
48     private XSAny wildcard;
49
50     /** <p>Creates a new instance of JAXBParticleSG.java.</p>
51      */

52     public JAXBParticleSG(SGFactory pFactory, XSParticle pParticle,
53                           Context pClassContext) throws SAXException JavaDoc {
54         minOccurs = pParticle.getMinOccurs();
55         maxOccurs = pParticle.getMaxOccurs();
56         type = pParticle.getType();
57         if (pParticle.isGroup()) {
58             groupSG = pFactory.getGroupSG(pParticle.getGroup(), pClassContext);
59             objectSG = null;
60         } else if (pParticle.isElement()) {
61             element = pParticle.getElement();
62             if (element.isGlobal()) {
63                 objectSG = pFactory.getObjectSG(element);
64             } else {
65                 objectSG = pFactory.getObjectSG(element, pClassContext);
66             }
67             groupSG = null;
68         } else if (pParticle.isWildcard()) {
69             objectSG = pFactory.getObjectSG(pParticle.getWildcard(), pClassContext);
70             groupSG = null;
71             wildcard = pParticle.getWildcard();
72         } else {
73             throw new IllegalStateException JavaDoc("Particle is neither group, nor element, or wildcard.");
74         }
75         locator = pParticle.getLocator();
76     }
77
78     public Object JavaDoc newPropertySGChain(ParticleSG pController) throws SAXException JavaDoc {
79         PropertySGChain result;
80         if (element != null) {
81             result = new JAXBPropertySG(objectSG, element);
82             element = null;
83         } else if (wildcard != null) {
84             result = new AnyElementPropertySG(objectSG, wildcard);
85             wildcard = null;
86         } else {
87             throw new IllegalStateException JavaDoc("A new PropertySGChain cannot be obtained.");
88         }
89         if (maxOccurs > 1 || maxOccurs == -1) {
90             // Dirty trick: We do not yet have the PropertySG available,
91
// so we fake one.
92
PropertySG pSG = new PropertySGImpl(result);
93             if ("indexed".equals(pSG.getCollectionType())) {
94                 result = new ArrayPropertySG(result, objectSG, minOccurs, maxOccurs);
95             } else {
96                 result = new MultiplePropertySG(result, objectSG, minOccurs, maxOccurs);
97             }
98         }
99         return result;
100     }
101
102     public void init(ParticleSG pController) throws SAXException JavaDoc {
103     }
104
105     public Locator JavaDoc getLocator(ParticleSG pController) { return locator; }
106     public int getMinOccurs(ParticleSG pController) { return minOccurs; }
107     public int getMaxOccurs(ParticleSG pController) { return maxOccurs; }
108     public boolean isMultiple(ParticleSG pController) { return maxOccurs == -1 || maxOccurs > 1; }
109     public boolean isGroup(ParticleSG pController) { return type.equals(XSParticle.GROUP); }
110     public boolean isElement(ParticleSG pController) { return type.equals(XSParticle.ELEMENT); }
111     public boolean isWildcard(ParticleSG pController) { return type.equals(XSParticle.WILDCARD); }
112
113     public PropertySG getPropertySG(ParticleSG pController) throws SAXException JavaDoc {
114         if (propertySG == null) {
115             if (element != null || wildcard != null) {
116                 PropertySGChain chain = (PropertySGChain) pController.newPropertySGChain();
117                 propertySG = new PropertySGImpl(chain);
118                 propertySG.init();
119             } else {
120                 throw new IllegalStateException JavaDoc("This particle has no PropertySG.");
121             }
122         }
123         return propertySG;
124     }
125
126     public ObjectSG getObjectSG(ParticleSG pController) {
127         if (objectSG == null) {
128             throw new IllegalStateException JavaDoc("This particle is neither an element nor a wildcard.");
129         }
130         return objectSG;
131     }
132
133     public GroupSG getGroupSG(ParticleSG pController) {
134         if (groupSG == null) {
135             throw new IllegalStateException JavaDoc("This particle is no group.");
136         }
137         return groupSG;
138     }
139
140     public void forAllNonNullValues(ParticleSG pController, JavaMethod pMethod,
141                                     DirectAccessible pElement, SGlet pSGlet) throws SAXException JavaDoc {
142         if (pController.isElement()) {
143             PropertySG pSG = pController.getPropertySG();
144             boolean hasIsSetMethod = pSG.hasIsSetMethod();
145             if (hasIsSetMethod) {
146                 pMethod.addIf(pSG.getXMLIsSetMethodName(), "()");
147             }
148             TypeSG typeSG = pController.getObjectSG().getTypeSG();
149             Object JavaDoc v = pController.getPropertySG().getValue(pElement);
150             if (typeSG.isComplex()) {
151                 pSGlet.generate(pMethod, v);
152             } else {
153                 typeSG.getSimpleTypeSG().forAllValues(pMethod, v, pSGlet);
154             }
155             if (hasIsSetMethod) {
156                 pMethod.addEndIf();
157             }
158         } else {
159             // TODO: Implement support for wildcards and subgroups
160
throw new IllegalStateException JavaDoc("Wildcards and subgroups are not yet supported.");
161         }
162     }
163 }
164
Popular Tags