All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tinkerpop.rexster.gremlin.converter.ConsoleResultConverter Maven / Gradle / Ivy

There is a newer version: 2.6.0
Show newest version
package com.tinkerpop.rexster.gremlin.converter;

import java.io.Writer;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ConsoleResultConverter implements ResultConverter> {
    private static final String LINE_SEPARATOR = System.getProperty("line.separator");

    private final Writer outputWriter;

    public ConsoleResultConverter(final Writer outputWriter) {
        this.outputWriter = outputWriter;
    }

    public List convert(final Object result) throws Exception {
        try {
            List resultLines = new ArrayList();
            if (result == null) {
                resultLines = new ArrayList();
                resultLines.add("null");
            } else if (result instanceof Iterable) {
                for (Object o : (Iterable) result) {
                    resultLines.add(o);
                }
            } else if (result instanceof Iterator) {
                // Table is handled through here and the toString() to get it formatted.
                final Iterator itty = (Iterator) result;
                while (itty.hasNext()) {
                    resultLines.add(itty.next());
                }
            } else if (result.getClass().isArray()) {
                final int length = Array.getLength(result);
                for (int ix = 0; ix < length; ix++) {
                    resultLines.add(Array.get(result, ix).toString());
                }
            } else if (result instanceof Map) {
                final Map map = (Map) result;
                for (Object key : map.keySet()) {
                    resultLines.add(key + "=" + map.get(key).toString());
                }
            } else if (result instanceof Throwable) {
                resultLines.add(((Throwable) result).getMessage());
            } else {
                resultLines.add(result);
            }

            // Handle output data
            final List outputLines = new ArrayList();

            // Handle eval() result
            final String[] printLines = this.outputWriter.toString().split(LINE_SEPARATOR);

            if (printLines.length > 0 && printLines[0].length() > 0) {
                for (String printLine : printLines) {
                    outputLines.add(printLine);
                }
            }

            if (resultLines != null && resultLines.size() > 0) {
                // Make sure all lines are strings
                for (Object resultLine : resultLines) {
                    outputLines.add(resultLine != null ? resultLine.toString() : "null");
                }
            }

            return outputLines;
        } catch (Exception ex) {
            final ArrayList resultList = new ArrayList();
            resultList.add(ex.getMessage());
            return resultList;
        }
    }
}