1 /**
2 * Copyright (c) 2002-2011 "Neo Technology,"
3 * Network Engine for Objects in Lund AB [http://neotechnology.com]
4 *
5 * This file is part of Neo4j.
6 *
7 * Neo4j is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 package org.neo4j.server.webadmin.console;
21
22 import groovy.lang.Closure;
23
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.Iterator;
27 import java.util.Map;
28
29 import org.codehaus.groovy.tools.shell.IO;
30
31 /**
32 * Version of ResultHookClosure that does not add ==> to the output. This is
33 * instead moved to the console or print stream to handle, so that all content
34 * printed to the output stream gets the same prompt.
35 *
36 * @author Marko A. Rodriguez (http://markorodriguez.com), Jacob Hansson
37 * <jacob@voltvoodoo.com>
38 */
39 public class GremlinResultHook extends Closure
40 {
41 /**
42 *
43 */
44 private static final long serialVersionUID = -8258126015903649440L;
45 private final IO io;
46
47 public GremlinResultHook( final Object owner, final IO io )
48 {
49 super( owner );
50 this.io = io;
51 }
52
53 @SuppressWarnings( "rawtypes" )
54 public Object call( final Object[] args )
55 {
56 Object result = args[0];
57 Iterator itty;
58 if ( result instanceof Iterator )
59 {
60 itty = (Iterator) result;
61 }
62 else if ( result instanceof Iterable )
63 {
64 itty = ( (Iterable) result ).iterator();
65 }
66 else if ( result instanceof Object[] )
67 {
68 itty = Arrays.asList( (Object[])result ).iterator();
69 }
70 else if ( result instanceof Map )
71 {
72 itty = ( (Map) result ).entrySet().iterator();
73 }
74 else if ( result == null)
75 {
76 itty = Collections.singleton( "" ).iterator();
77 }
78 else
79 {
80 itty = Collections.singleton( result ).iterator();
81 }
82
83 while ( itty.hasNext() )
84 {
85 this.io.out.println( itty.next() );
86 }
87
88 return null;
89 }
90 }