| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>StringValidator.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Fulcrum Intake Service</a> > <a href="index.source.html" class="el_package">org.apache.fulcrum.intake.validator</a> > <span class="el_source">StringValidator.java</span></div><h1>StringValidator.java</h1><pre class="source lang-java linenums">package org.apache.fulcrum.intake.validator; |
| |
| /* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you 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. |
| */ |
| |
| import java.util.Map; |
| import java.util.regex.Pattern; |
| import java.util.regex.PatternSyntaxException; |
| |
| import org.apache.commons.lang3.StringUtils; |
| |
| /** |
| * A validator that will compare a testValue against the following |
| * constraints: |
| * <table> |
| * <caption>Validation rules</caption> |
| * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr> |
| * <tr><td>required</td><td>true|false</td><td>false</td></tr> |
| * <tr><td>mask</td><td>regexp</td><td>&nbsp;</td></tr> |
| * <tr><td>minLength</td><td>integer</td><td>0</td></tr> |
| * <tr><td>maxLength</td><td>integer</td><td>&nbsp;</td></tr> |
| * </table> |
| * |
| * This validator can serve as the base class for more specific validators |
| * |
| * @author <a href="mailto:jmcnally@collab.net">John McNally</a> |
| * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a> |
| * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a> |
| * @author <a href="mailto:jh@byteaction.de">J&uuml;rgen Hoffmann</a> |
| * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a> |
| * @version $Id$ |
| */ |
| public class StringValidator |
| extends DefaultValidator<String> |
| { |
| /** The matching mask String as supplied by the XML input */ |
| <span class="fc" id="L53"> protected String maskString = null;</span> |
| |
| /** The compiled Regular Expression */ |
| <span class="fc" id="L56"> protected Pattern maskPattern = null;</span> |
| |
| /** The message to report if the mask constraint is not satisfied */ |
| <span class="fc" id="L59"> protected String maskMessage = null;</span> |
| |
| /** |
| * Default constructor |
| */ |
| public StringValidator() |
| { |
| <span class="fc" id="L66"> super();</span> |
| <span class="fc" id="L67"> }</span> |
| |
| /** |
| * Extract the relevant parameters from the constraints listed |
| * in &lt;rule&gt; tags within the intake.xml file. |
| * |
| * @param paramMap a <code>Map</code> of <code>Rule</code>'s |
| * containing constraints on the input. |
| * @throws InvalidMaskException An invalid mask was specified for one of the rules |
| */ |
| @Override |
| public void init(Map<String, ? extends Constraint> paramMap) |
| throws InvalidMaskException |
| { |
| <span class="fc" id="L81"> super.init(paramMap);</span> |
| |
| <span class="fc" id="L83"> Constraint constraint = paramMap.get(MASK_RULE_NAME);</span> |
| <span class="fc bfc" id="L84" title="All 2 branches covered."> if (constraint != null)</span> |
| { |
| <span class="fc" id="L86"> String param = constraint.getValue();</span> |
| <span class="fc" id="L87"> setMask(param);</span> |
| <span class="fc" id="L88"> maskMessage = constraint.getMessage();</span> |
| } |
| |
| <span class="fc" id="L91"> }</span> |
| |
| /** |
| * Determine whether a testValue meets the criteria specified |
| * in the constraints defined for this validator |
| * |
| * @param testValue a <code>String</code> to be tested |
| * @throws ValidationException containing an error message if the |
| * testValue did not pass the validation tests. |
| */ |
| @Override |
| public void assertValidity(String testValue) |
| throws ValidationException |
| { |
| <span class="fc" id="L105"> super.assertValidity(testValue);</span> |
| |
| <span class="pc bpc" id="L107" title="1 of 6 branches missed."> if ((required || StringUtils.isNotEmpty(testValue)) && maskPattern != null)</span> |
| { |
| /** JDK 1.4 matcher */ |
| <span class="fc" id="L110"> boolean patternMatch = maskPattern.matcher(testValue).matches();</span> |
| |
| <span class="fc" id="L112"> log.debug("Trying to match " + testValue</span> |
| + " to pattern " + maskString); |
| |
| <span class="fc bfc" id="L115" title="All 2 branches covered."> if (!patternMatch)</span> |
| { |
| <span class="fc" id="L117"> errorMessage = maskMessage;</span> |
| <span class="fc" id="L118"> throw new ValidationException(maskMessage);</span> |
| } |
| } |
| <span class="fc" id="L121"> }</span> |
| |
| // ************************************************************ |
| // ** Bean accessor methods ** |
| // ************************************************************ |
| |
| /** |
| * Get the value of mask. |
| * |
| * @return value of mask. |
| */ |
| public String getMask() |
| { |
| <span class="nc" id="L134"> return maskString;</span> |
| } |
| |
| /** |
| * Set the value of mask. |
| * |
| * @param mask Value to assign to mask. |
| * @throws InvalidMaskException the mask could not be compiled. |
| */ |
| public void setMask(String mask) |
| throws InvalidMaskException |
| { |
| <span class="fc" id="L146"> maskString = mask;</span> |
| |
| // Fixme. We should make this configureable by the XML file -- hps |
| <span class="fc" id="L149"> int maskOptions = 0;</span> |
| |
| try |
| { |
| <span class="fc" id="L153"> log.debug("Compiling pattern " + maskString);</span> |
| <span class="fc" id="L154"> maskPattern = Pattern.compile(maskString, maskOptions);</span> |
| } |
| <span class="nc" id="L156"> catch (PatternSyntaxException pe)</span> |
| { |
| <span class="nc" id="L158"> throw new InvalidMaskException("Could not compile pattern " + maskString, pe);</span> |
| <span class="fc" id="L159"> }</span> |
| <span class="fc" id="L160"> }</span> |
| |
| /** |
| * Get the value of maskMessage. |
| * |
| * @return value of maskMessage. |
| */ |
| public String getMaskMessage() |
| { |
| <span class="nc" id="L169"> return maskMessage;</span> |
| } |
| |
| /** |
| * Set the value of maskMessage. |
| * |
| * @param message Value to assign to maskMessage. |
| */ |
| public void setMaskMessage(String message) |
| { |
| <span class="nc" id="L179"> this.maskMessage = message;</span> |
| <span class="nc" id="L180"> }</span> |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.6.202009150832</span></div></body></html> |