1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.context.support;
  17. import java.io.Serializable;
  18. import org.springframework.context.MessageSourceResolvable;
  19. import org.springframework.util.StringUtils;
  20. /**
  21. * Default implementation of the MessageSourceResolvable interface.
  22. * Easy way to store all the necessary values needed to resolve
  23. * a message via a MessageSource.
  24. * @author Juergen Hoeller
  25. * @since 13.02.2004
  26. * @see org.springframework.context.MessageSource#getMessage(MessageSourceResolvable, java.util.Locale)
  27. */
  28. public class DefaultMessageSourceResolvable implements MessageSourceResolvable, Serializable {
  29. private final String[] codes;
  30. private final Object[] arguments;
  31. private final String defaultMessage;
  32. /**
  33. * Create a new DefaultMessageSourceResolvable.
  34. * @param codes the codes to be used to resolve this message
  35. */
  36. public DefaultMessageSourceResolvable(String[] codes) {
  37. this(codes, null, null);
  38. }
  39. /**
  40. * Create a new DefaultMessageSourceResolvable.
  41. * @param codes the codes to be used to resolve this message
  42. * @param arguments the array of arguments to be used to resolve this message
  43. */
  44. public DefaultMessageSourceResolvable(String[] codes, Object[] arguments) {
  45. this(codes, arguments, null);
  46. }
  47. /**
  48. * Create a new DefaultMessageSourceResolvable.
  49. * @param codes the codes to be used to resolve this message
  50. * @param arguments the array of arguments to be used to resolve this message
  51. * @param defaultMessage the default message to be used to resolve this message
  52. */
  53. public DefaultMessageSourceResolvable(String[] codes, Object[] arguments, String defaultMessage) {
  54. this.codes = codes;
  55. this.arguments = arguments;
  56. this.defaultMessage = defaultMessage;
  57. }
  58. /**
  59. * Copy constructor: Create a new instance from another resolvable.
  60. * @param resolvable the resolvable to copy from
  61. */
  62. public DefaultMessageSourceResolvable(MessageSourceResolvable resolvable) {
  63. this(resolvable.getCodes(), resolvable.getArguments(), resolvable.getDefaultMessage());
  64. }
  65. public String[] getCodes() {
  66. return codes;
  67. }
  68. /**
  69. * Return the default code of this resolvable, i.e. the last one in the
  70. * codes array.
  71. */
  72. public String getCode() {
  73. return (this.codes != null && this.codes.length > 0) ? this.codes[this.codes.length - 1] : null;
  74. }
  75. public Object[] getArguments() {
  76. return arguments;
  77. }
  78. public String getDefaultMessage() {
  79. return defaultMessage;
  80. }
  81. protected String resolvableToString() {
  82. StringBuffer buf = new StringBuffer();
  83. buf.append("codes=[").append(StringUtils.arrayToDelimitedString(getCodes(), ",")).append("]; arguments=[");
  84. if (this.arguments == null) {
  85. buf.append("null");
  86. }
  87. else {
  88. for (int i = 0; i < getArguments().length; i++) {
  89. buf.append('(').append(getArguments()[i].getClass().getName()).append(")[");
  90. buf.append(getArguments()[i]).append(']');
  91. if (i < getArguments().length - 1) {
  92. buf.append(", ");
  93. }
  94. }
  95. }
  96. buf.append("]; defaultMessage=[").append(getDefaultMessage()).append(']');
  97. return buf.toString();
  98. }
  99. public String toString() {
  100. return resolvableToString();
  101. }
  102. }