001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: *
005: * File:CLIPOption.java
006: *
007: * Copyright (c) 2001 Sun Microsystems, Inc.
008: * All rights reserved.
009: *
010: * Date - Dec/15/2001
011: * Author - alejandro.abdelnur@sun.com
012: * @author Raja Nagendra Kumar, Nagendra.Raja@sun.com
013: */
014: package com.sun.portal.rewriter.util.clip;
015:
016: import com.sun.portal.rewriter.util.i18n.LocaleHelper;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.List;
021: import java.util.StringTokenizer;
022:
023: /**
024: * The Option class defines a CLIP option.
025: * <P>
026: * It defines the long and short (if exists) names, if the option is mandatory, its type and default value (if any).
027: * <P>
028: */
029: public class CLIPOption {
030: static final CLIPOption[] EMTPY_CLIP_OPTION_ARRAY = new CLIPOption[0];
031:
032: private String longName;
033: private String shortName;
034: private boolean mandatory;
035: private int type;
036: private String[] defaultValues = null;
037: private final String helpMessageID;
038:
039: /**
040: * Creates a CLIPParser Option.
041: * <P>
042: * @param aLongName option's long name, it can not be NULL and it has to be at least 2 characters.
043: * @param aShortName option's short name, it can be undefined (NULL) or it has to be exactly one character.
044: * @param aType indicates the type of the option, it can be BOOLEAN, REGULAR or COLLECTION.
045: * @param aDefaultValues specifies the option default value if any, NULL indicates no default value.
046: * If the option has not default value the option is mandatory.
047: * If the option is a collection the elements have to be separated with commas or white spaces,
048: * if a white space is present then the commas are consider part of a single value.
049: * For boolean options the default value can be 'true' or 'false'.
050: * @param aHelpMessageID help message for the option.
051: * @throws IllegalArgumentException if the information passed is invalid.
052: */
053: public CLIPOption(final String aLongName, final String aShortName,
054: final int aType, final String aDefaultValues,
055: final String aHelpMessageID)
056: throws IllegalArgumentException {
057: setLongName(aLongName);
058: setShortName(aShortName);
059: setType(aType);
060: setDefaultValues(aType, aDefaultValues, aShortName);
061: helpMessageID = aHelpMessageID;
062: }//constructor
063:
064: public String getLongName() {
065: return longName;
066: }//getLongName()
067:
068: public String getShortName() {
069: return shortName;
070: }//getShortName()
071:
072: public boolean isMandatory() {
073: return mandatory;
074: }//isManadatory()
075:
076: public int getType() {
077: return type;
078: }//getType()
079:
080: public String[] getDefaultValues() {
081: return defaultValues;
082: }//getDefaultValues()
083:
084: public String getHelpMessage() {
085: return CLIPSpec.getLocaleHelper().getLocalizedString(
086: helpMessageID);
087: }//getHelpMessage()
088:
089: private void setLongName(final String aLongName) {
090: CLIPParser.verifyName(aLongName,
091: "CLIPOption - Option's long name");
092: longName = aLongName.trim();
093: }//setLongName()
094:
095: private void setMandatory(final String aDefaultValues) {
096: mandatory = (aDefaultValues == null || aDefaultValues.trim()
097: .equalsIgnoreCase(""));
098: }//setMandatory()
099:
100: private void setShortName(final String aShortName) {
101: if (aShortName == null) {
102: return;
103: }
104:
105: if (aShortName.length() > 1) {
106: throw new IllegalArgumentException(
107: "CLIPOption - Option's short name has to be undefined (NULL) or one character long");
108: }
109:
110: if (aShortName.equals("-")) {
111: throw new IllegalArgumentException(
112: "CLIPOption - Option's short name can not be '-'");
113: }
114:
115: shortName = aShortName.trim();
116: }//setShortName()
117:
118: private void setType(final int aType) {
119: type = aType;
120: }//setType()
121:
122: private void setDefaultValues(final int aType,
123: final String aDefaultValues, final String aShortName) {
124: setMandatory(aDefaultValues);
125:
126: //let the default value be set only if this is not a mandatory option
127: if (isMandatory()) {
128: return;
129: }
130:
131: switch (aType) {
132: case CLIPConstants.BOOLEAN: {
133: String value = aDefaultValues.trim();
134: if (!value.equals("true") && !value.equals("false")) {
135: throw new IllegalArgumentException(
136: "CLIPOption - Boolean default value has to be 'true' or 'false'");
137: }
138: defaultValues = new String[1];
139: defaultValues[0] = value;
140: break;
141: }
142: case CLIPConstants.REGULAR: {
143: defaultValues = new String[1];
144: defaultValues[0] = aDefaultValues;
145: break;
146: }
147: case CLIPConstants.COLLECTION: {
148: if (aShortName != null) {
149: throw new IllegalArgumentException(
150: "CLIPOption - Collection options can not have short names");
151: }
152: List lCollection = new ArrayList();
153: String tokenSeparator = (aDefaultValues.indexOf(" ") > -1) ? " "
154: : ",";
155: StringTokenizer tokens = new StringTokenizer(
156: aDefaultValues, tokenSeparator);
157: while (tokens.hasMoreTokens()) {
158: String token = tokens.nextToken();
159: lCollection.add(token);
160: }
161: defaultValues = new String[lCollection.size()];
162: lCollection.toArray(defaultValues);
163: break;
164: }
165: default: {
166: throw new IllegalArgumentException(
167: "CLIPOption - Invalid type");
168: }
169: }//switch/case
170: }//setDefaultValues()
171:
172: public String toString() {
173: return "\nLongName : " + getLongName() + "\n" + "ShortName : "
174: + getShortName() + "\n" + "Mandatory: " + isMandatory()
175: + "\n" + "Type: " + getType() + "\n"
176: + "DefaultValues: " + Arrays.asList(getDefaultValues())
177: + "\n" + "HelpMessage: " + getHelpMessage() + "\n";
178: }//toString()
179:
180: public static void main(String[] args) {
181: LocaleHelper.store("psrwCLI", LocaleHelper.getLocale(null));
182: CLIPOption clipOption = new CLIPOption("version", null,
183: CLIPConstants.COLLECTION, "a b c d e", "TestMessage");
184: System.out.println(clipOption.getHelpMessage());
185: }//main()
186: }//class CLIPOption
|