RegularExpressionService.java

  1. /*
  2.  * Copyright (c) 2002-2022, 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.portal.service.regularexpression;

  35. import fr.paris.lutece.portal.business.regularexpression.RegularExpression;
  36. import fr.paris.lutece.portal.service.plugin.PluginService;
  37. import fr.paris.lutece.portal.service.spring.SpringContextService;

  38. import org.springframework.beans.factory.BeanDefinitionStoreException;
  39. import org.springframework.beans.factory.CannotLoadBeanClassException;
  40. import org.springframework.beans.factory.NoSuchBeanDefinitionException;

  41. import java.util.List;

  42. /**
  43.  *
  44.  * this class provides services for use regular expression
  45.  */
  46. public final class RegularExpressionService
  47. {
  48.     private static final String PLUGIN_REGULAR_EXPRESSION_NAME = "regularexpression";
  49.     private static RegularExpressionService _singleton;
  50.     private boolean _bServiceAvailable = true;
  51.     private IRegularExpressionService _service;

  52.     /**
  53.      * Private constructor
  54.      */
  55.     private RegularExpressionService( )
  56.     {
  57.         try
  58.         {
  59.             _service = SpringContextService.getBean( "regularExpressionService" );
  60.             _bServiceAvailable = _service != null;
  61.         }
  62.         catch( CannotLoadBeanClassException | NoSuchBeanDefinitionException | BeanDefinitionStoreException e )
  63.         {
  64.             _bServiceAvailable = false;
  65.         }
  66.     }

  67.     /**
  68.      * Returns the unique instance of the service
  69.      *
  70.      * @return The instance of the service
  71.      */
  72.     public static synchronized RegularExpressionService getInstance( )
  73.     {
  74.         if ( _singleton == null )
  75.         {
  76.             _singleton = new RegularExpressionService( );
  77.         }
  78.         return _singleton;
  79.     }

  80.     /**
  81.      *
  82.      * @return true if the regular expression service is available
  83.      */
  84.     public boolean isAvailable( )
  85.     {
  86.         return _bServiceAvailable && PluginService.isPluginEnable( PLUGIN_REGULAR_EXPRESSION_NAME );
  87.     }

  88.     /**
  89.      * return false if the pattern is invalid
  90.      *
  91.      * @param strPattern
  92.      *            the pattern to test
  93.      * @return false if the pattern is invalid
  94.      */
  95.     boolean isPatternValide( String strPattern )
  96.     {
  97.         return isAvailable( ) && _service.isPatternValide( strPattern );
  98.     }

  99.     /**
  100.      * return false if the expression's syntax is invalid
  101.      *
  102.      * @param regularExpression
  103.      *            the regular expression object to test
  104.      * @return false if the expression's syntax is invalid
  105.      */
  106.     boolean isPatternValide( RegularExpression regularExpression )
  107.     {
  108.         return isAvailable( ) && _service.isPatternValide( regularExpression );
  109.     }

  110.     /**
  111.      * return true if the value in parameter verify the pattern
  112.      *
  113.      * @param strValueToTest
  114.      *            the value to test
  115.      * @param strPattern
  116.      *            the regular expression Pattern
  117.      * @return true if the value in parameter verify the pattern
  118.      */
  119.     public boolean isMatches( String strValueToTest, String strPattern )
  120.     {
  121.         return isAvailable( ) && _service.isMatches( strValueToTest, strPattern );
  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.         return isAvailable( ) && _service.isMatches( strValueToTest, regularExpression );
  135.     }

  136.     /**
  137.      * return the regular expression object whose identifier is specified in parameter
  138.      *
  139.      * @param nKey
  140.      *            the regular expression key
  141.      * @return the regular expression object whose identifier is specified in parameter
  142.      */
  143.     public RegularExpression getRegularExpressionByKey( int nKey )
  144.     {
  145.         return isAvailable( ) ? _service.getRegularExpressionByKey( nKey ) : null;
  146.     }

  147.     /**
  148.      * return a list of regular expression
  149.      *
  150.      * @return all regular expression
  151.      */
  152.     public List<RegularExpression> getAllRegularExpression( )
  153.     {
  154.         return isAvailable( ) ? _service.getAllRegularExpression( ) : null;
  155.     }
  156. }