Skip to content

Commit ae47b4d

Browse files
committed
support CSV view
1 parent d40396b commit ae47b4d

15 files changed

Lines changed: 184 additions & 67 deletions

‎pom.xml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,12 @@
365365
<version>1.2.7</version>
366366
</dependency>
367367

368+
<dependency>
369+
<groupId>com.thoughtworks.xstream</groupId>
370+
<artifactId>xstream</artifactId>
371+
<version>1.4.8</version>
372+
</dependency>
373+
368374
<dependency>
369375
<groupId>com.jamonapi</groupId>
370376
<artifactId>jamon</artifactId>
@@ -432,7 +438,6 @@
432438
<version>1.10</version>
433439
</dependency>
434440

435-
436441
<dependency>
437442
<groupId>com.cronutils</groupId>
438443
<artifactId>cron-utils</artifactId>

‎src/main/java/act/app/ActionContext.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ public boolean isJSON() {
159159
return accept() == H.Format.JSON;
160160
}
161161

162+
public boolean isXML() {
163+
return accept() == H.Format.XML;
164+
}
165+
162166
public boolean isAjax() {
163167
return req().isAjax();
164168
}

‎src/main/java/act/cli/CsvView.java‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package act.cli;
2+
3+
import act.util.PropertySpec;
4+
5+
import java.lang.annotation.ElementType;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
/**
11+
* Mark a command method return value shall be displayed using a csv file format. e.g
12+
* <pre>
13+
* id,onetime,trigger
14+
* __act_app_stop,true,null
15+
* __act_app_app_act_plugin_loaded,true,null
16+
* </pre>
17+
* <p>
18+
* {@code CsvView} can be used in conjunction with {@link PropertySpec}
19+
* to export only specified fields
20+
* </p>
21+
* <p>
22+
* Note if a method is marked with neither {@link CsvView} nor
23+
* {@link JsonView} then the console will simply use
24+
* {@link Object#toString()} to present the data.
25+
* </p>
26+
* @see JsonView
27+
* @see TableView
28+
* @see PropertySpec
29+
*/
30+
@Retention(RetentionPolicy.CLASS)
31+
@Target(ElementType.METHOD)
32+
public @interface CsvView {
33+
}

‎src/main/java/act/cli/HelpMsg.java‎

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎src/main/java/act/cli/XmlView.java‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package act.cli;
2+
3+
import act.util.PropertySpec;
4+
5+
import java.lang.annotation.ElementType;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
/**
11+
* Mark a command method return value shall be displayed as a XML object. e.g
12+
* <pre>
13+
* {@code
14+
* <item>
15+
* <id>__act_app_stop</id>
16+
* <onetime>true</onetime>
17+
* <trigger>null</trigger>
18+
* </item>
19+
* <item>
20+
* <id>__act_app_app_act_plugin_loaded</id>
21+
* <onetime>true</onetime>
22+
* <trigger>null</trigger>
23+
* </item>}
24+
* </pre>
25+
* <p>
26+
* {@code XmlView} can be used in conjunction with {@link PropertySpec}
27+
* to export only specified fields
28+
* </p>
29+
* @see TableView
30+
* @see JsonView
31+
* @see CsvView
32+
* @see PropertySpec
33+
*/
34+
@Retention(RetentionPolicy.CLASS)
35+
@Target(ElementType.METHOD)
36+
public @interface XmlView {
37+
}

‎src/main/java/act/cli/bytecode/CliHandlerScanner.java‎

Lines changed: 0 additions & 4 deletions
This file was deleted.

‎src/main/java/act/cli/bytecode/CommanderByteCodeScanner.java‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ public void visitEnd() {
246246
super.visitEnd();
247247
}
248248
};
249+
} else if ($.eq(AsmTypes.CSV_VIEW.asmType(), type)) {
250+
methodInfo.view(CliView.CSV);
251+
return super.visitAnnotation(desc, visible);
249252
} else if ($.eq(AsmTypes.TREE_VIEW.asmType(), type)) {
250253
methodInfo.view(CliView.TREE);
251254
return super.visitAnnotation(desc, visible);
@@ -255,16 +258,6 @@ public void visitEnd() {
255258
} else if ($.eq(AsmTypes.JSON_VIEW.asmType(), type)) {
256259
methodInfo.view(CliView.JSON);
257260
return super.visitAnnotation(desc, visible);
258-
} else if ($.eq(AsmTypes.HELP_MSG.asmType(), type)) {
259-
return new AnnotationVisitor(ASM5, av) {
260-
@Override
261-
public void visit(String name, Object value) {
262-
super.visit(name, value);
263-
if (S.eq("value", name)) {
264-
methodInfo.helpMsg(S.string(value));
265-
}
266-
}
267-
};
268261
} else if ($.eq(AsmTypes.PROPERTY_SPEC.asmType(), type)) {
269262
final PropertySpec.MetaInfo propSpec = new PropertySpec.MetaInfo();
270263
methodInfo.propertySpec(propSpec);

‎src/main/java/act/cli/view/CliView.java‎

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import act.util.PropertySpec;
1111
import com.alibaba.fastjson.serializer.SerializeFilter;
1212
import com.alibaba.fastjson.serializer.SerializerFeature;
13+
import com.thoughtworks.xstream.XStream;
1314
import org.osgl.$;
1415
import org.osgl.util.C;
1516
import org.osgl.util.E;
@@ -38,7 +39,7 @@ public String render(Object result, PropertySpec.MetaInfo spec, ActContext conte
3839
}
3940

4041
if (null == spec) {
41-
// TODO: support Table View when filter annotation is not presented
42+
// TODO support Table View when filter annotation is not presented
4243
return TO_STRING.render(result, null, context);
4344
}
4445

@@ -48,17 +49,8 @@ public String render(Object result, PropertySpec.MetaInfo spec, ActContext conte
4849

4950
CliContext cliContext = (CliContext) context;
5051

51-
List dataList;
52+
List dataList = toList(result);
5253
Class<?> componentType;
53-
if (result instanceof Iterable) {
54-
dataList = C.list((Iterable) result);
55-
} else if (result instanceof Iterator) {
56-
dataList = C.list((Iterator) result);
57-
} else if (result instanceof Enumeration) {
58-
dataList = C.list((Enumeration) result);
59-
} else {
60-
dataList = C.listOf(result);
61-
}
6254
if (dataList.isEmpty()) {
6355
return "no data";
6456
}
@@ -119,23 +111,52 @@ private void buildTree(StringBuilder sb, TreeNode node, String prefix, boolean i
119111
}
120112
},
121113

114+
/**
115+
* Present the result using {@link act.cli.XmlView}
116+
*/
117+
XML() {
118+
@Override
119+
public String render(Object result, PropertySpec.MetaInfo spec, ActContext context) {
120+
if (null == result) {
121+
return "<result></result>";
122+
}
123+
List dataList = toList(result);
124+
if (dataList.isEmpty()) {
125+
return "<result></result>";
126+
}
127+
boolean isList = dataList.get(0) != result;
128+
XStream xStream = new XStream();
129+
Class c = dataList.get(0).getClass();
130+
if (isList) {
131+
xStream.alias("result", List.class);
132+
xStream.alias(c.getSimpleName(), c);
133+
} else {
134+
xStream.alias("result", c);
135+
}
136+
if (null == spec) {
137+
return xStream.toXML(result);
138+
}
139+
return xStream.toXML(result);
140+
}
141+
},
142+
122143
/**
123144
* Present the result using {@link act.cli.JsonView}
124145
*/
125146
JSON () {
126147
@Override
127-
public String render(Object result, PropertySpec.MetaInfo filter, ActContext context) {
128-
return render(result, filter, context, context instanceof CliContext);
148+
public String render(Object result, PropertySpec.MetaInfo spec, ActContext context) {
149+
return render(result, spec, context, context instanceof CliContext);
129150
}
130151

131-
public String render(Object result, PropertySpec.MetaInfo filter, ActContext context, boolean format) {
152+
public String render(Object result, PropertySpec.MetaInfo spec, ActContext context, boolean format) {
132153
String json;
133-
if (null == filter) {
154+
if (null == spec) {
134155
json = com.alibaba.fastjson.JSON.toJSONString(result, SerializerFeature.PrettyFormat);
135156
} else {
136157
FastJsonPropertyPreFilter propertyFilter = new FastJsonPropertyPreFilter();
137-
List<String> outputs = filter.outputFields(context);
138-
Set<String> excluded = filter.excludedFields(context);
158+
List<String> outputs = spec.outputFields(context);
159+
Set<String> excluded = spec.excludedFields(context);
139160
if (excluded.isEmpty()) {
140161
if (outputs.isEmpty()) {
141162
propertyFilter = null; // no filter defined actually
@@ -155,7 +176,7 @@ public String render(Object result, PropertySpec.MetaInfo filter, ActContext con
155176
}
156177
}
157178

158-
MappedFastJsonNameFilter nameFilter = new MappedFastJsonNameFilter(filter.labelMapping());
179+
MappedFastJsonNameFilter nameFilter = new MappedFastJsonNameFilter(spec.labelMapping());
159180

160181
if (nameFilter.isEmpty()) {
161182
if (format) {
@@ -269,6 +290,7 @@ private String label(String key, Map<String, String> labels) {
269290
private String escape(Object o) {
270291
return Escape.CSV.apply(o).toString();
271292
}
293+
272294
};
273295

274296
public String render(Object result, PropertySpec.MetaInfo spec, ActContext context) {
@@ -278,4 +300,19 @@ public String render(Object result, PropertySpec.MetaInfo spec, ActContext conte
278300
public void print(Object result, PropertySpec.MetaInfo spec, CliContext context) {
279301
context.println(render(result, spec, context));
280302
}
303+
304+
protected List toList(Object result) {
305+
List dataList;
306+
Class<?> componentType;
307+
if (result instanceof Iterable) {
308+
dataList = C.list((Iterable) result);
309+
} else if (result instanceof Iterator) {
310+
dataList = C.list((Iterator) result);
311+
} else if (result instanceof Enumeration) {
312+
dataList = C.list((Enumeration) result);
313+
} else {
314+
dataList = C.listOf(result);
315+
}
316+
return dataList;
317+
}
281318
}

‎src/main/java/act/controller/Controller.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import act.view.*;
1111
import org.osgl.http.H;
1212
import org.osgl.mvc.result.*;
13+
import org.osgl.mvc.result.RenderXML;
1314
import org.osgl.storage.ISObject;
1415
import org.osgl.util.E;
1516
import org.osgl.util.IO;
@@ -569,9 +570,9 @@ public static Result inferResult(ISObject sobj, ActionContext context) {
569570
* to infer the {@code Result}</li>
570571
* </ul>
571572
*
572-
* @param v
573-
* @param actionContext
574-
* @return
573+
* @param v the value to be rendered
574+
* @param actionContext the action context
575+
* @return the rendered result
575576
*/
576577
public static Result inferResult(HandlerMethodMetaInfo meta, Object v, ActionContext actionContext, boolean hasTemplate) {
577578
if (null == v && !hasTemplate) {
@@ -613,6 +614,9 @@ public static Result inferResult(HandlerMethodMetaInfo meta, Object v, ActionCon
613614
return new RenderJSON(v);
614615
}
615616
return new FilteredRenderJSON(v, propertySpec, actionContext);
617+
} else if (actionContext.isXML()) {
618+
PropertySpec.MetaInfo propertySpec = (null == meta) ? null : meta.propertySpec();
619+
return new FilteredRenderXML(v, propertySpec, actionContext);
616620
} else if (actionContext.accept() == H.Format.CSV) {
617621
PropertySpec.MetaInfo propertySpec = (null == meta) ? null : meta.propertySpec();
618622
return new RenderCSV(v, propertySpec, actionContext);

‎src/main/java/act/data/DataPropertyRepository.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ private void buildPropertyPath(String context, Method m, List<String> repo) {
120120
}
121121
if (terminators.contains(c) || extendedTerminators.contains(c.getName())) {
122122
repo.add(context + propName);
123+
return;
123124
}
124125
List<String> retTypeProperties = propertyListOf(c);
125126
context = context + propName + ".";

0 commit comments

Comments
 (0)