Java: Expression Eval (System Property)

Aquí un sencillo código para evaluar una expresión usando system properties:

import java.text.ParseException;

public class MapExpression {
	public final String expression;
	private String mapped;

	/**
	 * Create Map Expression
	 * 
	 * @param expression
	 *            to map
	 */
	public MapExpression(final String expression) throws InvalidExpression {
		this.expression = expression;
		this.mapped = eval(expression);
	}

	/**
	 * Force reevaluate expression 
	 * (if system property is changed after MapExpression was created)
	 * 
	 * @throws InvalidExpression
	 */
	public MapExpression reEval() throws InvalidExpression {
		mapped = eval(expression);
		return this;
	}

	/**
	 * Get mapped expression
	 * 
	 * @return evaluated expression
	 */
	public String get() {
		return mapped;
	}

	/**
	 * Evaluate input expression and return mapped expression
	 * 
	 * @param expression
	 * @return mapped expression
	 * @throws InvalidExpression
	 *             if expression is wrong
	 */
	String eval(final String expression) throws InvalidExpression {
		if (expression == null)
			throw new IllegalArgumentException();
		if (expression.isEmpty())
			return expression;
		// Find all ${system.property}
		final int len = expression.length();
		final StringBuilder sb = new StringBuilder(len);
		int last = 0;
		for (int i = 0; i < len; i++) {
			final char cbegin = expression.charAt(i);
			if (cbegin == '$' && ((i + 1) < len)) {
				final char cnext = expression.charAt(++i);
				if (cnext == '{') {
					sb.append(expression.substring(last, i - 1));
					last = i + 1;
					for (; i < len; i++) {
						final char cend = expression.charAt(i);
						if (cend == '}') {
							sb.append(mapToken(expression.substring(last, i)));
							last = i + 1;
							break;
						}
					}
					if ((i == len) && (last <= len))
						throw new InvalidExpression("Not well ended expression: "
								+ expression.substring(last - 2, len), len);
				}
			}
		}
		if (last < len)
			sb.append(expression.substring(last, len));
		return sb.toString();
	}

	/**
	 * Map Token to System.getProperty()
	 * 
	 * @param property
	 *            name
	 * @return property value or null if not exists
	 * @throws InvalidExpression
	 *             if expression is wrong
	 */
	String mapToken(final String propName) throws InvalidExpression {
		if (propName.isEmpty())
			throw new InvalidExpression("Invalid property name (empty)", 0);
		return System.getProperty(propName);
	}

	public static class InvalidExpression extends ParseException {
		private static final long serialVersionUID = 42L;

		public InvalidExpression(final String s, final int errorOffset) {
			super(s, errorOffset);
		}
	}

	/**
	 * Simple Test
	 * 
	 * @param args
	 * @throws Throwable
	 */
	public static void main(final String[] args) throws Throwable {
		System.setProperty("user.state", "lucky");
		MapExpression m;
		m = new MapExpression("Hi ${user.name}, you are ${user.state}!!");
		System.out.println(m.get());
	}
}

Esto daría una salida como esta:

Hi developer, you are lucky!!

Extender la clase para darle más funcionalidades es tan sencillo como sobrecargar el metodo “mapToken”.

Source code: MapExpressionSimple.java
Improved: MapExpression.java

Referencias:
System Properties
System.getProperty(name)
Three methods to evaluate expressions in Java

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: