1. /*
  2. * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/Arg.java,v 1.17 2004/02/21 17:10:29 rleland Exp $
  3. * $Revision: 1.17 $
  4. * $Date: 2004/02/21 17:10:29 $
  5. *
  6. * ====================================================================
  7. * Copyright 2001-2004 The Apache Software Foundation
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. package org.apache.commons.validator;
  22. import java.io.Serializable;
  23. /**
  24. * <p>
  25. * A default argument or an argument for a
  26. * specific validator definition (ex: required)
  27. * can be stored to pass into a message as parameters. This can be used in a
  28. * pluggable validator for constructing locale
  29. * sensitive messages by using <code>java.text.MessageFormat</code>
  30. * or an equivalent class. The resource field can be
  31. * used to determine if the value stored in the argument
  32. * is a value to be retrieved from a locale sensitive
  33. * message retrieval system like <code>java.util.PropertyResourceBundle</code>.
  34. * The resource field defaults to 'true'.
  35. * </p>
  36. * <p>Instances of this class are configured with an <arg> xml element.</p>
  37. */
  38. public class Arg implements Cloneable, Serializable {
  39. /**
  40. * The resource bundle name that this Arg's <code>key</code> should be
  41. * resolved in (optional).
  42. * @since Validator 1.1
  43. */
  44. protected String bundle = null;
  45. /**
  46. * The key or value of the argument.
  47. */
  48. protected String key = null;
  49. /**
  50. * The name dependency that this argument goes with (optional).
  51. */
  52. protected String name = null;
  53. /**
  54. * This argument's position in the message. Set postion=0 to
  55. * make a replacement in this string: "some msg {0}".
  56. * @since Validator 1.1
  57. */
  58. protected int position = 0;
  59. /**
  60. * Whether or not the key is a message resource (optional). Defaults to
  61. * true. If it is 'true', the value will try to be resolved as a message
  62. * resource.
  63. */
  64. protected boolean resource = true;
  65. /**
  66. * Creates and returns a copy of this object.
  67. * @return A copy of this object.
  68. */
  69. public Object clone() {
  70. try {
  71. return super.clone();
  72. } catch(CloneNotSupportedException e) {
  73. throw new RuntimeException(e.toString());
  74. }
  75. }
  76. /**
  77. * Returns the resource bundle name.
  78. * @since Validator 1.1
  79. */
  80. public String getBundle() {
  81. return this.bundle;
  82. }
  83. /**
  84. * Gets the key/value.
  85. * @return the key value.
  86. */
  87. public String getKey() {
  88. return this.key;
  89. }
  90. /**
  91. * Gets the name of the dependency.
  92. * @return the name of the dependency.
  93. */
  94. public String getName() {
  95. return this.name;
  96. }
  97. /**
  98. * Argument's replacement position.
  99. * @return This argument's replacement position.
  100. */
  101. public int getPosition() {
  102. return this.position;
  103. }
  104. /**
  105. * Gets whether or not the key is a resource.
  106. * @return Returns true if key is a resource.
  107. * @deprecated Use isResource() instead.
  108. */
  109. public boolean getResource() {
  110. return this.isResource();
  111. }
  112. /**
  113. * Tests whether or not the key is a resource key or literal value.
  114. * @return <code>true</code> if key is a resource key.
  115. */
  116. public boolean isResource() {
  117. return this.resource;
  118. }
  119. /**
  120. * Sets the resource bundle name.
  121. * @param bundle The new bundle name.
  122. * @since Validator 1.1
  123. */
  124. public void setBundle(String bundle) {
  125. this.bundle = bundle;
  126. }
  127. /**
  128. * Sets the key/value.
  129. * @param key They to access the argument.
  130. */
  131. public void setKey(String key) {
  132. this.key = key;
  133. }
  134. /**
  135. * Sets the name of the dependency.
  136. * @param name the name of the dependency.
  137. */
  138. public void setName(String name) {
  139. this.name = name;
  140. }
  141. /**
  142. * Set this argument's replacement position.
  143. * @param position set this argument's replacement position.
  144. */
  145. public void setPosition(int position) {
  146. this.position = position;
  147. }
  148. /**
  149. * Sets whether or not the key is a resource.
  150. * @param resource If true indicates the key is a resource.
  151. */
  152. public void setResource(boolean resource) {
  153. this.resource = resource;
  154. }
  155. /**
  156. * Returns a string representation of the object.
  157. * @return a string representation of the object.
  158. */
  159. public String toString() {
  160. StringBuffer results = new StringBuffer();
  161. results.append("Arg: name=");
  162. results.append(name);
  163. results.append(" key=");
  164. results.append(key);
  165. results.append(" resource=");
  166. results.append(resource);
  167. results.append("\n");
  168. return results.toString();
  169. }
  170. }