KickJava   Java API By Example, From Geeks To Geeks.

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


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  */

17 package org.apache.ws.jaxme.generator.sg.impl;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.apache.ws.jaxme.generator.sg.ObjectSG;
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.SGlet;
26 import org.apache.ws.jaxme.generator.sg.TypeSG;
27 import org.apache.ws.jaxme.js.DirectAccessible;
28 import org.apache.ws.jaxme.js.JavaField;
29 import org.apache.ws.jaxme.js.JavaMethod;
30 import org.apache.ws.jaxme.js.JavaQName;
31 import org.apache.ws.jaxme.js.JavaQNameImpl;
32 import org.apache.ws.jaxme.js.JavaSource;
33 import org.apache.ws.jaxme.js.LocalJavaField;
34 import org.apache.ws.jaxme.js.TypedValue;
35 import org.xml.sax.SAXException JavaDoc;
36
37
38 /** <Implementation of a an objectSG for elements with multiplicity > 1.</p>
39  *
40  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
41  */

42 public class MultiplePropertySG extends PropertySGChainImpl {
43     protected static final JavaQName OBJECT_TYPE = JavaQNameImpl.getInstance(Object JavaDoc.class);
44     private final int minOccurs, maxOccurs;
45     private final ObjectSG objectSG;
46     
47     /** <p>Creates a new instance of MultipleObjectSG.</p>
48      */

49     protected MultiplePropertySG(PropertySGChain pBase, ObjectSG pObjectSG, int pMinOccurs, int pMaxOccurs) {
50         super(pBase);
51         objectSG = pObjectSG;
52         maxOccurs = pMaxOccurs;
53         minOccurs = pMinOccurs;
54     }
55     
56     protected int getMinOccurs() { return minOccurs; }
57     protected int getMaxOccurs() { return maxOccurs; }
58     protected ObjectSG getObjectSG() { return objectSG; }
59     protected JavaQName getInstanceClass() throws SAXException JavaDoc {
60         return getObjectSG().getTypeSG().getRuntimeType();
61     }
62     protected boolean isAutoBoxing() throws SAXException JavaDoc {
63         return getInstanceClass().isPrimitive();
64     }
65     protected JavaQName getObjectClass() throws SAXException JavaDoc {
66         return getInstanceClass().getObjectType();
67     }
68     protected Object JavaDoc asObject(Object JavaDoc pObject) throws SAXException JavaDoc {
69         if (isAutoBoxing()) {
70             return new Object JavaDoc[]{"new ", getObjectClass(), "(", pObject, ")"};
71         } else {
72             return pObject;
73         }
74     }
75
76     public JavaField getXMLField(PropertySG pController, JavaSource pSource) throws SAXException JavaDoc {
77         String JavaDoc fieldName = pController.getXMLFieldName();
78         JavaField result = pSource.newJavaField(fieldName, List JavaDoc.class, JavaSource.PRIVATE);
79         result.addLine("new ", ArrayList JavaDoc.class, "()");
80         return result;
81     }
82     
83     public JavaMethod getXMLSetMethod(PropertySG pController, JavaSource pSource) throws SAXException JavaDoc {
84         return null;
85     }
86     
87     public JavaMethod getXMLGetMethod(PropertySG pController, JavaSource pSource) throws SAXException JavaDoc {
88         String JavaDoc fieldName = pController.getXMLFieldName();
89         String JavaDoc methodName = pController.getXMLGetMethodName();
90         JavaMethod result = pSource.newJavaMethod(methodName, List JavaDoc.class, JavaSource.PUBLIC);
91         result.addLine("return ", fieldName, ";");
92         return result;
93     }
94     
95     public void forAllValues(PropertySG pController, JavaMethod pMethod, DirectAccessible pElement, SGlet pSGlet) throws SAXException JavaDoc {
96         LocalJavaField list = pMethod.newJavaField(List JavaDoc.class);
97         list.addLine(pController.getValue(pElement));
98         DirectAccessible i = pMethod.addForList(list);
99         TypeSG typeSG = objectSG.getTypeSG();
100         Object JavaDoc v;
101         boolean isCasting = !OBJECT_TYPE.equals(typeSG.getRuntimeType());
102         if (isCasting && pSGlet instanceof SGlet.TypedSGlet) {
103             isCasting = ((SGlet.TypedSGlet) pSGlet).isCasting();
104         }
105         if (isCasting) {
106             v = new Object JavaDoc[]{"(", typeSG.getRuntimeType(), ")", list, ".get(", i, ")"};
107         } else {
108             v = new Object JavaDoc[]{list, ".get(", i, ")"};
109         }
110         if (typeSG.isComplex()) {
111             pSGlet.generate(pMethod, v);
112         } else {
113             typeSG.getSimpleTypeSG().forAllValues(pMethod, v, pSGlet);
114         }
115         pMethod.addEndFor();
116     }
117     
118     public void forAllNonNullValues(PropertySG pController, JavaMethod pMethod, DirectAccessible pElement, SGlet pSGlet) throws SAXException JavaDoc {
119         LocalJavaField list = pMethod.newJavaField(List JavaDoc.class);
120         list.addLine(pController.getValue(pElement));
121         DirectAccessible i = pMethod.addForList(list);
122         TypeSG typeSG = objectSG.getTypeSG();
123         boolean isCasting = !OBJECT_TYPE.equals(typeSG.getRuntimeType());
124         JavaQName qName = typeSG.getRuntimeType();
125         if (isCasting && pSGlet instanceof SGlet.TypedSGlet) {
126             SGlet.TypedSGlet typedSGlet = (SGlet.TypedSGlet) pSGlet;
127             isCasting = typedSGlet.isCasting();
128             if (typedSGlet.getType() != null) {
129                 qName = typedSGlet.getType();
130             }
131         }
132         Object JavaDoc v = new Object JavaDoc[]{list, ".get(", i, ")"};
133         if (isCasting) {
134             if (qName.isPrimitive()) {
135                 v = new Object JavaDoc[]{"((", qName.getObjectType(), ")", v, ").", qName.getPrimitiveConversionMethod(), "()"};
136             } else {
137                 v = new Object JavaDoc[]{"(", qName, ")", v};
138             }
139         }
140         if (typeSG.isComplex()) {
141             pSGlet.generate(pMethod, v);
142         } else {
143             typeSG.getSimpleTypeSG().forAllNonNullValues(pMethod, v, pSGlet);
144         }
145         pMethod.addEndFor();
146     }
147     
148     public void setValue(PropertySG pController, JavaMethod pMethod, DirectAccessible pElement, Object JavaDoc pValue, JavaQName pType) throws SAXException JavaDoc {
149         if (pType != null) {
150             pValue = new Object JavaDoc[]{"(", pType, ") ", pValue};
151         }
152         LocalJavaField list = pMethod.newJavaField(List JavaDoc.class);
153         list.addLine(pController.getValue(pElement));
154         pMethod.addLine(list, ".clear();");
155         pMethod.addLine(list, ".addAll(", pValue, ");");
156     }
157     
158     public void addValue(PropertySG pController, JavaMethod pMethod, DirectAccessible pElement, TypedValue pValue, JavaQName pType) throws SAXException JavaDoc {
159         pMethod.addLine(pController.getValue(pElement), ".add(", asObject(pValue), ");");
160     }
161 }
162
Popular Tags