KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > zeus > transform > TransformerOptions


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  */

19 package org.enhydra.zeus.transform;
20
21 import org.enhydra.zeus.InvalidCollectionTypeException;
22 import org.enhydra.zeus.util.ClassUtils;
23
24 /**
25  * <p>
26  * <code>TransformerOptions</code> is used to specify options used in
27  * <code>{@link org.enhydra.zeus.Transformer}</code> and related classes
28  * and processes. It specifies global options for transformation such
29  * as packages, collection types, and more.
30  * </p>
31  *
32  * @author Brett McLaughlin
33  */

34 public class TransformerOptions {
35     
36     /** The default package for all classes, unless overridden */
37     protected String JavaDoc defaultPackage;
38     
39     /** The default interface package for this transformation */
40     protected String JavaDoc interfacePackage;
41     
42     /** The default implementation package for this transformation */
43     protected String JavaDoc implementationPackage;
44     
45     /** The default collection class type */
46     protected int defaultCollectionType;
47     
48     /**
49      * <p>
50      * This default constructor sets up default variable values.
51      * </p>
52      */

53     public TransformerOptions() {
54         this("", ClassUtils.COLLECTION_TYPE_LIST);
55     }
56     
57     /**
58      * <p>
59      * This convenience constructor allows options to be set on
60      * construction.
61      * </p>
62      *
63      * @param defaultPackage the package for all generated classes.
64      * @param defaultCollectionType constant for the collection type to use.
65      */

66     public TransformerOptions(String JavaDoc defaultPackage,
67                               int defaultCollectionType) {
68         this.defaultPackage = defaultPackage;
69         interfacePackage = null;
70         implementationPackage = null;
71         try {
72             setDefaultCollectionType(defaultCollectionType);
73         } catch (InvalidCollectionTypeException e) {
74             try {
75                 setDefaultCollectionType(ClassUtils.COLLECTION_TYPE_LIST);
76             } catch (InvalidCollectionTypeException neverHappens) { }
77         }
78     }
79     
80     /**
81      * <p>
82      * This convenience constructor allows options to be set on
83      * construction.
84      * </p>
85      *
86      * @param interfacePackage the package for all generated interfaces.
87      * @param implementationPackage the package for all generated implementation
88      * packages.
89      * @param defaultCollectionType constant for the collection type to use.
90      */

91     public TransformerOptions(String JavaDoc interfacePackage,
92                               String JavaDoc implementationPackage,
93                               int defaultCollectionType) {
94         defaultPackage = "";
95         interfacePackage = interfacePackage;
96         implementationPackage = implementationPackage;
97         try {
98             setDefaultCollectionType(defaultCollectionType);
99         } catch (InvalidCollectionTypeException e) {
100             try {
101                 setDefaultCollectionType(ClassUtils.COLLECTION_TYPE_LIST);
102             } catch (InvalidCollectionTypeException neverHappens) { }
103         }
104     }
105     
106     /**
107      * <p>
108      * This sets the default and global package for all generated
109      * classes, unless a specific package for interfaces and/or
110      * implementations is supplied, which then overrides this
111      * setting. Those packages can be set using the
112      * <code>{@link #setInterfacePackage}</code> and
113      * <code>{@link #setImplementationPackage}</code> methods.
114      * </p><p>
115      * There is intentionally not a <code>getDefaultPackage()</code> method.
116      * Instead, <code>{@link #getInterfacePackage}</code> and
117      * <code>{@link #getImplementationPackage}</code> should be used,
118      * which return correct values based on the default and specific
119      * packages set for both options.
120      *
121      * @param interfacePackage the package for generated interfaces.
122      */

123     public void setDefaultPackage(String JavaDoc defaultPackage) {
124         if (defaultPackage == null) {
125             throw new IllegalArgumentException JavaDoc("A TransformerOptions cannot " +
126                 "have a null value for the default package. To use the " +
127                 "default package, specify an empty String (\"\").");
128         }
129         
130         this.defaultPackage = defaultPackage;
131     }
132     
133     /**
134      * <p>
135      * This sets the default and global interface package for generated
136      * classes.
137      * </p>
138      *
139      * @param interfacePackage the package for generated interfaces.
140      */

141     public void setInterfacePackage(String JavaDoc interfacePackage) {
142         if (interfacePackage == null) {
143             throw new IllegalArgumentException JavaDoc("A TransformerOptions cannot " +
144                 "have a null value for the interface package. To use the " +
145                 "default package, specify an empty String (\"\").");
146         }
147         this.interfacePackage = interfacePackage;
148     }
149     
150     /**
151      * <p>
152      * This returns the default package in which generated interfaces are
153      * placed.
154      * </p>
155      *
156      * @return <code>String</code> - the default interface package.
157      */

158     public String JavaDoc getInterfacePackage() {
159         if (interfacePackage != null) {
160             return interfacePackage;
161         } else {
162             return defaultPackage;
163         }
164     }
165     
166     /**
167      * <p>
168      * This sets the default and global interface package for generated
169      * classes.
170      * </p>
171      *
172      * @param interfacePackage the package for generated interfaces.
173      */

174     public void setImplementationPackage(String JavaDoc implementationPackage) {
175         if (implementationPackage == null) {
176             throw new IllegalArgumentException JavaDoc("A TransformerOptions cannot " +
177                 "have a null value for the implementation package. To use " +
178                 "the default package, specify an empty String (\"\").");
179         }
180         this.implementationPackage = implementationPackage;
181     }
182     
183     /**
184      * <p>
185      * This returns the default package in which generated implementations are
186      * placed.
187      * </p>
188      *
189      * @return <code>String</code> - the default implementation package.
190      */

191     public String JavaDoc getImplementationPackage() {
192         if (implementationPackage != null) {
193             return implementationPackage;
194         } else {
195             return defaultPackage;
196         }
197     }
198     
199     /**
200      * <p>
201      * This allows setting of the default collection type for transformations,
202      * through constants defined in this class. If the supplied constant is
203      * an illegal type, an
204      * <code>{@link InvalidCollectionTypeException}</code> is thrown.
205      * </p>
206      *
207      * @param defaultCollectionType the collection type to use in
208      * transformations, as an <code>int</code> constant.
209      * @throws <code>InvalidCollectionTypeException</code> - when an illegal
210      * collection type is supplied.
211      * @see <code>{@link #COLLECTION_TYPE_LIST}</code>
212      * @see <code>{@link #COLLECTION_TYPE_ARRAY}</code>
213      */

214     public void setDefaultCollectionType(int defaultCollectionType)
215         throws InvalidCollectionTypeException {
216         
217         // Ensure a legal value
218
if (!ClassUtils.isCollectionConstant(defaultCollectionType)) {
219             throw new InvalidCollectionTypeException();
220         }
221         
222         // If legal, set as default collection type
223
this.defaultCollectionType = defaultCollectionType;
224     }
225     
226     /**
227      * <p>
228      * This allows setting of the default collection type for transformations,
229      * through supplying a <code>String</code> class name, such as
230      * <code>java.util.List</code>. If the supplied type is an illegal type,
231      * an <code>{@link InvalidCollectionTypeException}</code> is thrown.
232      * </p>
233      *
234      * @param defaultCollectionTypeString the String representation of the
235      * collection type to use.
236      * @throws <code>InvalidCollectionTypeException</code> - when an illegal
237      * collection type is supplied.
238      */

239     public void setDefaultCollectionType(String JavaDoc defaultCollectionTypeString)
240         throws InvalidCollectionTypeException {
241             
242         // If legal, set as default collection type
243
this.defaultCollectionType =
244             ClassUtils.getCollectionTypeAsInt(defaultCollectionTypeString);
245     }
246     
247     /**
248      * <p>
249      * This will return the default collection class name for this set
250      * of transformation options. This will return a fully-qualified class
251      * name, such as <code>java.util.List</code>.
252      * </p>
253      *
254      * @return <code>String</code> - the class name for the collection type.
255      */

256     public String JavaDoc getDefaultCollectionType() {
257         return ClassUtils.getCollectionTypeAsString(defaultCollectionType);
258     }
259 }
260
Popular Tags