1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.cli2.builder; |
18 | |
|
19 | |
import java.util.Iterator; |
20 | |
import java.util.LinkedHashSet; |
21 | |
import java.util.Set; |
22 | |
|
23 | |
import org.apache.commons.cli2.Argument; |
24 | |
import org.apache.commons.cli2.Option; |
25 | |
import org.apache.commons.cli2.validation.ClassValidator; |
26 | |
import org.apache.commons.cli2.validation.DateValidator; |
27 | |
import org.apache.commons.cli2.validation.FileValidator; |
28 | |
import org.apache.commons.cli2.validation.NumberValidator; |
29 | |
import org.apache.commons.cli2.validation.UrlValidator; |
30 | |
import org.apache.commons.cli2.validation.Validator; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class PatternBuilder { |
37 | |
|
38 | |
private final GroupBuilder gbuilder; |
39 | |
private final DefaultOptionBuilder obuilder; |
40 | |
private final ArgumentBuilder abuilder; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public PatternBuilder() { |
46 | 1 | this( |
47 | |
new GroupBuilder(), |
48 | |
new DefaultOptionBuilder(), |
49 | |
new ArgumentBuilder()); |
50 | 1 | } |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public PatternBuilder( |
59 | |
final GroupBuilder gbuilder, |
60 | |
final DefaultOptionBuilder obuilder, |
61 | 1 | final ArgumentBuilder abuilder) { |
62 | 1 | this.gbuilder = gbuilder; |
63 | 1 | this.obuilder = obuilder; |
64 | 1 | this.abuilder = abuilder; |
65 | 1 | } |
66 | |
|
67 | 1 | private final Set options = new LinkedHashSet(); |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public Option create() { |
74 | |
final Option option; |
75 | |
|
76 | 1 | if (options.size() == 1) { |
77 | 0 | option = (Option)options.iterator().next(); |
78 | |
} |
79 | |
else { |
80 | 1 | gbuilder.reset(); |
81 | 1 | for (final Iterator i = options.iterator(); i.hasNext();) { |
82 | 2 | gbuilder.withOption((Option)i.next()); |
83 | |
} |
84 | 1 | option = gbuilder.create(); |
85 | |
} |
86 | |
|
87 | 1 | reset(); |
88 | |
|
89 | 1 | return option; |
90 | |
} |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public PatternBuilder reset() { |
97 | 1 | options.clear(); |
98 | 1 | return this; |
99 | |
} |
100 | |
|
101 | |
private void createOption( |
102 | |
final char type, |
103 | |
final boolean required, |
104 | |
final char opt) { |
105 | |
final Argument argument; |
106 | 2 | if (type != ' ') { |
107 | 1 | abuilder.reset(); |
108 | 1 | abuilder.withValidator(validator(type)); |
109 | 1 | if (required) { |
110 | 1 | abuilder.withMinimum(1); |
111 | |
} |
112 | 1 | if (type != '*') { |
113 | 1 | abuilder.withMaximum(1); |
114 | |
} |
115 | 1 | argument = abuilder.create(); |
116 | |
} |
117 | |
else { |
118 | 1 | argument = null; |
119 | |
} |
120 | |
|
121 | 2 | obuilder.reset(); |
122 | 2 | obuilder.withArgument(argument); |
123 | 2 | obuilder.withShortName(String.valueOf(opt)); |
124 | 2 | obuilder.withRequired(required); |
125 | |
|
126 | 2 | options.add(obuilder.create()); |
127 | 2 | } |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
public void withPattern(final String pattern) { |
134 | 1 | int sz = pattern.length(); |
135 | |
|
136 | 1 | char opt = ' '; |
137 | 1 | char ch = ' '; |
138 | 1 | char type = ' '; |
139 | 1 | boolean required = false; |
140 | |
|
141 | 5 | for (int i = 0; i < sz; i++) { |
142 | 4 | ch = pattern.charAt(i); |
143 | |
|
144 | 4 | switch (ch) { |
145 | |
case '!' : |
146 | 1 | required = true; |
147 | 1 | break; |
148 | |
case '@' : |
149 | |
case ':' : |
150 | |
case '%' : |
151 | |
case '+' : |
152 | |
case '#' : |
153 | |
case '<' : |
154 | |
case '>' : |
155 | |
case '*' : |
156 | |
case '/' : |
157 | 1 | type = ch; |
158 | 1 | break; |
159 | |
default : |
160 | 2 | if (opt != ' ') { |
161 | 1 | createOption(type, required, opt); |
162 | 1 | required = false; |
163 | 1 | type = ' '; |
164 | |
} |
165 | |
|
166 | 2 | opt = ch; |
167 | |
} |
168 | |
} |
169 | |
|
170 | 1 | if (opt != ' ') { |
171 | 1 | createOption(type, required, opt); |
172 | |
} |
173 | 1 | } |
174 | |
|
175 | |
private static Validator validator(final char c) { |
176 | 1 | switch (c) { |
177 | |
case '@' : |
178 | 0 | final ClassValidator classv = new ClassValidator(); |
179 | 0 | classv.setInstance(true); |
180 | 0 | return classv; |
181 | |
case '+' : |
182 | 0 | final ClassValidator instancev = new ClassValidator(); |
183 | 0 | return instancev; |
184 | |
|
185 | |
case '%' : |
186 | 0 | return NumberValidator.getNumberInstance(); |
187 | |
case '#' : |
188 | 0 | return DateValidator.getDateInstance(); |
189 | |
case '<' : |
190 | 1 | final FileValidator existingv = new FileValidator(); |
191 | 1 | existingv.setExisting(true); |
192 | 1 | existingv.setFile(true); |
193 | 1 | return existingv; |
194 | |
case '>' : |
195 | |
case '*' : |
196 | 0 | return new FileValidator(); |
197 | |
case '/' : |
198 | 0 | return new UrlValidator(); |
199 | |
default : |
200 | 0 | return null; |
201 | |
} |
202 | |
} |
203 | |
} |