1010import act .util .PropertySpec ;
1111import com .alibaba .fastjson .serializer .SerializeFilter ;
1212import com .alibaba .fastjson .serializer .SerializerFeature ;
13+ import com .thoughtworks .xstream .XStream ;
1314import org .osgl .$ ;
1415import org .osgl .util .C ;
1516import 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}
0 commit comments