1 /*
2 * $Header: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/ElementDescriptor.java,v 1.6.2.1 2003/01/26 08:49:36 rdonkin Exp $
3 * $Revision: 1.6.2.1 $
4 * $Date: 2003/01/26 08:49:36 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * $Id: ElementDescriptor.java,v 1.6.2.1 2003/01/26 08:49:36 rdonkin Exp $
61 */
62 package org.apache.commons.betwixt;
63
64 import java.util.ArrayList;
65 import java.util.List;
66
67 import org.apache.commons.betwixt.expression.Expression;
68
69 /*** <p><code>ElementDescriptor</code> describes the XML elements
70 * to be created for a bean instance.</p>
71 *
72 * <p> It contains <code>AttributeDescriptor</code>'s for all it's attributes
73 * and <code>ElementDescriptor</code>'s for it's child elements.
74 *
75 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
76 * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
77 * @version $Revision: 1.6.2.1 $
78 */
79 public class ElementDescriptor extends NodeDescriptor {
80
81 /***
82 * Descriptors for attributes this element contains.
83 * Constructed lazily on demand from a List
84 */
85 private AttributeDescriptor[] attributeDescriptors;
86 /***
87 * Descriptors for child elements.
88 * Constructed lazily on demand from a List
89 */
90 private ElementDescriptor[] elementDescriptors;
91
92 /***
93 * The List used on construction. It will be GC'd
94 * after initilization and the array is lazily constructed
95 */
96 private List attributeList;
97
98 /***
99 * The List used on construction. It will be GC'd
100 * after initilization and the array is lazily constructed
101 */
102 private List elementList;
103
104 /*** the expression used to evaluate the new context of this node
105 * or null if the same context is to be used */
106 private Expression contextExpression;
107
108 /*** Whether this element refers to a primitive type (or property of a parent object) */
109 private boolean primitiveType;
110
111 /***
112 * Whether this collection element can be used
113 * as a collection element. Defaults to true
114 */
115 private boolean wrapCollectionsInElement = true;
116
117 /***
118 * Constructs an <code>ElementDescriptor</code> that refers to a primitive type.
119 */
120 public ElementDescriptor() {
121 }
122
123 /***
124 * Base constructor.
125 * @param primitiveType if true, this element refers to a primitive type
126 */
127 public ElementDescriptor(boolean primitiveType) {
128 this.primitiveType = primitiveType;
129 }
130
131 /***
132 * Creates a ElementDescriptor with no namespace URI or prefix.
133 *
134 * @param localName the (xml) local name of this node.
135 * This will be used to set both qualified and local name for this name.
136 */
137 public ElementDescriptor(String localName) {
138 super( localName );
139 }
140
141
142
143 /***
144 * Creates a <code>ElementDescriptor</code> with namespace URI and qualified name
145 * @param localName the (xml) local name of this node
146 * @param qualifiedName the (xml) qualified name of this node
147 * @param uri the (xml) namespace prefix of this node
148 */
149 public ElementDescriptor(String localName, String qualifiedName, String uri) {
150 super(localName, qualifiedName, uri);
151 }
152
153 /***
154 * Returns true if this element has child <code>ElementDescriptors</code>
155 * @return true if this element has child elements
156 * @see #getElementDescriptors
157 */
158 public boolean hasChildren() {
159 return elementDescriptors != null && elementDescriptors.length > 0;
160 }
161
162 /***
163 * Returns true if this element has <code>AttributeDescriptors</code>
164 * @return true if this element has attributes
165 * @see #getAttributeDescriptors
166 */
167 public boolean hasAttributes() {
168 return attributeDescriptors != null && attributeDescriptors.length > 0;
169 }
170
171 /***
172 * Sets whether <code>Collection</code> bean properties should wrap items in a parent element.
173 * In other words, should the mapping for bean properties which are <code>Collection</code>s
174 * enclosed the item elements within a parent element.
175 * Normally only used when this describes a collection bean property.
176 *
177 * @param wrapCollectionsInElement true if the elements for the items in the collection
178 * should be contained in a parent element
179 */
180 public void setWrapCollectionsInElement(boolean wrapCollectionsInElement) {
181 this.wrapCollectionsInElement = wrapCollectionsInElement;
182 }
183
184 /***
185 * Returns true if collective bean properties should wrap the items in a parent element.
186 * In other words, should the mapping for bean properties which are <code>Collection</code>s
187 * enclosed the item elements within a parent element.
188 * Normally only used when this describes a collection bean property.
189 *
190 * @return true if the elements for the items in the collection should be contained
191 * in a parent element
192 */
193 public boolean isWrapCollectionsInElement() {
194 return this.wrapCollectionsInElement;
195 }
196
197 /***
198 * Adds an attribute to the element this <code>ElementDescriptor</code> describes
199 * @param descriptor the <code>AttributeDescriptor</code> that will be added to the
200 * attributes associated with element this <code>ElementDescriptor</code> describes
201 */
202 public void addAttributeDescriptor(AttributeDescriptor descriptor) {
203 if ( attributeList == null ) {
204 attributeList = new ArrayList();
205 }
206 getAttributeList().add( descriptor );
207 attributeDescriptors = null;
208 }
209
210
211 /***
212 * Returns the attribute descriptors for this element
213 *
214 * @return descriptors for the attributes of the element that this
215 * <code>ElementDescriptor</code> describes
216 */
217 public AttributeDescriptor[] getAttributeDescriptors() {
218 if ( attributeDescriptors == null ) {
219 if ( attributeList == null ) {
220 attributeDescriptors = new AttributeDescriptor[0];
221 } else {
222 attributeDescriptors = new AttributeDescriptor[ attributeList.size() ];
223 attributeList.toArray( attributeDescriptors );
224
225 // allow GC of List when initialized
226 attributeList = null;
227 }
228 }
229 return attributeDescriptors;
230 }
231
232 /***
233 * Sets the <code>AttributesDescriptors</code> for this element.
234 * This sets descriptors for the attributes of the element describe by the
235 * <code>ElementDescriptor</code>.
236 *
237 * @param attributeDescriptors the <code>AttributeDescriptor</code> describe the attributes
238 * of the element described by this <code>ElementDescriptor</code>
239 */
240 public void setAttributeDescriptors(AttributeDescriptor[] attributeDescriptors) {
241 this.attributeDescriptors = attributeDescriptors;
242 this.attributeList = null;
243 }
244
245 /***
246 * Adds a descriptor for a child element.
247 *
248 * @param descriptor the <code>ElementDescriptor</code> describing the child element to add
249 */
250 public void addElementDescriptor(ElementDescriptor descriptor) {
251 if ( elementList == null ) {
252 elementList = new ArrayList();
253 }
254 getElementList().add( descriptor );
255 elementDescriptors = null;
256 }
257
258 /***
259 * Returns descriptors for the child elements of the element this describes.
260 * @return the <code>ElementDescriptor</code> describing the child elements
261 * of the element that this <code>ElementDescriptor</code> describes
262 */
263 public ElementDescriptor[] getElementDescriptors() {
264 if ( elementDescriptors == null ) {
265 if ( elementList == null ) {
266 elementDescriptors = new ElementDescriptor[0];
267 } else {
268 elementDescriptors = new ElementDescriptor[ elementList.size() ];
269 elementList.toArray( elementDescriptors );
270
271 // allow GC of List when initialized
272 elementList = null;
273 }
274 }
275 return elementDescriptors;
276 }
277
278 /***
279 * Sets the descriptors for the child element of the element this describes.
280 * @param elementDescriptors the <code>ElementDescriptor</code>s of the element
281 * that this describes
282 */
283 public void setElementDescriptors(ElementDescriptor[] elementDescriptors) {
284 this.elementDescriptors = elementDescriptors;
285 this.elementList = null;
286 }
287
288 /***
289 * Returns the expression used to evaluate the new context of this element.
290 * @return the expression used to evaluate the new context of this element
291 */
292 public Expression getContextExpression() {
293 return contextExpression;
294 }
295
296 /***
297 * Sets the expression used to evaluate the new context of this element
298 * @param contextExpression the expression used to evaluate the new context of this element
299 */
300 public void setContextExpression(Expression contextExpression) {
301 this.contextExpression = contextExpression;
302 }
303
304 /***
305 * Returns true if this element refers to a primitive type property
306 * @return whether this element refers to a primitive type (or property of a parent object)
307 */
308 public boolean isPrimitiveType() {
309 return primitiveType;
310 }
311
312 /***
313 * Sets whether this element refers to a primitive type (or property of a parent object)
314 * @param primitiveType true if this element refers to a primitive type
315 */
316 public void setPrimitiveType(boolean primitiveType) {
317 this.primitiveType = primitiveType;
318 }
319
320 // Implementation methods
321 //-------------------------------------------------------------------------
322
323 /***
324 * Lazily creates the mutable List.
325 * This nullifies the attributeDescriptors array so that
326 * as items are added to the list the Array is ignored until it is
327 * explicitly asked for.
328 *
329 * @return list of <code>AttributeDescriptors</code>'s describing the attributes
330 * of the element that this <code>ElementDescriptor</code> describes
331 */
332 protected List getAttributeList() {
333 if ( attributeList == null ) {
334 if ( attributeDescriptors != null ) {
335 int size = attributeDescriptors.length;
336 attributeList = new ArrayList( size );
337 for ( int i = 0; i < size; i++ ) {
338 attributeList.add( attributeDescriptors[i] );
339 }
340 // force lazy recreation later
341 attributeDescriptors = null;
342 } else {
343 attributeList = new ArrayList();
344 }
345 }
346 return attributeList;
347 }
348
349 /***
350 * Lazily creates the mutable List of child elements.
351 * This nullifies the elementDescriptors array so that
352 * as items are added to the list the Array is ignored until it is
353 * explicitly asked for.
354 *
355 * @return list of <code>ElementDescriptor</code>'s describe the child elements of
356 * the element that this <code>ElementDescriptor</code> describes
357 */
358 protected List getElementList() {
359 if ( elementList == null ) {
360 if ( elementDescriptors != null ) {
361 int size = elementDescriptors.length;
362 elementList = new ArrayList( size );
363 for ( int i = 0; i < size; i++ ) {
364 elementList.add( elementDescriptors[i] );
365 }
366 // force lazy recreation later
367 elementDescriptors = null;
368 } else {
369 elementList = new ArrayList();
370 }
371 }
372 return elementList;
373 }
374
375 /***
376 * Returns something useful for logging.
377 *
378 * @return a string useful for logging
379 */
380 public String toString() {
381 return
382 "ElementDescriptor[qname=" + getQualifiedName() + ",pname=" + getPropertyName()
383 + ",class=" + getPropertyType() + ",singular=" + getSingularPropertyType()
384 + ",updater=" + getUpdater() + "]";
385 }
386 }
This page was automatically generated by Maven