KickJava   Java API By Example, From Geeks To Geeks.

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


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 elements that define the binding structure for an object
35  * property. This is the base class for <i>structure</i> and <i>collection</i>
36  * elements.
37  *
38  * @author Dennis M. Sosnoski
39  * @version 1.0
40  */

41  
42 public abstract class StructureElementBase
43 extends ContainerElementBase implements IComponent
44 {
45     /** Enumeration of allowed attribute names */
46     public static final StringArray s_allowedAttributes =
47         new StringArray(new StringArray(PropertyAttributes.s_allowedAttributes,
48         NameAttributes.s_allowedAttributes),
49         ContainerElementBase.s_allowedAttributes);
50     
51     /** Flag for ordered child components. */
52     private boolean m_isOrdered;
53     
54     /** Property attributes information for nesting. */
55     private PropertyAttributes m_propertyAttrs;
56
57     /** Name attributes information for nesting. */
58     private NameAttributes m_nameAttrs;
59
60     /** Label for this structure definition. */
61     private String JavaDoc m_label;
62
63     /** Label for structure to be used as definition. */
64     private String JavaDoc m_using;
65     
66     /**
67      * Constructor.
68      *
69      * @param type element type code
70      */

71     protected StructureElementBase(int type) {
72         super(type);
73         m_nameAttrs = new NameAttributes();
74         m_propertyAttrs = new PropertyAttributes();
75         m_isOrdered = true;
76     }
77     
78     /**
79      * Set ordered child components.
80      *
81      * @param ordered <code>true</code> if ordered children, <code>false</code>
82      * if not
83      */

84     public void setOrdered(boolean ordered) {
85         m_isOrdered = ordered;
86     }
87     
88     /**
89      * Check if ordered child components.
90      *
91      * @return <code>true</code> if ordered children, <code>false</code> if not
92      */

93     public boolean isOrdered() {
94         return m_isOrdered;
95     }
96
97     /**
98      * Get label for this definition.
99      *
100      * @return label for this definition
101      */

102     public String JavaDoc getLabel() {
103         return m_label;
104     }
105
106     /**
107      * Set label for this definition.
108      *
109      * @param label label for this definition
110      */

111     public void setLabel(String JavaDoc label) {
112         m_label = label;
113     }
114
115     /**
116      * Get label for definition to be used.
117      *
118      * @return label for definition to be used
119      */

120     public String JavaDoc getUsing() {
121         return m_using;
122     }
123
124     /**
125      * Set label for definition to be used.
126      *
127      * @param label label for definition to be used
128      */

129     public void setUsing(String JavaDoc label) {
130         m_using = label;
131     }
132     
133     //
134
// Name attribute delegate methods
135

136     /**
137      * Get name.
138      *
139      * @return name text
140      */

141     public String JavaDoc getName() {
142         return m_nameAttrs.getName();
143     }
144
145     /**
146      * Set name.
147      *
148      * @param name text for name
149      */

150     public void setName(String JavaDoc name) {
151         m_nameAttrs.setName(name);
152     }
153
154     /**
155      * Get specified namespace URI.
156      *
157      * @return namespace URI (<code>null</code> if not set)
158      */

159     public String JavaDoc getUri() {
160         return m_nameAttrs.getUri();
161     }
162
163     /**
164      * Set namespace URI.
165      *
166      * @param uri namespace URI (<code>null</code> if not set)
167      */

168     public void setUri(String JavaDoc uri) {
169         m_nameAttrs.setUri(uri);
170     }
171
172     /**
173      * Get specified namespace prefix.
174      *
175      * @return namespace prefix (<code>null</code> if not set)
176      */

177     public String JavaDoc getPrefix() {
178         return m_nameAttrs.getPrefix();
179     }
180
181     /**
182      * Set namespace prefix.
183      *
184      * @param prefix namespace prefix (<code>null</code> if not set)
185      */

186     public void setPrefix(String JavaDoc prefix) {
187         m_nameAttrs.setPrefix(prefix);
188     }
189     
190     /**
191      * Get effective namespace information. This call is only meaningful after
192      * validation.
193      *
194      * @return effective namespace information
195      */

196     public NamespaceElement getNamespace() {
197         return m_nameAttrs.getNamespace();
198     }
199     
200     //
201
// Property attribute delegate methods
202

203     /**
204      * Get usage name.
205      *
206      * @return usage name
207      */

208     public String JavaDoc getUsageName() {
209         return m_propertyAttrs.getUsageName();
210     }
211     
212     /**
213      * Get usage value. This call is only meaningful after validation.
214      *
215      * @return usage value
216      */

217     public int getUsage() {
218         return m_propertyAttrs.getUsage();
219     }
220     
221     /**
222      * Set usage name.
223      *
224      * @param name usage name
225      */

226     public void setUsageName(String JavaDoc name) {
227         m_propertyAttrs.setUsageName(name);
228     }
229     
230     /**
231      * Get declared type name.
232      *
233      * @return type name (or <code>null</code> if none)
234      */

235     public String JavaDoc getDeclaredType() {
236         return m_propertyAttrs.getDeclaredType();
237     }
238     
239     /**
240      * Set declared type name.
241      *
242      * @param type name (or <code>null</code> if none)
243      */

244     public void setDeclaredType(String JavaDoc type) {
245         m_propertyAttrs.setDeclaredType(type);
246     }
247     
248     /**
249      * Get field name.
250      *
251      * @return field name (or <code>null</code> if none)
252      */

253     public String JavaDoc getFieldName() {
254         return m_propertyAttrs.getFieldName();
255     }
256     
257     /**
258      * Get field information. This call is only meaningful after validation.
259      *
260      * @return field information (or <code>null</code> if none)
261      */

262     public IClassItem getField() {
263         return m_propertyAttrs.getField();
264     }
265     
266     /**
267      * Set field name.
268      *
269      * @param field field name (or <code>null</code> if none)
270      */

271     public void setFieldName(String JavaDoc field) {
272         m_propertyAttrs.setFieldName(field);
273     }
274     
275     /**
276      * Get test method name.
277      *
278      * @return test method name (or <code>null</code> if none)
279      */

280     public String JavaDoc getTestName() {
281         return m_propertyAttrs.getTestName();
282     }
283     
284     /**
285      * Get test method information. This call is only meaningful after
286      * validation.
287      *
288      * @return test method information (or <code>null</code> if none)
289      */

290     public IClassItem getTest() {
291         return m_propertyAttrs.getTest();
292     }
293     
294     /**
295      * Set test method name.
296      *
297      * @param test test method name (or <code>null</code> if none)
298      */

299     public void setTestName(String JavaDoc test) {
300         m_propertyAttrs.setTestName(test);
301     }
302     
303     /**
304      * Get get method name.
305      *
306      * @return get method name (or <code>null</code> if none)
307      */

308     public String JavaDoc getGetName() {
309         return m_propertyAttrs.getGetName();
310     }
311     
312     /**
313      * Get get method information. This call is only meaningful after
314      * validation.
315      *
316      * @return get method information (or <code>null</code> if none)
317      */

318     public IClassItem getGet() {
319         return m_propertyAttrs.getGet();
320     }
321     
322     /**
323      * Get type for value loaded to stack. This call is only meaningful after
324      * validation.
325      *
326      * @return get value type (or <code>null</code> if none)
327      */

328     public IClass getGetType() {
329         return m_propertyAttrs.getGetType();
330     }
331     
332     /**
333      * Set get method name.
334      *
335      * @param get get method name (or <code>null</code> if none)
336      */

337     public void setGetName(String JavaDoc get) {
338         m_propertyAttrs.setGetName(get);
339     }
340     
341     /**
342      * Get set method name.
343      *
344      * @return set method name (or <code>null</code> if none)
345      */

346     public String JavaDoc getSetName() {
347         return m_propertyAttrs.getSetName();
348     }
349     
350     /**
351      * Get set method information. This call is only meaningful after
352      * validation.
353      *
354      * @return set method information (or <code>null</code> if none)
355      */

356     public IClassItem getSet() {
357         return m_propertyAttrs.getSet();
358     }
359     
360     /**
361      * Get type for value stored from stack. This call is only meaningful after
362      * validation.
363      *
364      * @return set value type (or <code>null</code> if none)
365      */

366     public IClass getSetType() {
367         return m_propertyAttrs.getSetType();
368     }
369     
370     /**
371      * Set set method name.
372      *
373      * @param set set method name (or <code>null</code> if none)
374      */

375     public void setSetName(String JavaDoc set) {
376         m_propertyAttrs.setSetName(set);
377     }
378     
379     /**
380      * Check if this structure implicitly uses the containing object. This call
381      * is only meaningful after prevalidation.
382      *
383      * @return <code>true</code> if using the containing object,
384      * <code>false</code> if own object
385      */

386     public boolean isImplicit() {
387         return m_propertyAttrs.isImplicit();
388     }
389     
390     //
391
// Implementation methods
392

393     /* (non-Javadoc)
394      * @see org.jibx.binding.model.ElementBase#isOptional()
395      */

396     public boolean isOptional() {
397         return m_propertyAttrs.getUsage() == PropertyAttributes.OPTIONAL_USAGE;
398     }
399     
400     //
401
// Implementation of methods from IComponent interface (used in extensions)
402

403     /* (non-Javadoc)
404      * @see org.jibx.binding.model.IComponent#hasAttribute()
405      */

406     public boolean hasAttribute() {
407         if (m_nameAttrs.getName() != null) {
408             return false;
409         } else {
410             return getAttributeComponents().size() > 0;
411         }
412     }
413
414     /* (non-Javadoc)
415      * @see org.jibx.binding.model.IComponent#hasContent()
416      */

417     public boolean hasContent() {
418         if (m_nameAttrs.getName() != null) {
419             return true;
420         } else {
421             return getContentComponents().size() > 0;
422         }
423     }
424
425     /* (non-Javadoc)
426      * @see org.jibx.binding.model.IComponent#hasName()
427      */

428     public boolean hasName() {
429         return m_nameAttrs.getName() != null;
430     }
431
432     /* (non-Javadoc)
433      * @see org.jibx.binding.model.IComponent#hasType()
434      */

435     public boolean hasType() {
436         return m_propertyAttrs.getType() != null;
437     }
438     
439     /* (non-Javadoc)
440      * @see org.jibx.binding.model.IComponent#getType()
441      */

442     public IClass getType() {
443         return m_propertyAttrs.getType();
444     }
445     
446     //
447
// Validation methods
448

449     /* (non-Javadoc)
450      * @see org.jibx.binding.model.ElementBase#prevalidate(org.jibx.binding.model.ValidationContext)
451      */

452     public void prevalidate(ValidationContext vctx) {
453         if (m_using != null && children().size() > 0) {
454             vctx.addFatal("Child elements not allowed with using attribute");
455         }
456         m_nameAttrs.prevalidate(vctx);
457         m_propertyAttrs.prevalidate(vctx);
458         if (!vctx.isSkipped(this)) {
459             super.prevalidate(vctx);
460         }
461     }
462     
463     /* (non-Javadoc)
464      * @see org.jibx.binding.model.ElementBase#validate(org.jibx.binding.model.ValidationContext)
465      */

466     public void validate(ValidationContext vctx) {
467         m_nameAttrs.validate(vctx);
468         m_propertyAttrs.validate(vctx);
469         if (!vctx.isSkipped(this)) {
470             super.validate(vctx);
471         }
472     }
473 }
Popular Tags