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