KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
32
33 import org.jibx.binding.util.StringArray;
34
35 /**
36  * Model component for elements that can contain detailed binding information in
37  * the form of nested child components. Elements of this type include
38  * <b>mapping</b>, <b>template</b>, <b>structure</b>, and <b>collection</b>
39  * elements.
40  *
41  * @author Dennis M. Sosnoski
42  * @version 1.0
43  */

44  
45 public abstract class ContainerElementBase extends NestingElementBase
46 {
47     /** Enumeration of allowed attribute names */
48     public static final StringArray s_allowedAttributes =
49         new StringArray(NestingElementBase.s_allowedAttributes,
50         new StringArray(ObjectAttributes.s_allowedAttributes,
51         StructureAttributes.s_allowedAttributes));
52     
53     /** Object attributes information for nesting. */
54     private ObjectAttributes m_objectAttrs;
55     
56     /** Structure attributes information for nesting. */
57     private StructureAttributes m_structureAttrs;
58     
59     /** Child component that contributes an ID (<code>null</code> if none). */
60     private IComponent m_idChild;
61     
62     /** Child components defining content (created during validation, contains
63      subset of child components defining element or character data content). */

64     private ArrayList JavaDoc m_contentComponents;
65     
66     /** Child components defining attributes (created during validation,
67      contains subset of child components defining attributes). */

68     private ArrayList JavaDoc m_attributeComponents;
69     
70     /**
71      * Constructor.
72      *
73      * @param type element type code
74      */

75     protected ContainerElementBase(int type) {
76         super(type);
77         m_objectAttrs = new ObjectAttributes();
78         m_structureAttrs = new StructureAttributes();
79     }
80
81     /**
82      * Get list of child components contributing content items to this
83      * container element. This call is only meaningful after validation.
84      *
85      * @return list of child binding components defining content items
86      */

87     public ArrayList JavaDoc getContentComponents() {
88         return m_contentComponents == null ?
89             EmptyList.INSTANCE : m_contentComponents;
90     }
91
92     /**
93      * Get list of child components contributing attribute items to this
94      * container element. This call is only meaningful after validation.
95      *
96      * @return list of child binding components defining attribute items
97      */

98     public ArrayList JavaDoc getAttributeComponents() {
99         return m_attributeComponents == null ?
100             EmptyList.INSTANCE : m_attributeComponents;
101     }
102     
103     /**
104      * Get effective type of bound object. This call is meaningful after
105      * prevalidation.
106      *
107      * @return information for class linked by binding
108      */

109     public abstract IClass getEffectiveType();
110     
111     /**
112      * Get actual class linked to binding element. This call is meaningful after
113      * prevalidation.
114      *
115      * @return information for class linked by binding
116      */

117     public abstract IClass getActualType();
118
119     /**
120      * Set ID property child. Used to set the ID property associated with a
121      * particular class instance. There can only be at most one child ID
122      * property for each actual object instance.
123      *
124      * @param child child defining the ID property
125      * @return <code>true</code> if successful, <code>false</code> if ID
126      * already defined
127      */

128     public final void setIdChild(IComponent child, ValidationContext vctx) {
129         if (m_idChild == null) {
130             m_idChild = child;
131             vctx.getBindingRoot().addIdClass(getActualType());
132         } else {
133             vctx.addError("Only one child ID property allowed for an object " +
134                 "- " + ValidationProblem.componentDescription(m_idChild) +
135                 " and " + ValidationProblem.componentDescription(child) +
136                 " refer to the same object");
137         }
138     }
139
140     /**
141      * Get ID property child.
142      */

143     public IComponent getId() {
144         return m_idChild;
145     }
146     
147     //
148
// Object attribute delegate methods
149

150     /**
151      * Get factory method name.
152      *
153      * @return fully-qualified factory class and method name (or
154      * <code>null</code> if none)
155      */

156     public String JavaDoc getFactoryName() {
157         return m_objectAttrs.getFactoryName();
158     }
159     
160     /**
161      * Get factory method information. This call is only meaningful after
162      * validation.
163      *
164      * @return factory method information (or <code>null</code> if none)
165      */

166     public IClassItem getFactory() {
167         return m_objectAttrs.getFactory();
168     }
169     
170     /**
171      * Set factory method name.
172      *
173      * @param name fully qualified class and method name for object factory
174      */

175     public void setFactoryName(String JavaDoc name) {
176         m_objectAttrs.setFactoryName(name);
177     }
178     
179     /**
180      * Get pre-set method name.
181      *
182      * @return pre-set method name (or <code>null</code> if none)
183      */

184     public String JavaDoc getPresetName() {
185         return m_objectAttrs.getPresetName();
186     }
187     
188     /**
189      * Get pre-set method information. This call is only meaningful after
190      * validation.
191      *
192      * @return pre-set method information (or <code>null</code> if none)
193      */

194     public IClassItem getPreset() {
195         return m_objectAttrs.getPreset();
196     }
197     
198     /**
199      * Set pre-set method name.
200      *
201      * @param name member method name to be called before unmarshalling
202      */

203     public void setPresetName(String JavaDoc name) {
204         m_objectAttrs.setPresetName(name);
205     }
206     
207     /**
208      * Get post-set method name.
209      *
210      * @return post-set method name (or <code>null</code> if none)
211      */

212     public String JavaDoc getPostsetName() {
213         return m_objectAttrs.getPostsetName();
214     }
215     
216     /**
217      * Get post-set method information. This call is only meaningful after
218      * validation.
219      *
220      * @return post-set method information (or <code>null</code> if none)
221      */

222     public IClassItem getPostset() {
223         return m_objectAttrs.getPostset();
224     }
225     
226     /**
227      * Set post-set method name.
228      *
229      * @param name member method name to be called after unmarshalling
230      */

231     public void setPostsetName(String JavaDoc name) {
232         m_objectAttrs.setPostsetName(name);
233     }
234     
235     /**
236      * Get pre-get method name.
237      *
238      * @return pre-get method name (or <code>null</code> if none)
239      */

240     public String JavaDoc getPregetName() {
241         return m_objectAttrs.getPregetName();
242     }
243     
244     /**
245      * Get pre-get method information. This call is only meaningful after
246      * validation.
247      *
248      * @return pre-get method information (or <code>null</code> if none)
249      */

250     public IClassItem getPreget() {
251         return m_objectAttrs.getPreget();
252     }
253     
254     /**
255      * Set pre-get method name.
256      *
257      * @param name member method name to be called before marshalling
258      */

259     public void setPreget(String JavaDoc name) {
260         m_objectAttrs.setPreget(name);
261     }
262     
263     /**
264      * Get marshaller class name.
265      *
266      * @return marshaller class name (or <code>null</code> if none)
267      */

268     public String JavaDoc getMarshallerName() {
269         return m_objectAttrs.getMarshallerName();
270     }
271     
272     /**
273      * Get marshaller class information. This call is only meaningful after
274      * validation.
275      *
276      * @return class information for marshaller (or <code>null</code> if none)
277      */

278     public IClass getMarshaller() {
279         return m_objectAttrs.getMarshaller();
280     }
281     
282     /**
283      * Set marshaller class name.
284      *
285      * @param name class name to be used for marshalling
286      */

287     public void setMarshallerName(String JavaDoc name) {
288         m_objectAttrs.setMarshallerName(name);
289     }
290     
291     /**
292      * Get unmarshaller class name.
293      *
294      * @return unmarshaller class name (or <code>null</code> if none)
295      */

296     public String JavaDoc getUnmarshallerName() {
297         return m_objectAttrs.getUnmarshallerName();
298     }
299     
300     /**
301      * Get unmarshaller class information. This call is only meaningful after
302      * validation.
303      *
304      * @return class information for unmarshaller (or <code>null</code> if none)
305      */

306     public IClass getUnmarshaller() {
307         return m_objectAttrs.getUnmarshaller();
308     }
309     
310     /**
311      * Set unmarshaller class name.
312      *
313      * @param name class name to be used for unmarshalling
314      */

315     public void setUnmarshallerName(String JavaDoc name) {
316         m_objectAttrs.setUnmarshallerName(name);
317     }
318     
319     //
320
// Structure attribute delegate methods
321

322     /**
323      * Check if child components are ordered.
324      *
325      * @return <code>true</code> if ordered, <code>false</code> if not
326      */

327     public boolean isOrdered() {
328         return m_structureAttrs.isOrdered();
329     }
330     
331     /**
332      * Set child components ordered flag.
333      *
334      * @param ordered <code>true</code> if ordered, <code>false</code> if not
335      */

336     public void setOrdered(boolean ordered) {
337         m_structureAttrs.setOrdered(ordered);
338     }
339     
340     //
341
// Validation methods.
342

343     /* (non-Javadoc)
344      * @see org.jibx.binding.model.ElementBase#prevalidate(org.jibx.binding.model.ValidationContext)
345      */

346     public void prevalidate(ValidationContext vctx) {
347         m_objectAttrs.prevalidate(vctx);
348         m_structureAttrs.prevalidate(vctx);
349         super.prevalidate(vctx);
350     }
351     
352     /* (non-Javadoc)
353      * @see org.jibx.binding.model.ElementBase#validate(org.jibx.binding.model.ValidationContext)
354      */

355     public void validate(ValidationContext vctx) {
356         super.validate(vctx);
357         m_objectAttrs.validate(vctx);
358         m_structureAttrs.validate(vctx);
359         ArrayList JavaDoc childs = children();
360         if (childs == null || childs.size() == 0) {
361             m_attributeComponents = EmptyList.INSTANCE;
362             m_contentComponents = EmptyList.INSTANCE;
363         } else {
364             m_attributeComponents = new ArrayList JavaDoc();
365             m_contentComponents = new ArrayList JavaDoc();
366             for (int i = 0; i < childs.size(); i++) {
367                 Object JavaDoc child = childs.get(i);
368                 if (child instanceof IComponent) {
369                     IComponent comp = (IComponent)child;
370                     if (comp.hasAttribute()) {
371                         m_attributeComponents.add(child);
372                     }
373                     if (comp.hasContent()) {
374                         m_contentComponents.add(child);
375                     }
376                 }
377             }
378         }
379     }
380 }
Popular Tags