|
1 |
package org.gvsig.tools.arguments;
|
|
2 |
|
|
3 |
import java.util.ArrayList;
|
|
4 |
import java.util.Collections;
|
|
5 |
import java.util.Date;
|
|
6 |
import java.util.HashMap;
|
|
7 |
import java.util.Iterator;
|
|
8 |
import java.util.List;
|
|
9 |
import java.util.Map;
|
|
10 |
import java.util.Objects;
|
|
11 |
import org.apache.commons.lang3.BooleanUtils;
|
|
12 |
import org.apache.commons.lang3.StringUtils;
|
|
13 |
import org.gvsig.tools.dataTypes.DataTypeUtils;
|
|
14 |
import static org.gvsig.tools.dataTypes.DataTypeUtils.coerce;
|
|
15 |
import org.gvsig.tools.dataTypes.DataTypes;
|
|
16 |
|
|
17 |
public class DefaultArguments implements Arguments {
|
|
18 |
|
|
19 |
private final Object[] args;
|
|
20 |
protected Map<String, Object> flags;
|
|
21 |
protected List<Object> parameters;
|
|
22 |
|
|
23 |
public DefaultArguments(Object args[]) {
|
|
24 |
this.args = args;
|
|
25 |
this.flags = new HashMap<>();
|
|
26 |
this.parameters = new ArrayList<>();
|
|
27 |
this.parse();
|
|
28 |
}
|
|
29 |
|
|
30 |
private void parse() {
|
|
31 |
String name;
|
|
32 |
Object value;
|
|
33 |
boolean acceptFlags = true;
|
|
34 |
String currentName = null;
|
|
35 |
for( Object arg : args ) {
|
|
36 |
if(currentName != null){
|
|
37 |
this.flags.put(currentName.toLowerCase(), arg);
|
|
38 |
currentName = null;
|
|
39 |
continue;
|
|
40 |
}
|
|
41 |
if( acceptFlags ) {
|
|
42 |
if( arg.equals("--") || !(arg instanceof String) ) {
|
|
43 |
acceptFlags = false;
|
|
44 |
continue;
|
|
45 |
}
|
|
46 |
String strArg = Objects.toString(arg);
|
|
47 |
if( strArg.startsWith("--") ) {
|
|
48 |
int n = strArg.indexOf("=");
|
|
49 |
if( n < 0 ) {
|
|
50 |
name = strArg.substring(2);
|
|
51 |
value = true;
|
|
52 |
this.flags.put(name.toLowerCase(), value);
|
|
53 |
} else {
|
|
54 |
name = strArg.substring(2, n);
|
|
55 |
if(strArg.length()-1==n){
|
|
56 |
currentName = name;
|
|
57 |
} else {
|
|
58 |
value = strArg.substring(n + 1);
|
|
59 |
this.flags.put(name.toLowerCase(), value);
|
|
60 |
}
|
|
61 |
}
|
|
62 |
} else {
|
|
63 |
this.parameters.add(arg);
|
|
64 |
}
|
|
65 |
} else {
|
|
66 |
this.parameters.add(arg);
|
|
67 |
}
|
|
68 |
}
|
|
69 |
}
|
|
70 |
|
|
71 |
@Override
|
|
72 |
public Object get(String name) {
|
|
73 |
if(StringUtils.isBlank(name)){
|
|
74 |
return null;
|
|
75 |
}
|
|
76 |
return this.flags.get(name.toLowerCase());
|
|
77 |
}
|
|
78 |
|
|
79 |
@Override
|
|
80 |
public Object get(String name, Object defaultValue) {
|
|
81 |
Object value = this.get(name);
|
|
82 |
if( defaultValue == null ) {
|
|
83 |
return value;
|
|
84 |
}
|
|
85 |
if( value == null || StringUtils.isBlank(Objects.toString(value, null)) ) {
|
|
86 |
return defaultValue;
|
|
87 |
}
|
|
88 |
if( defaultValue instanceof CharSequence ) {
|
|
89 |
return value;
|
|
90 |
}
|
|
91 |
if( defaultValue instanceof Integer ) {
|
|
92 |
return DataTypeUtils.toInteger(value, (Integer) defaultValue);
|
|
93 |
}
|
|
94 |
if( defaultValue instanceof Long ) {
|
|
95 |
return DataTypeUtils.toLong(value, (Long) defaultValue);
|
|
96 |
}
|
|
97 |
if( defaultValue instanceof Boolean ) {
|
|
98 |
try {
|
|
99 |
return DataTypeUtils.toBoolean(value, (Boolean) defaultValue);
|
|
100 |
} catch(Throwable th) {
|
|
101 |
return BooleanUtils.toBoolean(Objects.toString(value));
|
|
102 |
}
|
|
103 |
}
|
|
104 |
if( defaultValue instanceof Double ) {
|
|
105 |
return DataTypeUtils.toDouble(value, (Double) defaultValue);
|
|
106 |
}
|
|
107 |
if( defaultValue instanceof Float ) {
|
|
108 |
return DataTypeUtils.toDouble(value, (Float) defaultValue);
|
|
109 |
}
|
|
110 |
if( defaultValue instanceof Date ) {
|
|
111 |
return DataTypeUtils.toDate(value, (Date) defaultValue);
|
|
112 |
}
|
|
113 |
return value;
|
|
114 |
}
|
|
115 |
|
|
116 |
@Override
|
|
117 |
public boolean contains(String name) {
|
|
118 |
if(StringUtils.isBlank(name)){
|
|
119 |
return false;
|
|
120 |
}
|
|
121 |
return this.flags.containsKey(name.toLowerCase());
|
|
122 |
}
|
|
123 |
|
|
124 |
@Override
|
|
125 |
public boolean contains(String name, Object value) {
|
|
126 |
if(StringUtils.isBlank(name)){
|
|
127 |
return false;
|
|
128 |
}
|
|
129 |
Object x = this.flags.get(name.toLowerCase());
|
|
130 |
if( x==null ) {
|
|
131 |
return false;
|
|
132 |
}
|
|
133 |
if(value instanceof String && x instanceof String){
|
|
134 |
return StringUtils.equalsIgnoreCase((String)value, (String)x);
|
|
135 |
}
|
|
136 |
if(value instanceof Boolean){
|
|
137 |
return value == DataTypeUtils.coerce(DataTypes.BOOLEAN, x, null);
|
|
138 |
}
|
|
139 |
return Objects.equals(value, x);
|
|
140 |
|
|
141 |
}
|
|
142 |
|
|
143 |
@Override
|
|
144 |
public List<Object> parameters() {
|
|
145 |
return Collections.unmodifiableList(parameters);
|
|
146 |
}
|
|
147 |
|
|
148 |
@Override
|
|
149 |
public Map<String, Object> flags() {
|
|
150 |
return Collections.unmodifiableMap(flags);
|
|
151 |
}
|
|
152 |
|
|
153 |
@Override
|
|
154 |
public int size() {
|
|
155 |
return this.parameters.size();
|
|
156 |
}
|
|
157 |
|
|
158 |
@Override
|
|
159 |
public Object get(int n) {
|
|
160 |
return this.parameters.get(n);
|
|
161 |
}
|
|
162 |
|
|
163 |
@Override
|
|
164 |
public Iterator<Object> iterator() {
|
|
165 |
return this.parameters.iterator();
|
|
166 |
}
|
|
167 |
|
|
168 |
@Override
|
|
169 |
public Object get(int n, Class elementClass, Object defaultValue) {
|
|
170 |
if(n >= this.parameters.size() || n < 0){
|
|
171 |
return defaultValue;
|
|
172 |
}
|
|
173 |
Object o = this.parameters.get(n);
|
|
174 |
if (elementClass.isInstance(o)) {
|
|
175 |
return o;
|
|
176 |
}
|
|
177 |
return defaultValue;
|
|
178 |
}
|
|
179 |
|
|
180 |
@Override
|
|
181 |
public List getListOf(int n, Class elementClass) {
|
|
182 |
List list = (List) get(n, List.class, null);
|
|
183 |
if (list == null) {
|
|
184 |
return null;
|
|
185 |
}
|
|
186 |
if (list.isEmpty()) {
|
|
187 |
return null;
|
|
188 |
}
|
|
189 |
if (elementClass.isInstance(list.get(0))) {
|
|
190 |
return list;
|
|
191 |
}
|
|
192 |
return null;
|
|
193 |
}
|
|
194 |
|
|
195 |
@Override
|
|
196 |
public Object get(String name, Class elementClass, Object defaultValue) {
|
|
197 |
Object o = this.flags.get(name.toLowerCase());
|
|
198 |
if (elementClass.isInstance(o)) {
|
|
199 |
return o;
|
|
200 |
}
|
|
201 |
return defaultValue;
|
|
202 |
}
|
|
203 |
|
|
204 |
@Override
|
|
205 |
public List getListOf(String name, Class elementClass) {
|
|
206 |
List list = (List) get(name, List.class, null);
|
|
207 |
if (list == null) {
|
|
208 |
return null;
|
|
209 |
}
|
|
210 |
if (list.isEmpty()) {
|
|
211 |
return null;
|
|
212 |
}
|
|
213 |
if (elementClass.isInstance(list.get(0))) {
|
|
214 |
return list;
|
|
215 |
}
|
|
216 |
return null;
|
|
217 |
}
|
|
218 |
|
|
219 |
}
|