View Javadoc
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  
36  import fr.paris.lutece.portal.business.regularexpression.RegularExpression;
37  import fr.paris.lutece.portal.service.plugin.PluginService;
38  import fr.paris.lutece.portal.service.spring.SpringContextService;
39  
40  import org.springframework.beans.factory.BeanDefinitionStoreException;
41  import org.springframework.beans.factory.CannotLoadBeanClassException;
42  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
43  
44  import java.util.List;
45  
46  /**
47   *
48   * this class provides services for use regular expression
49   */
50  public final class RegularExpressionService
51  {
52      private static final String PLUGIN_REGULAR_EXPRESSION_NAME = "regularexpression";
53      private static RegularExpressionService _singleton;
54      private boolean _bServiceAvailable = true;
55      private IRegularExpressionService _service;
56  
57      /**
58       * Private constructor
59       */
60      private RegularExpressionService( )
61      {
62          try
63          {
64              _service = SpringContextService.getBean( "regularExpressionService" );
65              _bServiceAvailable = _service != null;
66          }
67          catch( CannotLoadBeanClassException | NoSuchBeanDefinitionException | BeanDefinitionStoreException e )
68          {
69              _bServiceAvailable = false;
70          }
71      }
72  
73      /**
74       * Returns the unique instance of the service
75       * 
76       * @return The instance of the service
77       */
78      public static synchronized RegularExpressionService getInstance( )
79      {
80          if ( _singleton == null )
81          {
82              _singleton = new RegularExpressionService( );
83          }
84          return _singleton;
85      }
86  
87      /**
88       *
89       * @return true if the regular expression service is available
90       */
91      public boolean isAvailable( )
92      {
93          return _bServiceAvailable && PluginService.isPluginEnable( PLUGIN_REGULAR_EXPRESSION_NAME );
94      }
95  
96      /**
97       * return false if the pattern is invalid
98       * 
99       * @param strPattern
100      *            the pattern to test
101      * @return false if the pattern is invalid
102      */
103     boolean isPatternValide( String strPattern )
104     {
105         return isAvailable( ) && _service.isPatternValide( strPattern );
106     }
107 
108     /**
109      * return false if the expression's syntax is invalid
110      * 
111      * @param regularExpression
112      *            the regular expression object to test
113      * @return false if the expression's syntax is invalid
114      */
115     boolean isPatternValide( RegularExpression regularExpression )
116     {
117         return isAvailable( ) && _service.isPatternValide( regularExpression );
118     }
119 
120     /**
121      * return true if the value in parameter verify the pattern
122      * 
123      * @param strValueToTest
124      *            the value to test
125      * @param strPattern
126      *            the regular expression Pattern
127      * @return true if the value in parameter verify the pattern
128      */
129     public boolean isMatches( String strValueToTest, String strPattern )
130     {
131         return isAvailable( ) && _service.isMatches( strValueToTest, strPattern );
132     }
133 
134     /**
135      * return true if the value in parameter verify the regular expression
136      * 
137      * @param strValueToTest
138      *            the value to test
139      * @param regularExpression
140      *            the regular expression
141      * @return true if the value verify the regular expression
142      */
143     public boolean isMatches( String strValueToTest, RegularExpression regularExpression )
144     {
145         return isAvailable( ) && _service.isMatches( strValueToTest, regularExpression );
146     }
147 
148     /**
149      * return the regular expression object whose identifier is specified in parameter
150      * 
151      * @param nKey
152      *            the regular expression key
153      * @return the regular expression object whose identifier is specified in parameter
154      */
155     public RegularExpression getRegularExpressionByKey( int nKey )
156     {
157         return isAvailable( ) ? _service.getRegularExpressionByKey( nKey ) : null;
158     }
159 
160     /**
161      * return a list of regular expression
162      * 
163      * @return all regular expression
164      */
165     public List<RegularExpression> getAllRegularExpression( )
166     {
167         return isAvailable( ) ? _service.getAllRegularExpression( ) : null;
168     }
169 }