/* $$ Clover has instrumented this file $$ */// Copyright 2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.tapestry.valid;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.Resource;
import org.apache.hivemind.util.ClasspathResource;
import org.apache.tapestry.IEngine;
import org.apache.tapestry.IForm;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.IScript;
import org.apache.tapestry.Tapestry;
import org.apache.tapestry.engine.IScriptSource;
import org.apache.tapestry.form.FormEventType;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.html.Body;
/**
* Abstract base class for {@link IValidator}. Supports a required and locale property.
*
* @author Howard Lewis Ship
* @since 1.0.8
*
**/
public abstract class BaseValidator implements IValidator
{public static com.cortexeb.tools.clover.d __CLOVER_445_0 = com.cortexeb.tools.clover.aq.getRecorder(new char[] {67,58,92,119,111,114,107,115,112,97,99,101,92,106,97,107,97,114,116,97,45,116,97,112,101,115,116,114,121,92,102,114,97,109,101,119,111,114,107,92,116,97,114,103,101,116,92,99,108,111,118,101,114,45,100,98},1096998272901L);
/**
* Input Symbol used to represent the field being validated.
*
* @see #processValidatorScript(String, IRequestCycle, IFormComponent, Map)
*
* @since 2.2
*
**/
public static final String FIELD_SYMBOL = "field";
/**
* Input symbol used to represent the validator itself to the script.
*
* @see #processValidatorScript(String, IRequestCycle, IFormComponent, Map)
*
* @since 2.2
*
**/
public static final String VALIDATOR_SYMBOL = "validator";
/**
* Input symbol used to represent the {@link IForm} containing the field
* to the script.
*
* @see #processValidatorScript(String, IRequestCycle, IFormComponent, Map)
*
* @since 2.2
**/
public static final String FORM_SYMBOL = "form";
/**
* Output symbol set by the script asthe name of the validator
* JavaScript function.
* The function implemented must return true or false (true
* if the field is valid, false otherwise).
* After the script is executed, the function is added
* to the {@link IForm} as a {@link org.apache.tapestry.form.FormEventType#SUBMIT}.
*
* @see #processValidatorScript(String, IRequestCycle, IFormComponent, Map)
*
* @since 2.2
*
**/
public static final String FUNCTION_SYMBOL = "function";
private boolean _required;
/** @since 3.0 */
private String _requiredMessage;
/**
* @since 2.2
*
**/
private boolean _clientScriptingEnabled = false;
/**
* Standard constructor. Leaves locale as system default and required as false.
*
**/
public BaseValidator()
{try { __CLOVER_445_0.M[2389]++;
} finally { }}
protected BaseValidator(boolean required)
{try { __CLOVER_445_0.M[2390]++;
__CLOVER_445_0.S[9835]++;_required = required;
} finally { }}
public boolean isRequired()
{try { __CLOVER_445_0.M[2391]++;
__CLOVER_445_0.S[9836]++;return _required;
} finally { }}
public void setRequired(boolean required)
{try { __CLOVER_445_0.M[2392]++;
__CLOVER_445_0.S[9837]++;_required = required;
} finally { }}
/**
* Gets a pattern, either as the default value, or as a localized key.
* If override is null, then the key from the
* <code>org.apache.tapestry.valid.ValidationStrings</code>
* {@link ResourceBundle} (in the specified locale) is used.
* The pattern can then be used with {@link #formatString(String, Object[])}.
*
* <p>Why do we not just lump these strings into TapestryStrings.properties?
* because TapestryStrings.properties is localized to the server's locale, which is fine
* for the logging, debugging and error messages it contains. For field validation, whose errors
* are visible to the end user normally, we want to localize to the page's locale.
*
* @param override The override value for the localized string from the bundle.
* @param key used to lookup pattern from bundle, if override is null.
* @param locale used to get right localization of bundle.
* @since 3.0
*/
protected String getPattern(String override, String key, Locale locale)
{try { __CLOVER_445_0.M[2393]++;
__CLOVER_445_0.S[9838]++;if ((((override != null) && (++__CLOVER_445_0.CT[1685] != 0)) || (++__CLOVER_445_0.CF[1685] == 0))){
__CLOVER_445_0.S[9839]++;return override;}
__CLOVER_445_0.S[9840]++;ResourceBundle strings =
ResourceBundle.getBundle("org.apache.tapestry.valid.ValidationStrings", locale);
__CLOVER_445_0.S[9841]++;return strings.getString(key);
} finally { }}
/**
* Gets a string from the standard resource bundle. The string in the bundle
* is treated as a pattern for {@link MessageFormat#format(java.lang.String, java.lang.Object[])}.
*
* @param pattern string the input pattern to be used with
* {@link MessageFormat#format(java.lang.String, java.lang.Object[])}.
* It may contain replaceable parameters, {0}, {1}, etc.
* @param args the arguments used to fill replaceable parameters {0}, {1}, etc.
*
* @since 3.0
*
**/
protected String formatString(String pattern, Object[] args)
{try { __CLOVER_445_0.M[2394]++;
__CLOVER_445_0.S[9842]++;return MessageFormat.format(pattern, args);
} finally { }}
/**
* Convienience method for invoking {@link #formatString(String, Object[])}.
*
* @since 3.0
*
**/
protected String formatString(String pattern, Object arg)
{try { __CLOVER_445_0.M[2395]++;
__CLOVER_445_0.S[9843]++;return formatString(pattern, new Object[] { arg });
} finally { }}
/**
* Convienience method for invoking {@link #formatString(String, Object[])}.
*
* @since 3.0
*
**/
protected String formatString(String pattern, Object arg1, Object arg2)
{try { __CLOVER_445_0.M[2396]++;
__CLOVER_445_0.S[9844]++;return formatString(pattern, new Object[] { arg1, arg2 });
} finally { }}
/**
* Invoked to check if the value is null. If the value is null (or empty),
* but the required flag is set, then this method throws a {@link ValidatorException}.
* Otherwise, returns true if the value is null.
*
**/
protected boolean checkRequired(IFormComponent field, String value) throws ValidatorException
{try { __CLOVER_445_0.M[2397]++;
__CLOVER_445_0.S[9845]++;boolean isEmpty = Tapestry.isBlank(value);
__CLOVER_445_0.S[9846]++;if ((((_required && isEmpty) && (++__CLOVER_445_0.CT[1686] != 0)) || (++__CLOVER_445_0.CF[1686] == 0))){
__CLOVER_445_0.S[9847]++;throw new ValidatorException(
buildRequiredMessage(field),
ValidationConstraint.REQUIRED);}
__CLOVER_445_0.S[9848]++;return isEmpty;
} finally { }}
/**
* Builds an error message indicating a value for a required
* field was not supplied.
*
* @since 3.0
*/
protected String buildRequiredMessage(IFormComponent field)
{try { __CLOVER_445_0.M[2398]++;
__CLOVER_445_0.S[9849]++;String pattern =
getPattern(_requiredMessage, "field-is-required", field.getPage().getLocale());
__CLOVER_445_0.S[9850]++;return formatString(pattern, field.getDisplayName());
} finally { }}
/**
* This implementation does nothing. Subclasses may supply their own implementation.
*
* @since 2.2
*
**/
public void renderValidatorContribution(
IFormComponent field,
IMarkupWriter writer,
IRequestCycle cycle)
{try { __CLOVER_445_0.M[2399]++;
} finally { }}
/**
* Invoked (from sub-class
* implementations of {@link #renderValidatorContribution(IFormComponent, IMarkupWriter, IRequestCycle)}
* to process a standard validation script. This expects that:
* <ul>
* <li>The {@link IFormComponent} is (ultimately) wrapped by a {@link Body}
* <li>The script generates a symbol named "function" (as per {@link #FUNCTION_SYMBOL})
* </ul>
*
* @param scriptPath the resource path of the script to execute
* @param cycle The active request cycle
* @param field The field to be validated
* @param symbols a set of input symbols needed by the script. These symbols
* are augmented with symbols for the field, form and validator. symbols may be
* null, but will be modified if not null.
* @throws ApplicationRuntimeException if there's an error processing the script.
*
* @since 2.2
*
**/
protected void processValidatorScript(
String scriptPath,
IRequestCycle cycle,
IFormComponent field,
Map symbols)
{try { __CLOVER_445_0.M[2400]++;
__CLOVER_445_0.S[9851]++;IEngine engine = field.getPage().getEngine();
__CLOVER_445_0.S[9852]++;IScriptSource source = engine.getScriptSource();
__CLOVER_445_0.S[9853]++;IForm form = field.getForm();
__CLOVER_445_0.S[9854]++;Map finalSymbols = ((((symbols == null) ) && (++__CLOVER_445_0.CT[1687] != 0)) || (++__CLOVER_445_0.CF[1687] == 0))? new HashMap() : symbols;
__CLOVER_445_0.S[9855]++;finalSymbols.put(FIELD_SYMBOL, field);
__CLOVER_445_0.S[9856]++;finalSymbols.put(FORM_SYMBOL, form);
__CLOVER_445_0.S[9857]++;finalSymbols.put(VALIDATOR_SYMBOL, this);
__CLOVER_445_0.S[9858]++;Resource location =
new ClasspathResource(engine.getClassResolver(), scriptPath);
__CLOVER_445_0.S[9859]++;IScript script = source.getScript(location);
__CLOVER_445_0.S[9860]++;Body body = Body.get(cycle);
__CLOVER_445_0.S[9861]++;if ((((body == null) && (++__CLOVER_445_0.CT[1688] != 0)) || (++__CLOVER_445_0.CF[1688] == 0))){
__CLOVER_445_0.S[9862]++;throw new ApplicationRuntimeException(
Tapestry.getMessage("ValidField.must-be-contained-by-body"),
field,
null,
null);}
__CLOVER_445_0.S[9863]++;script.execute(cycle, body, finalSymbols);
__CLOVER_445_0.S[9864]++;String functionName = (String) finalSymbols.get(FUNCTION_SYMBOL);
__CLOVER_445_0.S[9865]++;form.addEventHandler(FormEventType.SUBMIT, functionName);
} finally { }}
/**
* Returns true if client scripting is enabled. Some validators are
* capable of generating client-side scripting to perform validation
* when the form is submitted. By default, this flag is false and
* subclasses should check it
* (in {@link #renderValidatorContribution(IFormComponent, IMarkupWriter, IRequestCycle)})
* before generating client side script.
*
* @since 2.2
*
**/
public boolean isClientScriptingEnabled()
{try { __CLOVER_445_0.M[2401]++;
__CLOVER_445_0.S[9866]++;return _clientScriptingEnabled;
} finally { }}
public void setClientScriptingEnabled(boolean clientScriptingEnabled)
{try { __CLOVER_445_0.M[2402]++;
__CLOVER_445_0.S[9867]++;_clientScriptingEnabled = clientScriptingEnabled;
} finally { }}
public String getRequiredMessage()
{try { __CLOVER_445_0.M[2403]++;
__CLOVER_445_0.S[9868]++;return _requiredMessage;
} finally { }}
/**
* Overrides the <code>field-is-required</code> bundle key.
* Parameter {0} is the display name of the field.
*
* @since 3.0
*/
public void setRequiredMessage(String string)
{try { __CLOVER_445_0.M[2404]++;
__CLOVER_445_0.S[9869]++;_requiredMessage = string;
} finally { }}
}