Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/acmsl/regexpplugin/jakartaregexp/CompilerRegexpAdapter.java


1   /*
2                   Java Regular Expressions Plugin API
3   
4       Copyright (C) 2002  Jose San Leandro Armendáriz
5                           jsanleandro@yahoo.es
6                           chousz@yahoo.com
7   
8       This library is free software; you can redistribute it and/or
9       modify it under the terms of the GNU Lesser General Public
10      License as published by the Free Software Foundation; either
11      version 2.1 of the License, or (at your option) any later version.
12  
13      This library is distributed in the hope that it will be useful,
14      but WITHOUT ANY WARRANTY; without even the implied warranty of
15      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16      Lesser General Public License for more details.
17  
18      You should have received a copy of the GNU Lesser General Public
19      License along with this library; if not, write to the Free Software
20      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  
22      Thanks to ACM S.L. for distributing this library under the LGPL license.
23      Contact info: jsr000@terra.es
24      Postal Address: c/Playa de Lagoa, 1
25                      Urb. Valdecabañas
26                      Boadilla del monte
27                      28660 Madrid
28                      Spain
29  
30      This library uses some external APIs. So far I haven't released such
31      APIs as projects themselves, but you should be able
32      to download them from the web page where you got this source code.
33  
34   ******************************************************************************
35   *
36   * Filename: $RCSfile: CompilerRegexpAdapter.java,v $
37   *
38   * Author: Jose San Leandro Armendáriz
39   *
40   * Description: Jakarta Regexp-specific regexp compiler adapter.
41   *              This class makes possible the use of Jakarta Regexp
42   *              compilers inside this API. Delegation is used to be
43   *              able to write compile(String) method with different
44   *              signature as Jakarta Regexp's.
45   *
46   * Last modified by: $Author: dev $ at $Date: 2002/09/27 08:27:17 $
47   *
48   * File version: $Revision: 1.9 $
49   *
50   * Project version: $Name:  $
51   *                  ("Name" means no concrete version has been checked out)
52   *
53   * $Id: CompilerRegexpAdapter.java,v 1.9 2002/09/27 08:27:17 dev Exp $
54   *
55   */
56  package org.acmsl.regexpplugin.jakartaregexp;
57  
58  /*
59   * Importing project-specific classes.
60   */
61  import org.acmsl.regexpplugin.Compiler;
62  import org.acmsl.regexpplugin.MalformedPatternException;
63  import org.acmsl.regexpplugin.Pattern;
64  
65  /*
66   * Importing some ACM classes.
67   */
68  import org.acmsl.version.Version;
69  import org.acmsl.version.VersionFactory;
70  
71  /*
72   * Importing Jakarta Regexp classes.
73   */
74  import org.apache.regexp.RE;
75  import org.apache.regexp.RECompiler;
76  import org.apache.regexp.REProgram;
77  import org.apache.regexp.RESyntaxException;
78  
79  /**
80   * Jakarta Regexp-specific regexp compiler adapter. This class makes
81   * possible the use of Jakarta Regexp compilers inside this API.
82   * Delegation is used to be able to write compile(String) method
83   * with different signature as Jakarta Regexp's.
84   * @author <a href="mailto:jsanleandro@yahoo.es"
85             >Jose San Leandro Armendáriz</a>
86   * @version $Revision: 1.9 $
87   */
88  public class CompilerRegexpAdapter
89      implements  Compiler
90  {
91      /**
92       * Delegated instance.
93       */
94      private RECompiler m__Instance;
95  
96      /**
97       * Case sensitiveness.
98       */
99      private boolean m__bCaseSensitive;
100 
101     /**
102      * Multiline parsing.
103      */
104     private boolean m__bMultiline;
105 
106     /**
107      * Compiles given regular expression and creates a Pattern object to
108      * apply such rule on concrete text contents.
109      * @param regexp the regular expression to compile.
110      * @return the Pattern associated to such regular expression.
111      */
112     public Pattern compile(String regexp)
113         throws  MalformedPatternException
114     {
115         Pattern result = null;
116 
117         try
118         {
119             RECompiler t_Compiler = getDelegatedInstance();
120 
121             REProgram t_REProgram = t_Compiler.compile(regexp);
122 
123             RE t_RE = new RE(t_REProgram);
124 
125             int t_iOptions = 0;
126 
127             t_iOptions |=
128                 (isCaseSensitive())
129                 ?  RE.MATCH_NORMAL
130                 :  RE.MATCH_CASEINDEPENDENT;
131 
132             t_iOptions |=
133                 (isMultiline())
134                 ?  RE.MATCH_MULTILINE
135                 :  RE.MATCH_SINGLELINE;
136 
137             if (t_iOptions != 0)
138             {
139                 t_RE.setMatchFlags(t_iOptions);
140             }
141 
142             result = new PatternRegexpAdapter(t_REProgram, t_RE);
143         }
144         catch  (RESyntaxException exception)
145         {
146             throw new MalformedPatternExceptionRegexpAdapter(exception);
147         }
148         catch  (IllegalArgumentException illegalArgumentException)
149         {
150             if  (resetOptions())
151             {
152                 result = compile(regexp);
153             }
154         }
155 
156         return result;
157     }
158 
159     /**
160      * Resets the compiler options.
161      * @return true if the options actually changed.
162      */
163     private boolean resetOptions()
164     {
165         boolean result = false;
166 
167         result =
168             (   (isCaseSensitive())
169              || (isMultiline()));
170 
171         if  (result)
172         {
173             setCaseSensitive(false);
174 
175             setMultiline(false);
176         }
177 
178         return result;
179     }
180 
181     /**
182      * Retrieves an instance of RECompilerCompiler class.
183      * @return a new (or already existing) compiler.
184      */
185     protected RECompiler getDelegatedInstance()
186     {
187         RECompiler result = m__Instance;
188 
189         if  (m__Instance == null)
190         {
191             m__Instance = new RECompiler();
192 
193             result = m__Instance;
194         }
195 
196         return result;
197     }
198 
199     /**
200      * Sets whether the compiler should care about case sensitiveness
201      * or not.
202      * @param caseSensitive true for differentiate upper from lower case.
203      */
204     public void setCaseSensitive(boolean caseSensitive)
205     {
206         m__bCaseSensitive = caseSensitive;
207     }
208 
209     /**
210      * Retrieves whether the compiler should care about case sensitiveness
211      * or not.
212      * @return true if upper from lower cases are processed differently.
213      */
214     public boolean isCaseSensitive()
215     {
216         return m__bCaseSensitive;
217     }
218 
219     /**
220      * Sets whether the compiler should care about new line delimiters
221      * or not.
222      * @param multiline false for parsing each line at a time.
223      */
224     public void setMultiline(boolean multiline)
225     {
226         m__bMultiline = multiline;
227     }
228 
229     /**
230      * Sets whether the compiler should care about new line delimiters
231      * or not.
232      * @return false if the engine parses each line one at a time.
233      */
234     public boolean isMultiline()
235     {
236         return m__bMultiline;
237     }
238 
239     /**
240      * Concrete version object updated everytime it's checked-in in a CVS
241      * repository.
242      */
243     public static final Version VERSION =
244         VersionFactory.createVersion("$Revision: 1.9 $");
245 
246     /**
247      * Retrieves the current version of this object.
248      * @return the version object with such information.
249      */
250     public Version getVersion()
251     {
252         return VERSION;
253     }
254 
255     /**
256      * Retrieves the current version of this class.
257      * @return the object with class version information.
258      */
259     public static Version getClassVersion()
260     {
261         return VERSION;
262     }
263 }