KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > binding > model > ObjectAttributes


1 /*
2 Copyright (c) 2004-2005, Dennis M. Sosnoski
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8  * Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13  * Neither the name of JiBX nor the names of its contributors may be used
14    to endorse or promote products derived from this software without specific
15    prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */

28
29 package org.jibx.binding.model;
30
31 import org.jibx.binding.util.StringArray;
32
33 /**
34  * Model component for <i>object</i> attribute group in binding definition.
35  * TODO: Add "create" attribute to say whether object should be created or not
36  *
37  * @author Dennis M. Sosnoski
38  * @version 1.0
39  */

40  
41 public class ObjectAttributes extends AttributeBase
42 {
43     /** Enumeration of allowed attribute names */
44     public static final StringArray s_allowedAttributes =
45         new StringArray(new String JavaDoc[] { "factory", "marshaller", "post-set",
46         "pre-get", "pre-set", "unmarshaller" });
47     
48     //
49
// Constants and such related to code generation.
50

51     // recognized marshal hook method (pre-get) signatures.
52
private static final String JavaDoc[] MARSHAL_HOOK_SIGNATURES =
53     {
54         "(Lorg/jibx/runtime/IMarshallingContext;)V",
55         "(Ljava/lang/Object;)V",
56         "()V"
57     };
58     
59     // recognized factory hook method signatures.
60
private static final String JavaDoc[] FACTORY_HOOK_SIGNATURES =
61     {
62         "(Lorg/jibx/runtime/IUnmarshallingContext;)",
63         "(Ljava/lang/Object;)",
64         "()"
65     };
66     
67     // recognized unmarshal hook method (pre-set, post-set) signatures.
68
private static final String JavaDoc[] UNMARSHAL_HOOK_SIGNATURES =
69     {
70         "(Lorg/jibx/runtime/IUnmarshallingContext;)V",
71         "(Ljava/lang/Object;)V",
72         "()V"
73     };
74     
75     // marshaller/unmarshaller definitions
76
private static final String JavaDoc UNMARSHALLER_INTERFACE =
77         "org.jibx.runtime.IUnmarshaller";
78     private static final String JavaDoc MARSHALLER_INTERFACE =
79         "org.jibx.runtime.IMarshaller";
80     private static final String JavaDoc UNMARSHALLER_INTERFACETYPE =
81         "Lorg/jibx/runtime/IUnmarshaller;";
82     private static final String JavaDoc MARSHALLER_INTERFACETYPE =
83         "Lorg/jibx/runtime/IMarshaller;";
84     
85     //
86
// Instance data.
87

88     /** Factory method name (fully qualified, including package and class). */
89     private String JavaDoc m_factoryName;
90     
91     /** Pre-set method name. */
92     private String JavaDoc m_preSetName;
93     
94     /** Post-set method name. */
95     private String JavaDoc m_postSetName;
96     
97     /** Pre-get method name. */
98     private String JavaDoc m_preGetName;
99     
100     /** Object marshaller class name. */
101     private String JavaDoc m_marshallerName;
102     
103     /** Object unmarshaller class name. */
104     private String JavaDoc m_unmarshallerName;
105     
106     /** Factory method information. */
107     private IClassItem m_factoryItem;
108     
109     /** Pre-set method information. */
110     private IClassItem m_preSetItem;
111     
112     /** Post-set method information. */
113     private IClassItem m_postSetItem;
114     
115     /** Pre-get method information. */
116     private IClassItem m_preGetItem;
117     
118     /** Object marshaller class. */
119     private IClass m_marshallerClass;
120     
121     /** Object unmarshaller class. */
122     private IClass m_unmarshallerClass;
123     
124     /**
125      * Constructor.
126      *
127      * @param element owning element
128      */

129     public ObjectAttributes() {}
130     
131     /**
132      * Get factory method name.
133      *
134      * @return fully-qualified factory class and method name (or
135      * <code>null</code> if none)
136      */

137     public String JavaDoc getFactoryName() {
138         return m_factoryName;
139     }
140     
141     /**
142      * Get factory method information. This method is only usable after a
143      * call to {@link #validate}.
144      *
145      * @return factory method information (or <code>null</code> if none)
146      */

147     public IClassItem getFactory() {
148         return m_factoryItem;
149     }
150     
151     /**
152      * Set factory method name.
153      *
154      * @param name fully qualified class and method name for object factory
155      */

156     public void setFactoryName(String JavaDoc name) {
157         m_factoryName = name;
158     }
159     
160     /**
161      * Get pre-set method name.
162      *
163      * @return pre-set method name (or <code>null</code> if none)
164      */

165     public String JavaDoc getPresetName() {
166         return m_preSetName;
167     }
168     
169     /**
170      * Get pre-set method information. This method is only usable after a
171      * call to {@link #validate}.
172      *
173      * @return pre-set method information (or <code>null</code> if none)
174      */

175     public IClassItem getPreset() {
176         return m_preSetItem;
177     }
178     
179     /**
180      * Set pre-set method name.
181      *
182      * @param name member method name to be called before unmarshalling
183      */

184     public void setPresetName(String JavaDoc name) {
185         m_preSetName = name;
186     }
187     
188     /**
189      * Get post-set method name.
190      *
191      * @return post-set method name (or <code>null</code> if none)
192      */

193     public String JavaDoc getPostsetName() {
194         return m_postSetName;
195     }
196     
197     /**
198      * Get post-set method information. This method is only usable after a
199      * call to {@link #validate}.
200      *
201      * @return post-set method information (or <code>null</code> if none)
202      */

203     public IClassItem getPostset() {
204         return m_postSetItem;
205     }
206     
207     /**
208      * Set post-set method name.
209      *
210      * @param name member method name to be called after unmarshalling
211      */

212     public void setPostsetName(String JavaDoc name) {
213         m_postSetName = name;
214     }
215     
216     /**
217      * Get pre-get method name.
218      *
219      * @return pre-get method name (or <code>null</code> if none)
220      */

221     public String JavaDoc getPregetName() {
222         return m_preGetName;
223     }
224     
225     /**
226      * Get pre-get method information. This method is only usable after a
227      * call to {@link #validate}.
228      *
229      * @return pre-get method information (or <code>null</code> if none)
230      */

231     public IClassItem getPreget() {
232         return m_preGetItem;
233     }
234     
235     /**
236      * Set pre-get method name.
237      *
238      * @param name member method name to be called before marshalling
239      */

240     public void setPreget(String JavaDoc name) {
241         m_preGetName = name;
242     }
243     
244     /**
245      * Get marshaller class name.
246      *
247      * @return marshaller class name (or <code>null</code> if none)
248      */

249     public String JavaDoc getMarshallerName() {
250         return m_marshallerName;
251     }
252     
253     /**
254      * Get marshaller class information. This method is only usable after a
255      * call to {@link #validate}.
256      *
257      * @return class information for marshaller (or <code>null</code> if none)
258      */

259     public IClass getMarshaller() {
260         return m_marshallerClass;
261     }
262     
263     /**
264      * Set marshaller class name.
265      *
266      * @param name class name to be used for marshalling
267      */

268     public void setMarshallerName(String JavaDoc name) {
269         m_marshallerName = name;
270     }
271     
272     /**
273      * Get unmarshaller class name.
274      *
275      * @return unmarshaller class name (or <code>null</code> if none)
276      */

277     public String JavaDoc getUnmarshallerName() {
278         return m_unmarshallerName;
279     }
280     
281     /**
282      * Get unmarshaller class information. This method is only usable after a
283      * call to {@link #validate}.
284      *
285      * @return class information for unmarshaller (or <code>null</code> if none)
286      */

287     public IClass getUnmarshaller() {
288         return m_unmarshallerClass;
289     }
290     
291     /**
292      * Set unmarshaller class name.
293      *
294      * @param name class name to be used for unmarshalling
295      */

296     public void setUnmarshallerName(String JavaDoc name) {
297         m_unmarshallerName = name;
298     }
299     
300     /* (non-Javadoc)
301      * @see org.jibx.binding.model.AttributeBase#prevalidate(org.jibx.binding.model.ValidationContext)
302      */

303     public void prevalidate(ValidationContext vctx) {
304         
305         // first check for factory supplied
306
IClass iclas;
307         ElementBase element = vctx.getParentElement(0);
308         if (element instanceof StructureElementBase) {
309             iclas = ((StructureElementBase)element).getType();
310         } else if (element instanceof MappingElement) {
311             iclas = ((MappingElement)element).getHandledClass();
312         } else {
313             throw new IllegalStateException JavaDoc
314                 ("Unknown element for object attributes");
315         }
316         String JavaDoc type = iclas.getName();
317         if (m_factoryName != null && m_factoryItem == null) {
318             if (iclas == null) {
319                 vctx.addWarning
320                     ("No object for structure; factory attribute ignored");
321             } else {
322                 
323                 // find factory method and verify signature
324
m_factoryItem = ClassUtils.findStaticMethod(m_factoryName,
325                     FACTORY_HOOK_SIGNATURES, vctx);
326                 if (m_factoryItem == null) {
327                     vctx.addError("Static factory method " + m_factoryName +
328                         " not found");
329                 } else if (!m_factoryItem.getTypeName().equals(type)) {
330                     // TODO: check subclass here
331
vctx.addError("Static factory method " + m_factoryName +
332                         " return type is not " + type);
333                 }
334             }
335         }
336         
337         // handle pre-set, post-set, and pre-get methods
338
if (vctx.isInBinding()) {
339             if (m_preSetName != null) {
340                 if (iclas == null) {
341                     vctx.addWarning
342                         ("No object for structure; pre-set attribute ignored");
343                 } else {
344                     m_preSetItem = iclas.getMethod(m_preSetName,
345                         UNMARSHAL_HOOK_SIGNATURES);
346                     if (m_preSetItem == null) {
347                         vctx.addError("Nonstatic pre-set method " +
348                             m_preSetName + " not found");
349                     }
350                 }
351             }
352             if (m_postSetName != null) {
353                 if (iclas == null) {
354                     vctx.addWarning
355                         ("No object for structure; post-set attribute ignored");
356                 } else {
357                     m_postSetItem = iclas.getMethod(m_postSetName,
358                         UNMARSHAL_HOOK_SIGNATURES);
359                     if (m_postSetItem == null) {
360                         vctx.addError("Nonstatic post-set method " +
361                             m_postSetName + " not found");
362                     }
363                 }
364             }
365         } else {
366             if (iclas == null) {
367                 vctx.addWarning
368                     ("No object for structure; pre-get attribute ignored");
369             } else {
370                 if (m_preGetName != null) {
371                     m_preGetItem = iclas.getMethod(m_preGetName,
372                         MARSHAL_HOOK_SIGNATURES);
373                     if (m_preGetItem == null) {
374                         vctx.addError("Nonstatic pre-get method " +
375                             m_preGetName + " not found");
376                     }
377                 }
378             }
379         }
380         
381         // finish with marshaller and unmarshaller classes
382
NestingElementBase parent = vctx.getParentElement();
383         if (!vctx.isInBinding() && m_marshallerName != null) {
384             if (iclas == null) {
385                 vctx.addWarning
386                     ("No object for structure; marshaller attribute ignored");
387             } else {
388                 IClass mclas = vctx.getClassInfo(m_marshallerName);
389                 if (mclas == null) {
390                     vctx.addError("Marshaller class " + m_marshallerName +
391                         " not found");
392                 } else if (!mclas.isImplements(MARSHALLER_INTERFACETYPE)) {
393                     vctx.addError("Marshaller class " + m_marshallerName +
394                         " does not implement interface " +
395                         MARSHALLER_INTERFACE);
396                 } else {
397                     m_marshallerClass = mclas;
398                 }
399             }
400         }
401         if (vctx.isInBinding() && m_unmarshallerName != null) {
402             if (iclas == null) {
403                 vctx.addWarning
404                     ("No object for structure; unmarshaller attribute ignored");
405             } else {
406                 IClass uclas = vctx.getClassInfo(m_unmarshallerName);
407                 if (uclas == null) {
408                     vctx.addError("Unmarshaller class " + m_unmarshallerName +
409                         " not found");
410                 } else if (!uclas.isImplements(UNMARSHALLER_INTERFACETYPE)) {
411                     vctx.addError("Unmarshaller class " + m_unmarshallerName +
412                         " does not implement interface " +
413                         UNMARSHALLER_INTERFACE);
414                 } else {
415                     m_unmarshallerClass = uclas;
416                 }
417             }
418         }
419     }
420 }
Popular Tags