/* $$ 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.util.HashMap;
import java.util.Map;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.Tapestry;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.util.RegexpMatcher;
/**
* <p>The validator bean that provides a pattern validation service.
*
* <p>The actual pattern matching algorithm is provided by the
* {@link org.apache.tapestry.valid.PatternDelegate}. This enables the user to provide
* custom pattern matching implementations. In the event a custom implementation is not
* provided, this validator will use the {@link org.apache.tapestry.util.RegexpMatcher}.
*
* <p>This validator has the ability to provide client side validation on demand.
* To enable client side validation simply set the <code>clientScriptingEnabled</code>
* property to <code>true</code>.
* The default implementation of the script will be in JavaScript and allows the user to
* override this with a custom implementation by setting the path to the custom
* script via {@link #setScriptPath(String)}.
*
* @author Harish Krishnaswamy
* @since 3.0
*/
public class PatternValidator extends BaseValidator
{public static com.cortexeb.tools.clover.d __CLOVER_455_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);
/**
* The pattern that this validator will use to validate the input. The default
* pattern is an empty string.
*/
private String _patternString = "";
/**
* A custom message in the event of a validation failure.
*/
private String _patternNotMatchedMessage;
/**
* The object that handles pattern matching.
*/
private PatternDelegate _patternDelegate;
/**
* The location of the script specification for client side validation.
*/
private String _scriptPath = "/org/apache/tapestry/valid/PatternValidator.script";
/**
* Returns custom validation failure message. The default message comes from
* <code>ValidationStrings.properties</code> file for key
* <code>pattern-not-matched</code>.
*/
public String getPatternNotMatchedMessage()
{try { __CLOVER_455_0.M[2511]++;
__CLOVER_455_0.S[10156]++;return _patternNotMatchedMessage;
} finally { }}
/**
* Returns the pattern that this validator uses for validation.
*/
public String getPatternString()
{try { __CLOVER_455_0.M[2512]++;
__CLOVER_455_0.S[10157]++;return _patternString;
} finally { }}
/**
* Allows for a custom message to be set typically via the bean specification.
*/
public void setPatternNotMatchedMessage(String message)
{try { __CLOVER_455_0.M[2513]++;
__CLOVER_455_0.S[10158]++;_patternNotMatchedMessage = message;
} finally { }}
/**
* Allows the user to change the validation pattern.
*/
public void setPatternString(String pattern)
{try { __CLOVER_455_0.M[2514]++;
__CLOVER_455_0.S[10159]++;_patternString = pattern;
} finally { }}
/**
* Static inner class that acts as a delegate to RegexpMatcher and conforms to the
* PatternDelegate contract.
*/
private static class RegExpDelegate implements PatternDelegate
{
private RegexpMatcher _matcher;
private RegexpMatcher getPatternMatcher()
{try { __CLOVER_455_0.M[2515]++;
__CLOVER_455_0.S[10160]++;if ((((_matcher == null) && (++__CLOVER_455_0.CT[1732] != 0)) || (++__CLOVER_455_0.CF[1732] == 0))){
__CLOVER_455_0.S[10161]++;_matcher = new RegexpMatcher();}
__CLOVER_455_0.S[10162]++;return _matcher;
} finally { }}
public boolean contains(String patternString, String input)
{try { __CLOVER_455_0.M[2516]++;
__CLOVER_455_0.S[10163]++;return getPatternMatcher().contains(patternString, input);
} finally { }}
public String getEscapedPatternString(String patternString)
{try { __CLOVER_455_0.M[2517]++;
__CLOVER_455_0.S[10164]++;return getPatternMatcher().getEscapedPatternString(patternString);
} finally { }}
}
/**
* Allows for a custom implementation to do the pattern matching. The default pattern
* matching is done with {@link org.apache.tapestry.util.RegexpMatcher}.
*/
public void setPatternDelegate(PatternDelegate patternDelegate)
{try { __CLOVER_455_0.M[2518]++;
__CLOVER_455_0.S[10165]++;_patternDelegate = patternDelegate;
} finally { }}
/**
* Returns the custom pattern matcher if one is provided or creates and returns the
* default matcher laziliy.
*/
public PatternDelegate getPatternDelegate()
{try { __CLOVER_455_0.M[2519]++;
__CLOVER_455_0.S[10166]++;if ((((_patternDelegate == null) && (++__CLOVER_455_0.CT[1733] != 0)) || (++__CLOVER_455_0.CF[1733] == 0))){
__CLOVER_455_0.S[10167]++;_patternDelegate = new RegExpDelegate();}
__CLOVER_455_0.S[10168]++;return _patternDelegate;
} finally { }}
/**
* @see org.apache.tapestry.valid.IValidator#toString(org.apache.tapestry.form.IFormComponent, java.lang.Object)
*/
public String toString(IFormComponent field, Object value)
{try { __CLOVER_455_0.M[2520]++;
__CLOVER_455_0.S[10169]++;if ((((value == null) && (++__CLOVER_455_0.CT[1734] != 0)) || (++__CLOVER_455_0.CF[1734] == 0))){
__CLOVER_455_0.S[10170]++;return null;}
__CLOVER_455_0.S[10171]++;return value.toString();
} finally { }}
private String buildPatternNotMatchedMessage(IFormComponent field, String patternString)
{try { __CLOVER_455_0.M[2521]++;
__CLOVER_455_0.S[10172]++;String templateMessage =
getPattern(
_patternNotMatchedMessage,
"pattern-not-matched",
field.getPage().getLocale());
__CLOVER_455_0.S[10173]++;return formatString(templateMessage, field.getDisplayName(), patternString);
} finally { }}
/**
* @see org.apache.tapestry.valid.IValidator#toObject(org.apache.tapestry.form.IFormComponent, java.lang.String)
*/
public Object toObject(IFormComponent field, String input) throws ValidatorException
{try { __CLOVER_455_0.M[2522]++;
__CLOVER_455_0.S[10174]++;if ((((checkRequired(field, input)) && (++__CLOVER_455_0.CT[1735] != 0)) || (++__CLOVER_455_0.CF[1735] == 0))){
__CLOVER_455_0.S[10175]++;return null;}
__CLOVER_455_0.S[10176]++;boolean matched = false;
__CLOVER_455_0.S[10177]++;try
{
__CLOVER_455_0.S[10178]++;matched = getPatternDelegate().contains(_patternString, input);
}
catch (Throwable t)
{
__CLOVER_455_0.S[10179]++;throw new ApplicationRuntimeException(
Tapestry.format(
"PatternValidator.pattern-match-error",
_patternString,
field.getDisplayName()),
field,
field.getLocation(),
t);
}
__CLOVER_455_0.S[10180]++;if ((((!matched) && (++__CLOVER_455_0.CT[1736] != 0)) || (++__CLOVER_455_0.CF[1736] == 0))){
__CLOVER_455_0.S[10181]++;throw new ValidatorException(
buildPatternNotMatchedMessage(field, _patternString),
ValidationConstraint.PATTERN_MISMATCH);}
__CLOVER_455_0.S[10182]++;return input;
} finally { }}
/**
* Allows for a custom implementation of the client side validation.
*/
public void setScriptPath(String scriptPath)
{try { __CLOVER_455_0.M[2523]++;
__CLOVER_455_0.S[10183]++;_scriptPath = scriptPath;
} finally { }}
/**
* @see org.apache.tapestry.valid.IValidator#renderValidatorContribution(org.apache.tapestry.form.IFormComponent, org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
*/
public void renderValidatorContribution(
IFormComponent field,
IMarkupWriter writer,
IRequestCycle cycle)
{try { __CLOVER_455_0.M[2524]++;
__CLOVER_455_0.S[10184]++;if ((((!isClientScriptingEnabled()) && (++__CLOVER_455_0.CT[1737] != 0)) || (++__CLOVER_455_0.CF[1737] == 0))){
__CLOVER_455_0.S[10185]++;return;}
__CLOVER_455_0.S[10186]++;Map symbols = new HashMap();
__CLOVER_455_0.S[10187]++;if ((((isRequired()) && (++__CLOVER_455_0.CT[1738] != 0)) || (++__CLOVER_455_0.CF[1738] == 0))){
__CLOVER_455_0.S[10188]++;symbols.put("requiredMessage", buildRequiredMessage(field));}
__CLOVER_455_0.S[10189]++;symbols.put(
"patternNotMatchedMessage",
buildPatternNotMatchedMessage(field, getEscapedPatternString()));
__CLOVER_455_0.S[10190]++;processValidatorScript(_scriptPath, cycle, field, symbols);
} finally { }}
/**
* Returns the escaped sequence of the pattern string for rendering in the error message.
*/
public String getEscapedPatternString()
{try { __CLOVER_455_0.M[2525]++;
__CLOVER_455_0.S[10191]++;return getPatternDelegate().getEscapedPatternString(_patternString);
} finally { }}
public String toString()
{try { __CLOVER_455_0.M[2526]++;
__CLOVER_455_0.S[10192]++;return "Pattern: "
+ _patternString
+ "; Script Path: "
+ _scriptPath
+ "; Pattern Delegate: "
+ _patternDelegate;
} finally { }}
}