View Javadoc
1   /*
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Intalio, Inc.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 1999,2000 (C) Intalio, Inc. All Rights Reserved.
42   *
43   * Contribution(s):
44   *
45   * - Frank Thelen, frank.thelen@poet.de
46   *     - Moved creation of access methods into an appropriate
47   *       set of separate methods, for extensibility
48   *
49   * $Id$
50   */
51  package org.exolab.castor.builder.info;
52  
53  import org.exolab.castor.builder.SourceGeneratorConstants;
54  import org.exolab.castor.builder.factory.FieldMemberAndAccessorFactory;
55  import org.exolab.castor.builder.info.nature.XMLInfoNature;
56  import org.exolab.castor.builder.types.XSCollectionFactory;
57  import org.exolab.castor.builder.types.XSListType;
58  import org.exolab.castor.builder.types.XSType;
59  
60  /**
61   * A helper used for generating source that deals with Collections.
62   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
63   * @version $Revision$ $Date: 2006-02-23 01:08:24 -0700 (Thu, 23 Feb 2006) $
64   */
65  public class CollectionInfo extends FieldInfo {
66  
67      /** Default suffix for the setter/getter by reference method names. */
68      public static final String DEFAULT_REFERENCE_SUFFIX  = "AsReference";
69  
70      /**
71       * The property used to overwrite the reference suffix for extra collection
72       * methods.
73       */
74      public static final String REFERENCE_SUFFIX_PROPERTY =
75                         "org.exolab.castor.builder.collections.reference.suffix";
76  
77      /**
78       * A flag indicating that "extra" accessor methods should be created for
79       * returning and setting a reference to the underlying collection.
80       */
81      private boolean            _extraMethods;
82      /** The reference suffix to use. */
83      private String             _referenceSuffix          = DEFAULT_REFERENCE_SUFFIX;
84  
85      /** Element type name converted to a method suffix. */
86      private final String       _methodSuffix;
87      /** Element type name converted to a parameter prefix. */
88      private final String       _parameterPrefix;
89      /** FieldInfo describing the _content (i.e. the elements) of this collection. */
90      private final FieldInfo    _content;
91      /** The name to be used when referring to the elements of this collection. */
92      private final String       _elementName;
93  
94      /**
95       * Creates a new CollectionInfo.
96       *
97       * @param contentType
98       *            the _content type of the collection, ie. the type of objects
99       *            that the collection will contain
100      * @param name
101      *            the name of the Collection
102      * @param elementName
103      *            the element name for each element in collection
104      * @param useJava50
105      *            true if source code is supposed to be generated for Java 5
106      * @param memberAndAccessorFactory 
107      *            the FieldMemberAndAccessorFactory to be used
108      * @param contentMemberAndAccessorFactory 
109      *            the FieldMemberAndAccessorFactory for the content          
110      */
111     public CollectionInfo(final XSType contentType, final String name,
112             final String elementName, final boolean useJava50, 
113             final FieldMemberAndAccessorFactory memberAndAccessorFactory,
114             final FieldMemberAndAccessorFactory contentMemberAndAccessorFactory) {
115         super(XSCollectionFactory.createCollection(SourceGeneratorConstants.FIELD_INFO_VECTOR, 
116                 contentType, useJava50), name, memberAndAccessorFactory);
117 
118         if (elementName.charAt(0) == '_') {
119             this._elementName = elementName.substring(1);
120         } else {
121             this._elementName = elementName;
122         }
123 
124         this._methodSuffix = memberAndAccessorFactory.getJavaNaming().toJavaClassName(
125                 this.getElementName());
126         this._parameterPrefix = memberAndAccessorFactory.getJavaNaming().toJavaMemberName(
127                 this.getElementName());
128         this._content = new FieldInfo(contentType, "v" + this.getMethodSuffix(), 
129                 contentMemberAndAccessorFactory);
130         
131         // indicates that - per definition - we are dealing with multi-valuedness
132         new XMLInfoNature(this).setMultivalued(true);
133     } // -- CollectionInfo
134     
135 
136     /**
137      * Return the contents of the collection.
138      * @return the contents of the collection.
139      */
140     public final FieldInfo getContent() {
141         return this._content;
142     }
143 
144     /**
145      * Returns the variable name for the content of the collection.
146      * @return the variable name for the content of the collection.
147      */
148     public final String getContentName() {
149         return this.getContent().getName();
150     }
151 
152     /**
153      * Returns the type of content in this collection.
154      * @return the type of content in this collection.
155      */
156     public final XSType getContentType() {
157         return new XMLInfoNature(this.getContent()).getSchemaType();
158     }
159 
160     /**
161      * Returns the name to be used when referring to the elements of this
162      * collection.
163      *
164      * @return the name to be used when referring to the elements of this
165      *          collection.
166      */
167     public final String getElementName() {
168         return this._elementName;
169     }
170 
171     /**
172      * Returns the schema type represented by this collection.
173      * @return the schema type represented by this collection.
174      */
175     public final XSListType getXSList() {
176         return (XSListType) new XMLInfoNature(this).getSchemaType();
177     }
178 
179     /**
180      * Sets whether or not to create extra collection methods for accessing the
181      * actual collection.
182      *
183      * @param extraMethods
184      *            a boolean that when true indicates that extra collection
185      *            accessor methods should be created. False by default.
186      * @see #setReferenceMethodSuffix
187      */
188     public final void setCreateExtraMethods(final boolean extraMethods) {
189         this._extraMethods = extraMethods;
190     } // -- setCreateExtraMethods
191 
192     /**
193      * Sets the method suffix (ending) to use when creating the extra collection
194      * methods.
195      *
196      * @param suffix
197      *            the method suffix to use when creating the extra collection
198      *            methods. If null or emtpty the default value, as specified by
199      *            DEFAULT_REFERENCE_SUFFIX will used.
200      * @see #setCreateExtraMethods
201      */
202     public final void setReferenceMethodSuffix(final String suffix) {
203         if (suffix == null || suffix.length() == 0) {
204             this._referenceSuffix = DEFAULT_REFERENCE_SUFFIX;
205         } else {
206             this._referenceSuffix = suffix;
207         }
208     } // -- setReferenceMethodSuffix
209 
210     /**
211      * {@inheritDoc}
212      *
213      * @see org.exolab.castor.builder.info.FieldInfo#getMethodSuffix()
214      */
215     public final String getMethodSuffix() {
216         return this._methodSuffix;
217     }
218 
219     /**
220      * Returns the suffix (ending) that should be used when creating the extra
221      * collection methods.
222      *
223      * @return the suffix for the reference methods
224      */
225     public final String getReferenceMethodSuffix() {
226         return this._referenceSuffix;
227     } // -- getReferenceMethodSuffix
228     
229     /**
230      * Indicates whether extra collection methods should be created.
231      * @return True if extra collection methods will be created.
232      */
233     public boolean isExtraMethods() {
234         return _extraMethods;
235     }
236     
237     /**
238      * Returns the element type name converted to a parameter prefix.
239      * @return the element type name converted to a parameter prefix.
240      */
241     public String getParameterPrefix() {
242         return _parameterPrefix;
243     }
244 
245 
246     /**
247      * Returns the reference suffix to use for 'reference style' methods.
248      * @return the reference suffix to use 
249      */
250     public String getReferenceSuffix() {
251         return _referenceSuffix;
252     }
253 
254 }