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

Quick Search    Search Deep

Source code: org/acmsl/regexpplugin/jakartaoro/Perl5CompilerOROAdapter.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: Perl5CompilerOROAdapter.java,v $
37   *
38   * Author: Jose San Leandro Armendáriz
39   *
40   * Description: Jakarta ORO-specific regexp compiler adapter. This class makes
41   *              possible the use of ORO compilers inside this API. A delegation
42   *              is used because Perl5Compiler is a final class.
43   *
44   * Last modified by: $Author: dev $ at $Date: 2002/09/27 08:27:16 $
45   *
46   * File version: $Revision: 1.12 $
47   *
48   * Project version: $Name:  $
49   *                  ("Name" means no concrete version has been checked out)
50   *
51   * $Id: Perl5CompilerOROAdapter.java,v 1.12 2002/09/27 08:27:16 dev Exp $
52   *
53   */
54  package org.acmsl.regexpplugin.jakartaoro;
55  
56  /*
57   * Importing project-specific classes.
58   */
59  import org.acmsl.regexpplugin.Compiler;
60  import org.acmsl.regexpplugin.Pattern;
61  
62  /*
63   * Importing some ACM classes.
64   */
65  import org.acmsl.version.Version;
66  import org.acmsl.version.VersionFactory;
67  
68  /*
69   * Importing ORO classes.
70   */
71  import org.apache.oro.text.regex.MalformedPatternException;
72  import org.apache.oro.text.regex.Perl5Compiler;
73  
74  /**
75   * Jakarta ORO-specific regexp compiler adapter. This class makes possible the
76   * use of ORO compilers inside this API. A delegation is used because
77   * Perl5Compiler is a final class.
78   * @author <a href="mailto:jsanleandro@yahoo.es"
79             >Jose San Leandro Armendáriz</a>
80   * @version $Revision: 1.12 $
81   */
82  public class Perl5CompilerOROAdapter
83      implements  Compiler
84  {
85      /**
86       * Delegated instance.
87       */
88      private Perl5Compiler m__Instance;
89  
90      /**
91       * Case sensitiveness.
92       */
93      private boolean m__bCaseSensitive;
94  
95      /**
96       * Multiline parsing.
97       */
98      private boolean m__bMultiline;
99  
100     /**
101      * Compiles given regular expression and creates a Pattern object to
102      * apply such rule on concrete text contents.
103      * @param regexp the regular expression to compile.
104      * @return the Pattern associated to such regular expression.
105      * @throws MalformedPatternException if given regexp is malformed.
106      */
107     public Pattern compile(String regexp)
108         throws  org.acmsl.regexpplugin.MalformedPatternException
109     {
110         Pattern result = null;
111 
112         try
113         {
114             Perl5Compiler t_Compiler = getDelegatedInstance();
115 
116             int t_iOptions = Perl5Compiler.DEFAULT_MASK;
117 
118             t_iOptions |=
119                 (isCaseSensitive())
120                 ?  0
121                 :  Perl5Compiler.CASE_INSENSITIVE_MASK;
122 
123             t_iOptions |=
124                 (isMultiline())
125                 ?  Perl5Compiler.MULTILINE_MASK
126                 :  Perl5Compiler.SINGLELINE_MASK;
127 
128             result =
129                 new PatternOROAdapter(
130                     t_Compiler.compile(
131                         regexp,
132                         t_iOptions));
133         }
134         catch  (org.apache.oro.text.regex.MalformedPatternException
135                     malformedPatternException)
136         {
137             throw
138                 new MalformedPatternExceptionOROAdapter(
139                     malformedPatternException);
140         }
141         catch  (IllegalArgumentException illegalArgumentException)
142         {
143             if  (resetOptions())
144             {
145                 result = compile(regexp);
146             }
147         }
148 
149         return result;
150     }
151 
152     /**
153      * Resets the compiler options.
154      * @return true if the options actually changed.
155      */
156     private boolean resetOptions()
157     {
158         boolean result = false;
159 
160         result =
161             (   (isCaseSensitive())
162              || (isMultiline()));
163 
164         setCaseSensitive(false);
165 
166         setMultiline(false);
167 
168         return result;
169     }
170 
171     /**
172      * Retrieves an instance of Perl5Compiler class.
173      * @return a new (or already existing) compiler.
174      */
175     protected Perl5Compiler getDelegatedInstance()
176     {
177         Perl5Compiler result = m__Instance;
178 
179         if  (m__Instance == null)
180         {
181             setAdaptee(new Perl5Compiler());
182 
183             result = m__Instance;
184         }
185 
186         return result;
187     }
188 
189     /**
190      * Sets the adaptee.
191      * @param adaptee the compiler to adapt.
192      */
193     protected void setAdaptee(Perl5Compiler adaptee)
194     {
195         m__Instance = adaptee;
196     }
197 
198     /**
199      * Sets whether the compiler should care about case sensitiveness
200      * or not.
201      * @param caseSensitive true for differentiate upper from lower case.
202      */
203     public void setCaseSensitive(boolean caseSensitive)
204     {
205         m__bCaseSensitive = caseSensitive;
206     }
207 
208     /**
209      * Retrieves whether the compiler should care about case sensitiveness
210      * or not.
211      * @return true if upper from lower cases are processed differently.
212      */
213     public boolean isCaseSensitive()
214     {
215         return m__bCaseSensitive;
216     }
217 
218     /**
219      * Sets whether the compiler should care about new line delimiters
220      * or not.
221      * @param multiline false for parsing each line at a time.
222      */
223     public void setMultiline(boolean multiline)
224     {
225         m__bMultiline = multiline;
226     }
227 
228     /**
229      * Sets whether the compiler should care about new line delimiters
230      * or not.
231      * @return false if the engine parses each line one at a time.
232      */
233     public boolean isMultiline()
234     {
235         return m__bMultiline;
236     }
237 
238     /**
239      * Concrete version object updated everytime it's checked-in in a CVS
240      * repository.
241      */
242     public static final Version VERSION =
243         VersionFactory.createVersion("$Revision: 1.12 $");
244 
245     /**
246      * Retrieves the current version of this object.
247      * @return the version object with such information.
248      */
249     public Version getVersion()
250     {
251         return VERSION;
252     }
253 
254     /**
255      * Retrieves the current version of this class.
256      * @return the object with class version information.
257      */
258     public static Version getClassVersion()
259     {
260         return VERSION;
261     }
262 }