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: * http://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: */
017: package org.apache.wicket.markup.html.form.validation;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import org.apache.wicket.markup.html.form.FormComponent;
023: import org.apache.wicket.model.IModel;
024: import org.apache.wicket.util.lang.Classes;
025: import org.apache.wicket.validation.IValidatable;
026: import org.apache.wicket.validation.IValidationError;
027: import org.apache.wicket.validation.ValidationError;
028:
029: /**
030: * Base class for {@link org.apache.wicket.markup.html.form.validation.IFormValidator}s.
031: *
032: * @author Igor Vaynberg (ivaynberg)
033: */
034: public abstract class AbstractFormValidator implements IFormValidator {
035: /**
036: * DEPRECATED/UNSUPPORTED
037: *
038: * Gets the default variables for interpolation.
039: *
040: * @return a map with the variables for interpolation
041: *
042: * @deprecated use {@link #variablesMap(IValidatable)} instead
043: * @throws UnsupportedOperationException
044: *
045: * FIXME 2.0: remove asap
046: */
047: protected final Map messageModel() {
048: throw new UnsupportedOperationException(
049: "THIS METHOD IS DEPRECATED, SEE JAVADOC");
050: }
051:
052: /**
053: * Reports an error against validatable using the map returned by
054: * {@link #variablesMap(IValidatable)}for variable interpolations and
055: * message key returned by {@link #resourceKey()}.
056: *
057: * @param fc
058: * form component against which the error is reported
059: *
060: */
061: public void error(FormComponent fc) {
062: error(fc, resourceKey(), variablesMap());
063: }
064:
065: /**
066: * Reports an error against the validatalbe using the given resource key
067: *
068: * @param fc
069: * form component against which the error is reported
070: * @param resourceKey
071: * The message resource key to use
072: */
073: public void error(FormComponent fc, final String resourceKey) {
074: if (resourceKey == null) {
075: throw new IllegalArgumentException(
076: "Argument [[resourceKey]] cannot be null");
077: }
078: error(fc, resourceKey, variablesMap());
079: }
080:
081: /**
082: * Reports an error against the validatalbe using the given map for variable
083: * interpolations and message resource key provided by
084: * {@link #resourceKey()}
085: *
086: * @param fc
087: * form component against which the error is reported
088: * @param vars
089: * variables for variable interpolation
090: */
091: public void error(FormComponent fc, final Map vars) {
092: if (vars == null) {
093: throw new IllegalArgumentException(
094: "Argument [[vars]] cannot be null");
095: }
096: error(fc, resourceKey(), vars);
097: }
098:
099: /**
100: * Reports an error against the validatable using the specified resource key
101: * and variable map
102: *
103: * @param fc
104: * form component against which the error is reported
105: *
106: * @param validatable
107: * validatble being validated
108: * @param resourceKey
109: * The message resource key to use
110: * @param vars
111: * The model for variable interpolation
112: */
113: public void error(FormComponent fc, final String resourceKey,
114: Map vars) {
115: if (fc == null) {
116: throw new IllegalArgumentException(
117: "Argument [[fc]] cannot be null");
118: }
119: if (vars == null) {
120: throw new IllegalArgumentException(
121: "Argument [[vars]] cannot be null");
122: }
123: if (resourceKey == null) {
124: throw new IllegalArgumentException(
125: "Argument [[resourceKey]] cannot be null");
126: }
127:
128: ValidationError error = new ValidationError()
129: .addMessageKey(resourceKey);
130: final String defaultKey = Classes.simpleName(getClass());
131: if (!resourceKey.equals(defaultKey)) {
132: error.addMessageKey(defaultKey);
133: }
134:
135: error.setVariables(vars);
136: fc.error((IValidationError) error);
137: }
138:
139: /**
140: * Gets the default variables for interpolation. These are for every
141: * component:
142: * <ul>
143: * <li>${input(n)}: the user's input</li>
144: * <li>${name(n)}: the name of the component</li>
145: * <li>${label(n)}: the label of the component - either comes from
146: * FormComponent.labelModel or resource key [form-id].[form-component-id] in
147: * that order</li>
148: * </ul>
149: *
150: * @return a map with the variables for interpolation
151: */
152: protected Map variablesMap() {
153: FormComponent[] formComponents = getDependentFormComponents();
154:
155: if (formComponents != null && formComponents.length > 0) {
156: Map args = new HashMap(formComponents.length * 3);
157: for (int i = 0; i < formComponents.length; i++) {
158: final FormComponent formComponent = formComponents[i];
159:
160: String arg = "label" + i;
161: IModel label = formComponent.getLabel();
162: if (label != null) {
163: args.put(arg, label.getObject());
164: } else {
165: args.put(arg, formComponent.getLocalizer()
166: .getString(formComponent.getId(),
167: formComponent.getParent(),
168: formComponent.getId()));
169: }
170:
171: args.put("input" + i, formComponent.getInput());
172: args.put("name" + i, formComponent.getId());
173: }
174: return args;
175: } else {
176: return new HashMap(2);
177: }
178: }
179:
180: /**
181: * Gets the resource key for validator's error message from the
182: * ApplicationSettings class.
183: *
184: * @return the resource key based on the form component
185: */
186: protected String resourceKey() {
187: return Classes.simpleName(getClass());
188: }
189: }
|