
org.geomajas.internal.service.FeatureExpressionServiceImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of geomajas-impl Show documentation
Show all versions of geomajas-impl Show documentation
Geomajas server: Main - Implementation
The newest version!
/*
* This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
*
* Copyright 2008-2016 Geosparc nv, http://www.geosparc.com/, Belgium.
*
* The program is available in open source according to the GNU Affero
* General Public License. All contributions in this program are covered
* by the Geomajas Contributors License Agreement. For full licensing
* details, see LICENSE.txt in the project root.
*/
package org.geomajas.internal.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.geomajas.global.ExceptionCode;
import org.geomajas.internal.layer.feature.InternalFeaturePropertyAccessor;
import org.geomajas.layer.LayerException;
import org.geomajas.layer.feature.InternalFeature;
import org.geomajas.service.FeatureExpressionService;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.Expression;
import org.springframework.expression.ParseException;
import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
/**
* Default implementation of {@link FeatureExpressionService}, based on Spring Expression Language (SpEL). Expressions
* are evaluated against an evaluation context that contains the feature as the root object. The feature attributes can
* be accessed as if they were properties of the object. Sample expressions are:
*
* myAttr
: evaluates to the value of this primitive attribute
* country.code
: evaluates to the nested code value of the many-to-one attribute country
* myVar.doIt(cities)
: evaluates to the return value of the doIt() method of the variable myVar. See
* {@link #setVariables(Map)} on how to pass custom variables to the context. The argument is a list of association
* values of the one-to-many attribute cities.
*
*
* @author Jan De Moerloose
*/
@Component
public class FeatureExpressionServiceImpl implements FeatureExpressionService {
private final SpelExpressionParser parser = new SpelExpressionParser();
private final List propertyAccessors = new ArrayList();
private final Map variables = new HashMap();
private final Map expressionCache = new ConcurrentHashMap();
/**
* Construct a new service instance.
*/
public FeatureExpressionServiceImpl() {
propertyAccessors.add(new InternalFeaturePropertyAccessor());
}
@Override
public Object evaluate(String expression, InternalFeature feature) throws LayerException {
StandardEvaluationContext context = new StandardEvaluationContext(feature);
// set a new list of accessors, custom accessors first !
List allAccessors = new ArrayList(propertyAccessors);
allAccessors.addAll(context.getPropertyAccessors());
context.setPropertyAccessors(allAccessors);
// set context variables
context.setVariables(variables);
try {
return getExpression(expression).getValue(context);
} catch (ParseException e) {
throw new LayerException(e, ExceptionCode.EXPRESSION_INVALID, expression, feature.getLayer().getId());
} catch (EvaluationException e) {
throw new LayerException(e, ExceptionCode.EXPRESSION_EVALUATION_FAILED, expression, feature.getLayer()
.getId());
}
}
@Override
public void setVariables(Map variables) {
this.variables.putAll(variables);
}
/**
* Fetch the specified expression from the cache or create it if necessary.
*
* @param expressionString the expression string
* @return the expression
* @throws ParseException oops
*/
private Expression getExpression(String expressionString) throws ParseException {
if (!expressionCache.containsKey(expressionString)) {
Expression expression;
expression = parser.parseExpression(expressionString);
expressionCache.put(expressionString, expression);
}
return expressionCache.get(expressionString);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy