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