001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: */
019: package org.enhydra.zeus.transform;
020:
021: import org.enhydra.zeus.InvalidCollectionTypeException;
022: import org.enhydra.zeus.util.ClassUtils;
023:
024: /**
025: * <p>
026: * <code>TransformerOptions</code> is used to specify options used in
027: * <code>{@link org.enhydra.zeus.Transformer}</code> and related classes
028: * and processes. It specifies global options for transformation such
029: * as packages, collection types, and more.
030: * </p>
031: *
032: * @author Brett McLaughlin
033: */
034: public class TransformerOptions {
035:
036: /** The default package for all classes, unless overridden */
037: protected String defaultPackage;
038:
039: /** The default interface package for this transformation */
040: protected String interfacePackage;
041:
042: /** The default implementation package for this transformation */
043: protected String implementationPackage;
044:
045: /** The default collection class type */
046: protected int defaultCollectionType;
047:
048: /**
049: * <p>
050: * This default constructor sets up default variable values.
051: * </p>
052: */
053: public TransformerOptions() {
054: this ("", ClassUtils.COLLECTION_TYPE_LIST);
055: }
056:
057: /**
058: * <p>
059: * This convenience constructor allows options to be set on
060: * construction.
061: * </p>
062: *
063: * @param defaultPackage the package for all generated classes.
064: * @param defaultCollectionType constant for the collection type to use.
065: */
066: public TransformerOptions(String defaultPackage,
067: int defaultCollectionType) {
068: this .defaultPackage = defaultPackage;
069: interfacePackage = null;
070: implementationPackage = null;
071: try {
072: setDefaultCollectionType(defaultCollectionType);
073: } catch (InvalidCollectionTypeException e) {
074: try {
075: setDefaultCollectionType(ClassUtils.COLLECTION_TYPE_LIST);
076: } catch (InvalidCollectionTypeException neverHappens) {
077: }
078: }
079: }
080:
081: /**
082: * <p>
083: * This convenience constructor allows options to be set on
084: * construction.
085: * </p>
086: *
087: * @param interfacePackage the package for all generated interfaces.
088: * @param implementationPackage the package for all generated implementation
089: * packages.
090: * @param defaultCollectionType constant for the collection type to use.
091: */
092: public TransformerOptions(String interfacePackage,
093: String implementationPackage, int defaultCollectionType) {
094: defaultPackage = "";
095: interfacePackage = interfacePackage;
096: implementationPackage = implementationPackage;
097: try {
098: setDefaultCollectionType(defaultCollectionType);
099: } catch (InvalidCollectionTypeException e) {
100: try {
101: setDefaultCollectionType(ClassUtils.COLLECTION_TYPE_LIST);
102: } catch (InvalidCollectionTypeException neverHappens) {
103: }
104: }
105: }
106:
107: /**
108: * <p>
109: * This sets the default and global package for all generated
110: * classes, unless a specific package for interfaces and/or
111: * implementations is supplied, which then overrides this
112: * setting. Those packages can be set using the
113: * <code>{@link #setInterfacePackage}</code> and
114: * <code>{@link #setImplementationPackage}</code> methods.
115: * </p><p>
116: * There is intentionally not a <code>getDefaultPackage()</code> method.
117: * Instead, <code>{@link #getInterfacePackage}</code> and
118: * <code>{@link #getImplementationPackage}</code> should be used,
119: * which return correct values based on the default and specific
120: * packages set for both options.
121: *
122: * @param interfacePackage the package for generated interfaces.
123: */
124: public void setDefaultPackage(String defaultPackage) {
125: if (defaultPackage == null) {
126: throw new IllegalArgumentException(
127: "A TransformerOptions cannot "
128: + "have a null value for the default package. To use the "
129: + "default package, specify an empty String (\"\").");
130: }
131:
132: this .defaultPackage = defaultPackage;
133: }
134:
135: /**
136: * <p>
137: * This sets the default and global interface package for generated
138: * classes.
139: * </p>
140: *
141: * @param interfacePackage the package for generated interfaces.
142: */
143: public void setInterfacePackage(String interfacePackage) {
144: if (interfacePackage == null) {
145: throw new IllegalArgumentException(
146: "A TransformerOptions cannot "
147: + "have a null value for the interface package. To use the "
148: + "default package, specify an empty String (\"\").");
149: }
150: this .interfacePackage = interfacePackage;
151: }
152:
153: /**
154: * <p>
155: * This returns the default package in which generated interfaces are
156: * placed.
157: * </p>
158: *
159: * @return <code>String</code> - the default interface package.
160: */
161: public String getInterfacePackage() {
162: if (interfacePackage != null) {
163: return interfacePackage;
164: } else {
165: return defaultPackage;
166: }
167: }
168:
169: /**
170: * <p>
171: * This sets the default and global interface package for generated
172: * classes.
173: * </p>
174: *
175: * @param interfacePackage the package for generated interfaces.
176: */
177: public void setImplementationPackage(String implementationPackage) {
178: if (implementationPackage == null) {
179: throw new IllegalArgumentException(
180: "A TransformerOptions cannot "
181: + "have a null value for the implementation package. To use "
182: + "the default package, specify an empty String (\"\").");
183: }
184: this .implementationPackage = implementationPackage;
185: }
186:
187: /**
188: * <p>
189: * This returns the default package in which generated implementations are
190: * placed.
191: * </p>
192: *
193: * @return <code>String</code> - the default implementation package.
194: */
195: public String getImplementationPackage() {
196: if (implementationPackage != null) {
197: return implementationPackage;
198: } else {
199: return defaultPackage;
200: }
201: }
202:
203: /**
204: * <p>
205: * This allows setting of the default collection type for transformations,
206: * through constants defined in this class. If the supplied constant is
207: * an illegal type, an
208: * <code>{@link InvalidCollectionTypeException}</code> is thrown.
209: * </p>
210: *
211: * @param defaultCollectionType the collection type to use in
212: * transformations, as an <code>int</code> constant.
213: * @throws <code>InvalidCollectionTypeException</code> - when an illegal
214: * collection type is supplied.
215: * @see <code>{@link #COLLECTION_TYPE_LIST}</code>
216: * @see <code>{@link #COLLECTION_TYPE_ARRAY}</code>
217: */
218: public void setDefaultCollectionType(int defaultCollectionType)
219: throws InvalidCollectionTypeException {
220:
221: // Ensure a legal value
222: if (!ClassUtils.isCollectionConstant(defaultCollectionType)) {
223: throw new InvalidCollectionTypeException();
224: }
225:
226: // If legal, set as default collection type
227: this .defaultCollectionType = defaultCollectionType;
228: }
229:
230: /**
231: * <p>
232: * This allows setting of the default collection type for transformations,
233: * through supplying a <code>String</code> class name, such as
234: * <code>java.util.List</code>. If the supplied type is an illegal type,
235: * an <code>{@link InvalidCollectionTypeException}</code> is thrown.
236: * </p>
237: *
238: * @param defaultCollectionTypeString the String representation of the
239: * collection type to use.
240: * @throws <code>InvalidCollectionTypeException</code> - when an illegal
241: * collection type is supplied.
242: */
243: public void setDefaultCollectionType(
244: String defaultCollectionTypeString)
245: throws InvalidCollectionTypeException {
246:
247: // If legal, set as default collection type
248: this .defaultCollectionType = ClassUtils
249: .getCollectionTypeAsInt(defaultCollectionTypeString);
250: }
251:
252: /**
253: * <p>
254: * This will return the default collection class name for this set
255: * of transformation options. This will return a fully-qualified class
256: * name, such as <code>java.util.List</code>.
257: * </p>
258: *
259: * @return <code>String</code> - the class name for the collection type.
260: */
261: public String getDefaultCollectionType() {
262: return ClassUtils
263: .getCollectionTypeAsString(defaultCollectionType);
264: }
265: }
|