KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ecs > filter > RegexpFilter


1 /*
2  * ====================================================================
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "The Jakarta Project", "Jakarta Element Construction Set",
29  * "Jakarta ECS" , and "Apache Software Foundation" must not be used
30  * to endorse or promote products derived
31  * from this software without prior written permission. For written
32  * permission, please contact apache@apache.org.
33  *
34  * 5. Products derived from this software may not be called "Apache",
35  * "Jakarta Element Construction Set" nor "Jakarta ECS" nor may "Apache"
36  * appear in their names without prior written permission of the Apache Group.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This software consists of voluntary contributions made by many
53  * individuals on behalf of the Apache Software Foundation. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  *
57  */

58 package org.apache.ecs.filter;
59
60 import org.apache.ecs.Filter;
61
62 import org.apache.regexp.RE;
63 import org.apache.regexp.RESyntaxException;
64
65 import java.util.Hashtable;
66 import java.util.Enumeration;
67
68 /**
69  * This filter uses regexp to create expressions and allows
70  * you do a replace on them.
71  *
72  * It works like the Perl function called subst. Given a regular
73  * expression of "a*b", and a String to substituteIn of
74  * "aaaabfooaaabgarplyaaabwackyb" and the substitution String "-", the
75  * resulting String returned by subst would be "-foo-garply-wacky-".
76  *
77  * <pre><code>
78  * Filter filter = new RegexpFilter();
79  * filter.addAttribute("a*b","-");
80  * String text = "aaaabfooaaabgarplyaaabwackyb";
81  * String result = filter.process(text);
82  * System.out.println(result);
83  * </code></pre>
84  *
85  * Produces: -foo-garply-wacky-
86  *
87  * Note: "a*" means 0 or more occurences of 'a', therefore the last match
88  * is absolutely correct.
89  *
90  * For more information about the regular expression package please do
91  * visit <a HREF="http://jakarta.apache.org/regexp/">Jakarta Regexp"</a>
92  *
93  * @version $Id: RegexpFilter.java,v 1.2 2003/04/27 09:28:56 rdonkin Exp $
94  * @author <a HREF="mailto:Krzysztof.Zelazowski@cern.ch">K. Zelazowski</a>
95  */

96 public class RegexpFilter extends Hashtable implements Filter {
97     
98     public RegexpFilter()
99     {
100         super(4);
101     }
102
103     /** Returns the name of the filter */
104     public String getInfo()
105     {
106         return "RegexpFilter";
107     }
108
109     
110     /**
111      * This method actually performs the filtering.
112      */

113     public String process(String to_process)
114     {
115         if ( to_process == null || to_process.length() == 0 )
116         {
117             return "";
118         }
119         
120         String substituteIn = to_process;
121         Enumeration enum = keys();
122
123         while (enum.hasMoreElements()) {
124             RE r = (RE)enum.nextElement();
125             String substitution = (String)get(r);
126             substituteIn = r.subst(substituteIn, substitution);
127         }
128         
129         return substituteIn;
130     }
131
132     
133     /**
134      * Add regular expression - substitution pair.
135      *
136      * @return itself or null if the was an error in the syntax of the
137      * expression.
138      */

139     public Filter addAttribute(String expression,Object substitution)
140     {
141         try {
142             RE r = new RE(expression);
143             put(r, substitution);
144         }
145         catch (RESyntaxException e) {
146             return null;
147         }
148
149         return(this);
150     }
151
152     
153     /**
154      * Get rid of the specified expression filter.
155      */

156     public Filter removeAttribute(String expression)
157     {
158         try
159         {
160              RE r = new RE(expression);
161              remove(r);
162         }
163         catch(Exception e)
164         {
165         }
166         return(this);
167     }
168
169     
170     /**
171      * Returns true if this filter contains an expression specified
172      * as an argument.
173      */

174     public boolean hasAttribute(String expression)
175     {
176         try {
177             RE r = new RE(expression);
178             return containsKey(r);
179         }
180         catch (Exception e) {
181         }
182         return false;
183     }
184 }
185
Popular Tags