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

Quick Search    Search Deep

Source code: org/acmsl/regexpplugin/gnuregexp/MatcherGNUAdapter.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: MatcherGNUAdapter.java,v $
37   *
38   * Author: Jose San Leandro Armendáriz
39   *
40   * Description: GNU Regexp 1.1.4 specific matcher adapter. This class
41   *              makes possible the use of GNU Regexp 1.1.4 matchers
42   *              inside this API.
43   *
44   * Last modified by: $Author: dev $ at $Date: 2002/09/27 08:27:14 $
45   *
46   * File version: $Revision: 1.5 $
47   *
48   * Project version: $Name:  $
49   *                  ("Name" means no concrete version has been checked out)
50   *
51   * $Id: MatcherGNUAdapter.java,v 1.5 2002/09/27 08:27:14 dev Exp $
52   *
53   */
54  package org.acmsl.regexpplugin.gnuregexp;
55  
56  /*
57   * Importing project-specific classes.
58   */
59  import org.acmsl.regexpplugin.gnuregexp.PatternGNUAdapter;
60  import org.acmsl.regexpplugin.Matcher;
61  import org.acmsl.regexpplugin.MatchResult;
62  import org.acmsl.regexpplugin.Pattern;
63  
64  /*
65   * Importing GNU Regexp 1.1.4 classes.
66   */
67  import gnu.regexp.RE;
68  import gnu.regexp.REMatchEnumeration;
69  
70  /*
71   * Importing some ACM classes.
72   */
73  import org.acmsl.version.Version;
74  import org.acmsl.version.VersionFactory;
75  
76  /**
77   * GNU Regexp 1.1.4 matcher adapter. This class makes possible
78   * the use of GNU Regexp 1.1.4 matchers inside this API.
79   * @author <a href="mailto:jsanleandro@yahoo.es"
80             >Jose San Leandro Armendáriz</a>
81   * @version $Revision: 1.5 $
82   */
83  public class MatcherGNUAdapter
84      implements  Matcher
85  {
86      /**
87       * Concrete engine implementation.
88       */
89      private REMatchEnumeration m__REMatchEnumeration;
90  
91      /**
92       * Group count.
93       */
94      private int m__iGroups;
95  
96      /**
97       * Constructs a MatcherGNUAdapter.
98       */
99      public MatcherGNUAdapter()  {};
100 
101     /**
102      * Checks if given text contains specified pattern.
103      * @param text the text to analyze.
104      * @param pattern the regular expression to apply.
105      * @return true if the pattern is found.
106      */
107     public boolean contains(String text, Pattern pattern)
108     {
109         boolean result = false;
110 
111         if  (   (pattern != null)
112              && (pattern instanceof PatternGNUAdapter))
113         {
114             RE t_RE = ((PatternGNUAdapter) pattern).getDelegatedInstance();
115 
116             setREMatchEnumeration(t_RE.getMatchEnumeration(text));
117 
118             setGroups(t_RE.getNumSubs());
119 
120             result =
121                 (   (getREMatchEnumeration() != null)
122                  && (getREMatchEnumeration().hasMoreMatches()));
123         }
124 
125         return result;
126     }
127 
128     /**
129      * Sets the match enumeration.
130      * @param matchEnumeration such instance.
131      */
132     protected void setREMatchEnumeration(REMatchEnumeration reMatchEnumeration)
133     {
134         m__REMatchEnumeration = reMatchEnumeration;
135     }
136 
137     /**
138      * Retrieves the match enumeration.
139      * @return such instance.
140      */
141     protected REMatchEnumeration getREMatchEnumeration()
142     {
143         return m__REMatchEnumeration;
144     }
145 
146     /**
147      * Sets the group count.
148      * @param groups the number of groups.
149      */
150     protected void setGroups(int groups)
151     {
152         m__iGroups = groups;
153     }
154 
155     /**
156      * Retrieves the group count.
157      * @return the number of groups.
158      */
159     protected int getGroups()
160     {
161         return m__iGroups;
162     }
163 
164     /**
165      * Retrieves the last match found due to a previous call to
166      * <i>contains</i> method.
167      * @return such match result.
168      */
169     public MatchResult getMatch()
170     {
171         MatchResult result = null;
172 
173         if  (getREMatchEnumeration() != null)
174         {
175             result =
176                 new MatchResultGNUAdapter(
177                     getREMatchEnumeration().nextMatch(),
178                     getGroups());
179         }
180 
181         return result;
182     }
183 
184     /**
185      * Concrete version object updated everytime it's checked-in in a CVS
186      * repository.
187      */
188     public static final Version VERSION =
189         VersionFactory.createVersion("$Revision: 1.5 $");
190 
191     /**
192      * Retrieves the current version of this object.
193      * @return the version object with such information.
194      */
195     public Version getVersion()
196     {
197         return VERSION;
198     }
199 
200     /**
201      * Retrieves the current version of this class.
202      * @return the object with class version information.
203      */
204     public static Version getClassVersion()
205     {
206         return VERSION;
207     }
208 }