org.thymeleaf.spring4.util.FieldUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of thymeleaf-spring4 Show documentation
Show all versions of thymeleaf-spring4 Show documentation
XML/XHTML/HTML5 template engine for Java
/*
* =============================================================================
*
* Copyright (c) 2011-2014, The THYMELEAF team (http://www.thymeleaf.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* =============================================================================
*/
package org.thymeleaf.spring4.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.NotReadablePropertyException;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.servlet.support.BindStatus;
import org.springframework.web.servlet.support.RequestContext;
import org.thymeleaf.Arguments;
import org.thymeleaf.Configuration;
import org.thymeleaf.context.IProcessingContext;
import org.thymeleaf.exceptions.TemplateProcessingException;
import org.thymeleaf.spring4.naming.SpringContextVariableNames;
import org.thymeleaf.standard.expression.IStandardExpression;
import org.thymeleaf.standard.expression.IStandardExpressionParser;
import org.thymeleaf.standard.expression.SelectionVariableExpression;
import org.thymeleaf.standard.expression.StandardExpressions;
import org.thymeleaf.standard.expression.VariableExpression;
import org.thymeleaf.util.Validate;
/**
*
* @author Daniel Fernández
* @author Tobias Gafner
*
* @since 1.0
*
*/
public final class FieldUtils {
public static final String ALL_FIELDS = "*";
public static final String GLOBAL_EXPRESSION = "global";
public static final String ALL_EXPRESSION = "all";
public static boolean hasErrors(final Arguments arguments, final String field) {
return hasErrors(arguments.getConfiguration(), arguments, field);
}
/**
*
* @param arguments arguments
* @return the result
* @since 2.1.0
*/
public static boolean hasAnyErrors(final Arguments arguments) {
return hasAnyErrors(arguments.getConfiguration(), arguments);
}
/**
*
* @param arguments arguments
* @return the result
* @since 2.1.0
*/
public static boolean hasGlobalErrors(final Arguments arguments) {
return hasGlobalErrors(arguments.getConfiguration(), arguments);
}
public static boolean hasErrors(final Configuration configuration,
final IProcessingContext processingContext, final String field) {
return checkErrors(configuration, processingContext, convertToFieldExpression(field));
}
public static boolean hasAnyErrors(final Configuration configuration,
final IProcessingContext processingContext) {
return checkErrors(configuration, processingContext, ALL_EXPRESSION);
}
public static boolean hasGlobalErrors(final Configuration configuration,
final IProcessingContext processingContext) {
return checkErrors(configuration, processingContext, GLOBAL_EXPRESSION);
}
public static List errors(final Arguments arguments, final String field) {
return computeErrors(arguments.getConfiguration(), arguments, field);
}
public static List errors(final Configuration configuration,
final IProcessingContext processingContext, final String field) {
return computeErrors(configuration, processingContext, convertToFieldExpression(field));
}
public static List errors(final Configuration configuration,
final IProcessingContext processingContext) {
return computeErrors(configuration, processingContext, ALL_EXPRESSION);
}
public static List globalErrors(final Configuration configuration,
final IProcessingContext processingContext) {
return computeErrors(configuration, processingContext, GLOBAL_EXPRESSION);
}
private static List computeErrors(final Configuration configuration,
final IProcessingContext processingContext, final String fieldExpression) {
final BindStatus bindStatus =
FieldUtils.getBindStatus(configuration, processingContext, fieldExpression);
if (bindStatus == null) {
return Collections.EMPTY_LIST;
}
final String[] errorCodes = bindStatus.getErrorMessages();
return Arrays.asList(errorCodes);
}
/**
*
* @param configuration configuration
* @param processingContext processingContext
* @return the result
* @since 2.1.2
*/
public static List detailedErrors(
final Configuration configuration, final IProcessingContext processingContext) {
return computeDetailedErrors(configuration, processingContext, ALL_EXPRESSION, true, true);
}
/**
*
* @param configuration configuration
* @param processingContext processingContext
* @param fieldExpression fieldExpression
* @param includeGlobalErrors includeGlobalErrors
* @param includeFieldErrors includeFieldErrors
* @return the result
* @since 2.1.2
*/
private static List computeDetailedErrors(
final Configuration configuration, final IProcessingContext processingContext, final String fieldExpression,
final boolean includeGlobalErrors, final boolean includeFieldErrors) {
final BindStatus bindStatus =
FieldUtils.getBindStatus(configuration, processingContext, fieldExpression);
if (bindStatus == null) {
return Collections.EMPTY_LIST;
}
final Errors errors = bindStatus.getErrors();
if (errors == null) {
return Collections.EMPTY_LIST;
}
final RequestContext requestContext =
(RequestContext) processingContext.getContext().getVariables().get(SpringContextVariableNames.SPRING_REQUEST_CONTEXT);
if (requestContext == null) {
return Collections.EMPTY_LIST;
}
final List errorObjects = new ArrayList(errors.getErrorCount() + 2);
if (includeGlobalErrors) {
final List globalErrors = errors.getGlobalErrors();
for (final ObjectError globalError : globalErrors) {
final String message = requestContext.getMessage(globalError, false);
final DetailedError errorObject =
new DetailedError(globalError.getCode(), globalError.getArguments(), message);
errorObjects.add(errorObject);
}
}
if (includeFieldErrors) {
final List fieldErrors = errors.getFieldErrors();
for (final FieldError fieldError : fieldErrors) {
final String message = requestContext.getMessage(fieldError, false);
final DetailedError errorObject =
new DetailedError(fieldError.getField(), fieldError.getCode(), fieldError.getArguments(), message);
errorObjects.add(errorObject);
}
}
return errorObjects;
}
public static String idFromName(final String fieldName) {
return StringUtils.deleteAny(fieldName, "[]");
}
private static String convertToFieldExpression(final String field) {
if (field == null) {
return null;
}
if (field.trim().startsWith("*") || field.trim().startsWith("$")) {
return field;
}
final StringBuilder strBuilder = new StringBuilder(20);
strBuilder.append('*');
strBuilder.append('{');
strBuilder.append(field);
strBuilder.append('}');
return strBuilder.toString();
}
private static boolean checkErrors(final Configuration configuration,
final IProcessingContext processingContext, final String expression) {
final BindStatus bindStatus = FieldUtils.getBindStatus(configuration, processingContext, expression);
if (bindStatus == null) {
throw new TemplateProcessingException(
"Could not bind form errors using expression \"" + expression + "\". Please check this " +
"expression is being executed inside the adequate context (e.g. a