KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
20
21 import org.apache.ws.jaxme.generator.sg.AttributeSG;
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.SchemaSG;
27 import org.apache.ws.jaxme.generator.sg.SimpleTypeSG;
28 import org.apache.ws.jaxme.generator.sg.TypeSG;
29 import org.apache.ws.jaxme.generator.util.JavaNamer;
30 import org.apache.ws.jaxme.js.DirectAccessible;
31 import org.apache.ws.jaxme.js.JavaField;
32 import org.apache.ws.jaxme.js.JavaMethod;
33 import org.apache.ws.jaxme.js.JavaQName;
34 import org.apache.ws.jaxme.js.JavaQNameImpl;
35 import org.apache.ws.jaxme.js.JavaSource;
36 import org.apache.ws.jaxme.js.LocalJavaField;
37 import org.apache.ws.jaxme.js.TypedValue;
38 import org.apache.ws.jaxme.xs.XSAttribute;
39 import org.apache.ws.jaxme.xs.XSElement;
40 import org.apache.ws.jaxme.xs.XSObject;
41 import org.apache.ws.jaxme.xs.XSType;
42 import org.apache.ws.jaxme.xs.jaxb.JAXBProperty;
43 import org.apache.ws.jaxme.xs.jaxb.JAXBPropertyOwner;
44 import org.xml.sax.SAXException JavaDoc;
45
46 /**
47  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
48  * @author <a HREF="mailto:iasandcb@tmax.co.kr">Ias</a>
49  */

50 public class JAXBPropertySG implements PropertySGChain {
51     private final String JavaDoc propertyName;
52     private final String JavaDoc fieldName;
53     private final String JavaDoc collectionType;
54     private final boolean generateIsSetMethod;
55     private final String JavaDoc defaultValue;
56     private final TypeSG typeSG;
57
58     protected JAXBPropertySG(String JavaDoc pDefaultPropertyName, SchemaSG pSchema, XSObject pXSObject,
59                              String JavaDoc pDefaultValue, TypeSG pTypeSG) {
60         typeSG = pTypeSG;
61         defaultValue = pDefaultValue;
62         String JavaDoc myPropertyName = null;
63         boolean myGeneratedIsSetMethod = pSchema.isGeneratingIsSetMethod();
64         String JavaDoc myCollectionType = null;
65         if (pXSObject instanceof JAXBPropertyOwner) {
66             JAXBPropertyOwner jaxbPropertyOwner = (JAXBPropertyOwner) pXSObject;
67             JAXBProperty jaxbProperty = jaxbPropertyOwner.getJAXBProperty();
68             if (jaxbProperty != null) {
69                 myPropertyName = jaxbProperty.getName();
70                 myCollectionType = jaxbProperty.getCollectionType();
71                 Boolean JavaDoc jaxbGeneratedIsSetMethod = jaxbProperty.isGenerateIsSetMethod();
72                 if (jaxbGeneratedIsSetMethod != null) {
73                     myGeneratedIsSetMethod = jaxbGeneratedIsSetMethod.booleanValue();
74                 }
75             }
76         }
77
78         collectionType = myCollectionType == null ? pSchema.getCollectionType() : myCollectionType;
79         if (myPropertyName == null) {
80             myPropertyName = JavaNamer.convert(pDefaultPropertyName, pSchema);
81             myPropertyName = Character.toLowerCase(myPropertyName.charAt(0)) + myPropertyName.substring(1);
82         }
83         propertyName = myPropertyName;
84         fieldName = "_" + propertyName;
85         generateIsSetMethod = myGeneratedIsSetMethod;
86     }
87
88     protected JAXBPropertySG(AttributeSG pAttribute, XSAttribute pXSAttribute) {
89         this(pAttribute.getName().getLocalName(), pAttribute.getSchema(), pXSAttribute, pXSAttribute.getDefault(),
90              pAttribute.getTypeSG());
91     }
92     
93     protected JAXBPropertySG(ObjectSG pElement, XSElement pXSElement) {
94         this(pElement.getName().getLocalName(), pElement.getSchema(), pXSElement, pXSElement.getDefault(),
95              pElement.getTypeSG());
96     }
97     
98     protected JAXBPropertySG(TypeSG pComplexType, XSType pType) throws SAXException JavaDoc {
99         this("value", pComplexType.getSchema(), pType, null,
100              pComplexType.getComplexTypeSG().getSimpleContentSG().getContentTypeSG());
101     }
102
103     public void init(PropertySG pController) throws SAXException JavaDoc {}
104     
105     public boolean hasIsSetMethod(PropertySG pController) { return generateIsSetMethod; }
106     public String JavaDoc getCollectionType(PropertySG pController) { return collectionType; }
107     
108     public String JavaDoc getXMLFieldName(PropertySG pController) throws SAXException JavaDoc {
109         return fieldName;
110     }
111
112     public String JavaDoc getPropertyName(PropertySG pController) throws SAXException JavaDoc {
113         return propertyName;
114     }
115
116     public String JavaDoc getXMLGetMethodName(PropertySG pController) throws SAXException JavaDoc {
117         String JavaDoc prefix;
118         if (typeSG != null && !typeSG.isComplex() &&
119             typeSG.getSimpleTypeSG().getRuntimeType().equals(JavaQNameImpl.BOOLEAN)) {
120             prefix = "is";
121         } else {
122             prefix = "get";
123         }
124         String JavaDoc propName = pController.getPropertyName();
125         String JavaDoc methodName = prefix + Character.toUpperCase(propName.charAt(0)) + propName.substring(1);
126         if (methodName.equals("getClass")) {
127             throw new SAXException JavaDoc("Method name getClass() conflicts with java.lang.Object.getClass(), use jaxb:property to customize the property name.");
128         }
129         return methodName;
130     }
131     
132     public String JavaDoc getXMLSetMethodName(PropertySG pController) throws SAXException JavaDoc {
133         String JavaDoc propName = pController.getPropertyName();
134         return "set" + Character.toUpperCase(propName.charAt(0)) + propName.substring(1);
135     }
136     
137     public String JavaDoc getXMLIsSetMethodName(PropertySG pController) throws SAXException JavaDoc {
138         String JavaDoc propName = pController.getPropertyName();
139         return hasIsSetMethod(pController) ?
140                                            "isSet" + Character.toUpperCase(propName.charAt(0)) + propName.substring(1) : null;
141     }
142
143     public JavaField getXMLField(PropertySG pController, JavaSource pSource) throws SAXException JavaDoc {
144         return typeSG.getXMLField(pSource, pController.getXMLFieldName(), defaultValue);
145     }
146     
147     public JavaMethod getXMLGetMethod(PropertySG pController, JavaSource pSource) throws SAXException JavaDoc {
148         return typeSG.getXMLGetMethod(pSource, pController.getXMLFieldName(),
149                                       pController.getXMLGetMethodName());
150     }
151     
152     public JavaMethod getXMLSetMethod(PropertySG pController, JavaSource pSource) throws SAXException JavaDoc {
153         String JavaDoc propName = pController.getPropertyName();
154         String JavaDoc pName = "p" + Character.toUpperCase(propName.charAt(0)) + propName.substring(1);
155         return typeSG.getXMLSetMethod(pSource, pController.getXMLFieldName(),
156                                       pName, pController.getXMLSetMethodName(),
157                                       pController.hasIsSetMethod());
158     }
159     
160     public JavaMethod getXMLIsSetMethod(PropertySG pController, JavaSource pSource) throws SAXException JavaDoc {
161         return typeSG.getXMLIsSetMethod(pSource, pController.getXMLFieldName(),
162                 pController.getXMLIsSetMethodName());
163     }
164     
165     public void forAllNonNullValues(PropertySG pController, JavaMethod pMethod,
166                                     DirectAccessible pElement, SGlet pSGlet) throws SAXException JavaDoc {
167         boolean hasIsSetMethod = pController.hasIsSetMethod();
168         if (hasIsSetMethod) {
169             if (pElement == null) {
170                 pMethod.addIf(pController.getXMLIsSetMethodName(), "()");
171             } else {
172                 pMethod.addIf(pElement, ".", pController.getXMLIsSetMethodName(), "()");
173             }
174             if (typeSG.isComplex()) {
175                 pSGlet.generate(pMethod, pController.getValue(pElement));
176             } else {
177                 typeSG.getSimpleTypeSG().forAllValues(pMethod, pController.getValue(pElement), pSGlet);
178             }
179             pMethod.addEndIf();
180         } else {
181             if (typeSG.isComplex()) {
182                 LocalJavaField f = pMethod.newJavaField(typeSG.getComplexTypeSG().getClassContext().getXMLInterfaceName());
183                 f.addLine(pController.getValue(pElement));
184                 pMethod.addIf(f, " != null");
185                 pSGlet.generate(pMethod, pController.getValue(pElement));
186                 pMethod.addEndIf();
187             } else {
188                 typeSG.getSimpleTypeSG().forAllNonNullValues(pMethod, pController.getValue(pElement), pSGlet);
189             }
190         }
191     }
192     public void forAllValues(PropertySG pController, JavaMethod pMethod,
193                              DirectAccessible pElement, SGlet pSGlet) throws SAXException JavaDoc {
194         SimpleTypeSG simpleTypeSG = typeSG.getSimpleTypeSG();
195         simpleTypeSG.forAllValues(pMethod, pController.getValue(pElement), pSGlet);
196     }
197     
198     public Object JavaDoc getValue(PropertySG pController, DirectAccessible pElement) throws SAXException JavaDoc {
199         if (pElement == null) {
200             return new Object JavaDoc[]{pController.getXMLGetMethodName(), "()"};
201         } else {
202             return new Object JavaDoc[]{pElement, ".", pController.getXMLGetMethodName(), "()"};
203         }
204     }
205
206     public void generate(PropertySG pController, JavaSource pSource) throws SAXException JavaDoc {
207         if (typeSG != null && !typeSG.isGlobalType() && !typeSG.isGlobalClass() && pSource.isInterface()) {
208             typeSG.generate(pSource);
209         }
210         pController.getXMLField(pSource);
211         pController.getXMLGetMethod(pSource);
212         pController.getXMLSetMethod(pSource);
213         if (hasIsSetMethod(pController)) {
214             pController.getXMLIsSetMethod(pSource);
215         }
216     }
217     
218     public void setValue(PropertySG pController, JavaMethod pMethod,
219                          DirectAccessible pElement, Object JavaDoc pValue, JavaQName pType)
220             throws SAXException JavaDoc {
221         if (pType != null) {
222             pValue = new Object JavaDoc[]{"((", pType, ") ", pValue, ")"};
223         }
224         if (typeSG.isComplex() || !typeSG.getSimpleTypeSG().isList()) {
225             if (pElement == null) {
226                 pMethod.addLine(pController.getXMLSetMethodName(), "(", pValue, ");");
227             } else {
228                 pMethod.addLine(pElement, ".", pController.getXMLSetMethodName(), "(", pValue, ");");
229             }
230         } else {
231             LocalJavaField list = pMethod.newJavaField(List JavaDoc.class);
232             list.addLine(pController.getValue(pElement));
233             pMethod.addLine(list, ".clear();");
234             pMethod.addLine(list, ".addAll(", pValue, ");");
235         }
236     }
237     
238     public void addValue(PropertySG pController, JavaMethod pMethod, DirectAccessible pElement, TypedValue pValue, JavaQName pType) throws SAXException JavaDoc {
239         pController.setValue(pMethod, pElement, pValue, pType);
240     }
241 }
242
Popular Tags