|
|
|
@ -15,14 +15,18 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
package org.springframework.ui.message; |
|
|
|
package org.springframework.ui.message; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.LinkedHashMap; |
|
|
|
import java.util.LinkedHashSet; |
|
|
|
import java.util.LinkedHashSet; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.context.MessageSource; |
|
|
|
import org.springframework.context.MessageSource; |
|
|
|
import org.springframework.context.MessageSourceResolvable; |
|
|
|
import org.springframework.context.MessageSourceResolvable; |
|
|
|
|
|
|
|
import org.springframework.context.expression.MapAccessor; |
|
|
|
import org.springframework.core.style.ToStringCreator; |
|
|
|
import org.springframework.core.style.ToStringCreator; |
|
|
|
|
|
|
|
import org.springframework.expression.ExpressionParser; |
|
|
|
|
|
|
|
import org.springframework.expression.spel.standard.SpelExpressionParser; |
|
|
|
|
|
|
|
import org.springframework.expression.spel.support.StandardEvaluationContext; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* A convenient builder for building {@link MessageResolver} objects programmatically. |
|
|
|
* A convenient builder for building {@link MessageResolver} objects programmatically. |
|
|
|
@ -46,16 +50,18 @@ import org.springframework.core.style.ToStringCreator; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class MessageBuilder { |
|
|
|
public class MessageBuilder { |
|
|
|
|
|
|
|
|
|
|
|
private Set<String> codes = new LinkedHashSet<String>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Severity severity; |
|
|
|
private Severity severity; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Set<String> codes = new LinkedHashSet<String>(); |
|
|
|
|
|
|
|
|
|
|
|
private List<Object> args = new ArrayList<Object>(); |
|
|
|
private Map<String, Object> args = new LinkedHashMap<String, Object>(); |
|
|
|
|
|
|
|
|
|
|
|
private String defaultText; |
|
|
|
private String defaultText; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ExpressionParser expressionParser = new SpelExpressionParser(); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Records the severity of the message. |
|
|
|
* Set the severity of the message. |
|
|
|
* @return this, for fluent API usage |
|
|
|
* @return this, for fluent API usage |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public MessageBuilder severity(Severity severity) { |
|
|
|
public MessageBuilder severity(Severity severity) { |
|
|
|
@ -64,8 +70,8 @@ public class MessageBuilder { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Records that the message being built should try and resolve its text using the code provided. |
|
|
|
* Add a message code to use to resolve the message text. |
|
|
|
* Adds the code to the codes list. Successive calls to this method add additional codes. |
|
|
|
* Successive calls to this method add additional codes. |
|
|
|
* Codes are applied in the order they are added. |
|
|
|
* Codes are applied in the order they are added. |
|
|
|
* @param code the message code |
|
|
|
* @param code the message code |
|
|
|
* @return this, for fluent API usage |
|
|
|
* @return this, for fluent API usage |
|
|
|
@ -76,31 +82,31 @@ public class MessageBuilder { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Records that the message being built has a variable argument. |
|
|
|
* Add a message argument. |
|
|
|
* Adds the arg to the args list. Successive calls to this method add additional args. |
|
|
|
* Successive calls to this method add additional args. |
|
|
|
* Args are applied in the order they are added. |
|
|
|
* @param name the argument name |
|
|
|
* @param arg the message argument value |
|
|
|
* @param value the argument value |
|
|
|
* @return this, for fluent API usage |
|
|
|
* @return this, for fluent API usage |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public MessageBuilder arg(Object arg) { |
|
|
|
public MessageBuilder arg(String name, Object value) { |
|
|
|
args.add(arg); |
|
|
|
args.put(name, value); |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Records that the message being built has a variable argument, whose display value is also {@link MessageSourceResolvable}. |
|
|
|
* Add a message argument whose value is a resolvable message code. |
|
|
|
* Adds the arg to the args list. Successive calls to this method add additional resolvable args. |
|
|
|
* Successive calls to this method add additional resolvable arguements. |
|
|
|
* Args are applied in the order they are added. |
|
|
|
* @param name the argument name |
|
|
|
* @param arg the resolvable message argument |
|
|
|
* @param value the argument value |
|
|
|
* @return this, for fluent API usage |
|
|
|
* @return this, for fluent API usage |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public MessageBuilder resolvableArg(Object arg) { |
|
|
|
public MessageBuilder resolvableArg(String name, Object value) { |
|
|
|
args.add(new ResolvableArgument(arg)); |
|
|
|
args.put(name, new ResolvableArgumentValue(value)); |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Records the fallback text of the message being built. |
|
|
|
* Set the fallback text for the message. |
|
|
|
* If the message has no codes, this will always be used as the text. |
|
|
|
* If the message has no codes, this will always be used as the text. |
|
|
|
* If the message has codes but none can be resolved, this will always be used as the text. |
|
|
|
* If the message has codes but none can be resolved, this will always be used as the text. |
|
|
|
* @param text the default text |
|
|
|
* @param text the default text |
|
|
|
@ -113,28 +119,28 @@ public class MessageBuilder { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Builds the message that will be resolved. |
|
|
|
* Builds the message that will be resolved. |
|
|
|
* Call after recording builder instructions. |
|
|
|
* Call after recording all builder instructions. |
|
|
|
* @return the built message resolver |
|
|
|
* @return the built message resolver |
|
|
|
|
|
|
|
* @throws Illegal |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public MessageResolver build() { |
|
|
|
public MessageResolver build() { |
|
|
|
if (severity == null) { |
|
|
|
if (severity == null) { |
|
|
|
severity = Severity.INFO; |
|
|
|
severity = Severity.INFO; |
|
|
|
} |
|
|
|
} |
|
|
|
if (codes == null && defaultText == null) { |
|
|
|
if (codes == null && defaultText == null) { |
|
|
|
throw new IllegalArgumentException( |
|
|
|
throw new IllegalStateException( |
|
|
|
"A message code or the message text is required to build this message resolver"); |
|
|
|
"A message code or the message text is required to build this message resolver"); |
|
|
|
} |
|
|
|
} |
|
|
|
String[] codesArray = (String[]) codes.toArray(new String[codes.size()]); |
|
|
|
String[] codesArray = (String[]) codes.toArray(new String[codes.size()]); |
|
|
|
Object[] argsArray = args.toArray(new Object[args.size()]); |
|
|
|
return new DefaultMessageResolver(severity, codesArray, args, defaultText, expressionParser); |
|
|
|
return new DefaultMessageResolver(severity, codesArray, argsArray, defaultText); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static class ResolvableArgument implements MessageSourceResolvable { |
|
|
|
static class ResolvableArgumentValue implements MessageSourceResolvable { |
|
|
|
|
|
|
|
|
|
|
|
private Object arg; |
|
|
|
private Object value; |
|
|
|
|
|
|
|
|
|
|
|
public ResolvableArgument(Object arg) { |
|
|
|
public ResolvableArgumentValue(Object value) { |
|
|
|
this.arg = arg; |
|
|
|
this.value = value; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Object[] getArguments() { |
|
|
|
public Object[] getArguments() { |
|
|
|
@ -142,15 +148,15 @@ public class MessageBuilder { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String[] getCodes() { |
|
|
|
public String[] getCodes() { |
|
|
|
return new String[] { arg.toString() }; |
|
|
|
return new String[] { value.toString() }; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String getDefaultMessage() { |
|
|
|
public String getDefaultMessage() { |
|
|
|
return arg.toString(); |
|
|
|
return String.valueOf(value); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String toString() { |
|
|
|
public String toString() { |
|
|
|
return new ToStringCreator(this).append("arg", arg).toString(); |
|
|
|
return new ToStringCreator(this).append("value", value).toString(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|