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

org.javasimon.console.action.TableJsonAction Maven / Gradle / Ivy

There is a newer version: 4.2.0
Show newest version
package org.javasimon.console.action;

import org.javasimon.Sample;
import org.javasimon.console.ActionContext;
import org.javasimon.console.json.ArrayJS;
import org.javasimon.console.json.JsonStringifierFactory;
import org.javasimon.console.json.ObjectJS;
import org.javasimon.console.json.SimpleJS;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * Export Simons as a flat JSON array to be displayed in data-table.
 * Each JSON-ized simon as all attributes even if not present on real Simon.
 * Only a subset (defined in {@link AbstractTableAction#columns}) of attributes are exported.
 *
 * @author gquintana
 */
public class TableJsonAction extends AbstractTableAction {

	public static final String PATH = "/data/table.json";

	public TableJsonAction(ActionContext context) {
		super(context, "application/json");
		this.stringifierFactory = new JsonStringifierFactory();
		this.numberPattern = JsonStringifierFactory.INTEGER_NUMBER_PATTERN;
	}

	/** Current array of JSON-ized Simons. */
	private ArrayJS simonsJS;
	/** Current JSON-ized Simon. */
	private ObjectJS simonJS;

	@Override
	protected void printTable(PrintWriter writer) throws IOException {
		simonsJS = new ArrayJS();
		super.printTable(writer);
		simonsJS.write(writer);
		simonJS = null;
	}

	@Override
	protected void printHeaderRow(PrintWriter writer) throws IOException {
		// No header
	}

	@Override
	protected void printBodyRow(Sample sample, PrintWriter writer) throws IOException {
		simonJS = new ObjectJS();
		simonsJS.addElement(simonJS);
		super.printBodyRow(sample, writer);
		simonJS = null;
	}

	@Override
	@SuppressWarnings("unchecked")
	protected void printBodyCell(Column column, Sample sample, PrintWriter writer) {
		simonJS.setAttribute(column.getName(), new SimpleJS(column.getValue(sample), column.getStringifier(sample)));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy