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 * <p> 024 * A default argument or an argument for a 025 * specific validator definition (ex: required) 026 * can be stored to pass into a message as parameters. This can be used in a 027 * pluggable validator for constructing locale 028 * sensitive messages by using {@link MessageFormat} 029 * or an equivalent class. The resource field can be 030 * used to determine if the value stored in the argument 031 * is a value to be retrieved from a locale sensitive 032 * message retrieval system like {@code java.util.PropertyResourceBundle}. 033 * The resource field defaults to 'true'. 034 * </p> 035 * <p>Instances of this class are configured with an <arg> xml element.</p> 036 */ 037//TODO mutable non-private fields 038public class Arg implements Cloneable, Serializable { 039 040 private static final long serialVersionUID = -8922606779669839294L; 041 042 /** 043 * The resource bundle name that this Arg's {@code key} should be 044 * resolved in (optional). 045 * @since 1.1 046 */ 047 protected String bundle; 048 049 /** 050 * The key or value of the argument. 051 */ 052 protected String key; 053 054 /** 055 * The name dependency that this argument goes with (optional). 056 */ 057 protected String name; 058 059 /** 060 * This argument's position in the message. Set position=0 to 061 * make a replacement in this string: "some msg {0}". 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 * @return A copy of this object. 083 */ 084 @Override 085 public Object clone() { 086 try { 087 return super.clone(); 088 } catch (final CloneNotSupportedException e) { 089 throw new UnsupportedOperationException(e.toString(), e); 090 } 091 } 092 093 /** 094 * Gets the resource bundle name. 095 * 096 * @return the bundle name. 097 * @since 1.1 098 */ 099 public String getBundle() { 100 return bundle; 101 } 102 103 /** 104 * Gets the key/value. 105 * 106 * @return the key value. 107 */ 108 public String getKey() { 109 return key; 110 } 111 112 /** 113 * Gets the name of the dependency. 114 * 115 * @return the name of the dependency. 116 */ 117 public String getName() { 118 return name; 119 } 120 121 /** 122 * Gets the replacement position. 123 * 124 * @return This replacement position. 125 */ 126 public int getPosition() { 127 return position; 128 } 129 130 /** 131 * Tests whether or not the key is a resource key or literal value. 132 * 133 * @return {@code true} if key is a resource key. 134 */ 135 public boolean isResource() { 136 return resource; 137 } 138 139 /** 140 * Sets the resource bundle name. 141 * 142 * @param bundle The new bundle name. 143 * @since 1.1 144 */ 145 public void setBundle(final String bundle) { 146 this.bundle = bundle; 147 } 148 149 /** 150 * Sets the key/value. 151 * 152 * @param key They to access the argument. 153 */ 154 public void setKey(final String key) { 155 this.key = key; 156 } 157 158 /** 159 * Sets the name of the dependency. 160 * 161 * @param name the name of the dependency. 162 */ 163 public void setName(final String name) { 164 this.name = name; 165 } 166 167 /** 168 * Sets this argument's replacement position. 169 * 170 * @param position set this argument's replacement position. 171 */ 172 public void setPosition(final int position) { 173 this.position = position; 174 } 175 176 /** 177 * Sets whether or not the key is a resource. 178 * 179 * @param resource If true indicates the key is a resource. 180 */ 181 public void setResource(final boolean resource) { 182 this.resource = resource; 183 } 184 185 /** 186 * Returns a string representation of the object. 187 * 188 * @return a string representation of the object. 189 */ 190 @Override 191 public String toString() { 192 // @formatter:off 193 return new StringBuilder() 194 .append("Arg: name=") 195 .append(name) 196 .append(" key=") 197 .append(key) 198 .append(" position=") 199 .append(position) 200 .append(" bundle=") 201 .append(bundle) 202 .append(" resource=") 203 .append(resource) 204 .append("\n") 205 .toString(); 206 // @formatter:on 207 } 208 209}