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

net.sf.gluebooster.java.booster.basic.transformation.CallableToString Maven / Gradle / Ivy

Go to download

Basic classes to support the development of applications. There should be as few dependencies on other frameworks as possible.

The newest version!
package net.sf.gluebooster.java.booster.basic.transformation;

import java.io.InputStream;
import java.io.Reader;

import org.apache.commons.io.IOUtils;

import net.sf.gluebooster.java.booster.essentials.eventsCommands.CallableAbstraction;

/**
 * Converts objects into strings.
 * 
 * @defaultParamText name name of the callable
 * 
 * @author cbauer
 *
 */
public class CallableToString extends CallableAbstraction {

	/**
	 * Read all of a stream, etc.
	 */
	private static final String TYPE_READ_ALL = "read all";

	/**
	 * Prefix of the result string.
	 */
	private CharSequence prefix;

	/**
	 * Suffix of the result string.
	 */
	private CharSequence suffix;

	private CallableToString() {

	}

	/**
	 * 
	 * @param type
	 *            type of the callable
	 */
	public CallableToString(Object name, Object type) {
		super(name, type);
	}

	/**
	 * Creates a transformer that reads all from a stream, reader, ...
	 * 
	 * @return the created transformer
	 */
	public static CallableAbstraction createReadAll(Object name) {
		CallableToString result = new CallableToString(name, TYPE_READ_ALL);
		return result;
	}

	/**
	 * Creates a transformer that creates a default string representation (for example by invoking toString).
	 * 
	 * @return the created transformer
	 */
	public static CallableAbstraction createDefault(Object name) {
		CallableToString result = new CallableToString(name, TYPE_READ_ALL);
		return result;
	}

	@Override
	protected CharSequence callImpl(Object... parameters) throws Exception {

		if (parameters == null || parameters[0] == null) {
			return null;
		}

		Object type = getType();

		if (TYPE_READ_ALL.equals(type)) {
			Object input = parameters[0];
			if (input instanceof InputStream) {
				return IOUtils.toString((InputStream) input);
			} else if (input instanceof Reader) {
				return IOUtils.toString((Reader) input);
			} else if (input instanceof byte[]) {
				return IOUtils.toString((byte[]) input);
			} else {
				return input.toString();
			}
		} else {


			StringBuilder text = new StringBuilder(parameters[0].toString());
			if (suffix != null) {
				text.append(suffix);
			}
			if (prefix != null) {
				text.insert(0, prefix);
			}

			return text;
		}
	}

	public CharSequence getPrefix() {
		return prefix;
	}

	public void setPrefix(CharSequence prefix) {
		this.prefix = prefix;
	}

	public CharSequence getSuffix() {
		return suffix;
	}

	public void setSuffix(CharSequence suffix) {
		this.suffix = suffix;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy