Java Code Examples for org.springframework.expression.spel.SpelMessage#INVALID_SECOND_OPERAND_FOR_MATCHES_OPERATOR

The following examples show how to use org.springframework.expression.spel.SpelMessage#INVALID_SECOND_OPERAND_FOR_MATCHES_OPERATOR . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OperatorMatches.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check the first operand matches the regex specified as the second operand.
 * @param state the expression state
 * @return {@code true} if the first operand matches the regex specified as the
 * second operand, otherwise {@code false}
 * @throws EvaluationException if there is a problem evaluating the expression
 * (e.g. the regex is invalid)
 */
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	SpelNodeImpl leftOp = getLeftOperand();
	SpelNodeImpl rightOp = getRightOperand();
	Object left = leftOp.getValue(state, String.class);
	Object right = getRightOperand().getValueInternal(state).getValue();

	if (!(left instanceof String)) {
		throw new SpelEvaluationException(leftOp.getStartPosition(),
				SpelMessage.INVALID_FIRST_OPERAND_FOR_MATCHES_OPERATOR, left);
	}
	if (!(right instanceof String)) {
		throw new SpelEvaluationException(rightOp.getStartPosition(),
				SpelMessage.INVALID_SECOND_OPERAND_FOR_MATCHES_OPERATOR, right);
	}

	try {
		String leftString = (String) left;
		String rightString = (String) right;
		Pattern pattern = this.patternCache.get(rightString);
		if (pattern == null) {
			pattern = Pattern.compile(rightString);
			this.patternCache.putIfAbsent(rightString, pattern);
		}
		Matcher matcher = pattern.matcher(leftString);
		return BooleanTypedValue.forValue(matcher.matches());
	}
	catch (PatternSyntaxException ex) {
		throw new SpelEvaluationException(rightOp.getStartPosition(), ex, SpelMessage.INVALID_PATTERN, right);
	}
}
 
Example 2
Source File: OperatorMatches.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Check the first operand matches the regex specified as the second operand.
 * @param state the expression state
 * @return {@code true} if the first operand matches the regex specified as the
 * second operand, otherwise {@code false}
 * @throws EvaluationException if there is a problem evaluating the expression
 * (e.g. the regex is invalid)
 */
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	SpelNodeImpl leftOp = getLeftOperand();
	SpelNodeImpl rightOp = getRightOperand();
	Object left = leftOp.getValue(state, String.class);
	Object right = getRightOperand().getValueInternal(state).getValue();

	if (!(left instanceof String)) {
		throw new SpelEvaluationException(leftOp.getStartPosition(),
				SpelMessage.INVALID_FIRST_OPERAND_FOR_MATCHES_OPERATOR, left);
	}
	if (!(right instanceof String)) {
		throw new SpelEvaluationException(rightOp.getStartPosition(),
				SpelMessage.INVALID_SECOND_OPERAND_FOR_MATCHES_OPERATOR, right);
	}

	try {
		String leftString = (String) left;
		String rightString = (String) right;
		Pattern pattern = this.patternCache.get(rightString);
		if (pattern == null) {
			pattern = Pattern.compile(rightString);
			this.patternCache.putIfAbsent(rightString, pattern);
		}
		Matcher matcher = pattern.matcher(leftString);
		return BooleanTypedValue.forValue(matcher.matches());
	}
	catch (PatternSyntaxException ex) {
		throw new SpelEvaluationException(rightOp.getStartPosition(), ex, SpelMessage.INVALID_PATTERN, right);
	}
}