1 /* 2 * Copyright (c) 2002-2021, City of Paris 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice 10 * and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright notice 13 * and the following disclaimer in the documentation and/or other materials 14 * provided with the distribution. 15 * 16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 * 32 * License 1.0 33 */ 34 package fr.paris.lutece.plugins.regularexpression.service; 35 36 import fr.paris.lutece.plugins.regularexpression.business.RegularExpressionHome; 37 import fr.paris.lutece.portal.business.regularexpression.RegularExpression; 38 import fr.paris.lutece.portal.service.plugin.Plugin; 39 import fr.paris.lutece.portal.service.plugin.PluginService; 40 import fr.paris.lutece.portal.service.regularexpression.IRegularExpressionService; 41 42 import java.util.List; 43 import java.util.regex.Matcher; 44 import java.util.regex.Pattern; 45 import java.util.regex.PatternSyntaxException; 46 47 /** 48 * 49 * Regular expression service 50 * 51 */ 52 public class RegularExpressionService implements IRegularExpressionService 53 { 54 /** 55 * return false if the pattern is invalid 56 * 57 * @param strPattern 58 * the pattern to test 59 * @return false if the pattern is invalid 60 */ 61 public boolean isPatternValide( String strPattern ) 62 { 63 try 64 { 65 Pattern.compile( strPattern ); 66 } 67 catch( PatternSyntaxException exception ) 68 { 69 return false; 70 } 71 72 return true; 73 } 74 75 /** 76 * return false if the expression's syntax is invalid 77 * 78 * @param regularExpression 79 * the regular expression object to test 80 * @return false if the expression's syntax is invalid 81 */ 82 public boolean isPatternValide( RegularExpression regularExpression ) 83 { 84 try 85 { 86 Pattern.compile( regularExpression.getValue( ) ); 87 } 88 catch( PatternSyntaxException exception ) 89 { 90 return false; 91 } 92 93 return true; 94 } 95 96 /** 97 * return true if the value in parameter verify the pattern 98 * 99 * @param strValueToTest 100 * the value to test 101 * @param strPattern 102 * the regular expression Pattern 103 * @return true if the value in parameter verify the pattern 104 */ 105 public boolean isMatches( String strValueToTest, String strPattern ) 106 { 107 Pattern pattern = null; 108 109 try 110 { 111 pattern = Pattern.compile( strPattern ); 112 } 113 catch( PatternSyntaxException exception ) 114 { 115 return false; 116 } 117 118 Matcher controler = pattern.matcher( strValueToTest ); 119 120 return controler.matches( ); 121 } 122 123 /** 124 * return true if the value in parameter verify the regular expression 125 * 126 * @param strValueToTest 127 * the value to test 128 * @param regularExpression 129 * the regular expression 130 * @return true if the value verify the regular expression 131 */ 132 public boolean isMatches( String strValueToTest, RegularExpression regularExpression ) 133 { 134 Pattern pattern = null; 135 136 try 137 { 138 pattern = Pattern.compile( regularExpression.getValue( ) ); 139 } 140 catch( PatternSyntaxException exception ) 141 { 142 return false; 143 } 144 145 Matcher controler = pattern.matcher( strValueToTest ); 146 147 return controler.matches( ); 148 } 149 150 /** 151 * return the regular expression object whose identifier is specified in parameter 152 * 153 * @param nKey 154 * the regular expression key 155 * @return the regular expression object whose identifier is specified in parameter 156 */ 157 public RegularExpression getRegularExpressionByKey( int nKey ) 158 { 159 Plugin plugin = PluginService.getPlugin( RegularExpressionPlugin.PLUGIN_NAME ); 160 161 return RegularExpressionHome.findByPrimaryKey( nKey, plugin ); 162 } 163 164 /** 165 * return a list of regular expression 166 * 167 * @return all regular expression 168 */ 169 public List<RegularExpression> getAllRegularExpression( ) 170 { 171 Plugin plugin = PluginService.getPlugin( RegularExpressionPlugin.PLUGIN_NAME ); 172 173 return RegularExpressionHome.getList( plugin ); 174 } 175 }