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: * Contributor(s):
020: *
021: */
022: package org.enhydra.tool.codegen;
023:
024: // Toolbox imports
025: import org.enhydra.tool.common.ResUtil;
026: import java.util.ResourceBundle;
027:
028: /**
029: * GeneratorOption define a generator specific option for CodeGen. Generation
030: * options include information such as the directory where to generate
031: * an application and name to use for the generated application.
032: */
033: public class GeneratorOption {
034: static ResourceBundle res = ResourceBundle
035: .getBundle("org.enhydra.tool.codegen.Res"); // nores
036:
037: private String name = new String();
038: private String value = new String();
039: private String displayName = new String();
040: private String description = new String();
041: private boolean required = false;
042: private boolean persistent = true;
043: private boolean bool = false;
044:
045: /**
046: * Create a option for the current generator. Generators
047: * normally instantiate an array of options during
048: * initialization.
049: *
050: * @param name
051: * When passing command line arguments, the name must be prefixed
052: * with a dash.
053: *
054: * @param name
055: * When passing command line arguments, the name must be prefixed
056: * with a dash.
057: *
058: *
059: * @param value
060: * When the name refers to a boolean flag, any value other than 'true'
061: * should be treated as 'false'.
062: *
063: * @param description
064: * The description is what displays when help for the option is
065: * printed out from the command line. The description may also be
066: * used in a wizard panel.
067: *
068: * @param required
069: * True if a value for this option must be specified for the
070: * generation to be successful.
071: *
072: * @throws GeneratorException
073: * Thrown if name is a null or an empty string. Note that no exception
074: * is thrown during construction if required is true and the value is
075: * null or empty.
076: */
077: public GeneratorOption(String name, String value,
078: String displayName, String description, boolean required,
079: boolean persistent) throws GeneratorException {
080: if (name == null || name.trim().length() == 0) {
081: throw new GeneratorException(res
082: .getString("Option_name_cannot_be"));
083: } else {
084: this .name = name;
085: this .value = value;
086: this .displayName = displayName;
087: this .description = description;
088: this .required = required;
089: this .persistent = persistent;
090: this .bool = false;
091: }
092: }
093:
094: /**
095: * Create a option for the current generator. Generators
096: * normally instantiate an array of options during
097: * initialization.
098: *
099: * @param name
100: * When passing command line arguments, the name must be prefixed
101: * with a dash.
102: *
103: * @param value
104: * When the name refers to a boolean flag, any value other than 'true'
105: * should be treated as 'false'.
106: *
107: * @param description
108: * The description is what displays when help for the option is
109: * printed out from the command line. The description may also be
110: * used in a wizard panel.
111: *
112: *
113: * @throws GeneratorException
114: * Thrown if name is a null or an empty string. Note that no exception
115: * is thrown during construction if required is true and the value is
116: * null or empty.
117: */
118: public GeneratorOption(String n, boolean v, String desc,
119: boolean persist) throws GeneratorException {
120: this (n, (new Boolean(v)).toString(), new String(), desc, false,
121: persist);
122: this .bool = true;
123: }
124:
125: /**
126: * Get the option name. This should be a terse name used for command
127: * line and referencing within generator code. This terse format of
128: * indicating the option should not be displayed in a wizard.
129: *
130: * @return
131: * The option name. Prefix this with a dash for command line usage.
132: */
133: public String getName() {
134: return name;
135: }
136:
137: /**
138: * Get the option value.
139: *
140: * @return
141: * For boolean flags, treat any value other than 'true' as 'false'.
142: */
143: public String getValue() {
144: return value;
145: }
146:
147: /**
148: * Set the option value. The value can be set when processing
149: * arguments from the command line or during the writeOptions()
150: * method of an CodeGenPanel.
151: *
152: * @param value
153: * String value for the option.
154: *
155: * @throws GeneratorException
156: * Thrown if value is null or empty and required is true.
157: */
158: public void setValue(String value) throws GeneratorException {
159: if (isRequired()) {
160: if (value == null || value.trim().length() == 0) {
161: throw new GeneratorException(ResUtil.format(res
162: .getString("Required_value_cannot"),
163: getDisplayName()));
164: }
165: }
166: this .value = value;
167: }
168:
169: /**
170: * Set the value as a boolean.
171: *
172: * @param value
173: * Boolean value.
174: */
175: public void setValue(boolean value) {
176: Boolean b = new Boolean(value);
177:
178: this .value = b.toString();
179: }
180:
181: /**
182: * Get a string describing the option. The description
183: * is displayed by command line help and may be used
184: * for wizard panels.
185: *
186: * @return
187: * Descriptive text. String length should not be
188: * longer than 512 characters.
189: */
190: public String getDescription() {
191: return description;
192: }
193:
194: /**
195: * Flag indicating if the values needs to be set prior
196: * to generation. Ignore this flag when dealing with
197: * boolean generation options.
198: *
199: * @return
200: * If true, the generator requires that the value not be null
201: * or an empty string.
202: *
203: */
204: public boolean isRequired() {
205: return required;
206: }
207:
208: /**
209: * Check if the option value persistent between CodeGen sessions.
210: *
211: * @return
212: * True if the value is persisted between CodeGen sessions.
213: */
214: public boolean isPersistent() {
215: return persistent;
216: }
217:
218: /**
219: * Get name to display in Swing components.
220: *
221: * @return
222: * String to display in Swing components such as the generator
223: * selection combobox.
224: *
225: */
226: public String getDisplayName() {
227: return displayName;
228: }
229:
230: /**
231: * Get the value as a boolean.
232: *
233: * @return
234: * Returns true if the value is 'true'.
235: */
236: public boolean isValue() {
237: Boolean b = Boolean.valueOf(getValue().toLowerCase());
238:
239: return b.booleanValue();
240: }
241:
242: /**
243: * Determine if the option is a boolean switch.
244: *
245: * @return
246: * True if the option is a boolean.
247: */
248: public boolean isBoolean() {
249: return bool;
250: }
251:
252: public void clearValue() {
253: value = new String();
254: }
255:
256: public boolean isEmpty() {
257: return (value == null || value.trim().length() == 0);
258: }
259:
260: }
|