001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * https://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.validator; 018 019import java.io.Serializable; 020import java.text.MessageFormat; 021 022/** 023 * A default argument or an argument for a 024 * specific validator definition (ex: required) 025 * can be stored to pass into a message as parameters. This can be used in a 026 * pluggable validator for constructing locale 027 * sensitive messages by using {@link MessageFormat} 028 * or an equivalent class. The resource field can be 029 * used to determine if the value stored in the argument 030 * is a value to be retrieved from a locale sensitive 031 * message retrieval system like {@code java.util.PropertyResourceBundle}. 032 * The resource field defaults to 'true'. 033 * <p>Instances of this class are configured with an <arg> xml element.</p> 034 */ 035//TODO mutable non-private fields 036public class Arg implements Cloneable, Serializable { 037 038 private static final long serialVersionUID = -8922606779669839294L; 039 040 /** 041 * The resource bundle name that this Arg's {@code key} should be 042 * resolved in (optional). 043 * 044 * @since 1.1 045 */ 046 protected String bundle; 047 048 /** 049 * The key or value of the argument. 050 */ 051 protected String key; 052 053 /** 054 * The name dependency that this argument goes with (optional). 055 */ 056 protected String name; 057 058 /** 059 * This argument's position in the message. Set position=0 to 060 * make a replacement in this string: "some msg {0}". 061 * 062 * @since 1.1 063 */ 064 protected int position = -1; 065 066 /** 067 * Whether or not the key is a message resource (optional). Defaults to 068 * true. If it is 'true', the value will try to be resolved as a message 069 * resource. 070 */ 071 protected boolean resource = true; 072 073 /** 074 * Constructs a new instance. 075 */ 076 public Arg() { 077 // empty 078 } 079 080 /** 081 * Creates and returns a copy of this object. 082 * 083 * @return A copy of this object. 084 */ 085 @Override 086 public Object clone() { 087 try { 088 return super.clone(); 089 } catch (final CloneNotSupportedException e) { 090 throw new UnsupportedOperationException(e.toString(), e); 091 } 092 } 093 094 /** 095 * Gets the resource bundle name. 096 * 097 * @return The bundle name. 098 * @since 1.1 099 */ 100 public String getBundle() { 101 return bundle; 102 } 103 104 /** 105 * Gets the key/value. 106 * 107 * @return The key value. 108 */ 109 public String getKey() { 110 return key; 111 } 112 113 /** 114 * Gets the name of the dependency. 115 * 116 * @return The name of the dependency. 117 */ 118 public String getName() { 119 return name; 120 } 121 122 /** 123 * Gets the replacement position. 124 * 125 * @return This replacement position. 126 */ 127 public int getPosition() { 128 return position; 129 } 130 131 /** 132 * Tests whether or not the key is a resource key or literal value. 133 * 134 * @return {@code true} if key is a resource key. 135 */ 136 public boolean isResource() { 137 return resource; 138 } 139 140 /** 141 * Sets the resource bundle name. 142 * 143 * @param bundle The new bundle name. 144 * @since 1.1 145 */ 146 public void setBundle(final String bundle) { 147 this.bundle = bundle; 148 } 149 150 /** 151 * Sets the key/value. 152 * 153 * @param key They to access the argument. 154 */ 155 public void setKey(final String key) { 156 this.key = key; 157 } 158 159 /** 160 * Sets the name of the dependency. 161 * 162 * @param name The name of the dependency. 163 */ 164 public void setName(final String name) { 165 this.name = name; 166 } 167 168 /** 169 * Sets this argument's replacement position. 170 * 171 * @param position set this argument's replacement position. 172 */ 173 public void setPosition(final int position) { 174 this.position = position; 175 } 176 177 /** 178 * Sets whether or not the key is a resource. 179 * 180 * @param resource If true indicates the key is a resource. 181 */ 182 public void setResource(final boolean resource) { 183 this.resource = resource; 184 } 185 186 /** 187 * Returns a string representation of the object. 188 * 189 * @return A string representation of the object. 190 */ 191 @Override 192 public String toString() { 193 // @formatter:off 194 return new StringBuilder() 195 .append("Arg: name=") 196 .append(name) 197 .append(" key=") 198 .append(key) 199 .append(" position=") 200 .append(position) 201 .append(" bundle=") 202 .append(bundle) 203 .append(" resource=") 204 .append(resource) 205 .append("\n") 206 .toString(); 207 // @formatter:on 208 } 209 210}