forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCliView.java
More file actions
332 lines (300 loc) · 11.8 KB
/
CliView.java
File metadata and controls
332 lines (300 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package act.cli.view;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.cli.CliContext;
import act.cli.CliOverHttpContext;
import act.cli.ascii_table.impl.CollectionASCIITableAware;
import act.cli.tree.TreeNode;
import act.cli.util.CliCursor;
import act.cli.util.TableCursor;
import act.data.DataPropertyRepository;
import act.db.AdaptiveRecord;
import act.util.ActContext;
import act.util.JsonUtilConfig;
import act.util.PropertySpec;
import org.osgl.$;
import org.osgl.util.*;
import org.rythmengine.utils.Escape;
import java.io.Writer;
import java.util.*;
/**
* Define how the command method return result should
* be presented
*/
public enum CliView {
/**
* present the result using {@link act.cli.TableView}
*/
TABLE() {
@Override
@SuppressWarnings("unchecked")
public void render(Writer writer, Object result, PropertySpec.MetaInfo spec, ActContext context) {
if (null == result) {
return;
}
spec = PropertySpec.MetaInfo.withCurrent(spec, context);
if (null == spec) {
spec = new PropertySpec.MetaInfo();
}
if (!(context instanceof CliContext)) {
throw E.unsupport("TableView support in CliContext only. You context: ", context.getClass());
}
CliContext cliContext = (CliContext) context;
List dataList = toList(result);
int pageSize = context instanceof CliOverHttpContext ? dataList.size() : context.config().cliTablePageSize();
if (dataList.size() > pageSize) {
TableCursor cursor = new TableCursor(dataList, pageSize, spec);
cliContext.session().cursor(cursor);
cursor.output(cliContext);
return;
}
Class<?> componentType;
if (dataList.isEmpty()) {
return;
}
componentType = Object.class;
for (Object o : dataList) {
if (null != o) {
componentType = o.getClass();
break;
}
}
DataPropertyRepository repo = context.app().service(DataPropertyRepository.class);
List<String> outputFields = repo.outputFields(spec, componentType, context);
if (outputFields.isEmpty()) {
outputFields = C.list("this as Item");
}
String tableString = cliContext.getTable(new CollectionASCIITableAware(dataList, outputFields, spec.labels(outputFields, context)));
int itemsFound = dataList.size();
CliCursor cursor = cliContext.session().cursor();
String appendix = "";
if (null != cursor) {
itemsFound = cursor.records();
appendix = cursor.hasNext() ? "\nType \"it\" for more" : "";
}
IO.write(S.concat(tableString, "Items found: ", S.string(itemsFound), appendix), writer);
}
},
/**
* Present data in a Tree structure.
* <p>
* Note the {@code result} parameter must be a root {@link act.cli.tree.TreeNode node} of the tree,
* otherwise the data will be presented in
* </p>
* <ul>
* <li>{@link #TABLE Table view} if the result is an {@link Iterable}, or</li>
* <li>{@link #JSON JSON view} otherwise</li>
* </ul>
*/
TREE() {
@Override
public void render(Writer writer, Object result, PropertySpec.MetaInfo spec, ActContext context) {
if (result instanceof TreeNode) {
toTreeString(writer, (TreeNode) result);
} else if (result instanceof Iterable) {
TABLE.render(writer, result, spec, context);
} else if (null != spec) {
JSON.render(writer, result, spec, context);
} else {
IO.write(S.string(result), writer);
}
}
private void toTreeString(Writer writer, TreeNode result) {
buildTree(writer, result, "", true);
}
private void buildTree(Writer writer, TreeNode node, String prefix, boolean isTrail) {
StringBuilder sb = S.newBuilder().append(prefix).append(isTrail ? "└── " : "├── ").append(node.label()).append("\n");
IO.write(sb, writer);
List<TreeNode> children = node.children();
int sz = children.size();
if (sz == 0) {
return;
}
final String subPrefix = S.newBuilder().append(prefix).append(isTrail ? " " : "│ ").toString();
for (int i = 0; i < sz - 1; ++i) {
TreeNode child = children.get(i);
buildTree(writer, child, subPrefix, false);
}
buildTree(writer, children.get(sz - 1), subPrefix, true);
}
},
/**
* Present the result using {@link act.cli.XmlView}
*/
XML() {
@Override
public void render(Writer writer, Object result, PropertySpec.MetaInfo spec, ActContext context) {
throw E.unsupport();
}
},
/**
* Present the result using {@link act.util.JsonView}
*/
JSON() {
@Override
public void render(Writer writer, Object result, PropertySpec.MetaInfo spec, ActContext context) {
render(writer, result, spec, context, context instanceof CliContext);
}
public void render(Writer writer, Object result, PropertySpec.MetaInfo spec, ActContext context, boolean format) {
new JsonUtilConfig.JsonWriter(result, spec, format, context).apply(writer);
}
},
/**
* Present the result using {@link Object#toString()}
*/
TO_STRING() {
@Override
public void render(Writer writer, Object result, PropertySpec.MetaInfo filter, ActContext context) {
if (result instanceof Iterable) {
TABLE.render(writer, result, filter, context);
} else if (result instanceof TreeNode) {
TREE.render(writer, result, filter, context);
} else if (null != filter) {
JSON.render(writer, result, filter, context);
} else {
IO.write(S.string(result), writer);
}
}
},
CSV() {
@Override
public void render(Writer writer, Object result, PropertySpec.MetaInfo spec, ActContext context) {
if (null == result) {
return;
}
Iterator iterator;
Class<?> componentType;
if (result instanceof Iterable) {
iterator = ((Iterable) result).iterator();
} else if (result instanceof Iterator) {
iterator = (Iterator) result;
} else if (result instanceof Enumeration) {
final Enumeration enumeration = (Enumeration) result;
iterator = new Iterator() {
@Override
public boolean hasNext() {
return enumeration.hasMoreElements();
}
@Override
public Object next() {
return enumeration.nextElement();
}
@Override
public void remove() {
throw E.unsupport();
}
};
} else {
iterator = C.list(result).iterator();
}
Object firstElement = iterator.hasNext() ? iterator.next() : null;
if (null == firstElement) {
return;
}
componentType = firstElement.getClass();
DataPropertyRepository repo = context.app().service(DataPropertyRepository.class);
spec = PropertySpec.MetaInfo.withCurrent(spec, context);
if (null == spec) {
spec = new PropertySpec.MetaInfo();
spec.onValue("-not_exists");
}
List<String> outputFields = repo.outputFields(spec, componentType, context);
IO.write(buildHeaderLine(outputFields, spec.labelMapping()), writer);
IO.write($.OS.lineSeparator(), writer);
IO.write(buildDataLine(firstElement, outputFields), writer);
while (iterator.hasNext()) {
IO.write($.OS.lineSeparator(), writer);
IO.write(buildDataLine(iterator.next(), outputFields), writer);
IO.flush(writer);
}
}
private String buildDataLine(Object data, List<String> outputFields) {
Iterator<String> itr = outputFields.iterator();
String prop = itr.next();
S.Buffer buf = S.buffer();
buf.append(getProperty(data, prop));
while (itr.hasNext()) {
buf.append(",").append(getProperty(data, itr.next()));
}
return buf.toString();
}
private String getProperty(Object data, String prop) {
if ("this".equals(prop)) {
return (escape(data));
} else {
if (data instanceof AdaptiveRecord) {
return escape(S.string(((AdaptiveRecord) data).getValue(prop)));
}
return escape($.getProperty(data, prop));
}
}
private String buildHeaderLine(List<String> outputFields, Map<String, String> labels) {
if (null == labels) {
labels = new HashMap<>();
}
Iterator<String> itr = outputFields.iterator();
String label = label(itr.next(), labels);
S.Buffer buf = S.buffer();
buf.append(label);
while (itr.hasNext()) {
buf.append(",").append(escape(label(itr.next(), labels)));
}
return buf.toString();
}
private String label(String key, Map<String, String> labels) {
String s = labels.get(key);
return null == s ? key : s;
}
private String escape(Object o) {
if (null == o) {
return "";
}
String s = o.toString().trim();
if (s.startsWith("\"") && s.endsWith("\"")) {
return s;
}
return Escape.CSV.apply(o).toString();
}
};
public void render(Writer writer, Object result, PropertySpec.MetaInfo spec, ActContext context) {
throw E.unsupport();
}
public final String render(Object result, PropertySpec.MetaInfo spec, ActContext context) {
S.Buffer sb = S.buffer();
render($.convert(sb).to(Writer.class), result, spec, context);
return sb.toString();
}
public void print(Object result, PropertySpec.MetaInfo spec, CliContext context) {
context.println(render(result, spec, context));
}
protected List toList(Object result) {
List dataList;
if (result instanceof Iterable) {
dataList = C.list((Iterable) result);
} else if (result instanceof Iterator) {
dataList = C.list((Iterator) result);
} else if (result instanceof Enumeration) {
dataList = C.list((Enumeration) result);
} else {
dataList = C.listOf(result);
}
return dataList;
}
}