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:CLIPSubCommand.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 java.util.ArrayList;
017: import java.util.HashMap;
018: import java.util.List;
019: import java.util.Map;
020:
021: /**
022: * The SubCommand class defines a CLIP sub command.
023: * <P>
024: * A sub command has a set of options, a mininum and maximum number of operands
025: * and help messages.
026: * <P>
027: */
028: public class CLIPSubCommand {
029: private String subCommandName;
030: private List orderedOptions;
031: private Map validOptions;
032: private Map defaultOptionValues;
033: private int minOperands;
034: private int maxOperands;
035:
036: private final String operandsHelpID;
037: private final String subCommandHelpID;
038:
039: /**
040: * Creates a CLIPParser Option.
041: * <P>
042: *
043: * @param aSubCommandName name of the sub command or NULL if no sub command are expected for this CLIPParser.
044: * @param aOptions array with all the valid options for the sub command, or NULL if the sub command expects no options.
045: * @param aMinOperands minimum number of operands the sub command expects.
046: * @param aMaxOperands maximum number of operands the sub command expects.
047: * @param aSubCommandHelpID help message for the sub command.
048: * @param aOperandsHelpID help message for the operands.
049: * @throws IllegalArgumentException if invalid information is passed.
050: */
051: public CLIPSubCommand(final String aSubCommandName,
052: final CLIPOption[] aOptions, final int aMinOperands,
053: final int aMaxOperands, final String aOperandsHelpID,
054: final String aSubCommandHelpID)
055: throws IllegalArgumentException {
056: setSubCommandName(aSubCommandName);
057: setOptions(aSubCommandName, aOptions);
058: setMinOperands(aSubCommandName, aMinOperands);
059: setMaxOperands(aSubCommandName, aMinOperands, aMaxOperands);
060: subCommandHelpID = aSubCommandHelpID;
061: operandsHelpID = aOperandsHelpID;
062: }//constructor
063:
064: public String getSubCommandName() {
065: return subCommandName;
066: }//getSubCommandName()
067:
068: public List getOrderedOptions() {
069: return orderedOptions;
070: }//getOrderedOptions()
071:
072: public Map getValidOptions() {
073: return validOptions;
074: }//getValidOptions()
075:
076: public Map getDefaultOptionValues() {
077: return defaultOptionValues;
078: }//getDefaultOptionValues()
079:
080: public int getMinOperands() {
081: return minOperands;
082: }//getMinOperands()
083:
084: public int getMaxOperands() {
085: return maxOperands;
086: }//getMaxOperands()
087:
088: public String getSubCommandHelp() {
089: return CLIPSpec.getLocaleHelper().getLocalizedString(
090: subCommandHelpID);
091: }//getSubCommandHelp()
092:
093: public String getOperandsHelp() {
094: return CLIPSpec.getLocaleHelper().getLocalizedString(
095: operandsHelpID);
096: }//getOperandsHelp()
097:
098: private void setSubCommandName(final String aName) {
099: if (aName != null) {
100: CLIPParser.verifyName(aName, "CLIPSubCommand");
101: }
102: subCommandName = aName;
103: }//setSubCommandName()
104:
105: private void setOptions(final String aSubCommandName,
106: final CLIPOption[] aOptions) {
107: final CLIPOption helpOption = new CLIPOption("help", null,
108: CLIPConstants.BOOLEAN, "false", "optionHelp");
109:
110: validOptions = new HashMap();
111: orderedOptions = new ArrayList();
112:
113: validOptions.put(helpOption.getLongName(), helpOption);
114: orderedOptions.add(helpOption);
115:
116: if (aOptions != null) {
117: for (int i = 0; i < aOptions.length; i++) {
118: if (aOptions[i] == null) {
119: throw new IllegalArgumentException(
120: "CLIPSubCommand: "
121: + aSubCommandName
122: + "', option instances can not be NULL");
123: }
124: if (validOptions.containsKey(aOptions[i].getLongName())) {
125: throw new IllegalArgumentException(
126: "CLIPSubCommand: "
127: + aSubCommandName
128: + "', option long names can not be duplicated within a CLIPParser instance");
129: }
130: validOptions
131: .put(aOptions[i].getLongName(), aOptions[i]);
132: orderedOptions.add(aOptions[i]);
133: if (aOptions[i].getShortName() != null) {
134: if (validOptions.containsKey(aOptions[i]
135: .getShortName())) {
136: throw new IllegalArgumentException(
137: "CLIPSubCommand: "
138: + aSubCommandName
139: + "', option short names can not be duplicated within a CLIPParser instance");
140: }
141: validOptions.put(aOptions[i].getShortName(),
142: aOptions[i]);
143: }
144: }//for loop
145: }//if
146: setDefaultOptionValues(aOptions);
147: }//setOptions()
148:
149: private void setDefaultOptionValues(final CLIPOption[] aOptions) {
150: defaultOptionValues = new HashMap();
151: if (aOptions != null) {
152: for (int i = 0; i < aOptions.length; i++) {
153: String[] defaultValues = aOptions[i].getDefaultValues();
154: if (defaultValues != null) {
155: List l = new ArrayList();
156: for (int j = 0; j < defaultValues.length; j++) {
157: l.add(defaultValues[j]);
158: }
159: defaultOptionValues.put(aOptions[i].getLongName(),
160: l);
161: }
162: }
163: }
164: }//setDefaultOptionValues()
165:
166: private void setMinOperands(final String aSubCommandName,
167: final int aMinOperands) {
168: if (aMinOperands < 0) {
169: throw new IllegalArgumentException(
170: "CLIPSubCommand: "
171: + aSubCommandName
172: + "', minimum number of operands can not be less than zero");
173: }
174: minOperands = aMinOperands;
175: }//setMinOperands()
176:
177: private void setMaxOperands(final String aSubCommandName,
178: final int aMinOperands, final int aMaxOperands) {
179: if (aMinOperands > aMaxOperands) {
180: throw new IllegalArgumentException(
181: "CLIPSubCommand: "
182: + aSubCommandName
183: + "', maximum number of operands can not be less than minimum number of operands");
184: }
185: maxOperands = aMaxOperands;
186: }//setMaxOperands()
187:
188: public String toString() {
189: return "\nSubCommandName: " + subCommandName + "\n"
190: + "OrderedOptions: " + orderedOptions + "\n"
191: + "Valid Options: " + validOptions + "\n"
192: + "Default Option Values: " + defaultOptionValues
193: + "\n" + "Min Operands: " + minOperands + "\n"
194: + "Max Operands: " + maxOperands + "\n"
195: + "Sub Command Help: " + getSubCommandHelp() + "\n"
196: + "Operands Help: " + getOperandsHelp() + "\n";
197: }//toString()
198: }//class CLIPSubCommand
|