1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * ident "%Z%%M% %I% %E% SMI"
24 *
25 * Copyright (c) 2001 by Sun Microsystems, Inc.
26 * All rights reserved.
27 */
28 package com.sun.dhcpmgr.cli.common;
29
30 import java.lang.IllegalArgumentException;
31 import java.util.ArrayList;
32 import java.text.MessageFormat;
33
34 /**
35 * This class is used to represent all the command line options.
36 */
37 public class DhcpCliOptions {
38
39 /**
40 * Actual container for the options.
41 */
42 ArrayList options;
43
44 /**
45 * Basic constructor.
46 */
47 public DhcpCliOptions() {
48
49 options = new ArrayList();
50
51 } // constructor
52
53 /**
54 * Creates a new option and adds it to the list.
55 * @param flag the option key value
56 * @param value the option value
57 */
58 public void setOption(int flag, String value)
59 throws IllegalArgumentException {
60
61 DhcpCliOption option = new DhcpCliOption(flag, value);
62 int i = options.indexOf(option);
63 if (i == -1) {
64 options.add(option);
65 } else {
66 String format =
67 ResourceStrings.getString("repeated_option");
68 Object[] args = new Object[1];
69 args[0] = option.getOptionCharacter();
70 String msg = MessageFormat.format(format, args);
71 throw new IllegalArgumentException(msg);
72 }
73
74 } // setOption
75
76 /**
77 * Reports as to whether or not an option exists in the list.
78 * @param flag the option key value
79 * @return true if the option is in the list, false if not
80 */
81 public boolean isSet(int flag) {
82
83 DhcpCliOption option = new DhcpCliOption(flag);
84 return options.contains(option);
85
86 } // isSet
87
88 /**
89 * Returns the value of an option.
90 * @param flag the option key value
91 * @return the value of the option or null if the option is not
92 * in the list.
93 */
94 public String valueOf(int flag) {
95
96 DhcpCliOption option = new DhcpCliOption(flag);
97
98 int i = options.indexOf(option);
99 if (i != -1) {
100 return ((DhcpCliOption)options.get(i)).getValue();
101 } else {
102 return null;
103 }
104 } // valueOf
105
106 /**
107 * Given a list of supported options, validates that there are no
108 * options in the in the option list that are not supported.
109 * @param supportedOptions array of option key values
110 * @return true if there are no options in the option list that are
111 * not in the supportedOptions, false otherwise.
112 */
113 public void validate(int [] supportedOptions)
114 throws IllegalArgumentException {
115
116 boolean result = true;
117 int option = 0;
118
119 for (int i = 0; i < options.size() && result; i++) {
120 option = ((DhcpCliOption)options.get(i)).getOption();
121 result = false;
122 for (int j = 0; j < supportedOptions.length && !result; j++) {
123 if (supportedOptions[j] == option) {
124 result = true;
125 }
126 }
127 }
128
129 if (!result) {
130 String format =
131 ResourceStrings.getString("bad_function_option");
132 Object[] args = new Object[1];
133 args[0] = DhcpCliOption.getOptionCharacter(option);
134 String msg = MessageFormat.format(format, args);
135 throw new IllegalArgumentException(msg);
136 }
137
138 } // validate
139
140 } // DhcpCliOptions